Академический Документы
Профессиональный Документы
Культура Документы
Lecture 2
January 13, 2014
3D Game Development
An Example:
A simple example to start.
It is in these files.
ColoredSquare_Win8 and ColoredSquare_WP8
Camera:
We need to set up the camera that will be used to produce the viewed image.
Consists of a view matrix and a projection matrix.
Just use the Microsoft.Xna.Framework. Game class here.
First need to add two class level variables.
Variables required for the scene to be rendered
private BasicEffect _effect;
private VertexPositionColor[] _vertices = new VertexPositionColor[4];
Now we need to set them up for use with the camera through the initialization function.
The beginning of the Initialize function, creating the projection matrix.
protected override void Initialize()
{
// Calculate the screen aspect ratio
float aspectRatio =
(float)GraphicsDevice.Viewport.Bounds.Width
/ GraphicsDevice.Viewport.Bounds.Height;
// Create a projection matrix
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
aspectRatio, 0.1f, 1000.0f);
A matrix creation function is used to create the projection matrix here.
Next we need to create the view matrix that tells us what the camera sees.
Initializing the view matrix
// Calculate a view matrix (where we are looking from and to)
Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up);
Vertices can hold various pieces of information. They will always hold a position, but in addition
to that they might contain color information, texture information, and other data that affects the
way they are drawn. MonoGame provides some built-in configurations for common vertex
structures, and the one we will use here is called VertexPositionColor. As its name implies, it
stores position and color information, and nothing more.
The origin is in the center of the square and the square extends one unit in each direction from
the center.
Each Effect is a container for a set of techniques. Here just one technique.
The technique can have multiple passes. Here just one but allow for multiple for future use.
The Apply method is called to tell MonoGame to activate it. The code then calls
DrawUserPrimitives in order for triangles to be drawn, telling it the type of primitive that it is
rendering and passing various details about what to draw. The parameters for the
DrawUserPrimitives function are as follows:
primitiveType contains the type of primitive that we wish to draw. In this case, we draw a
TriangleStrip. vertexData allows us to pass the array of vertices that are defined.
vertexOffset is a value that allows us to start considering the vertices at a position within the
array other than its start. We are not using this, so we just pass 0.
primitiveCount is the number of primitives that we are drawing. As we specified that we are
drawing triangles, setting this to 2 means to draw 2 triangles. Remember that this is counting
primitives, not vertices.
Drawing the colored square
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
{
// Apply the pass
pass.Apply();
// Draw the square
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _vertices, 0, 2);
}
base.Draw(gameTime);
}
The colors are interpolated between the vertices here.