Вы находитесь на странице: 1из 5

CSC 6430

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);

The Effects Object:


This tells us what will be rendered on the screen. All rendering is done relative to effects objects.
Notice that upon initialization it is passed the view and projection matrices.
Creating and initializing the effect object
_effect = new BasicEffect(GraphicsDevice);
_effect.LightingEnabled = false;
_effect.TextureEnabled = false;
_effect.VertexColorEnabled = true;
_effect.Projection = projection;
_effect.View = view;
_effect.World = Matrix.Identity;

However. There is nothing to draw.


On Monogames objects are drawn in terms of vertices. There are are a number of parameters that
can hold information about a vertex as well.

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.

Notice that the z value is zero so the square is flat.


Next each vertex is assigned a set of vector coordinates.
In 2D we used vector2, here vector3.
Initializing the vertex positions to form a square
_vertices[0].Position = new Vector3(-1, -1, 0);
_vertices[1].Position = new Vector3(-1, 1, 0);
_vertices[2].Position = new Vector3(1, -1, 0);
_vertices[3].Position = new Vector3(1, 1, 0);
Now we set the color associated with each vertex.
Setting the vertex colors
_vertices[0].Color = Color.Red;
_vertices[1].Color = Color.White;
_vertices[2].Color = Color.Blue;
_vertices[3].Color = Color.Green;
base.Initialize();
}
Rendering:
There is no texture to be added to the vertices so Load content is not needed.
Next the background is set to Cornflower blue as before.
In 2D we used Sprite batch to render the objects.
Here we use Basic Effect:

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.

Вам также может понравиться