0

I have the following code to try to draw a rectangle using vertex arrays:

glEnableClientState( GL_NORMAL_ARRAY );
glNormalPointer( GL_FLOAT, 0, &mNorms[ 0 ] );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer( 2, GL_FLOAT, 0, &mTexCrds[ 0 ] );
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_FLOAT, 0, &mVtxs[ 0 ] );
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );

The intent is to draw a simple rectangle via triangle strip using the corner pattern Far Left, Near Left, Far Right, Near Right. The variable values are as follows:

         /* far left              near left            far right             near right */
mNorms   { 0.0, 1.0, 0.0,         0.0, 1.0, 0.0,       0.0, 1.0, 0.0,        0.0, 1.0, 0.0 }
mTexCrds { 0.0, 1.0,              0.0, 0.0,            1.0, 1.0,             1.0, 0.0 }
mVtxs    { -25.0, 0.0, -100.0,    -25.0, 0.0, -50.0,   25.0, 0.0, -100.0,    25.0, 0.0, -50.0 }

My texture is a black tile with a thin blue border around all edges. When the rectangle is drawn using the above instructions, it is completely blue:

bad.jpg http://www.graemecentralstation.com/img/bad.jpg

Obviously, given the above texture coordinates I am expecting my 2D texture to be completely visible.

If I replace glDrawArrays() with a manually invocation using the same array data like this...

glBegin( GL_TRIANGLE_STRIP );
{
    for( int i = 0; i < 4; ++i )
    {
        glNormal3fv( &mNorms[ i * 3 ] );
        glTexCoord2fv( &mTexCrds[ i * 2 ] );
        glVertex3fv( &mVtxs[ i * 3 ] );
    }
}
glEnd();

... then the rectangle is drawn with the full texture map image visible as I expect:

good.jpg http://www.graemecentralstation.com/img/good.jpg

Any idea why glDrawArrays() would skew my texture so badly?

xpnctoc
  • 63
  • 8
  • 1
    IMO if you're going through all the trouble of setting up `glVertexPointer`, etc. you may as well just use vertex buffer objects. It'll likely perform better. – Colonel Thirty Two Oct 20 '14 at 18:08

2 Answers2

0

You can try to debug with using texture coordinates as colors in the fragment shader, so then you can see whether texture coordinates are correct.gl_FragColor = vec4( vec2( texCoord),0,1);

Hakes
  • 631
  • 3
  • 13
-1

You perhaps need to add
glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_2D,texID);

just before glEnableClientState( GL_TEXTURE_COORD_ARRAY );

Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39