Game Development Reference
In-Depth Information
Specifying Materials
A material is defined by a couple of attributes. As with anything in OpenGL ES, a material is a
state and will be active until we change it again or the OpenGL ES context is lost. To set the
currently active material attributes, we can do the following:
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, ambientColor, 0);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, diffuseColor, 0);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, specularColor, 0);
As usual, we have an ambient, a diffuse, and a specular RGBA color to specify. We again do this
via four-element float arrays, just as we did with the light source attributes. Putting this together
into a little helper class is again very easy. Listing 11-5 shows the code.
Listing 11-5. Material.java, a Simple Abstraction of OpenGL ES Materials
package com.badlogic.androidgames.framework.gl;
import javax.microedition.khronos.opengles.GL10;
public class Material {
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 };
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 enable(GL10 gl) {
gl.glMaterialfv(GL10. GL_FRONT_AND_BACK , GL10. GL_AMBIENT , ambient, 0);
gl.glMaterialfv(GL10. GL_FRONT_AND_BACK , GL10. GL_DIFFUSE , diffuse, 0);
gl.glMaterialfv(GL10. GL_FRONT_AND_BACK , GL10. GL_SPECULAR , specular, 0);
}
}
 
Search WWH ::




Custom Search