Monogame Tutorial: Beginning 3D Programming

In this chapter we start looking at 3D game development using MonoGame.  Previously I called XNA a low level code focused engine and you are about to understand why.  If you come from a higher level game engine like Unity or even LibGDX you are about to be in for a shock.  Things you may take for granted in other engines/libraries, like cameras, are your responsibility in Monogame.  Don’t worry though, it’s not all that difficult.

This information is also available in HD Video.

This chapter is going to require some prior math experience, such as an understanding of Matrix mathematics.  Unfortunately teaching such concepts if far beyond the scope of what we can cover here without adding a few hundred more pages!  If you need to brush up on the underlying math, the Khan Academy is a very good place to start.  There are also a few books dedicated to teaching gamedev related math including 3D Math Primer for Graphics and Game Development and Mathematics for 3D Game Programming and Computer Graphics.  Don’t worry, Monogame/XNA provide the Matrix and Vector classes for you, but it’s good to understand when to use them and why.

Our First 3D Application

This might be one of those topics that’s easier explained by seeing.  So let’s jump right in with an example and follow it up with explanation.  This example creates then displays a simple triangle about the origin, then creates a user controlled camera that can orbit and zoom in/out on said triangle.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Test3D
{

public class Test3DDemo : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

//Camera
Vector3 camTarget;
Vector3 camPosition;
Matrix projectionMatrix;
Matrix viewMatrix;
Matrix worldMatrix;

//BasicEffect for rendering
BasicEffect basicEffect;

//Geometric info
VertexPositionColor[] triangleVertices;
VertexBuffer vertexBuffer;

//Orbit
bool orbit = false;

public Test3DDemo()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();

//Setup Camera
camTarget = new Vector3(0f, 0f, 0f);
camPosition = new Vector3(0f, 0f, -100f);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
GraphicsDevice.DisplayMode.AspectRatio,
1f, 1000f);
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget,
new Vector3(0f, 1f, 0f));// Y up
worldMatrix = Matrix.CreateWorld(camTarget, Vector3.
Forward, Vector3.Up);

//BasicEffect
basicEffect = new BasicEffect(GraphicsDevice);
basicEffect.Alpha = 1f;

// Want to see the colors of the vertices, this needs to
be on
basicEffect.VertexColorEnabled = true;

//Lighting requires normal information which
VertexPositionColor does not have
//If you want to use lighting and VPC you need to create a
custom def
basicEffect.LightingEnabled = false;

//Geometry  - a simple triangle about the origin
triangleVertices = new VertexPositionColor[3];
triangleVertices[0] = new VertexPositionColor(new Vector3(
0, 20, 0), Color.Red);
triangleVertices[1] = new VertexPositionColor(new Vector3(-
20, -20, 0), Color.Green);
triangleVertices[2] = new VertexPositionColor(new Vector3(
20, -20, 0), Color.Blue);

//Vert buffer
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(
VertexPositionColor), 3, BufferUsage.
WriteOnly);
vertexBuffer.SetData<VertexPositionColor>(triangleVertices)
;
}

{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

{
}

protected override void Update(GameTime gameTime)
{
ButtonState.Pressed || Keyboard.GetState().IsKeyDown(
Keys.Escape))
Exit();

if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
camPosition.X -= 1f;
camTarget.X -= 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
camPosition.X += 1f;
camTarget.X += 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
camPosition.Y -= 1f;
camTarget.Y -= 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
camPosition.Y += 1f;
camTarget.Y += 1f;
}
if(Keyboard.GetState().IsKeyDown(Keys.OemPlus))
{
camPosition.Z += 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemMinus))
{
camPosition.Z -= 1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
orbit = !orbit;
}

if (orbit)
{
Matrix rotationMatrix = Matrix.CreateRotationY(
camPosition = Vector3.Transform(camPosition,
rotationMatrix);
}
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget,
Vector3.Up);
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
basicEffect.Projection = projectionMatrix;
basicEffect.View = viewMatrix;
basicEffect.World = worldMatrix;

GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SetVertexBuffer(vertexBuffer);

//Turn off culling so we see both sides of our rendered
triangle
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rasterizerState;

foreach(EffectPass pass in basicEffect.CurrentTechnique.
Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.
TriangleList, 0, 3);
}

base.Draw(gameTime);
}
}
}

