Game Development Reference
In-Depth Information
In Figure 7-19 , we have a single model defined via a Vertices instance—for example, like this:
Vertices vertices = new Vertices(glGraphics, 4, 12, false , false );
vertices.setVertices( new float [] { −50, -50,
50, -50,
50, 50,
-50, 50 }, 0, 8);
vertices.setIndices( new short [] {0, 1, 2, 2, 3, 0}, 0, 6);
For our discussion, we just leave out any vertex colors or texture coordinates. Now, when we
render this model without any further modifications, it will be placed around the origin in the
world space in our final image. If we want to render it at a different position—say, its center
being at (200,300) in world space—we could redefine the vertex positions like this:
vertices.setVertices( new float [] { −50 + 200, -50 + 300,
50 + 200, -50 + 300,
50 + 200, 50 + 300,
-50 + 200, 50 + 300 }, 0, 8);
On the next call to vertices.draw() , the model would be rendered with its center at (200,300),
but this is a tad bit tedious, isn't it?
Matrices Again
Remember when we briefly talked about matrices? We discussed how matrices can encode
transformations, such as translations (moving stuff around), rotations, and scaling. The
projection matrix we use to project our vertices onto the projection plane encodes a special type
of transformation: a projection.
Matrices are the key to solving our previous problem more elegantly. Instead of manually
moving our vertex positions around by redefining them, we simply set a matrix that encodes
a translation. Since the projection matrix of OpenGL ES is already occupied by the orthogonal
graphics projection matrix we specified via glOrthof() , we use a different OpenGL ES matrix:
the model-view matrix. Here's how we could render our model with its origin moved to a specific
location in eye/world space:
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(200, 300, 0);
vertices.draw(GL10.GL_TRIANGLES, 0, 6);
We first have to tell OpenGL ES which matrix we want to manipulate. In our case, that's the
model-view matrix, which is specified by the constant GL10.GL_MODELVIEW . Next, we make
sure that the model-view matrix is set to an identity matrix. Basically, we just overwrite
anything that was in there already—we sort of clear the matrix. The next call is where the
magic happens.
The method glTranslatef() takes three arguments: the translation on the x, y, and z axes.
Since we want the origin of our model to be placed at (200,300) in eye/world space, we specify
 
Search WWH ::




Custom Search