Game Development Reference
In-Depth Information
color based on the ambient, diffuse, and specular properties of the material of the object and the
light source. The end result is a color for each vertex of an object that is then interpolated over
each triangle in combination with the calculated colors of the other vertices. This interpolated
color will then be combined with any texture maps we apply to the object.
This sounds scary, but it really isn't that bad. All we need to do is enable lighting and specify the
light sources, the material for the object we want to render, and the vertex normals, in addition to
the other vertex attributes we usually specify, like position and texture coordinates. Let's have a
look at how to implement all of this with OpenGL ES.
In Practice
We'll now go through all of the necessary steps to get lighting to work with OpenGL ES. Along
the way, we'll create a few little helper classes that make working with light sources a bit easier.
We'll put those in the com.badlogic.androidgames.framework.gl package.
Enabling and Disabling Lighting
As with all OpenGL ES states, we first have to enable the functionality in question. We do that
with this:
gl.glEnable(GL10.GL_LIGHTING);
Once enabled, lighting will be applied to all the objects that we render. Of course, we'll have to
specify the light sources, materials, and vertex normals to achieve meaningful results. After we
render all the objects that should be illuminated, we can disable lighting again:
gl.glDisable(GL10.GL_LIGHTING);
Specifying Light Sources
As previously discussed, OpenGL ES offers us four types of light sources: ambient lights, point
lights, directional lights, and spotlights. We'll take a look at how to define the first three. In order
for spotlights to be effective and to look good, we'd need to have a very high triangle count for
each of our objects' models. This is prohibitive on most current mobile devices.
OpenGL ES limits us to having a maximum of eight light sources in a scene, plus a global
ambient light. Each of the eight light sources has an identifier, from GL10.GL_LIGHT0 up to
GL10.GL_LIGHT7 . If we want to manipulate the properties of one of these light sources, we do
so by specifying the respective ID of that light source.
Light sources have to be enabled with this syntax:
gl.glEnable(GL10.GL_LIGHT0);
In this case, OpenGL ES will then take the properties of the light source with ID 0 and apply
it to all rendered objects accordingly. If we want to disable a light source, we can do it with a
statement like this:
gl.glDisable(GL10.GL_LIGHT0);
 
Search WWH ::




Custom Search