Game Development Reference
In-Depth Information
An Example
We want to create a cube mesh, as shown in the preceding snippets, with the crate texture
applied. Since we model the cube in model space around the origin, we have to use
glTranslatef()
to move it into world space, much like we did with Bob's model in the
BobTest
example. We also want our cube to spin around the y axis, which we can achieve by using
glRotatef()
, again like in the
BobTest
example. Listing 10-6 shows the complete code of the
CubeScreen
class contained in a
CubeTest
class.
Listing 10-6. Excerpt from CubeTest.java; Rendering a Texture Cube
class
CubeScreen
extends
GLScreen {
Vertices3 cube;
Texture texture;
float
angle = 0;
public
CubeScreen(Game game) {
super
(game);
cube = createCube();
texture =
new
Texture(glGame, "crate.png");
}
private
Vertices3 createCube() {
float
[] vertices = { −0.5f, -0.5f, 0.5f, 0, 1,
0.5f, -0.5f, 0.5f, 1, 1,
0.5f, 0.5f, 0.5f, 1, 0,
-0.5f, 0.5f, 0.5f, 0, 0,
0.5f, -0.5f, 0.5f, 0, 1,
0.5f, -0.5f, -0.5f, 1, 1,
0.5f, 0.5f, -0.5f, 1, 0,
0.5f, 0.5f, 0.5f, 0, 0,
0.5f, -0.5f, -0.5f, 0, 1,
-0.5f, -0.5f, -0.5f, 1, 1,
-0.5f, 0.5f, -0.5f, 1, 0,
0.5f, 0.5f, -0.5f, 0, 0,
-0.5f, -0.5f, -0.5f, 0, 1,
-0.5f, -0.5f, 0.5f, 1, 1,
-0.5f, 0.5f, 0.5f, 1, 0,
-0.5f, 0.5f, -0.5f, 0, 0,
-0.5f, 0.5f, 0.5f, 0, 1,
0.5f, 0.5f, 0.5f, 1, 1,
0.5f, 0.5f, -0.5f, 1, 0,
-0.5f, 0.5f, -0.5f, 0, 0,
-0.5f, -0.5f, 0.5f, 0, 1,
0.5f, -0.5f, 0.5f, 1, 1,