Game Development Reference
In-Depth Information
Ambient light is a special case, as it does not have an identifier. There is only one ambient light
ever in an OpenGL ES scene. Let's have a look at that.
Ambient Light
Ambient light is a special type of light, as explained already. It has no position or direction, but
only a color by which all objects in the scene will be uniformly lit. OpenGL ES lets us specify the
global ambient light as follows:
float[] ambientColor = { 0.2f, 0.2f, 0.2f, 1.0f };
gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, color, 0);
The ambientColor array holds the RGBA values of the ambient light's color encoded as floats in
the range 0 to 1. The glLightModelfv() method takes as parameters a constant specifying that
we want to set the ambient light's color; the float array holding the color; and an offset into the
float array from which the method should start reading the RGBA values. Let's put this into a
lovely little class. Listing 11-2 shows the code.
Listing 11-2. AmbientLight.java, a Simple Abstraction of OpenGL ES Global Ambient Light
package com.badlogic.androidgames.framework.gl;
import javax.microedition.khronos.opengles.GL10;
public class AmbientLight {
float [] color = {0.2f, 0.2f, 0.2f, 1};
public void setColor( float r, float g, float b, float a) {
color[0] = r;
color[1] = g;
color[2] = b;
color[3] = a;
}
public void enable(GL10 gl) {
gl.glLightModelfv(GL10. GL_LIGHT_MODEL_AMBIENT , color, 0);
}
}
All we do is store the ambient light's color in a float array and then provide two methods: one to
set the color and the other to make OpenGL ES use the ambient light color that we define. By
default, we use a gray ambient light color.
Point Lights
Point lights have a position as well as an ambient, diffuse, and specular color/intensity (we leave
out the emissive color/intensity). To specify the different colors, we can do the following:
gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_AMBIENT, ambientColor, 0);
gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_DIFFUSE, diffuseColor, 0);
gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_SPECULAR, specularColor, 0);
 
Search WWH ::




Custom Search