Alright… that’s a large code sample, but don’t worry, it’s not all that complicated.  At a top level what we do here is create a triangle oriented about the origin.  We then create a camera, offset –100 units along the z-axis but looking at the origin.  We then respond to keyboard, panning the camera in response to the arrow keys, zooming in and out in response to the plus and minus key and toggling orbit using the space bar.  Now let’s take a look at how we accomplish all of this.

First, when I said we create a camera, that is a misnomer, in fact we are creating three different Matrices (singular – Matrix), the View, Projection and World matrix.  These three matrices are combined to help position elements in your game world.  Let’s take a quick look at the function of each.

View Matrix  The View Matrix is used to transform coordinates from World to View space.  A much easier way to envision the View matrix is it represents the position and orientation of the camera.  It is created by passing in the camera location, where the camera is pointing and by specifying which axis represents “Up” in the universe.  XNA uses a Y-up orientation, which is important to be aware of when creating 3D models.  Blender by default treats Z as the up/down axis, while 3D Studio MAX uses the Y-axis as “Up”.

Projection Matrix The Projection Matrix is used to convert 3D view space to 2D.  In a nutshell, this is your actual camera lens and is created by specifying calling CreatePerspectiveFieldOfView() or CreateOrthographicFieldOfView().  With Orthographic projection, the size of things remain the same regardless to their “depth” within the scene.  For Perspective rendering it simulates the way an eye works, by rendering things smaller as they get further away.  As a general rule, for a 2D game you use Orthographic, while in 3D you use Perspective projection.  When creating a Perspective view we specify the field of view ( think of this as the degrees of visibility from the center of your eye view ), the aspect ratio ( the proportions between width and height of the display ), near and far plane ( minimum and maximum depth to render with camera… basically the range of the camera ).  These values all go together to calculate something called the view frustum, which can be thought of as a pyramid in 3D space representing what is currently available.

World Matrix The World matrix is used to position your entity within the scene.  Essentially this is your position in the 3D world.  In addition to positional information, the World matrix can also represent an objects orientation.

So nutshell way to think of it:

View Matrix –> Camera Location

Projection Matrix –> Camera Lens

World Matrix –> Object Position/Orientation in 3D Scene

By multiplying these three Matrices together we get the WorldProjView matrix, or a magic calculation that can turn a 3D object into pixels.

What value should I use for Field of View?

You may notice in this example I used a relatively small value of 45 degrees in this example.  What you may ask is the ideal setting for field of view?  Well, there isn’t one, although there are some commonly accepted values.  Human beings generally have a field of view of about 180 degrees, but this includes peripheral vision.  This means if you hold your hands straight out you should be able to just see them out of the edge of your vision.  Basically if its in front of you, you can see it.

However video games, at least not taking into account VR headset games, don’t really use the peripherals of your visual space.   Console games generally set of a field of view of about 60 degrees, while PC games often set the field of view higher, in the 80-100 degree range.  The difference is generally due to the size of the screen viewed and the distance from it.  The higher the field of view, the more of the scene that will be rendered on screen.

Next up we have the BasicEffect.  Remember how earlier we used a SpriteBatch to draw sprites on screen?  Well the BasicEffect is the 3D equivalent.  In reality it’s a wrapper over a HLSL shader responsible for rendering things to the screen.  Now HLSL coverage is way beyond the scope of what we can cover here, but basically it’s the instructions to the shader units on your graphic card telling how to render things.  Although I can’t go into a lot of details about how HLSL work, you are in luck, as Microsoft actually released the shader code used to create BasicEffect in the Stock Effect sample available at http://xbox.create.msdn.com/en-US/education/catalog/sample/stock_effects.  In order for BasicEffect to work it needs the View, Projection and Matrix matrixes specified, thankfully we just calculated all three of these.

Finally at the end of Intialize() we create an array of VertexPositionColor, which you can guess is a Vertex with positional and color data.  We then copy the triangle data to a VertexBuffer using a call to SetData().  You may be thinking to yourself… WOW, doesn’t XNA have simple primitives like this built in?  No, it doesn’t, although there are easy community examples you can download such as this one: http://xbox.create.msdn.com/en-US/education/catalog/sample/primitives_3d.

The logic in Update() is quite simple.  We check for input from the user and respond accordingly.  In the event of arrow keys being pressed, or +/- keys, we change the cameraPosition.  At the end of the update we then recalcuate the View matrix using our new Camera position.  Also in response to the space bar, we toggle orbiting the camera and if we are orbiting, we rotate the camera by another 1 degree relative to the origin.  Basically this shows how easy it is to update the camera by changing the viewMatrix.  Note the Projection Matrix generally isn’t updated after creation, unless the resolution changes.

