From hopengl at elisehuard.be Wed Jul 2 15:06:39 2014 From: hopengl at elisehuard.be (Elise Huard) Date: Wed, 2 Jul 2014 15:06:39 +0000 (UTC) Subject: [HOpenGL] 2D textures with OpenGL and GLUT Message-ID: Hi, I've been trying to get a texture mapped onto a circle. I've pounded my head against this problem for a good while, been reading lots of posts about how to do it in C, reproduced those as much as possible, but the best I get is a fairly distorted texture (I'm working with a basic checkerboard as displayed in the Redbook4 examples of GLUT, just to make sure I've got it right before moving on to actual images). Code here: loading texture https://github.com/elisehuard/game-prototype/blob/master/src/Main.hs#L90 using the texture in a circle (mapping): https://github.com/elisehuard/game-prototype/blob/master/src/Main.hs#L130 the result is here https://github.com/elisehuard/game-prototype/blob/master/images/texture.jpg (as you can see, it is not a checkerboard) My aim is to make a 2D game to start with, so the perspective is orthogonal, I don't know if that helps. This may be due to my being a noob with things OpenGL - I hope anyone can help me solve this, textures are a pretty important part. Thank you, From dagitj at gmail.com Wed Jul 2 16:20:21 2014 From: dagitj at gmail.com (Jason Dagit) Date: Wed, 2 Jul 2014 09:20:21 -0700 Subject: [HOpenGL] 2D textures with OpenGL and GLUT In-Reply-To: References: Message-ID: On Wed, Jul 2, 2014 at 8:06 AM, Elise Huard wrote: > Hi, > I've been trying to get a texture mapped onto a circle. > I've pounded my head against this problem for a good while, > been reading lots of posts about how to do it in C, > For folks who are experienced with low-level Haskell I actually recommend they use the OpenGLRaw library as it matches C examples more closely. Although, if you're a Haskell beginner that may just be an extra hassle. > reproduced those as much as possible, > but the best I get is a fairly distorted texture > (I'm working with a basic checkerboard as displayed in the Redbook4 > examples of GLUT, > just to make sure I've got it right before moving on to actual images). > Code here: > loading texture > https://github.com/elisehuard/game-prototype/blob/master/src/Main.hs#L90 > using the texture in a circle (mapping): > https://github.com/elisehuard/game-prototype/blob/master/src/Main.hs#L130 It's hard to say what's going on with all those unsafeCoerce in there. > > the result is here > https://github.com/elisehuard/game-prototype/blob/master/images/texture.jpg > > (as you can see, it is not a checkerboard) > My aim is to make a 2D game to start with, so the perspective is > orthogonal, > I don't know if that helps. > Lots of things can be orthogonal, but I believe in this case that orthographic is the word you want :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From hopengl at elisehuard.be Wed Jul 2 16:36:33 2014 From: hopengl at elisehuard.be (Elise Huard) Date: Wed, 2 Jul 2014 16:36:33 +0000 (UTC) Subject: [HOpenGL] 2D textures with OpenGL and GLUT References: Message-ID: Hi Jason, thanks for taking the time to answer. So trying to read between the lines: - do you have any _working_ examples of texture mapping with OpenGLRaw and low-level Haskell I could have a look at? (anything would help at this point) - do you think the unsafeCoerce (which I borrowed from examples) that transform CpFloat to GLdouble might be the issue, or is this just a general stylistic comment? Thank you, Elise From dagitj at gmail.com Wed Jul 2 17:11:10 2014 From: dagitj at gmail.com (Jason Dagit) Date: Wed, 2 Jul 2014 10:11:10 -0700 Subject: [HOpenGL] 2D textures with OpenGL and GLUT In-Reply-To: References: Message-ID: On Wed, Jul 2, 2014 at 9:33 AM, Elise Huard wrote: > Hi Jason, > thanks for taking the time to answer. > So trying to read between the lines: > - do you have any _working_ examples of texture mapping with OpenGLRaw > and low-level Haskell I could have a look at? (anything would help at > this point) > I do. I converted some of the old nehe tutorials: https://github.com/dagit/nehe-tuts If you look through the history you can find examples that use a very very old version of OpenGL (instead of OpenGLRaw). The OpenGL style in those examples is old and no longer the recommended way. The examples use immediate mode which is now deprecated in favor of shaders. I think lesson 6 makes a cube and throws some textures on it. The lesson numbers are meant to match these: * http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ * http://nehe.gamedev.net/tutorial/lessons_06__10/17010/ > - do you think the unsafeCoerce (which I borrowed from examples) that > transform CpFloat to GLdouble might be the issue, or is this just a > general stylistic comment? > Not just stylistic. As for correctness, I think the answer varies by platform. Specifically, I think CFloat is not required to match GLdouble in size (in fact, doubles should be expected to be twice as big). While both types should correspond to IEEE 754 floats the bit width is very likely different and that could easily cause the interpretation of the values to go wonky. Imagine passing 64bits when only the first 32bits correspond to a valid number or truncating a 64bit value to 32bits. Which examples use unsafeCoerce for this? I'm just sort of curious so I can recommend people not to use them :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From haskell at elisehuard.be Tue Jul 8 16:20:46 2014 From: haskell at elisehuard.be (Elise Huard) Date: Tue, 8 Jul 2014 12:20:46 -0400 Subject: [HOpenGL] 2D textures with OpenGL and GLUT In-Reply-To: References: Message-ID: I wanted to thank you, because pointing to unsafeCoerce was a nice bit of serendipity. I'll admit I thought it was nitpicking initially, but I did a quick pass at cleaning things up last week after your mail (not perfect, but this was a prototype so I'm not looking for pretty): https://github.com/elisehuard/game-prototype/commit/5cf85fa60f325dbe610b5e1f017c9427e304daf6 and that fixed the texture mapping! Consider me informed on that issue :) (as for which example, I used Redbook4 but I admit to extrapolating a little, this was my mistake) Thank you, On 2 July 2014 13:11, Jason Dagit wrote: > > > > On Wed, Jul 2, 2014 at 9:33 AM, Elise Huard wrote: >> >> Hi Jason, >> thanks for taking the time to answer. >> So trying to read between the lines: >> - do you have any _working_ examples of texture mapping with OpenGLRaw >> and low-level Haskell I could have a look at? (anything would help at >> this point) > > > I do. I converted some of the old nehe tutorials: > https://github.com/dagit/nehe-tuts > > If you look through the history you can find examples that use a very very > old version of OpenGL (instead of OpenGLRaw). The OpenGL style in those > examples is old and no longer the recommended way. The examples use > immediate mode which is now deprecated in favor of shaders. > > I think lesson 6 makes a cube and throws some textures on it. The lesson > numbers are meant to match these: > * http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ > * http://nehe.gamedev.net/tutorial/lessons_06__10/17010/ > > >> >> - do you think the unsafeCoerce (which I borrowed from examples) that >> transform CpFloat to GLdouble might be the issue, or is this just a >> general stylistic comment? > > > Not just stylistic. As for correctness, I think the answer varies by > platform. Specifically, I think CFloat is not required to match GLdouble in > size (in fact, doubles should be expected to be twice as big). While both > types should correspond to IEEE 754 floats the bit width is very likely > different and that could easily cause the interpretation of the values to go > wonky. Imagine passing 64bits when only the first 32bits correspond to a > valid number or truncating a 64bit value to 32bits. > > Which examples use unsafeCoerce for this? I'm just sort of curious so I can > recommend people not to use them :) > From dagitj at gmail.com Tue Jul 8 16:32:03 2014 From: dagitj at gmail.com (Jason Dagit) Date: Tue, 8 Jul 2014 09:32:03 -0700 Subject: [HOpenGL] 2D textures with OpenGL and GLUT In-Reply-To: References: Message-ID: On Tue, Jul 8, 2014 at 9:20 AM, Elise Huard wrote: > I wanted to thank you, because pointing to unsafeCoerce was a nice bit > of serendipity. I'll admit I thought it was nitpicking initially, but > I did a quick pass at cleaning things up last week after your mail > (not perfect, but this was a prototype so I'm not looking for pretty): > > > https://github.com/elisehuard/game-prototype/commit/5cf85fa60f325dbe610b5e1f017c9427e304daf6 > > and that fixed the texture mapping! > Consider me informed on that issue :) > Cool! By the way, the 'proper way' to convert between Double and CDouble is to use realToFrac: Prelude Foreign.C.Types ?> realToFrac (1 :: Double) :: CDouble 1.0 In specific case like this (Double <-> CDouble) it should be equivalent (but as a user of the library you shouldn't depend on know that). In some cases it can be a performance hit, but that's what RULEs pragmas are for. Moreover, some versions of the Haskell OpenGL bindings made it so that GLdouble was not the same type as CDouble. So the safest way is to use realToFrac and depend on the library writer to provide RULEs that make it efficient. I hope that helps, Jason -------------- next part -------------- An HTML attachment was scrubbed... URL: From haskell at elisehuard.be Tue Jul 8 18:14:09 2014 From: haskell at elisehuard.be (Elise Huard) Date: Tue, 8 Jul 2014 14:14:09 -0400 Subject: [HOpenGL] 2D textures with OpenGL and GLUT In-Reply-To: References: Message-ID: Definitely, I want to avoid unsafeCoerce completely. thanks, Elise On 8 July 2014 12:32, Jason Dagit wrote: > > > > On Tue, Jul 8, 2014 at 9:20 AM, Elise Huard wrote: >> >> I wanted to thank you, because pointing to unsafeCoerce was a nice bit >> of serendipity. I'll admit I thought it was nitpicking initially, but >> I did a quick pass at cleaning things up last week after your mail >> (not perfect, but this was a prototype so I'm not looking for pretty): >> >> >> https://github.com/elisehuard/game-prototype/commit/5cf85fa60f325dbe610b5e1f017c9427e304daf6 >> >> and that fixed the texture mapping! >> Consider me informed on that issue :) > > > Cool! By the way, the 'proper way' to convert between Double and CDouble is > to use realToFrac: > > Prelude Foreign.C.Types ?> realToFrac (1 :: Double) :: CDouble > 1.0 > > In specific case like this (Double <-> CDouble) it should be equivalent (but > as a user of the library you shouldn't depend on know that). In some cases > it can be a performance hit, but that's what RULEs pragmas are for. > Moreover, some versions of the Haskell OpenGL bindings made it so that > GLdouble was not the same type as CDouble. So the safest way is to use > realToFrac and depend on the library writer to provide RULEs that make it > efficient. > > I hope that helps, > Jason > >