Jump to content
  • Advertisement
Sign in to follow this  
Spoon Thumb

OpenGL Lighting and glMultMatrixf problem

This topic is 2189 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've written a basic graphics engine for OpenGL-ES 1.1 in Java for Android. I use the following code to orientate the spaceships in my game to point in the direction of travel:
[source lang="java"]gl.glPushMatrix();
transforms.transform(gl);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, light0Position);
model.drawVertexBufferObjects((GL11) gl);
gl.glPopMatrix();[/source]

[source lang="java"]public void transform(GL10 gl) {
gl.glTranslatef(xyz[0], xyz[1], xyz[2]);
// where xyz holds the coordinates of the spaceship

gl.glScalef(scale,scale,scale);

Orientate(gl);

}[/source]
[source lang="java"]

private FloatBuffer startMatrixFB;
protected void Orientate(GL10 gl){
if(startMatrixFB==null){
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(16 * 4);
byteBuffer.order(ByteOrder.nativeOrder());
startMatrixFB = byteBuffer.asFloatBuffer();
}

startMatrixFB.position(0);

startMatrixFB.put((float) -rightOrientationVector.mX);
startMatrixFB.put((float) -rightOrientationVector.mY);
startMatrixFB.put((float) -rightOrientationVector.mZ);

startMatrixFB.put(0);

startMatrixFB.put((float) upOrientationVector.mX);
startMatrixFB.put((float) upOrientationVector.mY);
startMatrixFB.put((float) upOrientationVector.mZ);

startMatrixFB.put(0);

startMatrixFB.put((float) directionVector.mX);
startMatrixFB.put((float) directionVector.mY);
startMatrixFB.put((float) directionVector.mZ);

startMatrixFB.put(0);

startMatrixFB.put(0);
startMatrixFB.put(0);
startMatrixFB.put(0);
startMatrixFB.put(1);

startMatrixFB.position(0);

gl.glMultMatrixf(startMatrixFB);

}[/source]
The problem is that this doesn't affect the diffuse lighting. The lighting on the ship appears without taking into account the changes made by the Orientate() method. I.e. the light is always in the same place relative to the model:

[attachment=10204:lighting_problem_iridian.png]

If anyone can help me solve this, I'd be most appreciative

Thanks
James

Share this post


Link to post
Share on other sites
Advertisement
Thats because you have the light transformed exactly how your model is. Say your model has normals and lets treat the light as a normal as well.

If you rotate your model (and its normals) then you are rotating the light normal as well. Only call glLightfv() after you have applied the camera matrix only.
Identity()
cameramatrix()
glLight()

Identity()
ObjectMatrix()
Draw()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!