Jump to content
  • Advertisement
Sign in to follow this  
Smit

Correct Opengl TexCoords?

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm confused about the correct order of texcoords in OpenGL. This article says that the bottom left is 0.0f/0.0f and the top right is 1.0f/1.0f but my texture is rotated when I draw using those coords. I load it from a BMP using com.sun.opengl.util.texture.TextureIO which works fine, and then draw it like so:
gl.glTexCoord2f( 0.0f, 0.0f );
gl.glVertex3f( 100.0f, 100.0f, 0.0f );
gl.glTexCoord2f( 0.0f, 1.0f );
gl.glVertex3f( 200.0f, 100.0f, 0.0f );
gl.glTexCoord2f( 1.0f, 1.0f );
gl.glVertex3f( 200.0f, 200.0f, 0.0f );
gl.glTexCoord2f( 1.0f, 0.0f );
gl.glVertex3f( 100.0f, 200.0f, 0.0f );
The texture looks like: However when I draw I get: Am I doing something stupid? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
1 gl.glTexCoord2f( 0.0f, 0.0f );
2 gl.glVertex3f( 100.0f, 100.0f, 0.0f );
3 gl.glTexCoord2f( 0.0f, 1.0f );
4 gl.glVertex3f( 200.0f, 100.0f, 0.0f );
5 gl.glTexCoord2f( 1.0f, 1.0f );
6 gl.glVertex3f( 200.0f, 200.0f, 0.0f );
7 gl.glTexCoord2f( 1.0f, 0.0f );
8 gl.glVertex3f( 100.0f, 200.0f, 0.0f );

Sinc you're selecting your texture coordinates counterclockwise and your vertices clockwise, you're bound to get the wrong result. I suggest swapping line 4 and 8.

Share this post


Link to post
Share on other sites
Ah, thank you. Next problem [smile], I changed the texcoords around instead of the vertices but now the texture is flipped vertically. I know BMPs are stored upside down but I thought that the TextureIO class would flip it the right way around when loading.

Changed code:

gl.glTexCoord2f( 0.0f, 0.0f );
gl.glVertex3f( 100.0f, 100.0f, 0.0f );
gl.glTexCoord2f( 1.0f, 0.0f );
gl.glVertex3f( 200.0f, 100.0f, 0.0f );
gl.glTexCoord2f( 1.0f, 1.0f );
gl.glVertex3f( 200.0f, 200.0f, 0.0f );
gl.glTexCoord2f( 0.0f, 1.0f );
gl.glVertex3f( 100.0f, 200.0f, 0.0f );


I could fix it by flipping the texcoords as well but that seems kind of hacky.

Share this post


Link to post
Share on other sites
My guess is that you set the coordinate system with the origin in the upper left corner. Like, gluOrtho2D(0, width, height, 0) or something. OpenGL defaults with the coordinate system in the LOWER left corner (positive Y-axis poiting upwards), and if you step away from that default, you also step away from any consistencies regarding coordinate systems in OpenGL.

Concider placing the origin in the lower left corner, adap your code, and watch everything become consistent... and correct [rolleyes]

Flipping texture coordinates is another solution, yes, and a good example of inconsistent coordinate systems.

Share this post


Link to post
Share on other sites
I'm using glu.gluOrtho2D( 0, 320, 0, 240 ); This should give me 0/0 bottom left and 340/240 top right shouldn't it?

Share this post


Link to post
Share on other sites
It should, yes. Have you verified that the image loader actually loads the image bottom up, which is what OpenGL expects, or do you just assume it does?

Share this post


Link to post
Share on other sites
An easy hack is to call glScale(1.0, -1.0, 1.0); on the texture coordinates and that will flip the image. Remember you will have to use the texture matrix for this.

Or you could use automatic texture coordinate generation and flip the Y there also. Which works nice to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!