Jump to content
  • Advertisement
Sign in to follow this  
suarsg

OpenGL Texture on Quad gets distorted

This topic is 3853 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

Hello guys, I'm not really a OpenGL-programmer and are just using OpenGL for a little thing I have to do, so please don't laugh at me :) I have several GL_QUADS on which I apply a picture as a texture. However, when I rotate the surfaces, I noticed that the texture gets sort of "distorted". I've spent literally several hours searching Google to find a solution. Just after a few minutes I found out that this is caused because internally OpenGL "splits" my quad into two triangles. And this is exactly what happens: Even though it's supposed to look (sorta) like this: I've even spent several more hours to try out solutions but none of them really worked (for example using glTexCoord4f() instead of glTexCoord2f()), or of course I was doing something wrong. Here's my current code for the quad:
glBegin(GL_QUADS);
  glTexCoord2f(0.0f, 0.0f);
  glVertex3f(-item->width/2.0f, 0.0f, z); //bottom left

  glTexCoord2f(1.0f, 0.0f);
  glVertex3f(item->width/2.0f, 0.0f, z); //bottom right

  glTexCoord2f(1.0f, 1.0f);
  glVertex3f(item->width/2.0f, item->height, z); //top right

  glTexCoord2f(0.0f, 1.0f);
  glVertex3f(-item->width/2.0f, item->height, z); //top left
glEnd();
The only solution that is slightly improving the view is to use a GL_POLYGON and draw a polygon with lots of vertexes, but I don't think that's the only solution? Thank you guys in advance!

Share this post


Link to post
Share on other sites
Advertisement
Thanks. But I already tried that and as I said it didn't produce my expected result (probably because I'm totally lost on how to actually use it correctly).
I looked at the sample code and tried to use it on mine:

glBegin(GL_QUADS);
GLfloat tx = item->height * item->width/2.0f;

glTexCoord2f(0.0f, 0.0f);
glVertex2f(-item->width/2.0f, 0.0f); //Bottom Left

glTexCoord2f(1.0f, 0.0f);
glVertex2f(item->width/2.0f, 0.0f); //Bottom Right

glTexCoord4f(tx, tx, 0.0f, tx);
glVertex2f(item->width/2.0f, item->height); //Top Right

glTexCoord4f(-tx, tx, 0.0f, tx);
glVertex2f(-item->width/2.0f, item->height); //Top Left

glEnd();


That's what the original picture looks like:


And here's what I get :)


I obviously have no clue on how to use that tx-variable correctly... Can someone please clue me in or something?

Thanks.

Share this post


Link to post
Share on other sites
First of all, in openGL usually texure has inverted y's compared to Vertices.
glBegin(GL_QUADS);
//TexCoords
//0,1----1,1
//| |
//| |
//| |
//| |
//| |
//| |
//0,0----1,0
//VertexCoords
//-x,-y---- x,-y
// | |
// | |
// | |
// | |
// | |
// | |
//-x, y---- x, y
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-item->width/2.0f, item->height/2.0f, z); //bottom left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(item->width/2.0f, item->height/2.0f, z); //bottom right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(item->width/2.0f, -item->height/2.0f, z); //top right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-item->width/2.0f, -item->height/2.0f, z); //top left
glEnd();

And second, most propably you may want to draw "from texture's middle" so it's good to have [-x/2,-y/2] to [x/2,y/2]
i fixed this for you, you're welcome.

PS. also this may become usefull as texcoord explaination
PS2. If this fails: divide the quad to two triangles AND figure out which Coords are needed.
glBegin(GL_TRIANGLES);//note that we can draw more than one figure between Begin and End
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-item->width/2.0f, item->height/2.0f, z); //bottom left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(item->width/2.0f, item->height/2.0f, z); //bottom right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-item->width/2.0f, -item->height/2.0f, z); //top left
//TexCoords
//0,1----1,1
//| \ |
//| \ |
//| \ |
//| \ |
//| \ |
//| \ |
//0,0----1,0
//VertexCoords
//-x,-y---- x,-y
// | \ |
// | \ |
// | \ |
// | \ |
// | \ |
// | \ |
//-x, y---- x, y
glTexCoord2f(1.0f, 0.0f);
glVertex3f(item->width/2.0f, item->height/2.0f, z); //bottom right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-item->width/2.0f, -item->height/2.0f, z); //top left
glTexCoord2f(1.0f, 1.0f);
glVertex3f(item->width/2.0f, -item->height/2.0f, z); //top right
glEnd();



[Edited by - YanPL on April 4, 2008 12:32:08 PM]

Share this post


Link to post
Share on other sites
Hello,

Thanks for the quick replies and the sample codes.
Unfortunately splitting the image into two triangles results in the same perspective distortion as here:


Also, having the texture start in the middle (width/2, height/2) didn't help it either :(

@dpadam450
I'm not quite sure what you're talking about :P
But here's my GL init:
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);

glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


And my resize:
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLfloat aspect = (GLfloat)width/(GLfloat)height;
GLfloat nearz = 0.1f;
GLfloat top = tan((90.0f*PI/180.0f)*0.5f)*nearz;

glFrustum(aspect*(-top), aspect*top, -top, top, nearz, 500.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!