Game Development Reference
In-Depth Information
Renderer _renderer = new Renderer();
public MatrixTestState(TextureManager textureManager)
{
_faceSprite.Texture = textureManager.Get("face");
Gl.glEnable(Gl.GL_TEXTURE_2D);
}
public void Render()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
_renderer.DrawSprite(_faceSprite);
_renderer.Render();
}
public void Update(double elapsedTime)
{
}
}
This code uses the face sprite from the earlier chapters. The matrices will be
applied to the sprite in the MatrixTestState constructor.
Matrix m = new Matrix();
m.SetRotate(new Vector(0, 0, 1), Math.PI/5);
for (int i = 0; i < _faceSprite.VertexPositions.Length; i++ )
{
_faceSprite.VertexPositions[i] *= m;
}
Run the code and you will notice that the face has been rotated. The rotation is
done along the Z axis (0, 0, 1); this is the axis that comes out of the screen.
Imagine the face sprite is a piece of paper on the screen. To rotate it, you stick a
pin through it, attaching it to the screen and damaging your monitor! The pin
represents the Z axis. Spinning the paper sprite now will spin it around that axis.
For 2D objects in an orthographic projection, the Y axis and Z axis aren't very
useful for rotations, but they would be useful in a 3D game. Any normalized axis
can be used to rotate an object, not just the major X, Y, and Z axes.
In the code example, the rotation amount is given in radians Math.PI/5 , which
is equivalent to 36 degrees. The rotation matrix is applied to each vertex that
makes up the sprite. We've now used one matrix that slightly rotates the sprite;
Search WWH ::




Custom Search