Game Development Reference
In-Depth Information
if (mTranslucentBackground) {
gl.glClearColor(0, 0, 0, 0.5f);
} else {
gl.glClearColor(1, 1, 1, 0.5f);
}
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private boolean mTranslucentBackground;
private Cube mCube;
private float mAngle;
}
Cube Class
CubeRenderer delegates drawing to the Cube class (see Listing 5-6). This class defines a 12-sided cube with
8 vertices (8 * x,y,z coordinates ), 32 colors (8 vertices * 4 ARGB values), and 36 indices for the x,y,z
coordinates of each side. The class consists of two methods:
Cube() : This is the class constructor. It initializes arrays for the vertices, colors, and
indices required to draw. It then uses direct Java buffers to place the data on the
native heap, where the garbage collector cannot move them. This is required by
the gl*Pointer() API functions that do the actual drawing.
draw() : To draw the cube, we simply set the vertices and colors, and issue a call to
glDrawElements using triangles ( GL_TRIANGLES ). Note that a cube has 6 faces, 8,
vertices, and 12 sides:
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, 36
, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
Listing 5-6. Cube Class for the GL Cubes Sample
package opengl.scenes.cubes;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
/**
* A vertex shaded cube.
*/
public class Cube {
public Cube() {
int one = 0x10000;
Search WWH ::




Custom Search