简体   繁体   中英

OpenGL texture coordinates being skewed when using vertex arrays

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?

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);

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

just before glEnableClientState( GL_TEXTURE_COORD_ARRAY );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM