PDA

View Full Version : Texture mapping, upper left hand origin?



allrussiantome
05-21-2010, 04:15 AM
I've been having some trouble with texture mapping. After much to and fro, testing out different image files and generally going insane, I've come to believe that the texture coordinates are actually applied using an upper left hand origin. Is this correct, as none of the tutorials I have been following or the book I've been using (Addison Wesley OpenGL Programming Guide 7th Ed.), mentions this. In fact the book leads me to believe that it uses a lower left hand origin.

"To apply the brick texture to a wall, for example, assuming the wall is square and meant to rep-resent one copy of the texture, the code would probably assign texture coor-dinates (0, 0), (1, 0), (1, 1), and (0, 1) to the four corners of the wall" - Addison Wesley OpenGL Programming Guide 7th Edition, Page 97, Texture-Mapped Squares.

These coordinates are also used in the example code.

Am I insane or am I doing something wrong?

mhagain
05-21-2010, 04:41 AM
I don't believe you're either. :)

I've ported OpenGL stuff to Direct3D in the past and have not needed to modify texture coords at all, so I'm of the opinion that the documentation is actually somewhat lacking in clarity here.

Dark Photon
05-21-2010, 04:45 AM
the book leads me to believe that it uses a lower left hand origin.
It is.


Am I insane or am I doing something wrong?
You're not insane. Next time just post a question before you start driving yourself nuts. ;) It happens to all of us occasionally.

And if you post more reasons leading to your current confusion, folks can help you out. What is suggesting to you (incorrectly) that the origin may be the upper left?

Post a short GLUT test program.

overlay
05-21-2010, 08:27 AM
Maybe you loaded the image and forgot to flip it? Most image file format start with upper-left but OpenGL expect lower-left.

allrussiantome
05-21-2010, 03:21 PM
Thanks for the help guys.

Dark - Without posting a short test program (I'm actually doing this in OpenGL-ES on android) what led me to believe the origin is upper left is the fact that when I started to use a texture map, trying to draw the faces of a dice on a cube. When looking at my image, a 256x256 .png file, trying to map the top left corner (64x64) I need to use the texture coordinates (0.0f,0.25f), (0.25f,0.25f), (0.25f,0.0f) and (0,0f,0.0f). This corresponds to a top left origin when looking at the original image, but a lower left origin on the loaded image which is flipped.

Original image below
http://www.freeimagehosting.net/uploads/9a87120b99.png

Alfonse Reinheart
05-21-2010, 06:38 PM
what led me to believe the origin is upper left is the fact that when I started to use a texture map, trying to draw the faces of a dice on a cube.

The OpenGL (ES) specification is very clear about this: the pointer you give to glTexImage2D or glTexSubImage2D is the pointer to the lower-left pixels of the image. So either whatever code you are using to load the image is flipping it, the image is flipped in the PNG file, or your OpenGL ES implementation has serious bugs in it.

allrussiantome
05-23-2010, 03:37 AM
During my trials and tribulations, I investigated the image that was loaded before it was passed to the texImage2d command, and the image was stored upper left hand pixel first. I have since learned that OpenGL expects lower left hand first, and therefore the image is "flipped", it would logically seem, by OpenGL.