PDA

View Full Version : [OpenGL] What is wrong with my texture coordinates?



wodzu
17-02-2011, 08:19 PM
Hi guys.

So I am fighting with the textures... and I don't get one thing.
In Nate's Robin OpenGL texture tutorial, the texture coordinates coresponding like this:

(0,0) Bottom Left of the texture
(1,0) Bottom Right of the texture
(1,1) Top Right of the texture
(0,1) Top Left of the texture

But if I try to set up texture coordinates in my project, the texture is rotated 180 degrees.

Take a look at the screenshot:

http://img145.imageshack.us/img145/5111/quadp.png (http://img145.imageshack.us/i/quadp.png/)

And the texture itself (sorry for the small size, it is 16x16 px):

http://img191.imageshack.us/img191/2580/smalltex.png (http://img191.imageshack.us/i/smalltex.png/)

And the code:



glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glLoadIdentity;
gluLookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, SmallTex);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(0, 0, 0);
glTexCoord2f(1,0); glVertex3f(1, 0, 0);
glTexCoord2f(1,1); glVertex3f(1, 1, 0);
glTexCoord2f(0,1); glVertex3f(0, 1, 0);
glEnd;
glDisable(GL_TEXTURE_2D);
SDL_GL_SWAPBUFFERS;




I've double checked the coordinates of my quad. I am not doing any rotations.

I am using: LoadGLTextureFromFile('SmallTex.png'); from Vampyre Imaging Library to load the texture. But it is hard for me to belive that this function is messing with the texture...

So what is wrong guys?


Thanks for your time.

chronozphere
18-02-2011, 12:04 AM
First I have to say that this looks really odd indeed. I know the OpenGL V axis is flipped, but you allready took that into account. ???



But it is hard for me to belive that this function is messing with the texture...


You could always check the sources and verify this. :)

Are you loading it as Bitmap or as TGA? Normally you would expect the first pixel to be the top-left one, but this might not be true, depending on your fileformat.

Also you can check your camera. Maybe you should try drawing a fullscreen quad with glVertex2i() or something to rule out any transformation issues.

Finally, it would help if you could post the full sources, if that's possible. :)

Edit: I just checked your camera from the given source and it should be Ok. I know nothing about PNG actually. Maybe try BMP and see if that works differently?

Edit2: I think the sollution to your issue can be found on this page (section 12 about 2D coordinates):
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/

After reading this, I am not sure if OpenGL just flips the V axis, reads image-data upside down or BOTH! I'd like to know what causes your problem.

User137
18-02-2011, 09:12 AM
I just tested your code with my engine and can say it looks to be working as intended. You have to flip texture Y coordinates for it to show otherwise.

This is why when drawing 2D i create Ortho mode so that 0,0 is top left of screen. 3D should always remain "flipped" so that 0 is "ground" level.

wodzu
18-02-2011, 10:26 AM
First of all,

Thank you very mich guys for your time :) I really appreciate it...

@chronozphere:

There is no problem to give my sources but I've stripped out any unecessery code. I've tourned off my camera object, cause it is mess. A real mess:( I've spent days with toying with it, and it works on some planes and on others it is just going crazy...:| It deserves another topic which I am about to write but for now I am just tired with it so I picked up texturing.

@User137:

Thank you, atleast I know that I am not doing anything wrong.

I've read the FAQ section which chronozphere gave to me. Honestly I don't know if it made things any clearer now :D
I knew that the 3D origin follows the lower-left rule and that 2D origin is commonly following upper-left corner rule for (0,0).

What I didn't know and I am afraid I still don't know is what rule is given for the texture coordinates. Is it lower-left or upper-left?

After further investigation and checking Nate's Robin texture in his tutorial http://www.xmission.com/~nate/tutors.html I see that oryginally the texture in file is flipped. So maybe he flipped the texture so texture coordinates could stay in convention with vertex coordinates.

That would make things easier with assigning texture coordinates but would require to flip the texture each time when you want to edit it in some program. Or to flip it after loading.

User137
18-02-2011, 04:56 PM
Like i mentioned, you can just flip the texture coordinates:

glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex3f(0, 0, 0);
glTexCoord2f(1,1); glVertex3f(1, 0, 0);
glTexCoord2f(1,0); glVertex3f(1, 1, 0);
glTexCoord2f(0,0); glVertex3f(0, 1, 0);
glEnd;

And also, the whole thing is only good to know when doing graphics manually. Much of 3D graphics are actual models and the texture coordinates of that system work exactly as is. Textures on 3D models look exactly the same as in modelling program.

wodzu
18-02-2011, 07:31 PM
And also, the whole thing is only good to know when doing graphics manually. Much of 3D graphics are actual models and the texture coordinates of that system work exactly as is. Textures on 3D models look exactly the same as in modelling program.

I am sure you have right, but for terrain it matters. Anyway, I don't want to move into to the 3D models without knowing the basics ;)