Game Development Reference
In-Depth Information
The first parameter is the light identifier; in this case we use the fourth light. The next parameter
specifies the light's attribute that we want to modify. The third parameter is again a float array
that holds the RGBA values, and the final parameter is an offset into that array. Specifying the
position is easy:
float[] position = {x, y, z, 1};
gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_POSITION, position, 0);
We again specify the attribute we want to modify (in this case the position), along with a four-
element array that stores the x, y, and z coordinates of the light in our world. Note that the fourth
element of the array must be set to 1 for a positional light source! Let's put this into a helper
class. Listing 11-3 shows the code.
Listing 11-3. PointLight.java, a Simple Abstraction of OpenGL ES Point Lights
package com.badlogic.androidgames.framework.gl;
import javax.microedition.khronos.opengles.GL10;
public class PointLight {
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 [] position = { 0, 0, 0, 1 };
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 setPosition( float x, float y, float z) {
position[0] = x;
position[1] = y;
position[2] = z;
}
Search WWH ::




Custom Search