Game Development Reference
In-Depth Information
The general form of homogeneous coordinates is (x,y,z,w). A point in homogeneous coordinates
can be converted to normal 3D Euclidean space coordinates by dividing all the coordinates by the
w coordinate. For example, given the point in homogeneous coordinates (x,y,z,w), the point in 3D
Euclidean space is (x/w,y/w,z/w).
A point in 3D Euclidean space denoted by (x,y,z) can be represented in homogeneous space by (x,y,z,1).
We will discuss OpenGL ES 2.0 vertex and fragment shaders in more depth in Chapter 4,
“3D Graphics Using OpenGL ES 2.0.”
Using Matrices to Move Objects
Matrices can be used to do many things, such as translating objects, rotating objects, scaling
objects, and projecting a 3D object onto a 2D screen. The matrix used to move objects in the 3D
world is called the translation matrix. In Figure 3-20 , the new position is calculated by converting the
old position into homogeneous coordinates, creating a matrix from this homogeneous coordinate,
and then multiplying it by the translation matrix. The values Tx, Ty, Tz indicate the amount to move
the object in the x, y, and z direction on the plane. Using matrix multiplication to find the new x, y,
and z coordinates results in the following:
x' = x + Tx
y' = y + Ty
z' = z + Tz
Figure 3-20. Translating an object
In terms of code, we use the translateM() function to translate the input matrix in place by x, y,
and z values.
For example, the following translates the m_PositionMatrix matrix in place:
//static void translateM(float[] m, int mOffset, float x, float y, float z)
//Translates matrix m by x, y, and z in place.
Matrix.translateM(m_PositionMatrix, 0, position.x, position.y, position.z);
Using Matrices to Rotate Objects
Matrices are also used to rotate 3D objects. One example of how to build a rotation matrix that
rotates around the x axis is shown in Figure 3-21 .
 
Search WWH ::




Custom Search