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
Lighting and glMultMatrixf problem
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()
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()
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement