简体   繁体   中英

OpenGL texture coordinates issue

I'm having an issue with texture mapping in 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): 在此处输入图片说明

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.

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.

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