When loading a texture in OpenGL, how do I specify the origin of the data I am loading?
For example, how would I load a Targa that has it's origin at the top left instead of the bottom left of the image?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
You can't, at least not directly. The origin of textures in OpenGL is the lower-left corner. You need to vertically flip your image if it doesn't match this coordinate system. So it's really an image processing problem, not an OpenGL problem.
(Alternatively, you can flip all your texture coordinates that refer to the image.)
As a first solution, you could just change the coordinates accordingly yourself when calling
glTexCoord (or when setting the texture coordinates in your vertex array.)
To flip your texture origin you could easily flip your coordinate system. A naive solution would be
glScalef(1.0f, -1.0f, 1.0f); (or alternative methods in modern matrix-less OpenGL), effectively flipping your Y coordinates and putting the origin at the top-left corner. Beware: you're not actually flipping the texture, but flipping your geometry, although it's a very easy solution if you're just drawing a simple scene like a full-screen quad.
That's rather hacky and will not work for complex situations (without hacking even more.) The correct way to go in old OpenGL is the
GL_TEXTURE matrix (which doesn't seem to be very popular, even when matrices were available.) It behaves exactly like any other matrix (see this link about transforms @ Songho.)
Another approach to this problem (the correct one IMHO, fully-compatible with modern OGL) is processing the image in shaders. It's very simple: your new
texCoord would be
1.0 - texCoord (for normalized textures.) This is fully compatible with matrix-less OpenGL (which you should be aiming for, btw) and will let you do all kind of further image processing.
You should flip the image data before uploading it to the GPU. Depending on the image library you are using it might be possible to flip the images already when loading without extra effort.
Don't try to workaround this by flipping texture coordinates or altering texture matrices. Those are not general solutions and only pollutes your code here and there. They are also incompatible with Framebuffer objects, since those expect the texture coordinates to be without any flipping.
Note that cube maps should not be flipped before uploading them. Those can be used directly as they are.