Game Development Reference
In-Depth Information
The model matrix is called the
m_OrientationMatrix
in our code. The
m_PositionMatrix
is the
translation matrix; the
m_RotationMatrix
is our rotation matrix; and the
m_ScaleMatrix
is our scale
matrix. We also have a
TempMatrix
for use for the temporary storage of matrices.
The
SetPositionMatrix()
function creates the translation matrix by first initializing the matrix to
the identity matrix, by calling
setIdentity()
and then creating the translation matrix by calling
translateM()
in the default Matrix class that is part of the standard Android library.
The
SetScaleMatrix()
function creates the scale matrix by first initializing the matrix to the identity
matrix and then calling
scaleM()
from the Matrix class library to create the scale matrix.
The
UpdateOrientation()
function actually builds the model matrix.
It first creates the translation matrix by calling
SetPositionMatrix()
.
1.
Next,
SetScaleMatrix()
is called to create the Scale Matrix.
2.
Then, the final model matrix starts to be built by calling
Matrix.multiplyMM()
to multiply the translation matrix by the rotation matrix.
3.
4.
Finally, the result matrix from step 3 is multiplied by the scale matrix and then
returned to the caller of the function. The net result is that a matrix is created
that first scales a 3D object, then rotates it around its axis of rotation, and
then finally puts it into the 3D world at a location specified by
m_Position
(see Listing 3-9).
Listing 3-9. Building the Model Matrix in the Orientation Class
// Orientation Matrices
private float[] m_OrientationMatrix = new float[16];
private float[] m_PositionMatrix = new float[16];
private float[] m_RotationMatrix = new float[16];
private float[] m_ScaleMatrix = new float[16];
private float[] TempMatrix = new float[16];
// Set Orientation Matrices
void SetPositionMatrix(Vector3 position)
{
// Build Translation Matrix
Matrix.setIdentityM(m_PositionMatrix, 0);
Matrix.translateM(m_PositionMatrix, 0, position.x, position.y, position.z);
}
void SetScaleMatrix(Vector3 Scale)
{
// Build Scale Matrix
Matrix.setIdentityM(m_ScaleMatrix, 0);
Matrix.scaleM(m_ScaleMatrix, 0, Scale.x, Scale.y, Scale.z);
}