简体   繁体   English

OpenGL纹理坐标问题

[英]OpenGL texture coordinates issue

I'm having an issue with texture mapping in OpenGL. 我在OpenGL中遇到纹理映射问题。 I'm attempting to apply different textures to different objects in the scene, but I'm finding that applying a second texture to a second object is changing the texture coordinates (I think) on the first one. 我试图将不同的纹理应用于场景中的不同对象,但是我发现将第二个纹理应用于第二个对象正在更改第一个对象的纹理坐标(我认为)。 The two objects are a cube and a cylinder. 这两个对象是一个立方体和一个圆柱体。

With only one texture enabled, the scene looks like this: 仅启用一个纹理,场景如下所示: 在此处输入图片说明

If I then enable textures for 1 or more other objects, I see this (I'm not too bothered about the bad texture mapping on the "wheels" right now): 如果然后为1个或多个其他对象启用纹理,我会看到这一点(我现在不太担心“滚轮”上不良的纹理映射): 在此处输入图片说明

What could be causing this? 是什么原因造成的? My drawing code for the cube and cylinders is below: 我的立方体和圆柱体的绘图代码如下:

Cube: 立方体:

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

Cylinder: 圆筒:

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

Assign your texture coordinates BEFORE each call to glVertex, not after - calls after that apply to the following glVertex call. 在每次调用glVertex之前而不是之后分配纹理坐标-之后的调用适用于以下glVertex调用。

The way you are doing it, the last coordinate set is being applied to the first vertex (ie a corner of your cube) in the next render cycle - hence the difference when you render additional objects. 这样做的方式是,在下一个渲染周期中将最后一个坐标集应用于第一个顶点(即多维数据集的一个角)-因此,渲染其他对象时会有所不同。

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

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