How to fix textures being rendered upside down?

Started by
2 comments, last by hughel 15 years, 7 months ago
Hi guys, Once again, i've hit a stumbing block with my texture blitting function and thought i'd turn to the forums for some help. This one is quite simple, so hopefully someone can easily point me in the right direction :) I've managed to write a rendering function for drawing textures to the screen but am having some troubles orientating my textures properly. For some reason, which i am unsure of, any image i load into my texture manager seems to be rendered upside down (but not back to front). Whilst i am unsure as to how to fix this issue, my question is: do i need to rectify this when loading my image into the texture manager, or at render time? I would assume it would be less intensive to correctly orientate it upon loading the texture as opposed to transforming it everytime the texture is blitted to the screen, but i just wanted to make sure that i am doing it in the right place before i waste what little time i have to spare on my personal project :) As for fixing the issue, ill have a dig around for a solution, but as long as i'm sure of where i should be attempting my fix, i'm willing to persevere :) Thanks for reading!
Advertisement
The solution depends on where in your chain of code the error actually is. The origin of everything in OpenGL is in the lower left corner. As long as you do what OpenGL does by default, you will never have problems like this. That means, for example, that you should load your images such that the bottom row of the image is the first one in memory, set up your coordinate system to have the origin in the lower left corner, and so on.

As soon as you step away from what OpenGL does by default, you will have to compensate in some, or may, places. So if you load the image with top row first, you have to compensate in the texture coordinates. If you put the origin in the upper left corner of the screen, you will have to compensate coordinates or texture coordinates, when drawing screen aligned stuff, for example. Basically, keeping everything with the origin in the lower left corner, everything will just work. Change something, and you will have to be aware of everything that's going on so you know what and how to compensate.
Hi BrotherBob, thanks for your reply :)

I currently have my 2D coordinate system setup so that the top left point is (0, 0) and the bottom right the (width, height) of the screen.

From your reply, im guessing that i will have to somehow modify my texture loading method to translate the texture coords to suit the projection coords.

I'm slowly getting there but my current fix has some issues (as i expected). I'm attempting to translate the current context to scale the y coords by -1 but seem to be constraining the texture with the wrong proportions, i guess its trial an error from here on ;)

Thanks for the push in the right direction, at least i know what i have to tackle, now the fun part is fixing it to work as i need :)
If your texture coordinates for each vertex assumes that 0,0 is the top left corner and width,height is the bottom right and you are using OpenGL then all you have to do is change the v component of each vertex's texture coordinate to 1-v that should fix it.

This topic is closed to new replies.

Advertisement