Thursday, April 20, 2017

java - Texture will not rotate with gluSphere


I have a gluSphere generated in my display that is rotating, but having a problem with the texture rotating with the sphere as well. So in a nutshell, no matter how the sphere rotates, the texture around the sphere remains in the same position regardless.


How am able to update the texture along with the sphere so that it rotates?


private static Texture testTex;
...


// This method is called in init()
public void sphereTexture(GL2 gl)
{
InputStream iStream = null;

try
{
iStream = getClass().getResourceAsStream("cratepng.png");
TextureData data = TextureIO.newTextureData(iStream, false, null);
testTex = TextureIO.newTexture(data);

testTex.getImageTexCoords();
testTex.setTexParameteri(GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
testTex.setTexParameteri(GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
iStream.close();
}
catch(IOException e)
{
e.printStackTrace();
System.exit(1);
}

// Set material properties
gl.glEnable(GL2.GL_TEXTURE_GEN_S);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);

gl.glEnable(GL2.GL_TEXTURE_GEN_T);
gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
gl.glTexGeni(GL2.GL_T, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);

gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);

gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);

testTex.setTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S);
testTex.setTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T);
}

// Draw Sphere (FILL, LINE, POINT) method called in display()
public void draw(GL2 gl, GLU glu)
{


gl.glTranslatef(1f, 0.0f, -20.0f);
gl.glRotatef(angleSphere, 1f, 0.0f, 0.0f); //90

GLUquadric sphere = glu.gluNewQuadric();
glu.gluQuadricDrawStyle(sphere, GLU.GLU_LINE);
glu.gluQuadricTexture(sphere, true);
glu.gluQuadricNormals(sphere, GLU.GLU_SMOOTH);
glu.gluQuadricOrientation(sphere, GLU.GLU_OUTSIDE);

testTex.enable(); // enable texture

testTex.bind(); // bind texture

glu.gluSphere(sphere, radius, slices, stacks);

testTex.disable();
glu.gluDeleteQuadric(sphere);
angleSphere += speed;
//System.out.println(angleSphere);

}


Answer



After posting your glEnables I see that you are enabling sphere mapping with glEnable(GL_TEXTURE_GEN_S) and glEnable(GL_TEXTURE_GEN_T) and generate texture coordinates. You shouldn't need to do this since this is done by glu (see here).


Nonetheless, what looks more suspicious is your use of GL_NORMAL_MAP in the glTexGeni function. In the openGL reference page there is no description of how the texture coordinates are calculated using this parameter. You should either remove these two texture generation blocks or try a different parameter in the glTexGeni i.e. GL_OBJECT_LINEAR.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...