Skip to main content
GameDev.net gamedev.net
🔒 Locked

texture is upside-down

Started by devnub Dec 17, 2009 at 12:54 PM 5 replies 2.1k views
Original Post
devnub
devnub
okay, I got some weird problem going on... i'm trying to display a texture in "perspective mode" but it's somehow upside-down... however in "ortho-mode" it's the right way, even tho the texture coordinates are the same for both. The texture is manually generated inside the program, so the pixel(0,0) is at the beginning of memory array and (size,size) is at the end of the memory array, and I send this over to OpenGL just as it is. Then I draw my quads using the following code:

// for ortho mode
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, 64.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f,  0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(64.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(64.0f, 64.0f);
glEnd();


// for perspective mode
glMatrixMode(GL_PROJECTION);		
glLoadIdentity();
gluPerspective(45.0f, 1.333f, 1.0f, 5000.0f);
glMatrixMode(GL_MODELVIEW);		
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -164.0f);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-32.0f, 32.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-32.0f,-32.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 32.0f,-32.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 32.0f, 32.0f, 0.0f);
glEnd();

Any ideas why the texture gets flipped in perspective mode?
Erik Rufelt
Erik Rufelt
In perspective mode -Y is down, and +Y is up. With your glOrtho parameters +Y will be down. (So it's actually the quad that is upside down).
devnub
devnub
okay so.. err.. lets get back to basics because i think i might have misunderstood something severely..

the texture coord 1.0 on the y-axis, does it map to the bottom or top of a top-left oriented image?

for example, lets say i have a texture of size 64,64, and it's stored in memory so that pixel 0,0 is at the beginning of the array and pixel 64,64 is at the end... Will texture coord 0.0,0.0 be referring to the pixel at the beginning of that array or the 0,63 pixel?
Erik Rufelt
Erik Rufelt
(0, 0) should be the bottom left corner of a texture. If your glOrtho code puts the texture correctly at the top left corner of the screen, then it doesn't in your case.. how are you loading your textures?
BMPs for example are stored in bottom to top order, so if you have a BMP you might get a double flip in the ortho case.
Erik Rufelt
Erik Rufelt
Quote:
Original post by devnub
for example, lets say i have a texture of size 64,64, and it's stored in memory so that pixel 0,0 is at the beginning of the array and pixel 64,64 is at the end... Will texture coord 0.0,0.0 be referring to the pixel at the beginning of that array or the 0,63 pixel?


At the beginning of the array. Textures are stored that way, it's just referred to as the bottom of the texture. I think you can change it with glPixelStorei.. (for glTexImage2D, which puts the pixels in the texture).
You need to match how you are loading your textures to the texture coordinates. If you load a texture in a way that expects the first value to be the top left corner, then you need to use (0, 0) for the top left corner.

EDIT: I stated it wrong in my previous post. The texture will be in whatever order you want it to be. If you load a top-left-first texture, then 0,0 will be the top left.
The upside-down problem is that your glOrtho and gluPerspective calls flip the Y-axis compared to each other.
Brother Bob
Brother Bob
Quote:
Original post by devnub
okay so.. err.. lets get back to basics because i think i might have misunderstood something severely..

the texture coord 1.0 on the y-axis, does it map to the bottom or top of a top-left oriented image?

for example, lets say i have a texture of size 64,64, and it's stored in memory so that pixel 0,0 is at the beginning of the array and pixel 64,64 is at the end... Will texture coord 0.0,0.0 be referring to the pixel at the beginning of that array or the 0,63 pixel?

Texture coordinate 0 maps to the first row/column of the image passed to glTexImage, and 1 maps to the last row/column. So texture coordinate (0,0) always refers to the very first pixel of the image as store in memory.

What is up or down, or left or right, is up to you to decice, but you have to know what you decide and design everything, including all coordinate systems, to properly and consistently use that convention.
devnub
devnub
thanks guys!

dunno how I got confused into thinking it was the other way around. lol.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.