Game Development Reference
In-Depth Information
@Override
public void pause() {
}
@Override
public void dispose() {
}
}
We have a field in which to store the cube's mesh, a Texture instance, and a float in which to
store the current rotation angle. In the constructor, we create the cube mesh and load the texture
from an asset file called crate.png , a 128×128-pixel image of one side of a crate.
The cube creation code is located in the createCube() method. It just sets up the vertices and
indices, and creates a Vertices3 instance from them. Each vertex has a 3D position and texture
coordinates.
The resume() method just tells the texture to reload it. Remember, textures must be reloaded
after an OpenGL ES context loss.
The update() method just increases the rotation angle by which we'll rotate the cube around the
y axis.
The present() method first sets the viewport and then clears the framebuffer and depthbuffer.
Next, we set up a perspective projection and load an identity matrix to the model-view matrix
of OpenGL ES. We enable depth testing and texturing, and bind the texture as well as the cube
mesh. We then use glTranslatef() to move the cube to the position (0,0,-3) in world space.
With glRotatef() , we rotate the cube in model space around the y axis. Remember that the
order in which these transformations get applied to the mesh is reversed. The cube will first be
rotated (in model space), and then the rotated version will be positioned in world space. Finally,
we draw the cube, unbind the mesh, and disable depth testing and texturing. We don't need to
disable those states; we simply disable those states in case we are going to render 2D elements
on top of the 3D scene. Figure 10-13 shows the output of the first real 3D program.
Figure 10-13. A spinning texture cube in 3D
 
Search WWH ::




Custom Search