3

Have noticed an unexpected behavior of OpenGL (nVidia's card), when I try to map a texture on a 2D polygon. The image seems to be "broken" into 2 pieces:

Broken texture on 2D polygon.

Here's the source texture:

the source texture

The simplified code snippet:

GLuint textId;
glGenTextures( 1, &textId );
glBindTexture( GL_TEXTURE_2D, textId );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, img.width(), img.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)img.bits() );
glEnable( GL_TEXTURE_2D );

glBegin( GL_TRIANGLE_FAN ); // the same effect for GL_QUADS
    glTexCoord2d( 0.0, 0.0 );
    glVertex2d( -0.5, -0.3 );
    glTexCoord2d( 1.0, 0.0 );
    glVertex2d(  0.5, -0.8 );
    glTexCoord2d( 1.0, 1.0 );
    glVertex2d(  0.5,  0.8 );
    glTexCoord2d( 0.0, 1.0 );
    glVertex2d( -0.5,  0.3 );
glEnd();

What is the cause for such a strange effect? I expect to see my texture just broadening from left to right, with the white lines remaining parallel.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • This happens because the texture coordinates are interpolated linearly along the diagonal. Actually, there is not much you can do about it. You could use a texture that matches the shape (i.e. a trapezoid). Or you could try bilinear interpolation. But that would require a special shader. To reduce the problem, you could subdivide the quad and specify correct texture coordinates at the diagonal. – Nico Schertler Sep 08 '16 at 14:21
  • 1
    You could make use of 4d texture coordinates as explained [here](http://stackoverflow.com/questions/15242507/perspective-correct-texturing-of-trapezoid-in-opengl-es-2-0) – BDL Sep 08 '16 at 14:53
  • Thanks for the link, it seems to be related to the same problem. – user6809218 Sep 08 '16 at 16:59

0 Answers0