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