Dealing with inverted textures in OpenGL
Is there a "best" way to deal with the fact that 2D Textures in OpenGL are inverted?
I have a simple 2D game that uses images mapped onto quads to create the sprites. I use the Texture2D sample code to load the textures, but they are upside down.
I could say something like
glRotatef(180, 0, 0, 1);
on every render cycle, but that seems like a waste.
Should I map them onto the quad upside down?
I can't figure out how they do it in the Crash Landing sample code, which has a _rotation variable, but that just seems to be used to orient the ship.
Any ideas?
I have a simple 2D game that uses images mapped onto quads to create the sprites. I use the Texture2D sample code to load the textures, but they are upside down.
I could say something like
glRotatef(180, 0, 0, 1);
on every render cycle, but that seems like a waste.
Should I map them onto the quad upside down?
I can't figure out how they do it in the Crash Landing sample code, which has a _rotation variable, but that just seems to be used to orient the ship.
Any ideas?
Use an upside down image
Sir, e^iπ + 1 = 0, hence God exists; reply!
Inverting the image is not ideal, particularly if you are working with other people that will keep trying to "correct" the picture.
As a matter of fact, the images in the Crash Landing sample code are not inverted.
As a matter of fact, the images in the Crash Landing sample code are not inverted.
Use a negative y coordinate for your texture coordinates. Or you can flip the image on load.
Part of the code modified from Texture2D:
Part of the code modified from Texture2D:
Code:
CGAffineTransform transform;
if (flipImage)
{
transform = CGAffineTransformTranslate(transform, 0, height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
}
imageData = (GLubyte *)calloc(1, (width * height * 4));
spriteContext = CGBitmapContextCreate(imageData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
CGContextConcatCTM(spriteContext, transform);
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
You can flip the images when you save them, flip the images on load, flip the texture matrix, flip the geometry for every object, or flip each object. All will work just fine.
Personally, I just flip the images at load time and be done with it. It's pretty easy to do, and isn't going to be a performance problem really.
Personally, I just flip the images at load time and be done with it. It's pretty easy to do, and isn't going to be a performance problem really.
Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
I'm with the "flip it on load" crowd.
Skorche Wrote:You can flip the images when you save them, flip the images on load, flip the texture matrix, flip the geometry for every object, or flip each object. All will work just fine.
Not "all", Any 3 will work though.
Sir, e^iπ + 1 = 0, hence God exists; reply!
unknown Wrote:Not "all", Any 3 will work though.
Um... Or any 1 or 5 of them.
Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Is Dealing with Opengl ES 2 worth the trouble? | Mattonaise | 9 | 7,132 |
Aug 23, 2011 08:56 PM Last Post: Mattonaise |
|
| [SOLVED]OpenGL edges of textures | mk12 | 2 | 3,628 |
Sep 2, 2010 08:07 PM Last Post: mk12 |
|
| OpenGL Image Textures | mikey | 52 | 20,019 |
Jun 30, 2009 10:42 AM Last Post: AnotherJake |
|
| Using textures OpenGL switches to software renderer | bruno | 2 | 3,111 |
Oct 12, 2008 03:06 AM Last Post: bruno |
|
| Dealing with GLUT and OpenGL... | RingoEST | 10 | 4,930 |
Aug 16, 2008 12:24 AM Last Post: RingoEST |
|

