简体   繁体   English

使用顶点数组时OpenGL纹理坐标偏斜

[英]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 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. 显然,鉴于上述纹理坐标,我期望2D纹理完全可见。

If I replace glDrawArrays() with a manually invocation using the same array data like this... 如果我使用类似这样的数组数据通过手动调用替换glDrawArrays(),则...

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 good.jpg http://www.graemecentralstation.com/img/good.jpg

Any idea why glDrawArrays() would skew my texture so badly? 知道为什么glDrawArrays()会严重扭曲我的纹理吗?

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 ); 就在glEnableClientState( GL_TEXTURE_COORD_ARRAY );之前glEnableClientState( GL_TEXTURE_COORD_ARRAY );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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