Finally we come to our Draw() call.  Here we set the view, projection and world matrix of the BasicEffect, clear the screen, load our VertexBuffer into the GraphicsDevice calling SetVertexBuffer().  Next we create a RasterState object and set culling off.  We do this so we don’t cull back faces, which would result in our triangle from being invisible when we rotate behind it.  Often you actually want to cull back faces, no sense drawing vertices that aren’t visible!  Finally we load through each of the Techniques in the BasicEffect ( look at the BasicEffect.fx HLSL file and this will make a great deal more sense.  Otherwise stay tuned for when we cover custom shaders later on ), finally we draw our triangle data to screen by calling DrawPrimitives, in this case it’s a TriangleList.  There are other options such as lines and triangle strips, you are basically telling it what kind of data is in the VertexBuffer.

I’ll admit, compared to many other engines, that’s a heck of a lot of code to just draw a triangle on screen!  Reality is though, you generally write this code once and that’s it.  Or you work at a higher level, such as with 3D models imported using the content pipeline.

Next we take a look at the process of bringing a 3D model in from a 3D application, in this case Blender.  The process of creating such a model is well beyond the scope of this tutorial, although I have created a video showing the entire process available right here.  Or you can simply download the created COLLADA file and texture.Which File Format works Best?

The MonoGame pipeline tool relies on an underlying library named Assimp for loading 3D models. You may wonder which if the many model formats supported should you use if exporting from Blender? FBX and COLLADA(dae) are the two most commonly used formats, while X and OBJ can often be used reliably with very simple non-animated meshes. That said, exporting from Blender is always a tricky prospect, and its a very good idea to use a viewer like the one included in the FBX Converter package to verify your exported model looks correct.

The above video also illustrates adding the model and texture using the content pipeline.  I won’t cover the process here as it works identically to when we used the content pipeline earlier.  Let’s jump right in to the code instead:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Test3D
{

public class Test3DDemo2 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

//Camera
Vector3 camTarget;
Vector3 camPosition;
Matrix projectionMatrix;
Matrix viewMatrix;
Matrix worldMatrix;

//Geometric info
Model model;

//Orbit
bool orbit = false;

public Test3DDemo2()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();

//Setup Camera
camTarget = new Vector3(0f, 0f, 0f);
camPosition = new Vector3(0f, 0f, -5);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
GraphicsDevice.Viewport.AspectRatio,
1f, 1000f);
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget,
new Vector3(0f, 1f, 0f));// Y up
worldMatrix = Matrix.CreateWorld(camTarget, Vector3.
Forward, Vector3.Up);

}

{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

{
}

protected override void Update(GameTime gameTime)
{
ButtonState.Pressed || Keyboard.GetState().IsKeyDown(
Keys.Escape))
Exit();

if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
camPosition.X -= 0.1f;
camTarget.X -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
camPosition.X += 0.1f;
camTarget.X += 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
camPosition.Y -= 0.1f;
camTarget.Y -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
camPosition.Y += 0.1f;
camTarget.Y += 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemPlus))
{
camPosition.Z += 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemMinus))
{
camPosition.Z -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
orbit = !orbit;
}

if (orbit)
{
Matrix rotationMatrix = Matrix.CreateRotationY(
camPosition = Vector3.Transform(camPosition,
rotationMatrix);
}
viewMatrix = Matrix.CreateLookAt(camPosition, camTarget,
Vector3.Up);
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

foreach(ModelMesh mesh in model.Meshes)
{
foreach(BasicEffect effect in mesh.Effects)
{
//effect.EnableDefaultLighting();
effect.AmbientLightColor = new Vector3(1f, 0, 0);
effect.View = viewMatrix;
effect.World = worldMatrix;
effect.Projection = projectionMatrix;
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
}

It operates almost identically to when we created the triangle by hand, except that model is loaded using a call to Content.Load<Model>().  The other major difference is you no longer have to create a BasicEffect, one is automatically created for you as part of the import process and is stored in the Mesh’s Effects property.  Simply loop through each effect, setting up the View, Projection and World matrix values, then call Draw().  If you have a custom effect you wish to use instead of the generated Effects, you can follow the process documented here: https://msdn.microsoft.com/en-us/library/bb975391(v=xnagamestudio.31).aspx.

Scroll to Top