Game Development Reference
In-Depth Information
public class
DirectionalLight {
float
[] ambient = { 0.2f, 0.2f, 0.2f, 1.0f };
float
[] diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
float
[] specular = { 0.0f, 0.0f, 0.0f, 1.0f };
float
[] direction = { 0, 0, -1, 0 };
int
lastLightId = 0;
public void
setAmbient(
float
r,
float
g,
float
b,
float
a) {
ambient[0] = r;
ambient[1] = g;
ambient[2] = b;
ambient[3] = a;
}
public void
setDiffuse(
float
r,
float
g,
float
b,
float
a) {
diffuse[0] = r;
diffuse[1] = g;
diffuse[2] = b;
diffuse[3] = a;
}
public void
setSpecular(
float
r,
float
g,
float
b,
float
a) {
specular[0] = r;
specular[1] = g;
specular[2] = b;
specular[3] = a;
}
public void
setDirection(
float
x,
float
y,
float
z) {
direction[0] = -x;
direction[1] = -y;
direction[2] = -z;
}
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
, direction, 0);
lastLightId = lightId;
}
public void
disable(GL10 gl) {
gl.glDisable(lastLightId);
}
}
Our helper class is nearly identical to the
PointLight
class. The only difference is that the
direction array has its fourth element set to 1. We also have a
setDirection()
method instead of a
setPosition()
method. The
setDirection()
method allows us to specify a direction, like (-1,0,0)
so that the light comes from the right side. Inside the method, we just negate all of the vector