我在OpenGL中遇到纹理映射问题。我正在尝试将不同的纹理应用于场景中的不同对象,但我发现将第二个纹理应用于第二个对象正在改变第一个纹理坐标(我认为)。这两个对象是立方体和圆柱体。
只启用了一个纹理,场景如下所示:
如果我然后为一个或多个其他对象启用纹理,我会看到这一点(我现在对“轮子”上的坏纹理映射不太感兴趣):
可能导致这种情况的原因是什么?我的立方体和圆柱的绘图代码如下:
立方体:
//Alias the values for easier reading
double X = Position().X;
double Y = Position().Y;
double Z = Position().Z;
double W = _fWidth;
double H = _fHeight;
double D = _fDepth;
//Set textures if necessary
if(super._blHasTexture)
{
super._obTexture.bind(gl);
}
_obTexture.enable(gl);
gl.glColor3f(0.5f, 0.5f, 0.7f);
//Draw
gl.glBegin(GL2.GL_QUADS);
//Front
gl.glNormal3f(0f, 0f, 1f);
gl.glVertex3d(X, Y, Z); gl.glTexCoord2f(0f, 0f);
gl.glVertex3d(X+W, Y, Z); gl.glTexCoord2f(1f, 0f);
gl.glVertex3d(X+W, Y-H, Z); gl.glTexCoord2f(1f, 1f);
gl.glVertex3d(X, Y-H, Z); gl.glTexCoord2f(0f, 1f);
//Back
gl.glNormal3f(0f, 0f, -1f);
gl.glVertex3d(X, Y, Z-D); gl.glTexCoord2f(0f, 0f);
gl.glVertex3d(X+W, Y, Z-D); gl.glTexCoord2f(1f, 0f);
gl.glVertex3d(X+W, Y-H, Z-D); gl.glTexCoord2f(1f, 1f);
gl.glVertex3d(X, Y-H, Z-D); gl.glTexCoord2f(0f, 1f);
//Left
gl.glNormal3f(-1f, 0f, 0f);
gl.glVertex3d(X, Y, Z); gl.glTexCoord2f(0f, 0f);
gl.glVertex3d(X, Y, Z-D); gl.glTexCoord2f(1f, 0f);
gl.glVertex3d(X, Y-H, Z-D); gl.glTexCoord2f(1f, 1f);
gl.glVertex3d(X, Y-H, Z); gl.glTexCoord2f(0f, 1f);
//Right
gl.glNormal3f(1f, 0f, 0f);
gl.glVertex3d(X+W, Y, Z); gl.glTexCoord2f(0f, 0f);
gl.glVertex3d(X+W, Y, Z-D); gl.glTexCoord2f(1f, 0f);
gl.glVertex3d(X+W, Y-H, Z-D); gl.glTexCoord2f(1f, 1f);
gl.glVertex3d(X+W, Y-H, Z); gl.glTexCoord2f(0f, 1f);
//Top
gl.glNormal3f(0f, 1f, 0f);
gl.glVertex3d(X, Y, Z); gl.glTexCoord2f(0f, 0f);
gl.glVertex3d(X+W, Y, Z); gl.glTexCoord2f(1f, 0f);
gl.glVertex3d(X+W, Y, Z-D); gl.glTexCoord2f(1f, 1f);
gl.glVertex3d(X, Y, Z-D); gl.glTexCoord2f(0f, 1f);
//Bottom
gl.glNormal3f(0f, -1f, 0f);
gl.glVertex3d(X, Y-H, Z); gl.glTexCoord2f(0f, 0f);
gl.glVertex3d(X+W, Y-H, Z); gl.glTexCoord2f(1f, 0f);
gl.glVertex3d(X+W, Y-H, Z-D); gl.glTexCoord2f(1f, 1f);
gl.glVertex3d(X, Y-H, Z-D); gl.glTexCoord2f(0f, 1f);
gl.glEnd();
_obTexture.disable(gl);
气缸:
GLUquadric quad = glu.gluNewQuadric();
glu.gluQuadricTexture(quad, true);
_obTexture.bind(gl);
_obTexture.enable(gl);
//Draw the caps
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glVertex3f(0f, 0f, 0.01f);
gl.glTexCoord2f(0f, 0f);
for(int t = 0; t < 20; t++)
{
float X = _fRadius * (float)Math.cos(t);
float Y = _fRadius * (float)Math.sin(t);
gl.glVertex3f(X, Y, 0.01f);
}
gl.glTexCoord2f(1f, 1f);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glVertex3f(0f, 0f, -0.01f + _fHeight);
gl.glTexCoord2f(0f, 0f);
for(int t = 0; t < 20; t++)
{
float X = _fRadius * (float)Math.cos(t);
float Y = _fRadius * (float)Math.sin(t);
gl.glVertex3f(X, Y, -0.01f + _fHeight);
}
gl.glTexCoord2f(1f, 1f);
gl.glEnd();
glu.gluCylinder(quad, _fRadius, _fRadius, _fHeight, 20, 1);
_obTexture.disable(gl);
答案 0 :(得分:4)
在每次调用glVertex之前分配纹理坐标,而不是在之后的调用之后分配你应用于以下glVertex调用。
您正在这样做,最后一个坐标集将应用于下一个渲染周期中的第一个顶点(即多维数据集的一个角) - 因此渲染其他对象时会有所不同。