Game Development Reference
In-Depth Information
public void enable(GL10 gl, int lightId) {
gl.glEnable(lightId);
gl.glLightfv(lightId, GL10. GL_AMBIENT , ambient, 0);
gl.glLightfv(lightId, GL10. GL_DIFFUSE , diffuse, 0);
gl.glLightfv(lightId, GL10. GL_SPECULAR , specular, 0);
gl.glLightfv(lightId, GL10. GL_POSITION , position, 0);
lastLightId = lightId;
}
public void disable(GL10 gl) {
gl.glDisable(lastLightId);
}
}
Our helper class stores the ambient, diffuse, and specular color components of the light as well
as the position (with the fourth element set to 1). In addition, we store the last light identifier used
for this light so that we can offer a disable() method that will turn off the light if necessary. For
each light attribute, we have a nice setter method. We also have an enable() method, which
takes a GL10 instance and a light identifier (like GL10.GL_LIGHT6 ). It enables the light, sets its
attributes, and stores the light identifier used. The disable() method just disables the light using
the lastLightId member set in enable() .
We use sensible defaults for the ambient, diffuse, and specular colors in the initializers of the
member arrays. The light will be white, and it will not produce any specular highlights because
the specular color is black.
Directional Lights
A directional light is nearly identical to a point light. The only difference is that it has a direction
instead of a position. The way the direction is expressed is somewhat confusing. Instead of
using a direction vector, OpenGL ES expects us to define a point in the world. The direction
is then calculated by taking the direction vector from that point to the origin of the world. The
following snippet would produce a directional light that comes from the right side of the world:
float[] dirPos = {1, 0, 0, 0};
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, dirPos, 0);
We can translate that to a direction vector:
dir = -dirPos = {-1, 0, 0, 0}
The rest of the attributes, like the ambient or diffuse color, are identical to those of a point light.
Listing 11-4 shows the code of a little helper class for diffuse lights.
Listing 11-4. DirectionalLight.java, a Simple Abstraction of OpenGL ES Directional Lights
package com.badlogic.androidgames.framework.gl;
import javax.microedition.khronos.opengles.GL10;
 
Search WWH ::




Custom Search