3D Graphics with XNA Game Studio 4.0 1849690049, 9781849690041

A step-by-step guide to adding the 3D graphics effects used by professionals to your XNA games. Improve the appearance o

331 9 6MB

English Pages 276 Year 2010

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

3D Graphics with XNA Game Studio 4.0
 1849690049, 9781849690041

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

3D Graphics with XNA Game Studio 4.0

Create attractive 3D graphics and visuals in your XNA games

Sean James

BIRMINGHAM - MUMBAI

3D Graphics with XNA Game Studio 4.0 Copyright © 2010 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: December 2010

Production Reference: 1071210

Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849690-04-1 www.packtpub.com

Cover Image by Charwak A ([email protected])

Table of Contents Preface Chapter 1: Getting Started with 3D Setting up a new project The 3D coordinate system Matrices Loading a model Drawing a model Creating a Custom Model class Creating a Camera class Creating a target camera Upgrading the camera to a free camera Calculating bounding spheres for models View frustum culling Additional camera types: Arc-Ball Additional camera types: chase camera Example—spaceship simulator XNA Graphics Profiles

Summary

Chapter 2: Introduction to HLSL Getting started Assigning a shader to a model Creating a simple effect Texture mapping Texture sampling Diffuse colors Ambient lighting Lambertian directional lighting Phong specular highlights

1 7 7 10 10 12 14 18 21 22 24 27 29 31 34 37 39

39

41 42 44 48 52 54 56 56 57 60

Table of Contents

Creating a Material class to store effect parameters Summary

Chapter 3: Advanced Lighting

65 67

69

Implementing a point light with HLSL Implementing a spot light with HLSL Drawing multiple lights Prelighting Storing depth and normal values Creating the light map Drawing models with the light map Creating the prelighting renderer Using the prelighting renderer Summary

Chapter 4: Projection and Shadowing Effects Projective texturing Shadow mapping—drawing the depth map Shadow mapping—projecting the depth texture onto the scene Shadow mapping—performing the depth comparison Variance shadow mapping—soft shadows Variance shadow mapping—blurring the depth texture Variance shadow mapping—generating shadows Summary

Chapter 5: Shader Effects

69 74 77 80 81 83 86 88 93 94

95 95 100 105 107 110 111 115 116

117

Fog Normal mapping Generating normal maps with Photoshop Cube mapping: Making a sky sphere Cube mapping: Reflections Rendering sky boxes with Terragen Creating a reflective water effect Summary

117 118 123 127 131 133 137 148

Chapter 6: Billboard and Particle Effects

149

Creating the BillboardSystem class Drawing Billboards Creating clouds with spherical billboarding Non-rotating billboards Particle effects Particle fire Particle smoke Summary [ ii ]

150 153 162 163 168 177 179 180

Table of Contents

Chapter 7: Environmental Effects Building a terrain from a heightmap Multitexturing Adding a detail texture to the terrain Placing plants on the terrain Adding the finishing touches Summary

Chapter 8: Advanced Materials and Post Processing Advanced Materials Post processing Black and white post processor Gaussian blur post processor Depth of field Glow post processor Summary

181 182 190 193 195 200 203

205 206 208 212 214 218 225 229

Chapter 9: Animation

231

Object animation Keyframed animation Curve interpolation Building a Race Track from a Curve Moving a car along the track Hierarchical animation Skinned animation Loading a skinned model Playing a skinned animation Changing an animation's play speed Model attachments Summary

Index

231 233 237 240 246 250 251 254 261 266 267 268

269

[ iii ]

Preface XNA is a very powerful API using which it's easy to make great games, especially when you have dazzling 3D effects. This book will show you how to implement the same 3D graphics used in professional games to make your games shine, and get those gamers addicted! This book will show you, step-by-step, how to implement the effects used in professional 3D games in your XNA games. Upon reaching the end of the book, you would have built an extensible framework for both basic 3D rendering and advanced effects. The one thing that can make or break a game is its appearance; players will mostly be attracted to a game if it looks good. One of the most common stopping points in an XNA game is its graphics, and many independent developers are not sure of how to implement the graphical effects needed to make great looking games. This book will help you avoid this pitfall, by walking you through the implementation of many common effects and graphics techniques used in professional games so that you can make your games look great.

What this book covers Chapter 1, Getting Started with 3D, introduces the fundamentals of 3D graphics, including coordinate systems, matrices, and so on, which will be used for the rest of the book. We start by learning some simple model drawing code and inish by building a framework to implement a number of camera types. We also take a look at view frustum culling and how it can speed up our game. Chapter 2, Introduction to HLSL, continues on the irst chapter, explaining the graphics pipeline and shaders. We then look at a number of lighting and texturing effects, expanding on the framework built in Chapter 1 and adding a system that will allows us to draw our models with any effect. Chapter 3, Advanced Lighting, continues our discussion of lighting, implementing more light types. We then look at several ways to increase the number of lights we can draw in a scene at a time.

Preface

Chapter 4, Projection and Shadowing Effects, builds on top of the renderer completed in Chapter 3 by adding two new effects: projected textures and shadow mapping. Chapter 5, Shader Effects, takes a look at some "shader effects" such as normal mapping and relections. We build a number of useful effects in this chapter such as a sky box and relective water effect. Chapter 6, Billboard and Particle Effects, investigates particle and billboarding effects—two effects that take advantage of 2D textures to create some interesting effects in 3D scenes such as foliage, clouds, and eficient trees and particle systems. Chapter 7, Environmental Effects, discusses several "environmental" effects such as terrain, randomly "grown" foliage, and more. The chapter inishes by combining many effects created in the book thus far to create a spectacular mountainous terrain scene. Chapter 8, Advanced Materials and Post Processing, expands on the material system created in the earlier chapter to allow for more advanced material types. It then takes a look at "post processing" effects like blurs, glows, and depth of ield. Chapter 9, Animation, takes a look at several different types of animation, including objects animation, keyframed animation, and skinned animation to introduce movement into our scenes.

What you need for this book All you need for this book is XNA and Visual Studio—the whole list and guide is available at creators.xna.com.

Who this book is for This book is mainly written for those who are familiar with object-oriented programming and C# and who are interested in improving the visual appearance of their XNA games. This book will be useful as a learning material for those who are new to graphics and for those who are looking to expand their toolset. Also, it can be used by game developers looking for an implementation guide or reference for effects or techniques they are already familiar with.

[2]

Preface

Please contact us at [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions You can contact us at [email protected] if you are having a problem with any aspect of the book, and we will do our best to address it.

[5]

Getting Started with 3D This chapter will provide you with a brief overview of the fundamentals of 3D graphics. We will create a number of useful classes and systems that will make work easier later on and provide us with a lexible framework for building games. This chapter will focus mainly on models, how they work, and how to view them with cameras. We will build a number of different types of camera that can be used in many situations we may encounter while building games. Next, we will look at a way to improve performance with a "view frustum culling" system, and inally, we'll build a small game that allows the player to ly a spaceship using keyboard controls.

Setting up a new project The irst step in any game is to set up the XNA game project in Visual Studio. 1. To begin with, ensure that XNA and Visual Studio are installed by following the guide available at creators.xna.com and launch Visual Studio. Once it has loaded, create a new project:

Chapter 1

The automatically generated Game1 class contains a lot of excess comments and code, so once you have familiarized yourself with the class and its functions, simplify it to the following: public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } // Called when the game should load its content protected override void LoadContent() { } // Called when the game should update itself protected override void Update(GameTime gameTime) { base.Update(gameTime); } // Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); base.Draw(gameTime); } }

[9]

Getting Started with 3D

The 3D coordinate system One thing that all 3D systems hold in common is a coordinate system. Coordinate systems are important because they allow us to represent points in 3D space in a consistent manner as distances from a center point called the origin along a number of axes. You're probably used to the idea of a 2D coordinate system from your math classes in school—the origin was at (0, 0) and the X and Y axes grew to the right and up respectively. A 3D coordinate system is very similar, except for the addition of a third axis labeled the Z-axis. XNA uses what is called a "right-handed" coordinate system, meaning that the X and Y axes grow the way you're used to (to the right and up respectively), and the Z-axis grows "towards" you. If the X and Y axes were placed lat on your computer screen, you can imagine the Z-axis as growing out of the screen towards you.

With this coordinate system, we can deine points in space. For example, let's assume that our coordinate system uses meters as units. Say for a moment, we were sitting at the origin (0, 0, 0) and were facing down the negative portion of the Z-axis. If we wanted to note the location of an object sitting ive meters in front of us, three meters to the right, on a table one meter tall, we would say that the object was at (3, 1, -5).

Matrices Matrices are mathematical structures that are used in 3D graphics to represent transformations—operations performed on a point to move it in some way. The three common transformations are translation (movement), rotation, and scaling (changing size). When a transformation is applied to a model, each vertex in the model is multiplied by the transformation's matrix until the entire model has been transformed.

[ 10 ]

Chapter 1

Matrices can be combined by multiplying them together. It is worth noting that matrix multiplication is done from right to left, so the last matrix to be multiplied will be the irst to affect the model and so on. This means that rotating and then moving a model will not have the same effect as moving and then rotating it. Generally, unless you mean to do otherwise, the matrices should be multiplied in the following order: scaling * rotation * transformation.

In the 3D graphics world, there are usually three matrices that must be calculated to draw an object onto the screen: the world, view, and projection matrices. The world matrix is the result of all of our transformation matrices multiplied together. Once this transformation has been applied, the model has moved from what is called "local" or "object space" to "world space". Each model in a scene has a different world matrix, as they all have different locations, orientations, and so on. It is also possible that each "piece" of a model (or mesh) may have its own world matrix. For example, the head and leg of a human model will likely have their own matrices to offset them from the center of the model (its root). When the model is drawn, each mesh has its transformation multiplied by the entire model's world matrix to calculate the inal world matrix. The view matrix is used to transform the scene from world space into view space: the world as seen by the camera. The world matrix for each model is simply multiplied by the view matrix to transform the scene. The projection matrix then transforms the three-dimensional position of each vertex in the scene into the two-dimensional projection of the scene that is drawn onto the screen. When the 3D world/view matrix combination is multiplied by the projection matrix, the scene is lattened out so that it can be drawn onto a 2D screen.

[ 11 ]

Getting Started with 3D

Loading a model A model is a ile exported from a 3D modeling package such as 3D Studio Max or Blender. The ile basically contains a list of points called vertices, which form the edges of polygons that, joined together, give the appearance of a smooth surface:

To load a model, we must add it to our game's content project. XNA will automatically build all of the content in our content project so that we can use it in our game. To add a model to the content project, open the Solution Explorer, rightclick on the content project (labeled Content), and click on Add Existing Item.

In addition to building all of the content in the content project, XNA builds any iles referenced by a piece of content. Because our model references its texture, we need to exclude the texture from the list of content to build or it will be built twice. Right-click on the texture and then select Exclude From Project. This will remove the texture from the content project but will not delete the ile itself, which will allow XNA to ind it when building the model but still only build it once. [ 12 ]

Chapter 1

Now that the content pipeline is building our model for us, we can load it into our game. We do this with the ContentManager class—a class used to access the runtime functionality of the content pipeline. The Game class already has an instance of the ContentManager class built-in, so we can go ahead and use it in the LoadContent() method. First, an instance of the Model class will be needed. The Model class contains all the data necessary to draw and work with a model. We will also need an array of matrices representing the model's built-in mesh transformations. Add the following member deinitions: Model model; Matrix[] transforms;

Now, in the LoadContent() method, we can use the ContentManager to load the model. The Load() function of the ContentManager class takes the name of the resource to load—the original ilename without its extension. Note that this means we can't have multiple iles with the same name only varying by extension. model = Content.Load("ship"); transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms);

In XNA, a model is made up of pieces called meshes. As described earlier, each model has its own transformation in 3D space, and each mesh also has its own transformation relative to the model's transformation as a whole. The Model class stores these transformations as a skeleton structure with each mesh attached to a bone. The last two lines in the previous code snippet copied that skeleton into the transforms array. [ 13 ]

Getting Started with 3D

Drawing a model Now that the model has been loaded, we are ready to draw it. We do this in the Draw() function of our game. Games redraw dozens of times per second, and each redraw is called a frame. We need to clear the screen before drawing a new frame, and we do so using XNA's GraphicsDevice.Clear() function. The single argument allows us to change the color to which the screen is cleared: GraphicsDevice.Clear(Color.CornflowerBlue);

The irst step of drawing the model itself is to calculate the view and projection matrices. To calculate the view matrix, we will use the CreateLookAt() static function of the Matrix class, which accepts as arguments a camera position, target, and up direction. The position is simply where in 3D space the camera should be placed, the target is the point the camera should be looking at, and the up vector is literally the direction that is "up" relative to the camera position. This code will go in the Draw() function after the GraphicsDevice is cleared: Matrix view = Matrix.CreateLookAt( new Vector3(200, 300, 900), new Vector3(0, 50, 0), Vector3.Up);

There are some exceptions, but usually we calculate the projection matrix using the Matrix class' CreatePerspectiveFieldOfView() function, which accepts, in order, a ield of view (in radians), the aspect ratio, and the near and far plane distances. These values deine the shape of the view frustum as seen in the following igure. It is used to decide which objects and vertices are onscreen and which are not when drawing, and how to squish the scene down to it onto the two-dimensional screen.

[ 14 ]

Chapter 1

The near plane and far plane determine the distances at which objects will start and stop being drawn. Outside of the range between the two planes, objects will be clipped—meaning they will not be drawn. The ield of view determines how "wide" the area seen by the camera is. Most irst person shooters use an angle between 45 and 60 degrees for their ield of view as anything beyond that range would start to distort the scene. A ish eye lens, on the other hand, would have a ield of view closer to 180 degrees. This allows it to see more of the scene without moving, but it also distorts the scene at the edges. 45 degrees is a good starting point as it matches human vision closest without warping the image. The inal value, the aspect ratio, is calculated by dividing the width of the screen by the height of the screen, and is used by the CreatePerspectiveFieldOfView() function to determine the "shape" of the screen in regards to its width and height. The GraphicsDevice has a precalculated aspect ratio value available that we can use when calculating the projection matrix: Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f);

The last matrix needed to draw the model is the world matrix. However, as discussed earlier, each mesh in the model has its own transformation relative to the model's overall transformation. This means that we will need to calculate a world matrix for each mesh. We'll start with the overall transformation and add the transformations from the modelTransformations array per mesh. Each mesh also has what is called an effect. We will look at effects in much more depth in the coming chapters, but for now, just remember that they are used to determine the appearance of a model. We will be using one of XNA's built-in effects (called BasicEffect) for now, so all we need to do is set its World, View, and Projection properties: // Calculate the starting world matrix Matrix baseWorld = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(MathHelper.ToRadians(180)); foreach (ModelMesh mesh in model.Meshes) { // Calculate each mesh's world matrix Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld; foreach (ModelMeshPart part in mesh.MeshParts) { BasicEffect e = (BasicEffect)part.Effect; // Set the world, view, and projection matrices to the effect e.World = localWorld; [ 15 ]

Getting Started with 3D e.View = view; e.Projection = projection; e.EnableDefaultLighting(); } // Draw the mesh mesh.Draw(); }

XNA will already have added the last piece of code for you as well, but it is important to ensure that this code is still in place at the end of the Draw() function. This line of code simply calls the Draw() function of the base Game class, ensuring that the game runs correctly. base.Draw(gameTime);

The complete code for the Game1 class is now as follows: public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Model model; Matrix[] modelTransforms; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 800; } // Called when the game should load its content protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); model = Content.Load("ship"); modelTransforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(modelTransforms); [ 16 ]

Chapter 1 } // Called when the game should update itself protected override void Update(GameTime gameTime) { base.Update(gameTime); } // Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix view = Matrix.CreateLookAt( new Vector3(200, 300, 900), new Vector3(0, 50, 0), Vector3.Up); Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f); // Calculate the starting world matrix Matrix baseWorld = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(MathHelper.ToRadians(180)); foreach (ModelMesh mesh in model.Meshes) { // Calculate each mesh's world matrix Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld; foreach (ModelMeshPart part in mesh.MeshParts) { BasicEffect e = (BasicEffect)part.Effect; // Set the world, view, and projection // matrices to the effect e.World = localWorld; e.View = view; e.Projection = projection; e.EnableDefaultLighting(); } [ 17 ]

Getting Started with 3D // Draw the mesh mesh.Draw(); } base.Draw(gameTime); } }

Run the game (Debug | Start Debugging, or F5) and you should see our spaceship in all its glory:

Creating a Custom Model class The previous code works for drawing one model, but what if we wanted to draw more than one? More than ten? Writing the previous code out for each model would quickly become unmanageable. To make our lives a little easier, we'll take the previous code and put it into a new class called CModel (for custom model). This class will handle loading the transformations from a model, setting the matrices to the mesh part effects, and so on. Later on, it will handle setting custom effects, manage textures, and more. For now, we will keep it simple: public class CModel { public Vector3 Position { get; set; } public Vector3 Rotation { get; set; } public Vector3 Scale { get; set; } public Model Model { get; private set; } private Matrix[] modelTransforms; private GraphicsDevice graphicsDevice; [ 18 ]

Chapter 1 public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice) { this.Model = Model; modelTransforms = new Matrix[Model.Bones.Count]; Model.CopyAbsoluteBoneTransformsTo(modelTransforms); this.Position = Position; this.Rotation = Rotation; this.Scale = Scale; this.graphicsDevice = graphicsDevice; } public void Draw(Matrix View, Matrix Projection) { // Calculate the base transformation by combining // translation, rotation, and scaling Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll( Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position); foreach (ModelMesh mesh in Model.Meshes) { Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld; foreach (ModelMeshPart meshPart in mesh.MeshParts) { BasicEffect effect = (BasicEffect)meshPart.Effect; effect.World = localWorld; effect.View = View; effect.Projection = Projection; effect.EnableDefaultLighting(); } mesh.Draw(); } } }

[ 19 ]

Getting Started with 3D

We can now simplify the Game1 class to draw a list of models: GraphicsDeviceManager graphics; SpriteBatch spriteBatch; List models = new List();

As a demonstration, let's add nine copies of our spaceship to that list in the LoadContent() method: for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) { Vector3 position = new Vector3( -600 + x * 600, -400 + y * 400, 0); models.Add(new CModel(Content.Load("ship"), position, new Vector3(0, MathHelper.ToRadians(90) * (y * 3 + x), 0), new Vector3(0.25f), GraphicsDevice)); }

We can now update our Draw() method to draw a list of models, then we can run the game and see the result: // Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Matrix view = Matrix.CreateLookAt( new Vector3(0, 300, 2000), new Vector3(0, 0, 0), Vector3.Up); Matrix projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f); foreach (CModel model in models) model.Draw(view, projection); base.Draw(gameTime); }

[ 20 ]

Chapter 1

Creating a Camera class Much like we did with the CModel class, let's create a reusable Camera class. We'll start with a base class that represents a camera at its lowest level: simply the view and projection matrices. We use a base class that all camera types will inherit from because we want to be able to use all camera types interchangeably. This Camera base class will also take care of calculating the projection matrix unless derived classes choose to do so themselves. public abstract class Camera { public Matrix View { get; set; } public Matrix Projection { get; set; } protected GraphicsDevice GraphicsDevice { get; set; } public Camera(GraphicsDevice graphicsDevice) { this.GraphicsDevice = graphicsDevice; generatePerspectiveProjectionMatrix(MathHelper.PiOver4); } private void generatePerspectiveProjectionMatrix(float FieldOfView) { PresentationParameters pp = GraphicsDevice.PresentationParameters; float aspectRatio = (float)pp.BackBufferWidth / (float)pp.BackBufferHeight;

[ 21 ]

Getting Started with 3D this.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), aspectRatio, 0.1f, 1000000.0f); } public virtual void Update() { } }

Creating a target camera Now that we have our base class, let's create the most basic type of camera—the target camera. This is simply a camera with two components—a position and a target. The camera points from the position towards the target, similar to the "camera" we used when we irst drew our model:

This data is more or less directly passed into the CreateLookAt() function in the Matrix class to calculate the view matrix. The call in the constructor to the Camera base class' constructor ensures that the projection matrix and other matrices are calculated for us: public class TargetCamera : Camera { public Vector3 Position { get; set; } public Vector3 Target { get; set; } public TargetCamera(Vector3 Position, Vector3 Target, GraphicsDevice graphicsDevice) : base(graphicsDevice) { this.Position = Position; this.Target = Target; } public override void Update() { Vector3 forward = Target - Position; Vector3 side = Vector3.Cross(forward, Vector3.Up); Vector3 up = Vector3.Cross(forward, side); this.View = Matrix.CreateLookAt(Position, Target, up); } } [ 22 ]

Chapter 1

As you can see, the Position and Target values can be set freely through their public properties or through the constructor to position the camera any way desired at any time, as the Update() function will update the view matrix as necessary. We can now once again update the Game1 class to use the TargetCamera instead of doing all the camera calculations itself. In addition to our list of models, we will also need a camera: List models = new List(); Camera camera;

We will need to initialize the camera along with any models in the LoadContent() method: models.Add(new CModel(Content.Load("ship"), Vector3.Zero, Vector3.Zero, new Vector3(0.6f), GraphicsDevice)); camera = new TargetCamera( new Vector3(300, 300, -1800), Vector3.Zero, GraphicsDevice);

We need to update the camera in the Update() method: // Called when the game should update itself protected override void Update(GameTime gameTime) { camera.Update(); base.Update(gameTime); }

Finally, we can use the View and Projection properties of camera in the Draw() method: // Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); foreach (CModel model in models) model.Draw(camera.View, camera.Projection); base.Draw(gameTime); }

[ 23 ]

Getting Started with 3D

We now have two classes, TargetCamera and CModel, which can be reused easily, and a base class for creating any type of camera we could need. We'll use the rest of this chapter to look at other types of cameras, and to add a system that will speed up the game by keeping it from drawing (culling) objects that are not in the camera's view.

Upgrading the camera to a free camera At the moment, we can put our camera at one point and aim it at another. This works for a static scene, but it doesn't allow for any freedom in a real game. Most games would be pretty dificult to play if the camera can't move around, so this will be our next task. The free camera we will create will operate like those found in First-Person-Shooter (FPS) games, which allow the player to move around with the W, S, A, and D keys (for forward, back, left, and right respectively), and look around with the mouse. On the Xbox 360, we would use the left joystick for movement and the right joystick to rotate the camera. public class FreeCamera : Camera { public float Yaw { get; set; } public float Pitch { get; set; } public Vector3 Position { get; set; } public Vector3 Target { get; private set; } private Vector3 translation; public FreeCamera(Vector3 Position, float Yaw, float Pitch, GraphicsDevice graphicsDevice) : base(graphicsDevice) { this.Position = Position; this.Yaw = Yaw; this.Pitch = Pitch; translation = Vector3.Zero; } }

The FreeCamera class adds two new values that the TargetCamera class didn't have—yaw and pitch. These two values (in radians) determine the amount that the camera has been rotated around the Y and X axes, respectively. The yaw and pitch values can be modiied (usually based on mouse movements) through the new Rotate() method. There is also a new value called translation that accumulates the amount that the camera has moved (in the direction the camera is facing) between frames. The Move() function modiies this value (usually based on keyboard or joystick input): [ 24 ]

Getting Started with 3D

Once again we are ready to modify the Game1 class, this time to switch to our new camera and add keyboard and mouse control. First, we need to change the type of camera we are creating in the LoadContent() function: camera = new FreeCamera(new Vector3(1000, 0, -2000), MathHelper.ToRadians(153), // Turned around 153 degrees MathHelper.ToRadians(5), // Pitched up 13 degrees GraphicsDevice);

Let's start by adding mouse control. In order to measure the amount of mouse movement between frames, we need to be able to store the last frame's mouse state. We can then compare that mouse state to the current mouse state. To store the last frame's mouse state, we will add a MouseState member variable: List models = new List(); Camera camera; MouseState lastMouseState;

This value needs to be initialized once before the Update method so that the game doesn't crash when trying to access it on the irst frame, so we will grab the mouse state at the end of the LoadContent method: lastMouseState = Mouse.GetState();

Now, we can create a new function that the Update() method will use to update the camera: // Called when the game should update itself protected override void Update(GameTime gameTime) { updateCamera(gameTime); base.Update(gameTime); } void updateCamera(GameTime gameTime) { // Get the new keyboard and mouse state MouseState mouseState = Mouse.GetState(); KeyboardState keyState = Keyboard.GetState(); // Determine how much the camera should turn float deltaX = (float)lastMouseState.X - (float)mouseState.X; float deltaY = (float)lastMouseState.Y - (float)mouseState.Y; // Rotate the camera ((FreeCamera)camera).Rotate(deltaX * .01f, deltaY * .01f); Vector3 translation = Vector3.Zero; [ 26 ]

Chapter 1 // if if if if

Determine in which direction (keyState.IsKeyDown(Keys.W)) (keyState.IsKeyDown(Keys.S)) (keyState.IsKeyDown(Keys.A)) (keyState.IsKeyDown(Keys.D))

to move the translation translation translation translation

camera += Vector3.Forward; += Vector3.Backward; += Vector3.Left; += Vector3.Right;

// Move 3 units per millisecond, independent of frame rate translation *= 3 * (float)gameTime.ElapsedGameTime. TotalMilliseconds; // Move the camera ((FreeCamera)camera).Move(translation); // Update the camera camera.Update(); // Update the mouse state lastMouseState = mouseState; }

Run the game again, and you should be able to move and rotate the camera with the mouse and W, S, A, and D keys.

Calculating bounding spheres for models It is often convenient for us to have a simpliied representation of the geometry of a model. Because complex models are made of hundreds if not thousands of vertices, it is often too ineficient to check for object intersection per vertex between every object in the scene when doing collision detection, for example. To simplify collision checks, we will use what is called a bounding volume, speciically a bounding sphere. Let's add some functionality to our CModel class to calculate bounding spheres for us. To start, we need to add a new BoundingSphere member variable to the CModel class: private Matrix[] modelTransforms; private GraphicsDevice graphicsDevice; private BoundingSphere boundingSphere;

Next, we will create a function to calculate this bounding sphere based on our model's geometry: private void buildBoundingSphere() { BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0); // Merge all the model's built in bounding spheres foreach (ModelMesh mesh in Model.Meshes) { BoundingSphere transformed = mesh.BoundingSphere.Transform( [ 27 ]

Getting Started with 3D modelTransforms[mesh.ParentBone.Index]); sphere = BoundingSphere.CreateMerged(sphere, transformed); } this.boundingSphere = sphere; }

We need to be sure to call this function in our constructor: public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice) { this.Model = Model; modelTransforms = new Matrix[Model.Bones.Count]; Model.CopyAbsoluteBoneTransformsTo(modelTransforms); buildBoundingSphere(); ...

However, there is one problem with this approach: this bounding sphere is centered at the origin, so if we were to move our model, the bounding sphere would no longer contain the model. To solve this problem, we will add a public property that translates our origin-centered boundingSphere value to the model's current position and scales it based on our model's scale: public BoundingSphere BoundingSphere { get { // No need for rotation, as this is a sphere Matrix worldTransform = Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position); BoundingSphere transformed = boundingSphere; transformed = transformed.Transform(worldTransform); return transformed; } }

[ 28 ]

Chapter 1

View frustum culling One usage of our new bounding sphere system is to determine which objects are onscreen. This is useful when, for example, we are drawing a large number of objects: if we irst check whether an object is onscreen before drawing it, we have a chance of improving the performance of our games if some of the objects in the scene leave the player's view. This is called view frustum culling because we are checking if an object's bounding sphere intersects the view frustum, and if it doesn't, we cull it (refrain from drawing it). It is worth noting that this method of culling is not an all-encompassing method of optimization—if you are drawing many small objects, it may not be worth the time to cull objects because the graphics card won't process pixels that are offscreen anyway. The irst thing we need to do is to actually calculate a view frustum. We can do this very simply once we know our view and projection matrices. Let's go back to our abstract Camera class and add a BoundingFrustum. The BoundingFrustum class has a function to check if a BoundingSphere is in view. public BoundingFrustum Frustum { get; private set; }

The view and projection matrices can change frequently, so we need to update the frustum whenever they change. We will do this by calling the generateFrustum() function that we will write shortly in the View and Projection properties' set accessors: Matrix view; Matrix projection; public Matrix Projection { get { return projection; } protected set { projection = value; generateFrustum(); } } public Matrix View { get { return view; } protected set { view = value; generateFrustum(); [ 29 ]

Getting Started with 3D } } private void generateFrustum() { Matrix viewProjection = View * Projection; Frustum = new BoundingFrustum(viewProjection); }

Finally, we will make things a little easier on ourselves by adding some shortcuts to check if bounding boxes and bounding spheres are visible. Because we are doing this in the base class, these functions will conveniently work for any camera type. public bool BoundingVolumeIsInView(BoundingSphere sphere) { return (Frustum.Contains(sphere) != ContainmentType.Disjoint); } public bool BoundingVolumeIsInView(BoundingBox box) { return (Frustum.Contains(box) != ContainmentType.Disjoint); }

Finally, we can update the Game1 class to use this new system. Our new Draw() function will check if the bounding sphere of each model to be drawn is in view, and if so, it will draw the model. If it doesn't end up drawing any models, it will clear the screen to red so we can tell if models are being culled correctly: // Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); int nModelsDrawn = 0; foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) { nModelsDrawn++; model.Draw(camera.View, camera.Projection); } if (nModelsDrawn == 0) GraphicsDevice.Clear(Color.Red); base.Draw(gameTime); }

[ 30 ]

Getting Started with 3D

Most games limit the vertical and zoom range of this type of camera, so our camera will allow this as well. The code for the ArcBall Camera class is as follows: public class ArcBallCamera : Camera { // Rotation around the two axes public float RotationX { get; set; } public float RotationY { get; set; } // Y axis rotation limits (radians) public float MinRotationY { get; set; } public float MaxRotationY { get; set; } // Distance between the target and camera public float Distance { get; set; } // Distance limits public float MinDistance { get; set; } public float MaxDistance { get; set; } // Calculated position and specified target public Vector3 Position { get; private set; } public Vector3 Target { get; set; } public ArcBallCamera(Vector3 Target, float RotationX, float RotationY, float MinRotationY, float MaxRotationY, float Distance, float MinDistance, float MaxDistance, GraphicsDevice graphicsDevice) : base(graphicsDevice) { this.Target = Target; this.MinRotationY = MinRotationY; this.MaxRotationY = MaxRotationY; // Lock the y axis rotation between the min and max values this.RotationY = MathHelper.Clamp(RotationY, MinRotationY, MaxRotationY); this.RotationX = RotationX; this.MinDistance = MinDistance; this.MaxDistance = MaxDistance; // Lock the distance between the min and max values

[ 32 ]

Chapter 1 this.Distance = MathHelper.Clamp(Distance, MinDistance, MaxDistance); } public void Move(float DistanceChange) { this.Distance += DistanceChange; this.Distance = MathHelper.Clamp(Distance, MinDistance, MaxDistance); } public void Rotate(float RotationXChange, float RotationYChange) { this.RotationX += RotationXChange; this.RotationY += -RotationYChange; this.RotationY = MathHelper.Clamp(RotationY, MinRotationY, MaxRotationY); } public void Translate(Vector3 PositionChange) { this.Position += PositionChange; } public override void Update() { // Calculate rotation matrix from rotation values Matrix rotation = Matrix.CreateFromYawPitchRoll(RotationX, RotationY, 0); // Translate down the Z axis by the desired distance // between the camera and object, then rotate that // vector to find the camera offset from the target Vector3 translation = new Vector3(0, 0, Distance); translation = Vector3.Transform(translation, rotation); Position = Target + translation; // Calculate the up vector from the rotation matrix Vector3 up = Vector3.Transform(Vector3.Up, rotation); View = Matrix.CreateLookAt(Position, Target, up); } } [ 33 ]

Getting Started with 3D

To implement this camera in the Game1 class, we irst instantiate our camera as an ArcBallCamera in the LoadContent() method: camera = new ArcBallCamera(Vector3.Zero, 0, 0, 0, MathHelper.PiOver2, 1200, 1000, 2000, GraphicsDevice);

Second, we need to update the updateCamera() function to relect the way this new camera type moves: void updateCamera(GameTime gameTime) { // Get the new keyboard and mouse state MouseState mouseState = Mouse.GetState(); KeyboardState keyState = Keyboard.GetState(); // Determine how much the camera should turn float deltaX = (float)lastMouseState.X - (float)mouseState.X; float deltaY = (float)lastMouseState.Y - (float)mouseState.Y; // Rotate the camera ((ArcBallCamera)camera).Rotate(deltaX * .01f, deltaY * .01f); // Calculate scroll wheel movement float scrollDelta = (float)lastMouseState.ScrollWheelValue (float)mouseState.ScrollWheelValue; // Move the camera ((ArcBallCamera)camera).Move(scrollDelta); // Update the camera camera.Update(); // Update the mouse state lastMouseState = mouseState; }

Run the game, and you will be able to rotate around the ship with the mouse, and move towards and away from it with the scroll wheel.

Additional camera types: chase camera The last camera type we will look at is the chase camera. A chase camera is designed to "chase" an object. Generally, the camera follows the object at some distance and turns with it. This is the type of camera used, for example, in most third person situations—racing games, third person shooters, light simulators, and so on. The chase distance and view direction are generally determined using an offset for the camera position and an offset for the target position from the object's position. The view matrix is then calculated as usual based on those values. [ 34 ]

Getting Started with 3D { this.PositionOffset = PositionOffset; this.TargetOffset = TargetOffset; this.RelativeCameraRotation = RelativeCameraRotation; } public void Move(Vector3 NewFollowTargetPosition, Vector3 NewFollowTargetRotation) { this.FollowTargetPosition = NewFollowTargetPosition; this.FollowTargetRotation = NewFollowTargetRotation; } public void Rotate(Vector3 RotationChange) { this.RelativeCameraRotation += RotationChange; } public override void Update() { // Sum the rotations of the model and the camera to ensure it // is rotated to the correct position relative to the model's // rotation Vector3 combinedRotation = FollowTargetRotation + RelativeCameraRotation; // Calculate the rotation matrix for the camera Matrix rotation = Matrix.CreateFromYawPitchRoll( combinedRotation.Y, combinedRotation.X, combinedRotation.Z); // Calculate the position the camera would be without the spring // value, using the rotation matrix and target position Vector3 desiredPosition = FollowTargetPosition + Vector3.Transform(PositionOffset, rotation); // Interpolate between the current position and desired position Position = Vector3.Lerp(Position, desiredPosition, Springiness); // Calculate the new target using the rotation matrix Target = FollowTargetPosition + Vector3.Transform(TargetOffset, rotation); // Obtain the up vector from the matrix Vector3 up = Vector3.Transform(Vector3.Up, rotation); // Recalculate the view matrix View = Matrix.CreateLookAt(Position, Target, up); } }

[ 36 ]

Chapter 1

Example—spaceship simulator Let's use the concepts and classes learned and created so far to create a simple game in which the player lies our spaceship around using the keyboard. You'll notice that the example uses the ChaseCamera to follow the spaceship and uses two models to represent the ground and spaceship. 1. We'll start by instantiating these values in the LoadContent() method: models.Add(new CModel(Content.Load("ship"), new Vector3(0, 400, 0), Vector3.Zero, new Vector3(0.4f), GraphicsDevice)); models.Add(new CModel(Content.Load("ground"), Vector3.Zero, Vector3.Zero, Vector3.One, GraphicsDevice)); camera = new ChaseCamera(new Vector3(0, 400, 1500), new Vector3(0, 200, 0), new Vector3(0, 0, 0), GraphicsDevice);

2. Next, we will create a new function that updates the position and rotation of our model based on keyboard input, which is called by the Update() function: // Called when the game should update itself protected override void Update(GameTime gameTime) { updateModel(gameTime); updateCamera(gameTime); base.Update(gameTime); } void updateModel(GameTime gameTime) { KeyboardState keyState = Keyboard.GetState(); Vector3 rotChange = new Vector3(0, 0, 0); // Determine on which axes the ship should be rotated on, if any if (keyState.IsKeyDown(Keys.W)) rotChange += new Vector3(1, 0, 0); if (keyState.IsKeyDown(Keys.S)) rotChange += new Vector3(-1, 0, 0); if (keyState.IsKeyDown(Keys.A)) rotChange += new Vector3(0, 1, 0); if (keyState.IsKeyDown(Keys.D)) rotChange += new Vector3(0, -1, 0); models[0].Rotation += rotChange * .025f; [ 37 ]

Getting Started with 3D // If space isn't down, the ship shouldn't move if (!keyState.IsKeyDown(Keys.Space)) return; // Determine what direction to move in Matrix rotation = Matrix.CreateFromYawPitchRoll( models[0].Rotation.Y, models[0].Rotation.X, models[0].Rotation.Z); // Move in the direction dictated by our rotation matrix models[0].Position += Vector3.Transform(Vector3.Forward, rotation) * (float)gameTime.ElapsedGameTime.TotalMilliseconds * 4; }

3. We can now greatly simplify the updateCamera() function: void updateCamera(GameTime gameTime) { // Move the camera to the new model's position and orientation ((ChaseCamera)camera).Move(models[0].Position, models[0].Rotation); // Update the camera camera.Update(); }

4. And with that, we're inished! Run your game one last time and you should be able to ly the ship around with W, S, A, D, and the Space bar:

[ 38 ]

Chapter 1

XNA Graphics Profiles In an effort to allow developers to easily maintain compatibility with a wide range of devices, XNA provides two "graphics proiles." A graphics proile is a set of features that are guaranteed to work on a certain machine, as long as the machine meets all of the requirements of that graphics proile. The two proiles XNA provides are "Reach" and "HiDef." Games developed with the Reach proile will work on a very large range of devices, but are limited in which graphics features they can use. Games developed with the HiDef proile will be able to use a large range of graphics features but they will only work on a much more limited range of devices. The Xbox 360 supports the HiDef proile. In order to implement many of the examples in this book, you will need to be developing under the Hidef proile. Some examples may work under the Reach proile but this book assumes you are working on a computer supporting HiDef. If you encounter errors while trying to build or run an example, you should irst ensure that the game is set to run under the HiDef proile. To do this, right click on your game's project in the solution explorer in Visual Studio (labelled "MyGame" if you have been following along) and click "Properties." Under the "XNA Game Studio" tab, select "Use HiDef to access the complete API (including features unavailable for Windows Phone." Rebuild the game and run it and any errors relating to the graphics proile will be ixed. More information can be found at http://msdn. microsoft.com/en-us/library/ff604995.aspx.

Summary Now that you have completed this chapter, you have an understanding of the fundamentals of 3D graphics. You know how to create a new game project with Visual Studio, how to add content to its content project, and how to remove content. You also have a basic understanding of the content pipeline and how to interact with it through code and the ContentManager. You have also created a number of useful classes that will be reused later, including the CModel and Camera classes, and all of the derived camera classes. Finally, you also have a way to determine which objects intersect others or which are onscreen. In the coming chapters, we will learn how to add new special effects to our games. We will start with "Effects" (which were mentioned earlier) and HLSL to implement some lighting and texturing effects.

[ 39 ]

Introduction to HLSL

Getting started For your convenience, I've provided the starting code for this chapter here. It is comparable to the code we had arrived at the end of the last chapter, but the camera has been switched back to a free camera: public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; List models = new List(); Camera camera; MouseState lastMouseState; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 800; } // Called when the game should load its content protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); models.Add(new CModel(Content.Load("ship"), new Vector3(0, 400, 0), Vector3.Zero, new Vector3(1f), GraphicsDevice)); models.Add(new CModel(Content.Load("ground"), Vector3.Zero, Vector3.Zero, Vector3.One, GraphicsDevice)); camera = new FreeCamera(new Vector3(1000, 500, -2000), MathHelper.ToRadians(153), // Turned around 153 degrees MathHelper.ToRadians(5), // Pitched up 13 degrees GraphicsDevice); lastMouseState = Mouse.GetState(); } // Called when the game should update itself protected override void Update(GameTime gameTime) [ 42 ]

Chapter 2 { updateCamera(gameTime); base.Update(gameTime); } void updateCamera(GameTime gameTime) { // Get the new keyboard and mouse state MouseState mouseState = Mouse.GetState(); KeyboardState keyState = Keyboard.GetState(); // Determine how much the camera should turn float deltaX = (float)lastMouseState.X - (float)mouseState.X; float deltaY = (float)lastMouseState.Y - (float)mouseState.Y; // Rotate the camera ((FreeCamera)camera).Rotate(deltaX * .005f, deltaY * .005f); Vector3 translation = Vector3.Zero; // if if if if

Determine in which direction (keyState.IsKeyDown(Keys.W)) (keyState.IsKeyDown(Keys.S)) (keyState.IsKeyDown(Keys.A)) (keyState.IsKeyDown(Keys.D))

to move the translation translation translation translation

camera += Vector3.Forward; += Vector3.Backward; += Vector3.Left; += Vector3.Right;

// Move 3 units per millisecond, independent of frame rate translation *= 4 * (float)gameTime.ElapsedGameTime.TotalMilliseconds; // Move the camera ((FreeCamera)camera).Move(translation); // Update the camera camera.Update(); // Update the mouse state lastMouseState = mouseState; } // Called when the game should draw itself protected override void Draw(GameTime gameTime) { [ 43 ]

Introduction to HLSL GraphicsDevice.Clear(Color.CornflowerBlue); foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); base.Draw(gameTime); } }

Assigning a shader to a model In order to draw a model with XNA, it needs to have an instance of the Effect class assigned to it. Recall from the irst chapter that each ModelMeshPart in a Model has its own Effect. This is because each ModelMeshPart may need to have a different appearance, as one ModelMeshPart may, for example, make up armor on a soldier while another may make up the head. If the two used the same effect (shader), then we could end up with a very shiny head or a very dull piece of armor. Instead, XNA provides us the option to give every ModelMeshPart a unique effect. As you may remember from the irst chapter, by default, each ModelMeshPart is loaded by the standard content pipeline model processor with an instance of the BasicEffect class assigned to it. In order to draw our models with our own effects, we need to replace the BasicEffect of every ModelMeshPart with our own effect loaded from the content pipeline. For now, we won't worry about the fact that each ModelMeshPart can have its own effect; we'll just be assigning one effect to an entire model. Later, however, we will add more functionality to allow different effects on each part of a model. However, before we start replacing the instances of BasicEffect assigned to our models, we need to extract some useful information from them, such as which texture and color to use for each ModelMeshPart. We will store this information in a new class that each ModelMeshPart will keep a reference to using its Tag properties: public class MeshTag { public Vector3 Color; public Texture2D Texture; public float SpecularPower; public Effect CachedEffect = null; public MeshTag(Vector3 Color, Texture2D Texture, float SpecularPower) [ 44 ]

Chapter 2 { this.Color = Color; this.Texture = Texture; this.SpecularPower = SpecularPower; } }

This information will be extracted using a new function in the CModel class: private void generateTags() { foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) if (part.Effect is BasicEffect) { BasicEffect effect = (BasicEffect)part.Effect; MeshTag tag = new MeshTag(effect.DiffuseColor, effect.Texture, effect.SpecularPower); part.Tag = tag; } }

This function will be called along with buildBoundingSphere() in the constructor: ... buildBoundingSphere(); generateTags(); ...

Notice that the MeshTag class has a CachedEffect variable that is not currently used. We will use this value as a location to store a reference to an effect that we want to be able to restore to the ModelMeshPart on demand. This is useful when we want to draw a model using a different effect temporarily without having to completely reload the model's effects afterwards. The functions that will allow us to do this are as shown: // Store references to all of the model's current effects public void CacheEffects() { foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) ((MeshTag)part.Tag).CachedEffect = part.Effect; } // Restore the effects referenced by the model's cache [ 45 ]

Introduction to HLSL public void RestoreEffects() { foreach (ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) if (((MeshTag)part.Tag).CachedEffect != null) part.Effect = ((MeshTag)part.Tag).CachedEffect; }

We are now ready to start assigning effects to our models. We will look at this in more detail in a moment, but it is worth noting that every Effect has a dictionary of effect parameters. These are variables that the Effect takes into account when performing its calculations—the world, view, and projection matrices, or colors and textures, for example. We modify a number of these parameters when assigning a new effect, so that each texture of ModelMeshPart can be informed of its speciic properties (which we extracted earlier from BasicEffect): public void SetModelEffect(Effect effect, bool CopyEffect) { foreach(ModelMesh mesh in Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) { Effect toSet = effect; // Copy the effect if necessary if (CopyEffect) toSet = effect.Clone(); MeshTag tag = ((MeshTag)part.Tag); // If this ModelMeshPart has a texture, set it to the effect if (tag.Texture != null) { setEffectParameter(toSet, "BasicTexture", tag.Texture); setEffectParameter(toSet, "TextureEnabled", true); } else setEffectParameter(toSet, "TextureEnabled", false); // Set our remaining parameters to the effect setEffectParameter(toSet, "DiffuseColor", tag.Color); setEffectParameter(toSet, "SpecularPower", tag.SpecularPower); part.Effect = toSet; } } // Sets the specified effect parameter to the given effect, if it // has that parameter [ 46 ]

Introduction to HLSL Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position); foreach (ModelMesh mesh in Model.Meshes) { Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld; foreach (ModelMeshPart meshPart in mesh.MeshParts) { Effect effect = meshPart.Effect; if (effect is BasicEffect) { ((BasicEffect)effect).World = localWorld; ((BasicEffect)effect).View = View; ((BasicEffect)effect).Projection = Projection; ((BasicEffect)effect).EnableDefaultLighting(); } else { setEffectParameter(effect, "World", localWorld); setEffectParameter(effect, "View", View); setEffectParameter(effect, "Projection", Projection); setEffectParameter(effect, "CameraPosition", CameraPosition); } } mesh.Draw(); } }

Creating a simple effect We will create our irst effect now, and assign it to our models so that we can see the result. To begin, right-click on the content project, choose Add New Item, and select Effect File. Call it something like SimpleEffect.fx:

[ 48 ]

Chapter 2

The code for the new ile is as follows. Don't worry, we'll go through each piece in a moment: float4x4 World; float4x4 View; float4x4 Projection; struct VertexShaderInput { float4 Position : POSITION0; }; struct VertexShaderOutput { float4 Position : POSITION0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); float4x4 viewProjection = mul(View, Projection); output.Position = mul(worldPosition, viewProjection); return output; }

[ 49 ]

Introduction to HLSL float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { return float4(.5, .5, .5, 1); } technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

To assign this effect to the models in our scene, we need to irst load it in the game's LoadContent() function, then use the SetModelEffect() function to assign the effect to each model. Add the following to the end of the LoadContent function: Effect simpleEffect = Content.Load("SimpleEffect"); models[0].SetModelEffect(simpleEffect, true); models[1].SetModelEffect(simpleEffect, true);

If you were to run the game now, you would notice that the models appear both lat and gray. This is the correct behavior, as the effect doesn't have the code necessary to do anything else at the moment. After we break down each piece of the shader, we will add some more exciting behavior:

Let's begin at the top. The irst three lines in this effect are its effect paremeters. These three should be familiar to you—they are the world, view, and projection matrices (in HLSL, float4x4 is the equivalent of XNA's Matrix class). There are many types of effect parameters and we will see more later. float4x4 World; float4x4 View; float4x4 Projection; [ 50 ]

Chapter 2

The next few lines are where we deine the structures used in the shaders. In this case, the two structs are VertexShaderInput and VertexShaderOutput. As you might guess, these two structs are used to send input into the vertex shader and retrieve the output from it. The data in the VertexShaderOutput struct is then interpolated between vertices and sent to the pixel shader. This way, when we access the Position value in the pixel shader for a pixel that sits between two vertices, we will get the actual position of that location instead of the position of one of the two vertices. In this case, the input and output are very simple: just the position of the vertex before and after it has been transformed using the world, view, and projection matrices: struct VertexShaderInput { float4 Position : POSITION0; }; struct VertexShaderOutput { float4 Position : POSITION0; };

You may note that the members of these structs are a little different from the properties of a class in C#—in that they must also include what are called semantics. Microsoft's deinition for shader semantics is as follows (http://msdn.microsoft. com/en-us/library/bb509647%28VS.85%29.aspx): A semantic is a string attached to a shader input or output that conveys information about the intended use of a parameter. Basically, we need to specify what we intend to do with each member of our structs so that the graphics card can correctly map the vertex shader's outputs with the pixel shader's inputs. For example, in the previous code, we use the POSITION0 semantics to tell the graphics card that this value is the one that holds the position at which to draw the vertex. The next few lines are the vertex shader itself. Basically, we are just multiplying the input (object space or untransformed) vertex position by the world, view, and projection matrices (the mul function is part of HLSL and is used to multiply matrices and vertices) and returning that value in a new instance of the VertexShaderOutput struct: VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); [ 51 ]

Introduction to HLSL float4x4 viewProjection = mul(View, Projection); output.Position = mul(worldPosition, viewProjection); return output; }

The next bit of code makes up the pixel shader. It accepts a VertexShaderOutput struct as its input (which is passed from the vertex shader), and returns a float4— equivelent to XNA's Vector4 class, in that it is basically a set of four loating point (decimal) numbers. We use the COLOR0 semantic for our return value to let the pipeline know that this function is returning the inal pixel color. In this case, we are using those numbers to represent the red, green, blue, and transparency values respectively of the pixel that we are shading. In this extremely simple pixel shader, we are just returning the color gray (.5, .5, .5), so any pixel covered by the model we are drawing will be gray (like in the previous screenshot). float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { return float4(.5, .5, .5, 1); }

The last part of the shader is the shader deinition. Here, we tell the graphics card which vertex and pixel shader versions to use (every graphics card supports a different set, but in this case we are using vertex shader 1.1 and pixel shader 2.0) and which functions in our code make up the vertex and pixel shaders: technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

Texture mapping Let's now improve our shader by allowing it to render the textures each ModelMeshPart has assigned. As you may recall, the SetModelEffect function in the CModel class attempts to set the texture of each ModelMeshPart to its respective effect. However, it attempts to do so only if it inds the BasicTexture parameter on the effect. Let's add this parameter to our effect now (under the world, view, and projection properties): texture BasicTexture; [ 52 ]

Chapter 2

We need one more parameter in order to draw textures on our models, and that is an instance of a sampler. The sampler is used by HLSL to retrieve the color of the pixel at a given position in a texture—which will be useful later on—in our pixel shader where we will need to retrieve the pixel from the texture corresponding the point on the model we are shading: sampler BasicTextureSampler = sampler_state { texture = ; };

A third effect parameter will allow us to turn texturing on and off: bool TextureEnabled = false;

Every model that has a texture should also have what are called texture coordinates. The texture coordinates are basically two-dimensional coordinates called UV coordinates that range from (0, 0) to (1, 1) and that are assigned to every vertex in the model. These coordinates correspond to the point on the texture that should be drawn onto that vertex. A UV coordinate of (0, 0) corresponds to the top-left of the texture and (1, 1) corresponds to the bottom-right. The texture coordinates allow us to wrap twodimensional textures onto the three-dimensional surfaces of our models. We need to include the texture coordinates in the input and output of the vertex shader, and add the code to pass the UV coordinates through the vertex shader to the pixel shader: struct VertexShaderInput { float4 Position : POSITION0; float2 UV : TEXCOORD0; }; struct VertexShaderOutput { float4 Position : POSITION0; float2 UV : TEXCOORD0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); float4x4 viewProjection = mul(View, Projection); output.Position = mul(worldPosition, viewProjection); output.UV = input.UV; return output; } [ 53 ]

Introduction to HLSL

Finally, we can use the texture sampler, the texture coordinates (also called UV coordinates), and HLSL's tex2D function to retrieve the texture color corresponding to the pixel we are drawing on the model: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { float3 output = float3(1, 1, 1); if (TextureEnabled) output *= tex2D(BasicTextureSampler, input.UV); return float4(output, 1); }

If you run the game now, you will see that the textures are properly drawn onto the models:

Texture sampling The problem with texture sampling is that we are rarely able to simply copy each pixel from a texture directly onto the screen because our models bend and distort the texture due to their shape. Textures are distorted further by the transformations we apply to our models—rotation and other transformations. This means that we almost always have to calculate the approximate position in a texture to sample from and return that value, which is what HLSL's sampler2D does for us. There are a number of considerations to make when sampling.

[ 54 ]

Chapter 2

How we sample from our textures can have a big impact on both our game's appearance and performance. More advanced sampling (or iltering) algorithms look better but slow down the game. Mip mapping refers to the use of multiple sizes of the same texture. These multiple sizes are calculated before the game is run and stored in the same texture, and the graphics card will swap them out on the ly, using a smaller version of the texture for objects in the distance, and so on. Finally, the address mode that we use when sampling will affect how the graphics card handles UV coordinates outside the (0, 1) range. For example, if the address mode is set to "clamp", the UV coordinates will be clamped to (0, 1). If the address mode is set to "wrap," the coordinates will be wrapped through the texture repeatedly. This can be used to create a tiling effect on terrain, for example. For now, because we are drawing so few models, we will use anisotropic iltering. We will also enable mip mapping and set the address mode to "wrap". sampler BasicTextureSampler = sampler_state { texture = ; MinFilter = Anisotropic; // Minification Filter MagFilter = Anisotropic; // Magnification Filter MipFilter = Linear; // Mip-mapping AddressU = Wrap; // Address Mode for U Coordinates AddressV = Wrap; // Address Mode for V Coordinates };

This will give our models a nice, smooth appearance in the foreground and a uniform appearance in the background:

[ 55 ]

Introduction to HLSL

Diffuse colors Looking at the screenshot that appears immediately before the Texture sampling section, there is a problem with our render: some parts of the model are completely white. This is because this particular model does not have textures assigned to those pieces. Right now, if we don't have a texture assigned, our effect simply defaults to white. However, this model also speciies what are called diffuse colors. These are basic color values assigned to each ModelMeshPart. In this case, drawing the diffuse colors will ix our problem. We are already loading the diffuse colors into the MeshTag class, so all we need to do is add a parameter for them to our effect: float3 DiffuseColor = float3(1, 1, 1);

Now we can make a small change to our pixel shader to use the diffuse color values instead of white: float3 output = DiffuseColor;

Ambient lighting Our model is now textured correctly and is using the correct diffuse color values, but it still looks lat and uninteresting. With the groundwork out of the way, we can start recreating some of the lighting effects provided by the BasicEffect shader. Let's start with ambient lighting: Ambient lighting is an attempt at simulating all the light that bounces off of other objects, the ground, and so on, which would be found in the real world. If you look at an object that doesn't have light shining directly onto it, you can still see it because light has bounced off of other objects nearby and lit it somewhat. As we can't possibly simulate all the bounced rays of light (technically, [ 56 ]

Chapter 2

we can with a technique called ray tracing, but this is very slow), we instead simplify it into a constant color value. To add an ambient light value, we simply add another effect parameter: float3 AmbientLightColor = float3(.1, .1, .1);

Now, we once again need only a small modiication to the pixel shader: float3 output = DiffuseColor + AmbientColor;

This will produce the following output (if you're following along with the code iles, I've changed the model to a teapot at this point, as it will demonstrate lighting better due to its shape). The object should now look darker, as this light is mainly meant to ill in darker areas as though light were being bounced onto the object from its surroundings.

Lambertian directional lighting Our next lighting type, directional lighting, is meant to provide some deinition to our objects. The formula used for this lighting type is called Lambertian lighting, and is very simple: kdiff = max(l • n, 0) This equation simply means that the lighting amount on a given face is the dot product of the light vector and the face's normal. The dot product is deined as: X • Y = |X| |Y| cos θ [ 57 ]

Chapter 2

We next need to update the vertex shader to transform the normals passed in (which are in object space) with the world matrix to move them into world space. The following line does just this, and should be inserted before the VertexShaderOutput is returned from the vertex shader. Note that the value is "normalized" with the normalize() function. This resizes the normal vector to length 1 as it may have been scaled by the world matrix, which would cause the lighting to be incorrect later on. As discussed earlier, keeping our vectors at length 1 keeps dot products simple. output.Normal = mul(input.Normal, World);

We also need to add a parameter at the beginning of the effect for the light direction. While we're at it, we will also add a parameter for the light color: float3 LightDirection = float3(1, 1, 1); float3 LightColor = float3(0.9, 0.9, 0.9);

Finally, we can update the pixel shader to perform the lighting calculation. Note that once again we use the normalize function, this time to ensure that the user-given light vector's components fall within the -1 to 1 range. The dot product of the vertex's normal and the light direction is multiplied by the light color and added to the total amount of light. Note that the saturate() function clamps the bottom end of a number to 0 to avoid negative light amounts: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Start with diffuse color float3 color = DiffuseColor; // Texture if necessary if (TextureEnabled) color *= tex2D(BasicTextureSampler, input.UV); // Start with ambient lighting float3 lighting = AmbientColor; float3 lightDir = normalize(LightDirection); float3 normal = normalize(input.Normal); // Add lambertian lighting lighting += saturate(dot(lightDir, normal)) * LightColor; // Calculate final color float3 output = saturate(lighting) * color; return float4(output, 1); }

[ 59 ]

Introduction to HLSL

This produces the effect of a light falling diagonally across the model, and highlights its edges well:

Phong specular highlights Our object is now looking very deined, but it is still missing one thing: specular highlights. Specular highlights is the formal term for the "shininess" you see when looking at the relection of a light source on an object's surface. Think of the highlights like you are looking at a light bulb through a mirror—you can clearly see the light source. Now, fog the mirror. You can't see the light source, but you can see the circular gradiant effect where the light source would appear on a shinier surface. It should be no surprise, then, that the formula for calculating specular highlights (what is called the phong shading model) is as follows: kspec = max(r • v, 0)n Translate this to a mirror: r is the ray of the light bouncing off from the light source of the mirror (a mirror of our last equation's l), and v is the view direction. Given the behavior of the last equation and the dot product, it would make sense that the closer v, or the direction your eye is facing is to the relected light vector, the brighter the specular highlight would appear. If you were to move your eye far enough across the mirror, you would lose sight of the light entirely. In that case, there would be no specular highlight as you no longer have light relecting across the mirror into your eye.

[ 60 ]

Introduction to HLSL

Finally, we can update the pixel shader: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Start with diffuse color float3 color = DiffuseColor; // Texture if necessary if (TextureEnabled) color *= tex2D(BasicTextureSampler, input.UV); // Start with ambient lighting float3 lighting = AmbientColor; float3 lightDir = normalize(LightDirection); float3 normal = normalize(input.Normal); // Add lambertian lighting lighting += saturate(dot(lightDir, normal)) * LightColor; float3 refl = reflect(lightDir, normal); float3 view = normalize(input.ViewDirection); // Add specular highlights lighting += pow(saturate(dot(refl, view)), SpecularPower) * SpecularColor; // Calculate final color float3 output = saturate(lighting) * color; return float4(output, 1); }

For your convenience, the full shader is reproduced here—the screenshot of the results of our specular highlights, texturing, directional lighting, ambient lighting, and diffuse color:

[ 62 ]

Chapter 2 float4x4 World; float4x4 View; float4x4 Projection; float3 CameraPosition; texture BasicTexture; sampler BasicTextureSampler = sampler_state { texture = ; MinFilter = Anisotropic; // Minification Filter MagFilter = Anisotropic; // Magnification Filter MipFilter = Linear; // Mip-mapping AddressU = Wrap; // Address Mode for U Coordinates AddressV = Wrap; // Address Mode for V Coordinates }; bool TextureEnabled = false; float3 DiffuseColor = float3(1, 1, 1); float3 AmbientColor = float3(0.1, 0.1, 0.1); float3 LightDirection = float3(1, 1, 1); float3 LightColor = float3(0.9, 0.9, 0.9); float SpecularPower = 32; float3 SpecularColor = float3(1, 1, 1); struct VertexShaderInput { float4 Position : POSITION0; float2 UV : TEXCOORD0; float3 Normal : NORMAL0; }; struct VertexShaderOutput { float4 Position : POSITION0; float2 UV : TEXCOORD0; float3 Normal : TEXCOORD1; float3 ViewDirection : TEXCOORD2; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output;

[ 63 ]

Introduction to HLSL float4 worldPosition = mul(input.Position, World); float4x4 viewProjection = mul(View, Projection); output.Position = mul(worldPosition, viewProjection); output.UV = input.UV; output.Normal = mul(input.Normal, World); output.ViewDirection = worldPosition - CameraPosition; return output; } float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Start with diffuse color float3 color = DiffuseColor; // Texture if necessary if (TextureEnabled) color *= tex2D(BasicTextureSampler, input.UV); // Start with ambient lighting float3 lighting = AmbientColor; float3 lightDir = normalize(LightDirection); float3 normal = normalize(input.Normal); // Add lambertian lighting lighting += saturate(dot(lightDir, normal)) * LightColor; float3 refl = reflect(lightDir, normal); float3 view = normalize(input.ViewDirection); // Add specular highlights lighting += pow(saturate(dot(refl, view)), SpecularPower) * SpecularColor; // Calculate final color float3 output = saturate(lighting) * color; return float4(output, 1); } technique Technique1 { [ 64 ]

Chapter 2 pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

Creating a Material class to store effect parameters By now we have accumulated a large number of effect parameters in our lighting effect. It would be great if we had an easy way to set and change them from our C# code, so we will add a class called Material that does solely this. Each Model will have its own material that will store surface properties such as specularity, diffuse color, and so on. This class will then handle setting those properties to an instance of the Effect class. As we may have many different types of material, we will also set up a base class: public class Material { public virtual void SetEffectParameters(Effect effect) { } } public class LightingMaterial : Material { public Vector3 AmbientColor { get; set; } public Vector3 LightDirection { get; set; } public Vector3 LightColor { get; set; } public Vector3 SpecularColor { get; set; } public LightingMaterial() { AmbientColor = new Vector3(.1f, .1f, .1f); LightDirection = new Vector3(1, 1, 1); LightColor = new Vector3(.9f, .9f, .9f); SpecularColor = new Vector3(1, 1, 1); } public override void SetEffectParameters(Effect effect) {

[ 65 ]

Introduction to HLSL if (effect.Parameters["AmbientColor"] != null) effect.Parameters["AmbientColor"].SetValue(AmbientColor); if (effect.Parameters["LightDirection"] != null) effect.Parameters["LightDirection"].SetValue(LightDirection); if (effect.Parameters["LightColor"] != null) effect.Parameters["LightColor"].SetValue(LightColor); if (effect.Parameters["SpecularColor"] != null) effect.Parameters["SpecularColor"].SetValue(SpecularColor); } }

We will now update the model class to use the Material class. First, we need an instance of the Material class: public Material Material { get; set; }

We initialize it into a new material in the constructor. As this is the simplest material, no changes will be made to the effect when drawing. This is good as, by default, that effect is a BasicEffect, and does not match our parameters. this.Material = new Material();

Finally, after the world, view, and projection matrices have been set to the effect in the Draw() function, we will call SetEffectParameters() on our material: Material.SetEffectParameters(effect);

We can inally update the Game1 class to set the material to the model in the LoadContent() method, immediately after setting the effect to the model: Effect simpleEffect = Content.Load("LightingEffect"); models[0].SetModelEffect(simpleEffect, true); models[1].SetModelEffect(simpleEffect, true); LightingMaterial mat = new LightingMaterial(); models[0].Material = mat; models[1].Material = mat;

[ 66 ]

Chapter 2

So if we now, for example, wanted to light our pot blue with red ambient light, we could set the following options to the modelMaterial and they would be automatically relected onto the shader when the model is drawn: mat.AmbientColor = Color.Red.ToVector3() * .15f; mat.LightColor = Color.Blue.ToVector3() * .85f;

Summary Now that you've completed this chapter, you've learned the basics of shading—what the programmable pipeline is, what shaders and effects are, and how to write them in HLSL. You saw how to implement a number of lighting types with HLSL, as well as other effects such as diffuse colors and texturing. You upgraded the CModel class to support custom shaders and created a Material class to manage effect parameters. If you'd like to know more about HLSL, Microsoft's oficial documentation is very extensive. It covers all of the built-in functions, iltering, and address modes, and so on, and has several example shaders. It is available at http://msdn.microsoft. com/en-us/library/bb509638%28VS.85%29.aspx. In the next chapter, we will look at more lighting effects and expand our lighting system to support multiple lights. We will look at several ways of doing so and will ultimately create a system that supports an arbitrary number of models and lights while remaining reasonably fast.

[ 67 ]

Advanced Lighting By the end of the last chapter, we had developed a system to draw a model with custom effects and we had created an effect that rendered a directional light shining onto a model. This is useful if we want a scene with only sunlight, for example, but what if we wanted to light up a speciic area? To start with, we'd need a light type that more accurately models real world lights—the "point" light. We will start this chapter by implementing this type of light in HLSL. We will then look at a similar light type—the "spot" light. We will spend the rest of the chapter looking at two ways to draw multiple lights at the same time.

Implementing a point light with HLSL A point light is just a light that shines equally in all directions around itself (like a light bulb) and falls off over a given distance:

In this case, a point light is simply modeled as a directional light that will slowly fade to darkness over a given distance. To achieve a linear attenuation, we would simply divide the distance between the light and the object by the attenuation distance, invert the result (subtract from 1), and then multiply the lambertian lighting with the result. This would cause an object directly next to the light source to be fully lit, and an object at the maximum attenuation distance to be completely unlit.

Advanced Lighting

However, in practice, we will raise the result of the division to a given power before inverting it to achieve a more exponential falloff:

Katt = 1 – (d / a)

f

In the previous equation, Katt is the brightness scalar that we will multiply the lighting amount by, d is the distance between the vertex and light source, a is the distance at which the light should stop affecting objects, and f is the falloff exponent that determines the shape of the curve. We can implement this easily with HLSL and a new Material class. The new Material class is similar to the material for a directional light, but speciies a light position rather than a light direction. For the sake of simplicity, the effect we will use will not calculate specular highlights, so the material does not include a "specularity" value. It also includes new values, LightAttenuation and LightFalloff, which specify the distance at which the light is no longer visible and what power to raise the division to. public class PointLightMaterial : Material { public Vector3 AmbientLightColor { get; set; } public Vector3 LightPosition { get; set; } public Vector3 LightColor { get; set; } public float LightAttenuation { get; set; } public float LightFalloff { get; set; } public PointLightMaterial() { AmbientLightColor = new Vector3(.15f, .15f, .15f); LightPosition = new Vector3(0, 0, 0); LightColor = new Vector3(.85f, .85f, .85f); LightAttenuation = 5000; LightFalloff = 2; } public override void SetEffectParameters(Effect effect) { if (effect.Parameters["AmbientLightColor"] != null) [ 70 ]

Chapter 3 effect.Parameters["AmbientLightColor"].SetValue( AmbientLightColor); if (effect.Parameters["LightPosition"] != null) effect.Parameters["LightPosition"].SetValue(LightPosition); if (effect.Parameters["LightColor"] != null) effect.Parameters["LightColor"].SetValue(LightColor); if (effect.Parameters["LightAttenuation"] != null) effect.Parameters["LightAttenuation"].SetValue( LightAttenuation); if (effect.Parameters["LightFalloff"] != null) effect.Parameters["LightFalloff"].SetValue(LightFalloff); } }

The new effect has parameters to relect those values: float4x4 World; float4x4 View; float4x4 Projection; float3 AmbientLightColor = float3(.15, .15, .15); float3 DiffuseColor = float3(.85, .85, .85); float3 LightPosition = float3(0, 0, 0); float3 LightColor = float3(1, 1, 1); float LightAttenuation = 5000; float LightFalloff = 2; texture BasicTexture; sampler BasicTextureSampler = sampler_state { texture = ; }; bool TextureEnabled = true;

The vertex shader output struct now includes a copy of the vertex's world position that will be used to calculate the light falloff (attenuation) and light direction. struct VertexShaderInput { float4 Position : POSITION0; float2 UV : TEXCOORD0; float3 Normal : NORMAL0; };

[ 71 ]

Advanced Lighting struct VertexShaderOutput { float4 Position : POSITION0; float2 UV : TEXCOORD0; float3 Normal : TEXCOORD1; float4 WorldPosition : TEXCOORD2; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4 worldPosition = mul(input.Position, World); float4 viewPosition = mul(worldPosition, View); output.Position = mul(viewPosition, Projection); output.WorldPosition = worldPosition; output.UV = input.UV; output.Normal = mul(input.Normal, World); return output; }

Finally, the pixel shader calculates the light much the same way that the directional light did, but uses a per-vertex light direction rather than a global light direction. It also determines how far along the attenuation value the vertex's position is and darkens it accordingly. The texture, ambient light, and diffuse color are calculated as usual: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { float3 diffuseColor = DiffuseColor; if (TextureEnabled) diffuseColor *= tex2D(BasicTextureSampler, input.UV).rgb; float3 totalLight = float3(0, 0, 0); totalLight += AmbientLightColor; float3 lightDir = normalize(LightPosition - input.WorldPosition); float diffuse = saturate(dot(normalize(input.Normal), lightDir)); float d = distance(LightPosition, input.WorldPosition); float att = 1 - pow(clamp(d / LightAttenuation, 0, 1), LightFalloff); totalLight += diffuse * att * LightColor; return float4(diffuseColor * totalLight, 1); } [ 72 ]

Chapter 3

We can now achieve the above image using the following scene setup from the Game1 class: models.Add(new CModel(Content.Load("teapot"), new Vector3(0, 60, 0), Vector3.Zero, new Vector3(60), GraphicsDevice)); models.Add(new CModel(Content.Load("ground"), Vector3.Zero, Vector3.Zero, Vector3.One, GraphicsDevice)); Effect simpleEffect = Content.Load("PointLightEffect"); models[0].SetModelEffect(simpleEffect, true); models[1].SetModelEffect(simpleEffect, true); PointLightMaterial mat = new PointLightMaterial(); mat.LightPosition = new Vector3(0, 1500, 1500); mat.LightAttenuation = 3000; models[0].Material = mat; models[1].Material = mat; camera = new FreeCamera(new Vector3(0, 300, 1600), MathHelper.ToRadians(0), // Turned around 153 degrees MathHelper.ToRadians(5), // Pitched up 13 degrees GraphicsDevice);

[ 73 ]

Chapter 3 if (effect.Parameters["AmbientLightColor"] != null) effect.Parameters["AmbientLightColor"].SetValue( AmbientLightColor); if (effect.Parameters["LightPosition"] != null) effect.Parameters["LightPosition"].SetValue(LightPosition); if (effect.Parameters["LightColor"] != null) effect.Parameters["LightColor"].SetValue(LightColor); if (effect.Parameters["LightDirection"] != null) effect.Parameters["LightDirection"].SetValue(LightDirection); if (effect.Parameters["ConeAngle"] != null) effect.Parameters["ConeAngle"].SetValue( MathHelper.ToRadians(ConeAngle / 2)); if (effect.Parameters["LightFalloff"] != null) effect.Parameters["LightFalloff"].SetValue(LightFalloff); } }

Now we can create a new effect that will render a spot light. We will start by copying the point light's effect and making the following changes to the second block of effect parameters: float3 AmbientLightColor = float3(.15, .15, .15); float3 DiffuseColor = float3(.85, .85, .85); float3 LightPosition = float3(0, 5000, 0); float3 LightDirection = float3(0, -1, 0); float ConeAngle = 90; float3 LightColor = float3(1, 1, 1); float LightFalloff = 20;

Finally, we can update the pixel shader to perform the lighting calculations: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { float3 diffuseColor = DiffuseColor; if (TextureEnabled) diffuseColor *= tex2D(BasicTextureSampler, input.UV).rgb; float3 totalLight = float3(0, 0, 0); totalLight += AmbientLightColor; float3 lightDir = normalize(LightPosition - input.WorldPosition); [ 75 ]

Advanced Lighting float diffuse = saturate(dot(normalize(input.Normal), lightDir)); // (dot(p - lp, ld) / cos(a))^f float d = dot(-lightDir, normalize(LightDirection)); float a = cos(ConeAngle); float att = 0; if (a < d) att = 1 - pow(clamp(a / d, 0, 1), LightFalloff); totalLight += diffuse * att * LightColor; return float4(diffuseColor * totalLight, 1); }

If we were to then set up the material as follows and use our new effect, we would see the following result: SpotLightMaterial mat = new SpotLightMaterial(); mat.LightDirection = new Vector3(0, -1, -1); mat.LightPosition = new Vector3(0, 3000, 2700); mat.LightFalloff = 200;

[ 76 ]

Chapter 3

Drawing multiple lights Now that we can draw one light, the natural question to ask is how to draw more than one light. Well this, unfortunately, is not simple. There are a number of approaches—the easiest of which is to simply loop through a certain number of lights in the pixel shader and sum a total lighting value. Let's create a new shader based on the directional light effect that we created in the last chapter to do just that. We'll start by copying that effect, then modifying some of the effect parameters as follows. Notice that instead of a single light direction and color, we instead have an array of three of each, allowing us to draw up to three lights: #define NUMLIGHTS 3 float3 DiffuseColor = float3(1, 1, 1); float3 AmbientColor = float3(0.1, 0.1, 0.1); float3 LightDirection[NUMLIGHTS]; float3 LightColor[NUMLIGHTS]; float SpecularPower = 32; float3 SpecularColor = float3(1, 1, 1);

Second, we need to update the pixel shader to do the lighting calculations one time per light: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Start with diffuse color float3 color = DiffuseColor; // Texture if necessary if (TextureEnabled) color *= tex2D(BasicTextureSampler, input.UV); // Start with ambient lighting float3 lighting = AmbientColor; float3 normal = normalize(input.Normal); float3 view = normalize(input.ViewDirection); // Perform lighting calculations per light for (int i = 0; i < NUMLIGHTS; i++) { float3 lightDir = normalize(LightDirection[i]); // Add lambertian lighting lighting += saturate(dot(lightDir, normal)) * LightColor[i]; [ 77 ]

Advanced Lighting float3 refl = reflect(lightDir, normal); // Add specular highlights lighting += pow(saturate(dot(refl, view)), SpecularPower) * SpecularColor; } // Calculate final color float3 output = saturate(lighting) * color; return float4(output, 1); }

We now need a new Material class to work with this shader: public class MultiLightingMaterial : Material { public Vector3 AmbientColor { get; set; } public Vector3[] LightDirection { get; set; } public Vector3[] LightColor { get; set; } public Vector3 SpecularColor { get; set; } public MultiLightingMaterial() { AmbientColor = new Vector3(.1f, .1f, .1f); LightDirection = new Vector3[3]; LightColor = new Vector3[] { Vector3.One, Vector3.One, Vector3.One }; SpecularColor = new Vector3(1, 1, 1); } public override void SetEffectParameters(Effect effect) { if (effect.Parameters["AmbientColor"] != null) effect.Parameters["AmbientColor"].SetValue(AmbientColor); if (effect.Parameters["LightDirection"] != null) effect.Parameters["LightDirection"].SetValue(LightDirection); if (effect.Parameters["LightColor"] != null) effect.Parameters["LightColor"].SetValue(LightColor); if (effect.Parameters["SpecularColor"] != null) effect.Parameters["SpecularColor"].SetValue(SpecularColor); } } [ 78 ]

Chapter 3

If we wanted to implement the three directional light systems found in the BasicEffect class, we would now just need to copy the light direction values over to our shader: Effect simpleEffect = Content.Load("MultiLightingEffect"); models[0].SetModelEffect(simpleEffect, true); models[1].SetModelEffect(simpleEffect, true); MultiLightingMaterial mat = new MultiLightingMaterial(); BasicEffect effect = new BasicEffect(GraphicsDevice); effect.EnableDefaultLighting(); mat.LightDirection[0] = -effect.DirectionalLight0.Direction; mat.LightDirection[1] = -effect.DirectionalLight1.Direction; mat.LightDirection[2] = -effect.DirectionalLight2.Direction; mat.LightColor = new Vector3[] { new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0.5f) }; models[0].Material = mat; models[1].Material = mat;

[ 79 ]

Advanced Lighting

Prelighting This method works, but it limits us to just three lights. In any real game, this number is way too small. We could add more lights to the shader, but we would still be quite limited as we cannot do more because we have a limited number of shader instructions to work with. Our next option would be to concoct a system to draw the scene repeatedly with multiple light sources, and then blend them together. However, this would force us to draw every object in the scene once for every light in the scene—an extremely ineficient approach. Instead, we will use an approach called prelighting. In this approach, we store the information that we need to calculate lighting for a pixel into textures that can then be loaded later on by another shader to do the lighting calculation themselves. This has two beneits: First, we are drawing each object only once. Second, we use spheres to approximate our lights so that we can run a pixel shader on only the pixels a light would affect, limiting the lighting calculations to pixels that are actually affected by a light. Therefore, if a light is small enough or distant enough, we don't need to perform its lighting calculations for every pixel on the screen. The prelighting process is as follows: 1. Render the scene into two textures, storing the distance of each vertex from the camera and the normal at each vertex. 2. Render the scene into another texture, rendering each (point) light as a sphere, performing lighting calculations in the pixel shader using the values stored in the corresponding pixels of the last step's textures. 3. Render the inal scene, multiplying diffuse colors, textures, and so on in the pixel shader by the lighting values stored in the corresponding pixels of the last step's texture. We will implement prelighting in the next sections. This is a bit of a process, but in the end, you'll be able to draw a large number of lights and models in your scene—well worth the effort as this is a common stumbling block for new game developers. As an example, the following scene was rendered with eight different-colored point lights:

[ 80 ]

Chapter 3

Storing depth and normal values Recall that we need two pieces of information to calculate simple Lambertian lighting—the position of each vertex and its normals. In a prelighting approach to lighting, we store these two pieces of information into two textures using "multiple render targets". To use multiple render targets, we output multiple values from the pixel shader using multiple COLOR (COLOR0, COLOR1, and so on) semantics. The output from the effect will then be stored into the "render targets" (similar to textures) of our choosing. We will see shortly how this is set up from the XNA side. We store the distance between each vertex and the camera into one texture and the "depth" texture and each vertex's normal into the second texture. Note that the depth is divided by the far plane distance before storing it into the texture to keep it in the 0 to 1 range:

Similarly, the normals are scaled from the -1 to 1 range to the 0 to 1 range.

[ 81 ]

Advanced Lighting

The effect that stores the depth and normal values is as follows. Create a new effect in your content project called PPDepthNormal.fx and add the following code: float4x4 World; float4x4 View; float4x4 Projection; struct VertexShaderInput { float4 Position : POSITION0; float3 Normal : NORMAL0; }; struct VertexShaderOutput { float4 Position : POSITION0; float2 Depth : TEXCOORD0; float3 Normal : TEXCOORD1; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4x4 viewProjection = mul(View, Projection); float4x4 worldViewProjection = mul(World, viewProjection); output.Position = mul(input.Position, worldViewProjection); output.Normal = mul(input.Normal, World); // Position's z and w components correspond to the distance // from camera and distance of the far plane respectively output.Depth.xy = output.Position.zw; return output; } // We render to two targets simultaneously, so we can't // simply return a float4 from the pixel shader struct PixelShaderOutput { float4 Normal : COLOR0; float4 Depth : COLOR1; }; [ 82 ]

Chapter 3 PixelShaderOutput PixelShaderFunction(VertexShaderOutput input) { PixelShaderOutput output; // Depth is stored as distance from camera / far plane distance // to get value between 0 and 1 output.Depth = input.Depth.x / input.Depth.y; // Normal map simply stores X, Y and Z components of normal // shifted from (-1 to 1) range to (0 to 1) range output.Normal.xyz = (normalize(input.Normal).xyz / 2) + .5; // Other components must be initialized to compile output.Depth.a = 1; output.Normal.a = 1; return output; } technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

Creating the light map Once we have our normal and depth values recorded, we can generate the light map. We'll be creating a class in a moment to tie all of the steps together, but irst, let's look at the effect that generates light maps. Because the depth and normal values are stored in a texture and we can't pass them from a vertex shader, we need a way to map 3D positions to pixel coordinates in the two textures. For the sake of convenience, we will place the functions that do so in a shared ile that will be included in a few of the remaining effects. You'll need to create a new effect ile and rename it to PPShared.vsi. float viewportWidth; float viewportHeight; // Calculate the 2D screen position of a 3D position float2 postProjToScreen(float4 position) [ 83 ]

Advanced Lighting { float2 screenPos = position.xy / position.w; return 0.5f * (float2(screenPos.x, -screenPos.y) + 1); } // Calculate the size of one half of a pixel, to convert // between texels and pixels float2 halfPixel() { return 0.5f / float2(viewportWidth, viewportHeight); }

Now we can create the effect that uses these values to perform the lighting calculations. The effect parameters are fairly self-explanatory—we include texture parameters for the depth and normal textures, world, view, and projection matrices (remember that we are drawing the light as a spherical model), and point light parameters. The vertex shader simply transforms from object space to screen space: float4x4 WorldViewProjection; float4x4 InvViewProjection; texture2D DepthTexture; texture2D NormalTexture; sampler2D depthSampler = sampler_state { texture = ; minfilter = point; magfilter = point; mipfilter = point; }; sampler2D normalSampler = sampler_state { texture = ; minfilter = point; magfilter = point; mipfilter = point; }; float3 LightColor; float3 LightPosition; float LightAttenuation; // Include shared functions #include "PPShared.vsi"

[ 84 ]

Chapter 3 struct VertexShaderInput { float4 Position : POSITION0; }; struct VertexShaderOutput { float4 Position : POSITION0; float4 LightPosition : TEXCOORD0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; output.Position = mul(input.Position, WorldViewProjection); output.LightPosition = output.Position; return output; }

The pixel shader is where the magic happens—we sample the depth and normal values from the textures that we rendered earlier and use the depth values to reconstruct our original world space position. We then use that position and its normal to perform the lighting calculations that we saw earlier: float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Find the pixel coordinates of the input position in the depth // and normal textures float2 texCoord = postProjToScreen(input.LightPosition) + halfPixel(); // Extract the depth for this pixel from the depth map float4 depth = tex2D(depthSampler, texCoord); // Recreate the position with the UV coordinates and depth value float4 position; position.x = texCoord.x * 2 - 1; position.y = (1 - texCoord.y) * 2 - 1; position.z = depth.r; position.w = 1.0f; // Transform position from screen space to world space position = mul(position, InvViewProjection); [ 85 ]

Advanced Lighting position.xyz /= position.w; // Extract the normal from the normal map and move from // 0 to 1 range to -1 to 1 range float4 normal = (tex2D(normalSampler, texCoord) - .5) * 2; // Perform the lighting calculations for a point light float3 lightDirection = normalize(LightPosition - position); float lighting = clamp(dot(normal, lightDirection), 0, 1); // Attenuate the light to simulate a point light float d = distance(LightPosition, position); float att = 1 - pow(d / LightAttenuation, 6); return float4(LightColor * lighting * att, 1); }

Drawing models with the light map After we have created the light map, we can sample the values it stores when drawing our models for the inal pass instead of performing the lighting equations. We will again use the functions in our shared ile to sample from the light map. The rest of the effects are similar to those we have already seen, transforming to screen space in the vertex shader and performing texture lookups in the pixel shader. At the end of the pixel shader, we multiply the lighting value sampled from the light map with the diffuse color to get the inal color: float4x4 World; float4x4 View; float4x4 Projection; texture2D BasicTexture; sampler2D basicTextureSampler = sampler_state { texture = ; addressU = wrap; addressV = wrap; minfilter = anisotropic; magfilter = anisotropic; mipfilter = linear; }; bool TextureEnabled = true; texture2D LightTexture; [ 86 ]

Chapter 3 texture2D LightTexture; sampler2D lightSampler = sampler_state { texture = ; minfilter = point; magfilter = point; mipfilter = point; }; float3 AmbientColor = float3(0.15, 0.15, 0.15); float3 DiffuseColor; #include "PPShared.vsi" struct VertexShaderInput { float4 Position : POSITION0; float2 UV : TEXCOORD0; }; struct VertexShaderOutput { float4 Position : POSITION0; float2 UV : TEXCOORD0; float4 PositionCopy : TEXCOORD1; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; float4x4 worldViewProjection = mul(World, mul(View, Projection)); output.Position = mul(input.Position, worldViewProjection); output.PositionCopy = output.Position; output.UV = input.UV; return output; } float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Sample model's texture [ 87 ]

Advanced Lighting float3 basicTexture = tex2D(basicTextureSampler, input.UV); if (!TextureEnabled) basicTexture = float4(1, 1, 1, 1); // Extract lighting value from light map float2 texCoord = postProjToScreen(input.PositionCopy) + halfPixel(); float3 light = tex2D(lightSampler, texCoord); light += AmbientColor; return float4(basicTexture * DiffuseColor * light, 1); }

Creating the prelighting renderer Let's now create a class that manages the effects we created and the rest of the prelighting process. This class, PrelightingRenderer, will be responsible for calculating the depth and normal maps, light map, and eventually preparing models to be drawn with the calculated lighting values. The following framework version loads all of the effects and the model that we will need to perform the prelighting process. The PrelightingRenderer also handles the creation of three "surfaces" or "render targets" that we will render the depth, normal, and light maps into. Render targets serve to capture the output of the graphics card and store it in memory, much like a texture. We can then access the data in that texture later, when we are calculating the light map, for example. We can also draw into multiple render targets at the same time using the various color semantics, as we saw earlier in DepthNormal.fx. public class PrelightingRenderer { // Normal, depth, and light map render targets RenderTarget2D depthTarg; RenderTarget2D normalTarg; RenderTarget2D lightTarg; // Depth/normal effect and light mapping effect Effect depthNormalEffect; Effect lightingEffect; // Point light (sphere) mesh Model lightMesh;

[ 88 ]

Chapter 3 // List of models, lights, and the camera public List Models { get; set; } public List Lights { get; set; } public Camera Camera { get; set; } GraphicsDevice graphicsDevice; int viewWidth = 0, viewHeight = 0; public PrelightingRenderer(GraphicsDevice GraphicsDevice, ContentManager Content) { viewWidth = GraphicsDevice.Viewport.Width; viewHeight = GraphicsDevice.Viewport.Height; // Create the three render targets depthTarg = new RenderTarget2D(GraphicsDevice, viewWidth, viewHeight, false, SurfaceFormat.Single, DepthFormat.Depth24); normalTarg = new RenderTarget2D(GraphicsDevice, viewWidth, viewHeight, false, SurfaceFormat.Color, DepthFormat.Depth24); lightTarg = new RenderTarget2D(GraphicsDevice, viewWidth, viewHeight, false, SurfaceFormat.Color, DepthFormat.Depth24); // Load effects depthNormalEffect = Content.Load("PPDepthNormal"); lightingEffect = Content.Load("PPLight"); // Set effect parameters to light mapping effect lightingEffect.Parameters["viewportWidth"].SetValue(viewWidth); lightingEffect.Parameters["viewportHeight"].SetValue(viewHeight); // Load point light mesh and set light mapping effect to it lightMesh = Content.Load("PPLightMesh"); lightMesh.Meshes[0].MeshParts[0].Effect = lightingEffect; this.graphicsDevice = GraphicsDevice; } public void Draw() { drawDepthNormalMap(); drawLightMap(); prepareMainPass(); } [ 89 ]

Advanced Lighting void drawDepthNormalMap() { } void drawLightMap() { } void prepareMainPass() { } }

Now we can start illing in the three empty functions in the framework of this class. The drawDepthNormalMap() function will be responsible for capturing the depth and normal map information from all of the models currently in view. We already wrote the effect that does this, so all we need to do is set our render target and draw the models with the PPDepthNormal.fx effect: void drawDepthNormalMap() { // Set the render targets to 'slots' 1 and 2 graphicsDevice.SetRenderTargets(normalTarg, depthTarg); // Clear the render target to 1 (infinite depth) graphicsDevice.Clear(Color.White); // Draw each model with the PPDepthNormal effect foreach (CModel model in Models) { model.CacheEffects(); model.SetModelEffect(depthNormalEffect, false); model.Draw(Camera.View, Camera.Projection, ((FreeCamera)Camera).Position); model.RestoreEffects(); } // Un-set the render targets graphicsDevice.SetRenderTargets(null); }

[ 90 ]

Chapter 3

The second function takes the depth and normal map data from the irst and uses it to perform the lighting calculations for each point light in the scene, approximated as spheres: void drawLightMap() { // Set the depth and normal map info to the effect lightingEffect.Parameters["DepthTexture"].SetValue(depthTarg); lightingEffect.Parameters["NormalTexture"].SetValue(normalTarg); // Calculate the view * projection matrix Matrix viewProjection = Camera.View * Camera.Projection; // Set the inverse of the view * projection matrix to the effect Matrix invViewProjection = Matrix.Invert(viewProjection); lightingEffect.Parameters["InvViewProjection"].SetValue( invViewProjection); // Set the render target to the graphics device graphicsDevice.SetRenderTarget(lightTarg); // Clear the render target to black (no light) graphicsDevice.Clear(Color.Black); // Set render states to additive (lights will add their influences) graphicsDevice.BlendState = BlendState.Additive; graphicsDevice.DepthStencilState = DepthStencilState.None; foreach (PPPointLight light in Lights) { // Set the light's parameters to the effect light.SetEffectParameters(lightingEffect); // Calculate the world * view * projection matrix and set it to // the effect Matrix wvp = (Matrix.CreateScale(light.Attenuation) * Matrix.CreateTranslation(light.Position)) * viewProjection; lightingEffect.Parameters["WorldViewProjection"].SetValue(wvp); // Determine the distance between the light and camera float dist = Vector3.Distance(((FreeCamera)Camera).Position, light.Position);

[ 91 ]

Advanced Lighting // If the camera is inside the light-sphere, invert the cull mode // to draw the inside of the sphere instead of the outside if (dist < light.Attenuation) graphicsDevice.RasterizerState = RasterizerState.CullClockwise; // Draw the point-light-sphere lightMesh.Meshes[0].Draw(); // Revert the cull mode graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; } // Revert the blending and depth render states graphicsDevice.BlendState = BlendState.Opaque; graphicsDevice.DepthStencilState = DepthStencilState.Default; // Un-set the render target graphicsDevice.SetRenderTarget(null); }

The last function, prepareMainPass(), attempts to set the light map and viewport width/height to the effect each model is currently using. The models can then sample from the light map to obtain lighting information, as our PPLight.fx function does: void prepareMainPass() { foreach (CModel model in Models) foreach (ModelMesh mesh in model.Model.Meshes) foreach (ModelMeshPart part in mesh.MeshParts) { // Set the light map and viewport parameters to each model's effect if (part.Effect.Parameters["LightTexture"] != null) part.Effect.Parameters["LightTexture"].SetValue(lightTarg); if (part.Effect.Parameters["viewportWidth"] != null) part.Effect.Parameters["viewportWidth"].SetValue(viewWidth); if (part.Effect.Parameters["viewportHeight"] != null) part.Effect.Parameters["viewportHeight"]. SetValue(viewHeight); } } [ 92 ]

Chapter 3

Using the prelighting renderer With that, we've inished the prelighting renderer and can now implement it into our game. To begin with, we'll need an instance variable of the renderer in the Game1 class: PrelightingRenderer renderer;

Next, we set the scene up as follows in the LoadContent() function, using our PPLight.fx effect and four point lights: models.Add(new CModel(Content.Load("teapot"), new Vector3(0, 60, 0), Vector3.Zero, new Vector3(60), GraphicsDevice)); models.Add(new CModel(Content.Load("ground"), Vector3.Zero, Vector3.Zero, Vector3.One, GraphicsDevice)); Effect effect = Content.Load("PPModel"); models[0].SetModelEffect(effect, true); models[1].SetModelEffect(effect, true); camera = new FreeCamera(new Vector3(0, 300, 1600), MathHelper.ToRadians(0), // Turned around 153 degrees MathHelper.ToRadians(5), // Pitched up 13 degrees GraphicsDevice); renderer = new PrelightingRenderer(GraphicsDevice, Content); renderer.Models = models; renderer.Camera = camera; renderer.Lights = new List() { new PPPointLight(new Vector3(-1000, 1000, 0), Color.Red * .85f, 2000), new PPPointLight(new Vector3(1000, 1000, 0), Color.Blue * .85f, 2000), new PPPointLight(new Vector3(0, 1000, 1000), Color.Green * .85f, 2000), new PPPointLight(new Vector3(0, 1000, -1000), Color.White * .85f, 2000) };

[ 93 ]

Advanced Lighting

Finally, we need to call the Draw() function of the renderer before drawing our models for the inal pass, making sure to clear the graphics card irst: protected override void Draw(GameTime gameTime) { renderer.Draw(); GraphicsDevice.Clear(Color.Black); foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); base.Draw(gameTime); }

Summary Having completed this chapter, you've learned how to implement point lights and spot lights in HLSL. You've also learned of the limitations of the programmable pipeline, as far as lighting is concerned, and learned two ways to draw multiple lights in your scenes relatively eficiently. There are many other lighting and shading techniques, and we'll look at many more of them in the rest of the book, starting with shadows and projection effects in the next chapter.

[ 94 ]

Projection and Shadowing Effects Our PrelightingRenderer is a great way to render a large number of lights in a relatively eficient manner and it also makes a great framework for implementing some other fairly complicated effects. In this chapter, we will look at two related effects—projective texturing and shadow mapping. We saw some aspects of projective texturing in the last chapter, but in this chapter we will learn how to project an image across the scene as though it was projected by a real world projector. Next, we will look at two implementations of shadow mapping. Shadow mapping is similar to projective texturing—in that we use the same process to project a depth texture across the scene, which we then use to generate shadows. However, the process is limited to hard-edged shadows, so we will look at a second way to perform shadow mapping that allows us to blur the depth texture and achieve soft shadows.

Projective texturing Projective texturing is a technique that "projects" a texture across the scene, as though it were an image being projected onto the objects in a room by a real-world projector. This is useful for simulating effects such as video projectors or non-circular spot lights, and the process (as we shall see later in the chapter) can be extended to support what is called shadow mapping—a way to add shadows to the objects in our scenes.

Projection and Shadowing Effects

In this example, our projector will be placed at one position and will point towards a target position. A texture will be projected toward the target position and appear on any objects that lie in its path:

The projective texturing process is fairly simple and, in fact, we have been doing it since the last chapter. In the last chapter, we looked up the normal, depth, and lighting values for various pixels in our pixel shaders by inding the screen space positions of the geometry being shaded at those pixels, and then sampling our various texture maps at those pixel coordinates. The only difference with projective texturing is that we use different view and projection matrices when calculating that screen space position—in this case, we calculate the view and projection matrices for a virtual "camera" at the projector's position pointing towards its target. We then take the screen space position of our vertices and look up the corresponding pixel colors in the texture we are projecting. Finally, we add those colors to the pixel shader's output. The irst thing we will need is a new Material class to store the projector settings: public class ProjectedTextureMaterial : Material { public Vector3 ProjectorPosition { get; set; } public Vector3 ProjectorTarget { get; set; } public Texture2D ProjectedTexture { get; set; } public bool ProjectorEnabled { get; set; } public float Scale { get; set; } float halfWidth, halfHeight; public ProjectedTextureMaterial(Texture2D Texture, [ 96 ]

Chapter 4 GraphicsDevice graphicsDevice) { ProjectorPosition = new Vector3(1500, 1500, 1500); ProjectorTarget = new Vector3(0, 150, 0); ProjectorEnabled = true; ProjectedTexture = Texture; // We determine how large the texture will be based on the // texture dimensions and a scaling value halfWidth = Texture.Width / 2.0f; halfHeight = Texture.Height / 2.0f; Scale = 1; } public override void SetEffectParameters(Effect effect) { if (effect.Parameters["ProjectorEnabled"] != null) effect.Parameters["ProjectorEnabled"].SetValue( ProjectorEnabled); if (!ProjectorEnabled) return; // Calculate an orthographic projection matrix for the // projector "camera" Matrix projection = Matrix.CreateOrthographicOffCenter( -halfWidth * Scale, halfWidth * Scale, -halfHeight * Scale, halfHeight * Scale, -100000, 100000); // Calculate view matrix as usual Matrix view = Matrix.CreateLookAt(ProjectorPosition, ProjectorTarget, Vector3.Up); if (effect.Parameters["ProjectorViewProjection"] != null) effect.Parameters["ProjectorViewProjection"].SetValue( view * projection); if (effect.Parameters["ProjectedTexture"] != null) effect.Parameters["ProjectedTexture"].SetValue( ProjectedTexture); } }

[ 97 ]

Projection and Shadowing Effects

Note that the projection matrix that this material generates is different from those that our cameras have been using—in that this matrix is orthographic. When a scene is viewed with an orthographic camera, objects do not appear to get smaller as their distance from the camera decreases. This is perfect for projective texturing, as the rays our projector casts will all be parallel:

Keep in mind that while we are using an orthographic projection matrix for this example; we could easily use a perspective projection matrix to achieve an effect more like a real-world projector (where moving the projector away from the screen would make the image larger because the rays it casts diverge from the center rather than remaining parallel). The next step is to create an effect for projective texturing. To allow us to use the lighting system that we are used to, we will build a new effect based on the PPModel.fx effect. Start by copying that effect, and then add the following effect parameters. The parameters represent the view and projection matrices that our virtual "projector" is using, and which texture we would like to project: float4x4 ProjectorViewProjection; texture2D ProjectedTexture; sampler2D projectorSampler = sampler_state { texture = ; }; bool ProjectorEnabled = false;

[ 98 ]

Chapter 4

Our vertex shader output struct will need one more ield to store the screen space position of our vertices according to the ProjectorViewProjection matrix: struct VertexShaderOutput { float4 Position : POSITION0; float2 UV : TEXCOORD0; float4 PositionCopy : TEXCOORD1; float4 ProjectorScreenPosition : TEXCOORD2; };

Then, our vertex shader will need to calculate this value: output.ProjectorScreenPosition = mul(mul(input.Position, World), ProjectorViewProjection);

The last step is to sample from the texture that we would like to project. However, we cannot simply use the UV coordinates calculated from the screen space position, as we may be shading vertices outside of the range of our projected texture (that is, the vertices the projected image is not meant to fall on to.) To solve this problem, we need to return only a texture sample if the point being sampled is visible to the projector, by limiting UV coordinates to [0, 1] and [0, 1]. Outside of that range we will simply use black, so that nothing is added to the output color: float3 sampleProjector(float2 UV) { if (UV.x < 0 || UV.x > 1 || UV.y < 0 || UV.y > 1) return float3(0, 0, 0); return tex2D(projectorSampler, UV); }

We can then use this new function to sample from the texture in the pixel shader: light += AmbientColor; float3 projection = float3(0, 0, 0); if (ProjectorEnabled) projection = sampleProjector(postProjToScreen( input.ProjectorScreenPosition) + halfPixel()); return float4(basicTexture * DiffuseColor * light + projection, 1);

[ 99 ]

Projection and Shadowing Effects

Finally, we can update our game to use the projective texturing effect and material: Effect effect = Content.Load("ProjectedTexture"); models[0].SetModelEffect(effect, true); models[1].SetModelEffect(effect, true); ProjectedTextureMaterial mat = new ProjectedTextureMaterial( Content.Load("projected texture"), GraphicsDevice); mat.ProjectorPosition = new Vector3(0, 4500, 4500); mat.ProjectorTarget = new Vector3(0, 0, 0); mat.Scale = 2; models[0].Material = mat; models[1].Material = mat;

Shadow mapping—drawing the depth map Now that we've seen how to perform projective texturing, we are ready to learn how to add shadows to our scene with shadow mapping. Shadow mapping is a process where we: 1. Render the scene to a depth texture. This is similar to the depth texture we rendered to perform prelighting, but this time we render the scene from the light's point of view, pretending that the light we are generating shadows for is a point-target type of camera. The depth texture will store the distance from the light of the closest vertices that are in view of the light's virtual camera. 2. Render the scene using the normal effects, but project the depth texture onto the scene from the light's point of view. This will allow us to extract the depth value in the depth texture for each vertex that we shade. [ 100 ]

Chapter 4

3. Calculate the actual distance between each vertex and the light. We can then compare the value stored in the depth texture and the actual distance. If the actual distance between a vertex and the light is greater than the distance stored in the depth texture, then there must have been another vertex between it and the light that overwrote the original vertex's depth value. Therefore, the vertex we are currently shading must be obscured from the light's point of view and therefore, is in shadow. 4. If a vertex is in shadow, we multiply its inal color value by some amount to darken the areas that are in shadow.

The irst step is to render the scene as a depth texture from the light's point of view. We did something similar in the previous chapter with our depth/normal buffer; however, that was rendered from the camera's point of view. The depth buffer we will render uses only the red component of the color output, in order to store depth with the full 32-bit accuracy afforded by disregarding the green, blue, and alpha channels. The distance from the camera is divided by a certain far-plane distance to get a number between 0 and 1, which can then be returned as the color value:

[ 101 ]

Projection and Shadowing Effects

Let's start by writing a shader that will do this for us. The shader simply calculates the screen space position of each vertex as usual, and calculates the depth (distance from the camera) of each vertex in the pixel shader. It then returns that value divided by the far plane and clamped to return a value between 0 and 1, where 0 would represent a vertex on the camera's near plane, and 1 would represent a vertex on the camera's far plane. float4x4 World; float4x4 View; float4x4 Projection; float FarPlane = 10000; struct VertexShaderInput { float4 Position : POSITION0; }; struct VertexShaderOutput { float4 Position : POSITION0; float4 ScreenPosition : TEXCOORD0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; // Calculate the screen space position float4x4 wvp = mul(World, mul(View, Projection)); float4 position = mul(input.Position, wvp); output.Position = position; output.ScreenPosition = position; return output; } float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Determine the depth of this vertex / by the far plane distance, // limited to [0, 1] float depth = clamp(input.ScreenPosition.z / FarPlane, 0, 1);

[ 102 ]

Chapter 4 // Return only the depth value return float4(depth, 0, 0, 1); } technique Technique1 { pass Pass1 { VertexShader = compile vs_1_1 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

We now need to update our PrelightingRenderer class to draw this depth map during its draw cycle. First, we will need a number of new instance variables and properties: // Position and target of the shadowing light public Vector3 ShadowLightPosition { get; set; } public Vector3 ShadowLightTarget { get; set; } // Shadow depth target and depth-texture effect RenderTarget2D shadowDepthTarg; Effect shadowDepthEffect; // Depth texture parameters int shadowMapSize = 2048; int shadowFarPlane = 10000; // Shadow light view and projection Matrix shadowView, shadowProjection; // Shadow properties public bool DoShadowMapping { get; set; } public float ShadowMult { get; set; }

The depth texture target and the depth texture effect need to be initialized in the constructor: shadowDepthTarg = new RenderTarget2D(GraphicsDevice, shadowMapSize, shadowMapSize, false, SurfaceFormat.Single, DepthFormat.Depth24); shadowDepthEffect = Content.Load("ShadowDepthEffect"); shadowDepthEffect.Parameters["FarPlane"].SetValue(shadowFarPlane);

[ 103 ]

Projection and Shadowing Effects

Next, we'll need a function to render the depth map: void drawShadowDepthMap() { // Calculate view and projection matrices for the "light" // shadows are being calculated for shadowView = Matrix.CreateLookAt(ShadowLightPosition, ShadowLightTarget, Vector3.Up); shadowProjection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45), 1, 1, shadowFarPlane); // Set render target graphicsDevice.SetRenderTarget(shadowDepthTarg); // Clear the render target to 1 (infinite depth) graphicsDevice.Clear(Color.White); // Draw each model with the ShadowDepthEffect effect foreach (CModel model in Models) { model.CacheEffects(); model.SetModelEffect(shadowDepthEffect, false); model.Draw(shadowView, shadowProjection, ShadowLightPosition); model.RestoreEffects(); } // Un-set the render targets graphicsDevice.SetRenderTarget(null); }

Finally, we must be sure to call this function from the Draw() function: public void Draw() { drawDepthNormalMap(); drawLightMap(); if (DoShadowMapping) drawShadowDepthMap(); prepareMainPass(); }

[ 104 ]

Chapter 4

Shadow mapping—projecting the depth texture onto the scene The shadow mapping technique depends on the fact that the pixel shader shades only the vertices that are closest to the camera. Therefore, only the objects closest to the "light" will be drawn into the depth buffer when the scene is drawn from the light's point of view. When we are drawing our objects from the camera's point of view, we can determine each vertex's actual distance from the light source, which we can then compare to the value stored in the depth buffer. If the value stored in the depth buffer is lesser than the vertex's actual distance from the light source, it must have an object between it and the camera and must therefore, be in shadow:

In order to get the value stored in the depth map for a given pixel, we will project the depth map onto the scene exactly the same way we did in the previous example. We will be adding the shadowing code to another extension of the PPModel.fx effect, so begin by copying that effect and adding the parameters we will need to project the texture onto the scene: bool DoShadowMapping = true; float4x4 ShadowView; float4x4 ShadowProjection; texture2D ShadowMap; sampler2D shadowSampler = sampler_state { texture = ; minfilter = point; magfilter = point; mipfilter = point; }; [ 105 ]

Projection and Shadowing Effects

Our VertexShaderOutput struct will need new values as well—the screen space position of the vertex as seen by the light: float4 ShadowScreenPosition : TEXCOORD2;

The vertex shader will then need to calculate this value: output.ShadowScreenPosition = mul(mul(input.Position, World), mul(ShadowView, ShadowProjection));

Now, we will need to sample from the depth texture. Once again, we must be careful to sample from the texture only when the UV coordinates are in the [0, 1] range, otherwise there will not be a smooth transition between those areas that the virtual light camera can see. float sampleShadowMap(float2 UV) { if (UV.x < 0 || UV.x > 1 || UV.y < 0 || UV.y > 1) return 1; return tex2D(shadowSampler, UV).r; }

Finally, we can use this function to sample from the depth texture in the pixel shader: float2 shadowTexCoord = postProjToScreen(input.ShadowScreenPosition) + halfPixel(); float mapDepth = sampleShadowMap(shadowTexCoord);

If we were to return this value alone, we would be projecting the depth texture onto the scene: return float4(mapDepth, mapDepth, mapDepth, 1);

[ 106 ]

Chapter 4

Before we actually use this depth value to generate shadows, we need to be sure that we are setting all of the effect parameters that we just added in the prepareMainPass() function of PrelightingRenderer: if (part.Effect.Parameters["DoShadowMapping"] != null) part.Effect.Parameters["DoShadowMapping"].SetValue(DoShadowMapping); if (!DoShadowMapping) continue; if (part.Effect.Parameters["ShadowMap"] != null) part.Effect.Parameters["ShadowMap"].SetValue(shadowDepthTarg); if (part.Effect.Parameters["ShadowView"] != null) part.Effect.Parameters["ShadowView"].SetValue(shadowView); if (part.Effect.Parameters["ShadowProjection"] != null) part.Effect.Parameters["ShadowProjection"]. SetValue(shadowProjection);

Shadow mapping—performing the depth comparison The inal step in shadow mapping is to compare the depth value stored in the depth texture to the actual distance between each vertex and the camera. We will need three more effect parameters to calculate and compare depth values and to perform the inal shading in the pixel shader: float3 ShadowLightPosition; float ShadowFarPlane; float ShadowMult = 0.3f; float ShadowBias = 1.0f / 50.0f;

[ 107 ]

Projection and Shadowing Effects

The last parameter is what is called the depth bias. Remember that we are comparing a relatively precise actual distance between each vertex and the light to a distance that has been stored in a texture. Because that texture is limited in size and precision, the depth value that we will get when sampling from it will be somewhat inaccurate. If we tried to compare that value to the actual distance we calculated directly, we would end up with very messy shadows on some surfaces, instead of a smoothly transitioning shadow:

In the case of the previous screenshot, the surface of the teapot is curved, which leads to banding where the depth texture had stored somewhat inaccurate depth values. Notice also that the loor is entirely in shadow in this image—another law caused by the depth texture's lack of precision. We solve this problem with the depth bias, by subtracting a small amount from the actual depth before comparing it to the value stored in the depth texture. A large enough bias will reduce these laws, but may also cause a lack of shadows on steep enough surfaces. Finding the right depth bias is more of an art than a science, and there are other techniques available (using a slope-scaled depth bias for example), but for simplicity's sake, we will use a constant bias. Increasing the size of the depth texture will also help to reduce these errors. We perform the depth comparison as follows—multiplying the output color by the shadow darkness, if we determine the area to be in shadow: float2 shadowTexCoord = postProjToScreen(input.ShadowScreenPosition) + halfPixel(); float mapDepth = sampleShadowMap(shadowTexCoord); [ 108 ]

Chapter 4 float realDepth = input.ShadowScreenPosition.z / ShadowFarPlane; float shadow = 1; if (realDepth < 1 && realDepth - ShadowBias > mapDepth) shadow = ShadowMult; return float4(basicTexture * DiffuseColor * light * shadow, 1);

We will also need to be sure to set our new Effect parameters in the PrelightingRenderer class: if (part.Effect.Parameters["ShadowLightPosition"] != null) part.Effect.Parameters["ShadowLightPosition"]. SetValue(ShadowLightPosition); if (part.Effect.Parameters["ShadowFarPlane"] != null) part.Effect.Parameters["ShadowFarPlane"].SetValue(shadowFarPlane); if (part.Effect.Parameters["ShadowMult"] != null) part.Effect.Parameters["ShadowMult"].SetValue(ShadowMult);

Finally, we set up the renderer to generate shadows in the Game1 class: renderer.ShadowLightPosition = new Vector3(1500, 1500, 2000); renderer.ShadowLightTarget = new Vector3(0, 150, 0); renderer.DoShadowMapping = true; renderer.ShadowMult = 0.3f;

[ 109 ]

Projection and Shadowing Effects

Variance shadow mapping—soft shadows Our current shadows look nice, but they do not behave like those found in the real world. While shadows often do have sharp edges, most of the time they do not. On overcast days, or when objects are hit with an indirect light source, shadows will often appear fuzzy and blurry. In the following sections, we will implement soft shadows with a technique called Variance Shadow Maps (VSM). The major beneit of variance shadow mapping is that we can ilter the depth texture like a regular texture—in this case, blurring it—without ruining the shadows that result.

This section will focus more on the implementation of VSM, but the original paper and presentation for the technique as well as another example implementation are available at http://www.punkuser.net/vsm. The irst difference between "regular" shadow mapping and VSM is that we store both the depth and the square of the depth in the depth texture. We later use these values to approximate shadows. First, we will need to update the depth texture rendering effect to return both of these values: return float4(depth, depth * depth, 0, 1);

Now that our depth texture effect is returning two values, we need to change the surface format that we are using. In the past, we've used SurfaceFormat.Single, which is a 32-bit format with all 32 bits allocated to the red channel. This allows us to store relatively precise depth values in the red channel of a render target—much more precise than SurfaceFormat.Color for example, which allocates only 8 bits to the red channel. [ 110 ]

Chapter 4

Because we are now storing two values, we will use SurfaceFormat.HalfVector2. This is also a 32-bit format, allocating 16 bits to the red channel and 16 bits to the green channel. This is less precise than what we have been using, but because we are blurring the shadow map the difference is not very noticeable. This will allow us to keep memory requirements down, especially given the number of render targets we have accumulated. shadowDepthTarg = new RenderTarget2D(GraphicsDevice, shadowMapSize, shadowMapSize, false, SurfaceFormat.HalfVector2, DepthFormat.Depth24);

Variance shadow mapping—blurring the depth texture The next step in the VSM process is to blur the depth texture that we just rendered, using what is called a Gaussian blur. Chapter 8 covers Gaussian blurs in much more detail, but for simplicity's sake, we will use pre-calculated settings for the blur. Blurring the depth texture will give us soft shadows. If we did not blur the depth texture at all, we would get shadows nearly identical to those we created earlier.

Blurring is simply the process of averaging a pixel and its neighbors for every pixel in the image—"smoothing" out the image as a whole. The Gaussian blur improves this somewhat by using speciic pixel offsets and weights but the process is the same. The Gaussian blur effect is as follows: // The texture to blur texture ScreenTexture; sampler2D tex = sampler_state { [ 111 ]

Projection and Shadowing Effects texture = minfilter magfilter mipfilter

; = point; = point; = point;

}; // Precalculated weights and offsets float weights[15] = { 0.1061154, 0.1028506, 0.1028506, 0.09364651, 0.09364651, 0.0801001, 0.0801001, 0.06436224, 0.06436224, 0.04858317, 0.04858317, 0.03445063, 0.03445063, 0.02294906, 0.02294906 }; float offsets[15] = { 0, 0.00125, -0.00125, 0.002916667, -0.002916667, 0.004583334, -0.004583334, 0.00625, -0.00625, 0.007916667, -0.007916667, 0.009583334, -0.009583334, 0.01125, -0.01125 }; // Blurs the input image horizontally float4 BlurHorizontal(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0 { float4 output = float4(0, 0, 0, 1); // Sample from the surrounding pixels using the precalculated // pixel offsets and color weights for (int i = 0; i < 15; i++) output += tex2D(tex, UV + float2(offsets[i], 0)) * weights[i]; return output; } // Blurs the input image vertically float4 BlurVertical(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0 { float4 output = float4(0, 0, 0, 1); for (int i = 0; i < 15; i++) output += tex2D(tex, UV + float2(0, offsets[i])) * weights[i]; return output; } technique Technique1 { [ 112 ]

Chapter 4 pass Horizontal { PixelShader = compile ps_2_0 BlurHorizontal(); } pass Vertical { PixelShader = compile ps_2_0 BlurVertical(); } }

Notice that this effect contains two techniques—one for blurring the image horizontally and one for blurring vertically. We blur each direction independently to create a smoother blur. Notice also that each pixel shader simply adds the contributions of 15 neighboring pixels and averages the result as discussed earlier. For the PrelightingRenderer class to perform the blur, we will need a few more instance variables—a SpriteBatch to draw the depth map into render targets, the Gaussian blur effect, and a render target where we will store the result of the horizontal blur. We blur vertically using the original depth buffer as the target, sampling the horizontally blurred scene from this secondary render target. SpriteBatch spriteBatch; RenderTarget2D shadowBlurTarg; Effect shadowBlurEffect;

These values need to be initialized in the constructor: spriteBatch = new SpriteBatch(GraphicsDevice); shadowBlurEffect = Content.Load("GaussianBlur"); shadowBlurTarg = new RenderTarget2D(GraphicsDevice, shadowMapSize, shadowMapSize, false, SurfaceFormat.Color, DepthFormat.Depth24);

Next, we'll create the function that performs the blur. Notice that we specify which render target to sample from and which render target to draw into. We also specify which technique we'd like to use with the dir parameter—0 for the horizontal blur technique and 1 for the vertical blur technique. void blurShadow(RenderTarget2D to, RenderTarget2D from, int dir) { // Set the target render target graphicsDevice.SetRenderTarget(to); graphicsDevice.Clear(Color.Black);

[ 113 ]

Projection and Shadowing Effects spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque); // Start the Gaussian blur effect shadowBlurEffect.CurrentTechnique.Passes[dir].Apply(); // Draw the contents of the source render target so they can // be blurred by the gaussian blur pixel shader spriteBatch.Draw(from, Vector2.Zero, Color.White); spriteBatch.End(); // Clean up after the sprite batch graphicsDevice.BlendState = BlendState.Opaque; graphicsDevice.DepthStencilState = DepthStencilState.Default; // Remove the render target graphicsDevice.SetRenderTarget(null); }

In the last change to the PrelightingRenderer class, we need to be sure to blur the shadow in the Draw() function. Notice that we irst copy from the depth target to the blur target, blurring horizontally along the way, then copy from the blur target back to the depth target, blurring vertically along the way. public void Draw() { drawDepthNormalMap(); drawLightMap(); if (DoShadowMapping) { drawShadowDepthMap(); blurShadow(shadowBlurTarg, shadowDepthTarg, 0); blurShadow(shadowDepthTarg, shadowBlurTarg, 1); } prepareMainPass(); }

[ 114 ]

Chapter 4

Variance shadow mapping—generating shadows The last step in variance shadow mapping is to generate the shadows themselves. We will do this in the same place where we calculated the shadows in the previous example. As we are now storing two values in the depth texture, we irst need to update our sampling function to return the red and green values: float2 sampleShadowMap(float2 UV) { if (UV.x < 0 || UV.x > 1 || UV.y < 0 || UV.y > 1) return float2(1, 1); return tex2D(shadowSampler, UV).rg; }

Finally, we can update the pixel shader to do the variance shadow mapping calculations. We sample from the depth texture as usual to get the depth it contains, and calculate the light distance as usual, offsetting it with a small bias. From there, we perform the shadow calculations as demonstrated in the VSM example code: float shadow = 1; if (DoShadowMapping) { float2 shadowTexCoord = postProjToScreen(input.ShadowScreenPosition) + halfPixel(); float realDepth = input.ShadowScreenPosition.z / ShadowFarPlane - ShadowBias; if (realDepth < 1) { // Variance shadow mapping code below from the variance shadow // mapping demo code @ http://www.punkuser.net/vsm/ // Sample from depth texture float2 moments = sampleShadowMap(shadowTexCoord); // Check if we're in shadow float lit_factor = (realDepth 2300 && y < 3200) treePositions.Add(new Vector3(x, y, z)); else i--; } trees = new BillboardSystem(GraphicsDevice, Content, Content.Load("tree_billboard"), new Vector2(200), treePositions.ToArray()); trees.Mode = BillboardSystem.BillboardMode.Cylindrical; trees.EnsureOcclusion = true;

Finally, we can draw the trees in the Draw() function: trees.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Up, ((FreeCamera)camera).Right);

[ 197 ]

Environmental Effects

We will now use a second method to add grass to the terrain. Here, we will use a texture as a "map" to dictate where to place the terrain. We could use the previous technique equally well, but the purpose here is more to demonstrate this technique. We will add a second billboard system to draw the grass billboards, and place them according to our map, where a brighter pixel means a higher chance of placing a grass billboard:

We'll need a second BillboardSystem: BillboardSystem grass;

We initialize the grass value as follows: // List of positions to place grass billboards List grassPositions = new List(); // Retrieve pixel grid from grass map Texture2D grassMap = Content.Load("grass_map"); Color[] grassPixels = new Color[grassMap.Width * grassMap.Height]; grassMap.GetData(grassPixels); // Loop until 1000 billboards have been placed for (int i = 0; i < 300; i++) { // Get X and Z coordinates from the random generator, between // [-(terrain width) / 2 * (cell size), (terrain width) / 2 * (cell size)] float x = r.Next(-256 * 30, 256 * 30); float z = r.Next(-256 * 30, 256 * 30); // Get corresponding coordinates in grass map

[ 198 ]

Chapter 7 int xCoord = (int)(x / 30) + 256; int zCoord = (int)(z / 30) + 256; // Get value between 0 and 1 from grass map float texVal = grassPixels[zCoord * 512 + xCoord].R / 255f; // Retrieve height float steepness; float y = terrain.GetHeightAtPosition(x, z, out steepness) + 50; // Randomly place a billboard here based on pixel color in grass // map if ((int)((float)r.NextDouble() * texVal * 10) == 1) grassPositions.Add(new Vector3(x, y, z)); else i--; } // Create grass billboard system grass = new BillboardSystem(GraphicsDevice, Content, Content.Load("grass_billboard"), new Vector2(100), grassPositions.ToArray()); grass.Mode = BillboardSystem.BillboardMode.Cylindrical; grass.EnsureOcclusion = false;

Again, we need to draw this billboard system in the Draw() function: grass.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Up, ((FreeCamera)camera).Right);

[ 199 ]

Environmental Effects

Adding the finishing touches Our scene is starting to look pretty good! Let's inish it off by irst adding a billboard system to render clouds, and inally bring in our sky and water effects. First, we will add a third billboard system for our clouds. BillboardSystem clouds;

Note that the following function creates more believable clouds than in the last chapter by "clumping" billboards together instead of spreading them evenly across the sky. List cloudPositions = new List(); // Create 20 "clusters" of clouds for (int i = 0; i < 20; i++) { Vector3 cloudLoc = new Vector3(r.Next(-8000, 8000), r.Next(4000, 6000), r.Next(-8000, 8000)); // Add 10 cloud billboards around each cluster point for (int j = 0; j < 10; j++) { cloudPositions.Add(cloudLoc + new Vector3(r.Next(-3000, 3000), r.Next(-300, 900), r.Next(-1500, 1500))); } } clouds = new BillboardSystem(GraphicsDevice, Content, Content.Load("cloud2"), new Vector2(2000), cloudPositions.ToArray()); clouds.Mode = BillboardSystem.BillboardMode.Spherical; clouds.EnsureOcclusion = false;

Once again, we'll need to draw the clouds in the Draw() function: clouds.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Up, ((FreeCamera)camera).Right);

[ 200 ]

Chapter 7

Finally, let's add in water and a sky: SkySphere sky; Water water;

We'll initialize them as follows: sky = new SkySphere(Content, GraphicsDevice, Content.Load("clouds")); water = new Water(Content, GraphicsDevice, new Vector3(0, 1600, 0), new Vector2(256 * 30)); water.Objects.Add(sky);

Now, we can update the Draw() function to draw everything: // Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); water.PreDraw(camera, gameTime); GraphicsDevice.Clear(Color.Black); sky.Draw(camera.View, camera.Projection, ((FreeCamera)camera). Position); foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); terrain.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); water.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); trees.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Up, ((FreeCamera)camera).Right); grass.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Up, ((FreeCamera)camera).Right); clouds.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Up, ((FreeCamera)camera).Right); base.Draw(gameTime); } [ 201 ]

Environmental Effects

There is one last thing we need to do—make the terrain IRenderable. We want the water to relect the sky and the terrain, so we need to ensure that the Water class knows how to draw the terrain. We'll start by updating the Terrain class to include the IRenderable interface: public class Terrain : IRenderable

We'll need to add a SetClipPlane function to this class as well: public void SetClipPlane(Vector4? Plane) { effect.Parameters["ClipPlaneEnabled"].SetValue(Plane.HasValue); if (Plane.HasValue) effect.Parameters["ClipPlane"].SetValue(Plane.Value); }

Now, all we need to do is update the TerrainEffect.fx ile to include clipping. First, we add the necessary effect parameters: float4 ClipPlane; bool ClipPlaneEnabled = false;

The VertexShaderOutput struct will now need to include the world space position: float3 WorldPosition : TEXCOORD3;

We'll set this value in the vertex shader: output.WorldPosition = input.Position;

Finally, we can perform the clipping at the beginning of the pixel shader: if (ClipPlaneEnabled) clip(dot(float4(input.WorldPosition, 1), ClipPlane));

[ 202 ]

Chapter 7

Finally, we can add the terrain to the list of items the water should relect (in the LoadContent() function of the Game1 class) and we're inished: water.Objects.Add(terrain);

Summary In this chapter, we learned a lot about environmental effects—terrain, clever placement of billboards, so-called "region growing" (placing billboards according to a texture)—and we also learned a lot about more advanced texturing techniques such as multitexturing and detail textures. We now have a very lexible terrain class, and a lovely environment to show it off in! In the next chapter, we will look at some post processing effects—blur, glow, and so on.

[ 203 ]

Advanced Materials and Post Processing In this chapter, we will cover two topics: First, we will extend our CModel class to allow us to apply materials to objects per mesh rather than assigning materials to objects as a whole. This is important because often there are times where we do not want an entire model to have the same appearance. For example, the windows of a car shouldn't have the same relectivity or transparency as the body of the car, or the tires or seats, but it would be a pain to try to work with each piece individually instead of as parts of a larger model, especially if we wanted to move the car, rotate it, and so on, and have each piece stay in position. The second topic we will cover is post processing. A post processing effect is one applied to the rendered scene as a whole rather than one applied to individual objects in the scene. Basically, the full rendered scene as it would be drawn onto the screen is passed as an image to an Effect consisting only of a pixel shader. Instead of processing geometry, that Effect is processing only the pixels in an image. Post processing effects can be as simple as converting an image to black and white, or can be more complex, like a blur. In fact, the lighting calculation stage of our prelighting renderer is basically a post processor because it operates on the normals and depths of the scene stored in an image. We will create a framework for creating post processing effects, including a black and white effect, a full screen blur, a depth of ield effect, and a glow effect.

Advanced Materials and Post Processing

Advanced Materials XNA represents models as a set of meshes, which in turn are sets of "mesh parts." Each mesh part can have its own effect, texture, and so on. Because each mesh part can have a different effect, we will set up our CModel class to allow each mesh part to have its own effect and material. This will allow us to use different materials on different parts of the model: for example, rubber-like wheels and a shiny body on a car. Our MeshTag class already allows us to set the effect used on each mesh part, so we'll just extend this and also have it keep track of a material: public Material Material = new Material();

Next, we'll add a function to the CModel class that will allow us to set a given effect to any given mesh part: public void SetMeshEffect(string MeshName, Effect effect, bool CopyEffect) { foreach (ModelMesh mesh in Model.Meshes) { if (mesh.Name != MeshName) continue; foreach (ModelMeshPart part in mesh.MeshParts) { Effect toSet = effect; // Copy the effect if necessary if (CopyEffect) toSet = effect.Clone(); MeshTag tag = ((MeshTag)part.Tag); // If this ModelMeshPart has if (tag.Texture != null) { setEffectParameter(toSet, setEffectParameter(toSet, } else setEffectParameter(toSet,

a texture, set it to the effect

"BasicTexture", tag.Texture); "TextureEnabled", true);

"TextureEnabled", false);

// Set our remaining parameters to the effect setEffectParameter(toSet, "DiffuseColor", tag.Color); setEffectParameter(toSet, "SpecularPower", tag.SpecularPower); part.Effect = toSet; } } } [ 206 ]

Chapter 8

Similarly, we'll add a function to set a material to a given mesh part: public void SetMeshMaterial(string MeshName, Material material) { foreach (ModelMesh mesh in Model.Meshes) { if (mesh.Name != MeshName) continue; foreach (ModelMeshPart meshPart in mesh.MeshParts) ((MeshTag)meshPart.Tag).Material = material; } }

We can also update the SetModelEffect() and SetModelMaterial() functions to use these functions: public void SetModelEffect(Effect effect, bool CopyEffect) { foreach (ModelMesh mesh in Model.Meshes) SetMeshEffect(mesh.Name, effect, CopyEffect); } public void SetModelMaterial(Material material) { foreach (ModelMesh mesh in Model.Meshes) SetMeshMaterial(mesh.Name, material); }

Finally, we can remove the effect-wide material and change the draw function to use the mesh part speciic materials: ((MeshTag)meshPart.Tag).Material.SetEffectParameters(effect);

We could now use multiple materials on a model by setting our scene up as follows: Effect lit = Content.Load("LightingEffect"); Effect normal = Content.Load("NormalMapEffect"); LightingMaterial marble = new LightingMaterial(); marble.SpecularColor = Color.White.ToVector3(); LightingMaterial steel = new LightingMaterial(); steel.SpecularColor = Color.Gray.ToVector3(); NormalMapMaterial brick = new NormalMapMaterial( Content.Load("brick_normal_map")); NormalMapMaterial wood = new NormalMapMaterial( Content.Load("wood_normal")); [ 207 ]

Advanced Materials and Post Processing CModel model = new CModel(Content.Load("multimesh"), Vector3.Zero, Vector3.Zero, Vector3.One, GraphicsDevice); model.SetMeshEffect("Box01", normal, true); model.SetMeshMaterial("Box01", wood); model.SetMeshEffect("Pyramid01", normal, true); model.SetMeshMaterial("Pyramid01", brick); model.SetMeshEffect("Sphere01", lit, true); model.SetMeshMaterial("Sphere01", marble); model.SetMeshEffect("Plane01", lit, true); model.SetMeshMaterial("Plane01", steel); models.Add(model);

Post processing A post processor takes in an image and does some form of processing on it, whether it be desaturation of the image (converting to black and white), or averaging pixels and their neighbors to create a blur. Generally, the image that is processed is the rendered scene, so effects such as motion blur, glow, HDR bloom, and so on can be applied. However, it is also common to use post processors in intermediate stages of the rendering process—blurring a shadow map, for example. Because we want to be able to implement many different kinds of post processing effects, we will create a framework that we can either use directly for simple post processing effects, or build on top of for more complex effects. public class PostProcessor { } [ 208 ]

Chapter 8

Any post processing effect (as was mentioned at the beginning of the chapter) is basically a shader with only a pixel shader component. Thus, we will need an Effect instance to use that pixel shader. We also need a Texture2D to process: // Pixel shader public Effect Effect { get; protected set; } // Texture to process public Texture2D Input { get; set; }

We will also need a few other miscellaneous things—a GraphicsDevice and a SpriteBatch: // GraphicsDevice and SpriteBatch for drawing protected GraphicsDevice graphicsDevice; protected static SpriteBatch spriteBatch;

The constructor will initialize these values: public PostProcessor(Effect Effect, GraphicsDevice graphicsDevice) { this.Effect = Effect; if (spriteBatch == null) spriteBatch = new SpriteBatch(graphicsDevice); this.graphicsDevice = graphicsDevice; }

The Draw() function draws the texture with the effect applied: // Draws the input texture using the pixel shader postprocessor public virtual void Draw() { // Set effect parameters if necessary if (Effect.Parameters["ScreenWidth"] != null) Effect.Parameters["ScreenWidth"]. SetValue(graphicsDevice.Viewport.Width); if (Effect.Parameters["ScreenHeight"] != null) Effect.Parameters["ScreenHeight"]. SetValue(graphicsDevice.Viewport.Height); // Initialize the spritebatch and effect spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque); Effect.CurrentTechnique.Passes[0].Apply(); [ 209 ]

Advanced Materials and Post Processing // Draw the input texture spriteBatch.Draw(Input, Vector2.Zero, Color.White); // End the spritebatch and effect spriteBatch.End(); // Clean up render states changed by the spritebatch graphicsDevice.DepthStencilState = DepthStencilState.Default; graphicsDevice.BlendState = BlendState.Opaque; }

Next, we will create a class that will easily capture what is being drawn—in this case, to use as input to the post processor: public class RenderCapture { RenderTarget2D renderTarget; GraphicsDevice graphicsDevice; public RenderCapture(GraphicsDevice GraphicsDevice) { this.graphicsDevice = GraphicsDevice; renderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24); } // Begins capturing from the graphics device public void Begin() { graphicsDevice.SetRenderTarget(renderTarget); } // Stop capturing public void End() { graphicsDevice.SetRenderTarget(null); } // Returns what was captured public Texture2D GetTexture() { return renderTarget; } } [ 210 ]

Chapter 8

We begin setting up the post processor by adding instance variables of the PostProcessor and RenderCapture classes in the Game1 class: RenderCapture renderCapture; PostProcessor postprocessor;

We initialize these two values in the LoadContent() function (we'll write the BWPostProcessor.fx ile in a moment): renderCapture = new RenderCapture(GraphicsDevice); postprocessor = new PostProcessor(Content.Load ("BWPostProcessor"), GraphicsDevice);

Finally, we will use the RenderCapture to capture the initial render of the scene, and use the PostProcessor to perform whatever effect it is performing: // Called when the game should draw itself protected override void Draw(GameTime gameTime) { // Capture the render renderCapture.Begin(); GraphicsDevice.Clear(Color.CornflowerBlue); // Draw all of the models foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); // End capturing renderCapture.End(); GraphicsDevice.Clear(Color.Black); // Perform postprocessing with the render of the scene postprocessor.Input = renderCapture.GetTexture(); postprocessor.Draw(); base.Draw(gameTime); }

[ 211 ]

Advanced Materials and Post Processing

Black and white post processor For our irst post processing effect, we'll create a simple post processor that will convert the following screenshot (or any image) to grayscale (black and white).

We start by creating a new Effect ile, with only a pixel shader: float4 PixelShaderFunction(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0 { } technique Technique1 { pass Pass1 { PixelShader = compile ps_1_1 PixelShaderFunction(); } }

We need a way to access the input texture(s). To make this easy on ourselves, we will use an array of texture samplers instead of specifying each explicitly. We will see later how we set values to this array. For this shader, the SpriteBatch will set it for us automatically. sampler2D tex[1]; // Input textures (just 1 in this case)

[ 212 ]

Chapter 8

All we need to do now is add up the weighted values of each color channel. We weigh them because our eyes naturally pick up more information from the green channel, followed by red and blue roughly proportioned according to the weight values that we will use. This will allow us to get the clearest image possible because more useful information is stored in the green and red channels. We then set each pixel to the summed value to get a monotone image (black and white): float4 PixelShaderFunction(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0 { float4 color = tex2D(tex[0], UV); float intensity = 0.3f * color.r + 0.59f * color.g + 0.11f * color.b; return float4(intensity, intensity, intensity, color.a); }

Our game is already set up to load and use this post processing effect, so we can simply run the game to see it at work:

[ 213 ]

Advanced Materials and Post Processing

Gaussian blur post processor The next post processing effect that we will look at is blurring, speciically with what is called a "Gaussian" blur. A Gaussian blur is a blur which, instead of simply calculating a weighted average of a pixel and its neighbors, calculates a weighted average, weighing pixels based on their distance from the center pixel. The weights are calculated according to the Gaussian (or "normal") function:

In normal use of this function, x is the distance from the mean of a data set, and σ is the standard deviation. In this case, x is a pixel's distance from the center pixel and σ is used as a parameter to determine how much the image is blurred. The following graph shows how much a pixel will be weighted as its distance approaches 1, 2, 3, and so on σ's from the center pixel:

The GaussianBlur post processor is an extension of the basic PostProcessor class: public class GaussianBlur : PostProcessor { }

[ 214 ]

Chapter 8

We will sample from the input texture 15 times and calculate a weighted average of the sampled values. Thus, in addition to the overall blur amount parameter, we will store weights and offsets for each sample: float blurAmount; float[] weightsH, weightsV; Vector2[] offsetsH, offsetsV;

We calculate the weights and offsets for the horizontal and vertical directions separately. Because a Gaussian blur can work in each direction independently, we perform the full blur in two passes—one blur horizontally and one blur vertically, giving us a stronger blur. The following function will calculate the weights and offsets for one pass: void calcSettings(float w, float h, out float[] weights, out Vector2[] offsets) { // 15 Samples weights = new float[15]; offsets = new Vector2[15]; // Calculate values for center pixel weights[0] = gaussianFn(0); offsets[0] = new Vector2(0, 0); float total = weights[0]; // Calculate samples in pairs for (int i = 0; i < 7; i++) { // Weight each pair of samples according to Gaussian function float weight = gaussianFn(i + 1); weights[i * 2 + 1] = weight; weights[i * 2 + 2] = weight; total += weight * 2; // Samples are offset by 1.5 pixels, to make use of // filtering halfway between pixels float offset = i * 2 + 1.5f; Vector2 offsetVec = new Vector2(w, h) * offset; offsets[i * 2 + 1] = offsetVec; offsets[i * 2 + 2] = -offsetVec; } // Divide all weights by total so they will add up to 1 for (int i = 0; i < weights.Length; i++) weights[i] /= total; } [ 215 ]

Advanced Materials and Post Processing

This function makes use of the gaussianFn() function, which is an implementation of the previous mathematical function: float gaussianFn(float x) { return (float)((1.0f / Math.Sqrt(2 * Math.PI * blurAmount * blurAmount)) * Math.Exp(-(x * x) / (2 * blurAmount * blurAmount))); }

The GaussianBlur class will need a RenderCapture of its own to capture the output of the irst blur pass, which it will then use as input to the second pass: RenderCapture capture;

This value, along with the weights and offsets for each pass, will be initialized in the constructor: public GaussianBlur(GraphicsDevice graphicsDevice, ContentManager Content, float BlurAmount) : base(Content.Load ("GaussianBlur"), graphicsDevice) { this.blurAmount = BlurAmount; // Calculate weights/offsets for horizontal pass calcSettings(1.0f / (float)graphicsDevice.Viewport.Width, 0, out weightsH, out offsetsH); // Calculate weights/offsets for vertical pass calcSettings(0, 1.0f / (float)graphicsDevice.Viewport.Height, out weightsV, out offsetsV); capture = new RenderCapture(graphicsDevice); }

Finally, we perform the two blur passes in the overridden Draw() function: public override void Draw() { // Set values for horizontal pass Effect.Parameters["Offsets"].SetValue(offsetsH); Effect.Parameters["Weights"].SetValue(weightsH); // Render this pass into the RenderCapture

[ 216 ]

Chapter 8 capture.Begin(); base.Draw(); capture.End(); // Get the results of the first pass Input = capture.GetTexture(); // Set values for the vertical pass Effect.Parameters["Offsets"].SetValue(offsetsV); Effect.Parameters["Weights"].SetValue(weightsV); // Render the final pass base.Draw(); }

The next step is to write the Gaussian blur effect ile. Again, we'll need a texture sampler array. Additionally, we will need effect parameters for our weight and offset values: sampler2D tex[1]; float2 Offsets[15]; float Weights[15];

The pixel shader performs 15 texture lookups based on the offsets from the main pixel being shaded, and weighs the results based on the weights calculated by the Gaussian function: float4 PixelShaderFunction(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0 { float4 output = float4(0, 0, 0, 1); for (int i = 0; i < 15; i++) output += tex2D(tex[0], UV + Offsets[i]) * Weights[i]; return output; }

The technique deinition simply deines the pixel shader: technique Technique1 { pass p0 { PixelShader = compile ps_2_0 PixelShaderFunction(); } } [ 217 ]

Advanced Materials and Post Processing

All we need to do to use this in our game is change our black and white PostProcessor class to a GaussianBlur: postprocessor = new GaussianBlur(GraphicsDevice, Content, 2);

Depth of field The next effect that we will look at builds on our Gaussian blur effect to recreate the "depth of ield" effect. In the real world, cameras have one speciic range they can focus clearly on, and everything outside of that range is blurred somewhat, getting increasingly blurry as the subjects move farther from the focal range. Generally, the effect isn't terribly noticeable, but in some cases it can be exaggerated intentionally for aesthetic effects. The process is simple: First, we render the scene storing only the depth values in a depth map (like we did in Chapter 3, Advanced Lighting). Then we render the scene normally, storing both the original render and a blurred version of it. Finally, we combine the blurred and non-blurred images in another post processor, which determines how much to blur the image based on the depths stored in the depth map at each pixel. The effect that we will need to render the depth map is very simple, and we have seen most if not all of it before: float4x4 World; float4x4 View; float4x4 Projection; [ 218 ]

Chapter 8 float MaxDepth = 20000; struct VertexShaderInput { float4 Position : POSITION0; }; struct VertexShaderOutput { float4 Position : POSITION0; float Depth : TEXCOORD0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; // Output position and depth output.Position = mul(input.Position, mul(World, mul(View, Projection))); output.Depth = output.Position.z; return output; } float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { // Return depth, scaled/clamped to [0, 1] return float4(input.Depth / MaxDepth, 0, 0, 1); } technique Technique1 { pass Pass1 { VertexShader = compile vs_2_0 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

[ 219 ]

Advanced Materials and Post Processing

For simplicity's sake, we will render the depth map directly in the Game1 class. We will need to load the effect that we have just created and a RenderCapture: RenderCapture depthCapture; Effect depthEffect;

These values will be initialized in the LoadContent() function: depthEffect = Content.Load("DepthEffect"); depthCapture = new RenderCapture(GraphicsDevice, SurfaceFormat. HalfSingle);

We will then use these to start off the Draw() function by rendering the depth map: // Start rendering to depth map depthCapture.Begin(); // Clear to white (max depth) GraphicsDevice.Clear(Color.White); foreach(CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) { model.CacheEffects(); // Cache effect model.SetModelEffect(depthEffect, false); // Set depth effect model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); model.RestoreEffects(); // Restore effects }

[ 220 ]

Chapter 8 // Finish rendering to depth map depthCapture.End(); In the next step, we will render the scene as usual, into our existing RenderCapture: // Begin rendering the main render renderCapture.Begin(); GraphicsDevice.Clear(Color.CornflowerBlue); // Draw all models foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((FreeCamera)camera). Position); // Finish the main render renderCapture.End();

The next step is to blur the rendered scene. However, our GaussianBlur class will output only its results directly to the screen. We could try wrapping its draw call with a RenderCapture, but this will not work as it uses a RenderCapture of its own internally to capture the results of its irst pass. What we need to do is capture the results of its second pass, so we will have to have the GaussianBlur class do this for us. Let's add a value to the GaussianBlur class that will allow us to specify where we want to send its output: public RenderCapture ResultCapture = null;

If this value is set to null when the Draw() function of GaussianBlur is called, it will simply output the results of its second pass directly to the screen. Otherwise, it will render into whichever RenderCapture is speciied: if (ResultCapture != null) ResultCapture.Begin(); // Set values for the vertical pass Effect.Parameters["Offsets"].SetValue(offsetsV); Effect.Parameters["Weights"].SetValue(weightsV); // Render the final pass base.Draw(); if (ResultCapture != null) ResultCapture.End(); [ 221 ]

Advanced Materials and Post Processing

To capture the results of the blur, the Game1 class will need another RenderCapture: RenderCapture blurCapture;

This RenderCapture will need to be initialized in the LoadContent() function: blurCapture = new RenderCapture(GraphicsDevice, SurfaceFormat.Color);

Now, we can continue the Draw() function by blurring the main render: // Prepare to blur results of main render postprocessor.Input = renderCapture.GetTexture(); // Output blur to our RenderCapture ((GaussianBlur)postprocessor).ResultCapture = blurCapture; // Perform blur postprocessor.Draw();

Now that we've rendered all of these various views of the scene, we need to combine them into the inal result. We will do this with another PostProcessor called DepthOfField, whose main role is to handle setting all three textures to the GraphicsDevice and loading the Effect that combines them: public class DepthOfField : PostProcessor { // Depth map and un-blurred render of scene. The blurred render // will be set as the Input value public Texture2D DepthMap; public Texture2D Unblurred; public DepthOfField(GraphicsDevice graphicsDevice, ContentManager Content): base(Content.Load ("DepthOfField"), graphicsDevice) { } public override void Draw() { // Set the two textures above to the second and third // texture slots graphicsDevice.Textures[1] = Unblurred; graphicsDevice.Textures[2] = DepthMap; // Set the samplers for all three textures to PointClamp // so we can sample pixel values directly graphicsDevice.SamplerStates[0] = SamplerState.PointClamp; graphicsDevice.SamplerStates[1] = SamplerState.PointClamp; graphicsDevice.SamplerStates[2] = SamplerState.PointClamp; base.Draw(); } } [ 222 ]

Chapter 8

Finally, the effect ile simply samples the depth from the depth map and converts it back to its full range. It then samples the blurred and non-blurred copies of the scene, and blends between them using essentially the same calculations that we used to draw fog in the earlier chapters: sampler2D tex[3]; float MaxDepth = 20000; // Distance at which blur starts float BlurStart = 600; // Distance at which scene is fully blurred float BlurEnd = 1000; float4 PixelShaderFunction(float4 Position : POSITION0, float2 UV : TEXCOORD0) : COLOR0 { // Determine depth float depth = tex2D(tex[2], UV).r * MaxDepth; // Get blurred and unblurred render of scene float4 unblurred = tex2D(tex[1], UV); float4 blurred = tex2D(tex[0], UV); // Determine blur amount (similar to fog calculation) float blurAmt = clamp((depth - BlurStart) / (BlurEnd - BlurStart), 0, 1); // Blend between unblurred and blurred images float4 mix = lerp(unblurred, blurred, blurAmt); return mix; } technique Technique1 { pass p0 { PixelShader = compile ps_2_0 PixelShaderFunction(); } }

[ 223 ]

Advanced Materials and Post Processing

All we need to do now is create an instance of this class in the Game1 class: DepthOfField dof;

We will set it up in the LoadContent() function: dof = new DepthOfField(GraphicsDevice, Content);

Finally, we can inish off the Draw() function by using the DepthOfField class to combine all of the images into the inal result. We must also be sure to call base. Draw() when we are done: // Set the three images to the DOF class dof.DepthMap = depthCapture.GetTexture(); dof.Unblurred = renderCapture.GetTexture(); dof.Input = ((GaussianBlur)postprocessor).ResultCapture.GetTexture(); // Combine the images into the final result dof.Draw(); base.Draw(gameTime);

This inishes up the depth of the ield effect. Notice when running the game that objects that are close to the camera stay entirely in focus, and fade to a blur between the FogStart and FogEnd ranges. In a "real" implementation of the depth of ield, these values would change while the camera was moved to focus on different objects. For example, depth of ield is often used in game cinematics, focusing on a character who is talking and blurring the scene behind them. Depth of ield can also be useful for a weapon scope effect, focusing on the object the player is currently targeting, for example. In that case, we would simply cast a ray into the scene to determine the depth at which we want to start the blur.

[ 224 ]

Chapter 8

Glow post processor The inal effect that we will look at is glow. Glow is generally implemented by rendering the parts of the scene that are meant to glow into a render target, then blurring that render target. The blurred image is then drawn additively over the original render of the scene to make it look like parts of the scene are in fact emitting light. The following screenshot shows the non-blurred glow channel on the left, which transitions to the blurred version on the right:

We will need a few things to implement glow in our game: RenderCapture for the regular render of the scene and the glow render, an effect to used to render the glow, and a GaussianBlur to blur the glowing scene. RenderCapture renderCapture; RenderCapture glowCapture; Effect glowEffect; GaussianBlur blur; We set these values up as follows in the LoadContent() function: renderCapture = new RenderCapture(GraphicsDevice); glowCapture = new RenderCapture(GraphicsDevice); glowEffect = Content.Load("GlowEffect"); glowEffect.Parameters["GlowTexture"].SetValue( Content.Load("glow_map")); blur = new GaussianBlur(GraphicsDevice, Content, 4);

[ 225 ]

Advanced Materials and Post Processing

The contents of the GlowEffect.fx effect are very simple, no more complicated than drawing a texture onto a model—the same way we did back in Chapter 2, Introduction to HLSL: float4x4 World; float4x4 View; float4x4 Projection; texture GlowTexture; sampler2D GlowSampler = sampler_state { texture = ; }; struct VertexShaderInput { float4 Position : POSITION0; float2 UV : TEXCOORD0; }; struct VertexShaderOutput { float4 Position : POSITION0; float2 UV : TEXCOORD0; }; VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; output.Position = mul(input.Position, mul(World, mul(View, Projection))); output.UV = input.UV; return output; } float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 { return tex2D(GlowSampler, input.UV); } technique Technique1 { [ 226 ]

Chapter 8 pass Pass1 { VertexShader = compile vs_2_0 VertexShaderFunction(); PixelShader = compile ps_2_0 PixelShaderFunction(); } }

Now all we need to do is update the Draw() function. We begin drawing by rendering the glowing scene to the corresponding RenderCapture: // Begin capturing the glow render glowCapture.Begin(); GraphicsDevice.Clear(Color.Black); // Draw all models with the glow effect/texture applied, reverting // the effect when finished foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) { model.CacheEffects(); model.SetModelEffect(glowEffect, false); model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); model.RestoreEffects(); } // Finish capturing the glow glowCapture.End();

Next, we draw the scene as usual into the second RenderCapture: // Draw the scene regularly into the other RenderCapture renderCapture.Begin(); GraphicsDevice.Clear(Color.Black); // Draw all models foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position); // Finish capturing renderCapture.End();

[ 227 ]

Advanced Materials and Post Processing

Next, we blur the glow render, storing the result into the same RenderCapture: // Blur the glow render back into the glow RenderCapture blur.Input = glowCapture.GetTexture(); blur.ResultCapture = glowCapture; blur.Draw();

Finally, we simply draw the two renderers of the scene on top of each other with the SpriteBatch, making sure to blend additively so that the glowing areas of the scene will brighten the image: // Draw the blurred glow render over the normal render additively spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive); spriteBatch.Draw(renderCapture.GetTexture(), Vector2.Zero, Color. White); spriteBatch.Draw(glowCapture.GetTexture(), Vector2.Zero, Color.White); spriteBatch.End(); // Clean up after the SpriteBatch GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; base.Draw(gameTime);

[ 228 ]

Chapter 8

Summary Having inished this chapter, we have extended the CModel class to allow us to set materials and effects to individual meshes rather than limiting us to setting these only to models as a whole. We have also created a lexible framework for implementing various post processing effects, and have implemented a number of post processing effects of our own, including a black and white ilter, a full screen blur, depth of ield, and glow. In the next and the inal chapter of the book we will look at various topics in animation, including animation of objects as a whole (moving a vehicle around a track, for example), animation of speciic meshes of a model (spinning the blades on a windmill, for example), and what is called "skinned animation"—animation of the bones in a model that causes the model itself to deform. This is useful for character animation, where the "skin" of the model will bend, stretch, and lex along with the model's bones.

[ 229 ]

Animation In this chapter, we will look at several ways to make the objects in our scene move. First, we will look at the animation of objects as a whole. We will do this through simple linear interpolation between start and end values, and through a more complex curve interpolation. We will also look at more complex animations through keyframed animation. We will look at hierarchical animation to animate speciic pieces of a model, and inally, we will look at skinned animation—the animation of the vertices of a model themselves according to the model's skeletal structure.

Object animation We will irst look at the animation of objects as a whole. The most common ways to animate an object are rotation and translation (movement). We will begin by creating a class that will interpolate a position and rotation value between two extremes over a given amount of time. We could also have it interpolate between two scaling values, but it is very uncommon for an object to change size in a smooth manner during gameplay, so we will leave it out for simplicity's sake. The ObjectAnimation class has a number of parameters—starting and ending position and rotation values, a duration to interpolate during those values, and a Boolean indicating whether or not the animation should loop or just remain at the end value after the duration has passed: public class ObjectAnimation { Vector3 startPosition, endPosition, startRotation, endRotation; TimeSpan duration; bool loop; }

Animation

We will also store the amount of time that has elapsed since the animation began, and the current position and rotation values: TimeSpan elapsedTime = TimeSpan.FromSeconds(0); public Vector3 Position { get; private set; } public Vector3 Rotation { get; private set; }

The constructor will initialize these values: public ObjectAnimation(Vector3 StartPosition, Vector3 EndPosition, Vector3 StartRotation, Vector3 EndRotation, TimeSpan Duration, bool Loop) { this.startPosition = StartPosition; this.endPosition = EndPosition; this.startRotation = StartRotation; this.endRotation = EndRotation; this.duration = Duration; this.loop = Loop; Position = startPosition; Rotation = startRotation; }

Finally, the Update() function takes the amount of time that has elapsed since the last update and updates the position and rotation values accordingly: public void Update(TimeSpan Elapsed) { // Update the time this.elapsedTime += Elapsed; // Determine how far along the duration value we are (0 to 1) float amt = (float)elapsedTime.TotalSeconds / (float)duration. TotalSeconds; if (loop) while (amt > 1) // Wrap the time if we are looping amt -= 1; else // Clamp to the end value if we are not amt = MathHelper.Clamp(amt, 0, 1); // Update the current position and rotation Position = Vector3.Lerp(startPosition, endPosition, amt); Rotation = Vector3.Lerp(startRotation, endRotation, amt); } [ 232 ]

Chapter 9

As a simple example, we'll create an animation (in the Game1 class) that rotates our spaceship in a circle over a few seconds:

We'll also have it move the model up and down for demonstration's sake: ObjectAnimation anim;

We initialize it in the constructor: models.Add(new CModel(Content.Load("ship"), Vector3.Zero, Vector3.Zero, new Vector3(0.25f), GraphicsDevice)); anim = new ObjectAnimation(new Vector3(0, -150, 0), new Vector3(0, 150, 0), Vector3.Zero, new Vector3(0, -MathHelper.TwoPi, 0), TimeSpan.FromSeconds(10), true);

We update it as follows: anim.Update(gameTime.ElapsedGameTime); models[0].Position = anim.Position; models[0].Rotation = anim.Rotation;

Keyframed animation Our ObjectAnimation class allows us to create simple linear animations, but we can't create anything more complex. For example, we can't make our spaceship move in a circle with this class. To achieve more complex animations, we will use what is called keyframed animation. In this method, we specify "key" frames where we want the object to be in a speciic position and orientation. We then rely on the code to interpolate between those values to ill in the frames between the key frames.

[ 233 ]

Animation

The following screenshot shows our spaceship at the keyframed positions along a path, and the black line shows the path that would be taken by interpolating between keyframes:

Keyframed animation is useful because it is a fast way to create somewhat complex animations without having to animate each frame. For example, birds lying through the air, soldiers on patrol, or even a camera lying through a scene, can all be animated through keyframes. This is probably the easiest way to move the camera during a cutscene, for example. We represent a key frame with the ObjectAnimationFrame class. Like the previous class, it contains position and rotation values. It also, however, contains a time value, marking this frame's time offset from the beginning of the animation. public class ObjectAnimationFrame { public Vector3 Position { get; private set; } public Vector3 Rotation { get; private set; } public TimeSpan Time { get; private set; } public ObjectAnimationFrame(Vector3 Position, Vector3 Rotation, TimeSpan Time) { this.Position = Position; this.Rotation = Rotation; this.Time = Time; } }

[ 234 ]

Chapter 9

We can now create a new animation class that uses key frames: public class KeyframedObjectAnimation { List frames = new List(); bool loop; TimeSpan elapsedTime = TimeSpan.FromSeconds(0); public Vector3 Position { get; private set; } public Vector3 Rotation { get; private set; } public KeyframedObjectAnimation(List Frames, bool Loop) { this.frames = Frames; this.loop = Loop; Position = Frames[0].Position; Rotation = Frames[0].Rotation; } }

Finally, the Update() function igures out which frame we are on and interpolates between its values and the next frame's values, based on how far between them we are: public void Update(TimeSpan Elapsed) { // Update the time this.elapsedTime += Elapsed; TimeSpan totalTime = elapsedTime; TimeSpan end = frames[frames.Count - 1].Time; if (loop) // Loop around the total time if necessary while (totalTime > end) totalTime -= end; else // Otherwise, clamp to the end values { Position = frames[frames.Count - 1].Position; Rotation = frames[frames.Count - 1].Rotation; return; } int i = 0;

[ 235 ]

Animation // Find the index of the current frame while(frames[i + 1].Time < totalTime) i++; // Find the time since the beginning of this frame totalTime -= frames[i].Time; // Find how far we are between the current and next frame (0 to 1) float amt = (float)((totalTime.TotalSeconds) / (frames[i + 1].Time - frames[i].Time).TotalSeconds); // Interpolate position and rotation values between frames Position = Vector3.Lerp(frames[i].Position, frames[i + 1].Position, amt); Rotation = Vector3.Lerp(frames[i].Rotation, frames[i + 1].Rotation, amt); }

For example, we can now create a new animation to move our spaceship in a square: KeyframedObjectAnimation anim;

We set it up as follows: List frames = new List(); frames.Add(new ObjectAnimationFrame(new Vector3(-1000, 100, -1000), new Vector3(0, MathHelper.ToRadians(-90), 0), TimeSpan.FromSeconds(0))); frames.Add(new ObjectAnimationFrame(new Vector3(1000, 100, -1000), new Vector3(0, MathHelper.ToRadians(-90), 0), TimeSpan.FromSeconds(3))); frames.Add(new ObjectAnimationFrame(new Vector3(1000, 100, -1000), new Vector3(0, MathHelper.ToRadians(-180), 0), TimeSpan.FromSeconds(6))); frames.Add(new ObjectAnimationFrame(new Vector3(1000, 100, 1000), new Vector3(0, MathHelper.ToRadians(-180), 0), TimeSpan.FromSeconds(9))); frames.Add(new ObjectAnimationFrame(new Vector3(1000, 100, 1000), new Vector3(0, MathHelper.ToRadians(-270), 0), TimeSpan.FromSeconds(12))); frames.Add(new ObjectAnimationFrame(new Vector3(-1000, 100, 1000), new Vector3(0, MathHelper.ToRadians(-270), 0), TimeSpan.FromSeconds(15))); frames.Add(new ObjectAnimationFrame(new Vector3(-1000, 100, 1000), new Vector3(0, MathHelper.ToRadians(-360), 0), TimeSpan.FromSeconds(18))); [ 236 ]

Chapter 9 frames.Add(new ObjectAnimationFrame(new Vector3(-1000, 100, -1000), new Vector3(0, MathHelper.ToRadians(-360), 0), TimeSpan.FromSeconds(21))); frames.Add(new ObjectAnimationFrame(new Vector3(-1000, 100, -1000), new Vector3(0, MathHelper.ToRadians(-450), 0), TimeSpan.FromSeconds(24))); anim = new KeyframedObjectAnimation(frames, true);

The Update code remains the same. Running the game, you will see the spaceship move from corner to corner of a box, turning towards the next corner at each stop.

Curve interpolation We now have the ability to make animations with multiple key frames, which allows us to create more complex animations. However, we are still interpolating linearly between those key frames. This looks good for rotations, for example, but it would not look good for an object following a path, as the object would abruptly change direction after reaching a key frame in its animation. Instead, we want to be able to have our objects follow a smooth curve through the positions deined in the key frames. We will do this with what is called Catmull-Rom interpolation. This is a process that will create a curve through our key frame positions, allowing for much smoother object animation:

[ 237 ]

Animation

Let's modify the KeyframedObjectAnimation class to use Catmull-Rom interpolation for the position value. XNA has a built-in function to calculate an interpolated position between the second and third points in a set of four points using Catmull-rom interpolation. However, it works only in one dimension, so we'll need to create a function that will interpolate between a set of instances of Vector3: Vector3 catmullRom3D(Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4, float amt) { return new Vector3( MathHelper.CatmullRom(v1.X, v2.X, v3.X, v4.X, amt), MathHelper.CatmullRom(v1.Y, v2.Y, v3.Y, v4.Y, amt), MathHelper.CatmullRom(v1.Z, v2.Z, v3.Z, v4.Z, amt)); }

The amt argument speciies how far (0 to 1) between the second and third vectors the new position should be. We can now modify the position calculation to use this new function: // Interpolate position and rotation values between frames Position = catmullRom3D(frames[wrap(i - 1, frames.Count - 1)]. Position, frames[wrap(i, frames.Count - 1)].Position, frames[wrap(i + 1, frames.Count - 1)].Position, frames[wrap(i + 2, frames.Count - 1)].Position, amt);

The wrap() function wraps the value that it is given around a certain interval—in this case [0, frames.Count – 1]. This means that we will not have to worry about our indices going out of range when inding the last point, next point, and so on, but it does mean that this type of interpolation will work best with a closed curve—a circle, for example: // Wraps the "value" argument around [0, max] int wrap(int value, int max) { while (value > max) value -= max; while (value < 0) value += max; return value; }

[ 238 ]

Chapter 9

We could now create the following keyframed animation with a curved path to demonstrate our new interpolation method: List frames = new List(); frames.Add(new ObjectAnimationFrame(new Vector3(-500, 100, 1000), new Vector3(0, MathHelper.ToRadians(0), 0), TimeSpan.FromSeconds(0))); frames.Add(new ObjectAnimationFrame(new Vector3(500, 100, 500), new Vector3(0, MathHelper.ToRadians(0), 0), TimeSpan.FromSeconds(3))); frames.Add(new ObjectAnimationFrame(new Vector3(-500, 100, 0), new Vector3(0, MathHelper.ToRadians(0), 0), TimeSpan.FromSeconds(6))); frames.Add(new ObjectAnimationFrame(new Vector3(500, 100, -500), new Vector3(0, MathHelper.ToRadians(0), 0), TimeSpan.FromSeconds(9))); frames.Add(new ObjectAnimationFrame(new Vector3(-500, 100, -1000), new Vector3(0, MathHelper.ToRadians(180), 0), TimeSpan.FromSeconds(12))); frames.Add(new ObjectAnimationFrame(new Vector3(-500, 100, 1000), new Vector3(0, MathHelper.ToRadians(180), 0), TimeSpan.FromSeconds(15))); frames.Add(new ObjectAnimationFrame(new Vector3(-500, 100, 1000), new Vector3(0, MathHelper.ToRadians(360), 0), TimeSpan.FromSeconds(18))); anim = new KeyframedObjectAnimation(frames, true);

[ 239 ]

Animation

Building a Race Track from a Curve Our method of Catmull-Rom curve interpolation works well for moving objects along a smooth path, but can also be used for many other things. In this example, we will build a race track from a curve and have a car follow that path at the player's command. The RaceTrack class will take care of creating and rendering the track: public class RaceTrack { // List of control points List positions; // Vertex and index buffers VertexBuffer vb; IndexBuffer ib; int nVertices, nIndices; // Rendering variables GraphicsDevice graphicsDevice; BasicEffect effect; Texture2D texture; // Total length of the track float trackLength; }

Note that we store the positions as a list of instances of Vector2, as the track is lat on the ground and requires no Y position component. The constructor will direct the initialization of the track: public RaceTrack(List positions, int nDivisions, float trackWidth, int textureRepetitions, GraphicsDevice graphicsDevice, ContentManager content) { this.graphicsDevice = graphicsDevice; this.positions = interpolatePositions(positions, nDivisions); effect = new BasicEffect(graphicsDevice); texture = content.Load("track"); createBuffers(trackWidth, textureRepetitions); }

[ 240 ]

Chapter 9

The constructor irst calls the interpolatePositions() function, which adds a given number of points to the list of control points based on Catmull-Rom interpolation. We've already seen most of this in the last section—we are simply using a similar Catmull-Rom function to insert extra positions into a list: // Adds the given number of positions between the control points specified, // to subdivide/smooth the path List interpolatePositions(List positions, int nDivisions) { // Create a new list of positions List newPositions = new List(); // Between each control point... for (int i = 0; i < positions.Count - 1; i++) { // Add the control point to the new list newPositions.Add(positions[i]); // Add the specified number of interpolated points for (int j = 0; j < nDivisions; j++) { // Determine how far to interpolate float amt = (float)(j + 1) / (float)(nDivisions + 2); // Find the position based on catmull-rom interpolation Vector2 interp = catmullRomV2( positions[wrapIndex(i - 1, positions.Count - 1)], positions[i], positions[wrapIndex(i + 1, positions.Count - 1)], positions[wrapIndex(i + 2, positions.Count - 1)], amt); // Add the new position to the new list newPositions.Add(interp); } } return newPositions; } // Wraps a number around 0 and the "max" value int wrapIndex(int value, int max) { while (value > max)

[ 241 ]

Animation value -= max; while (value < 0) value += max; return value; } // Performs a Catmull-Rom interpolation for each component of a Vector2 based // on the given control points and interpolation distance Vector2 catmullRomV2(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4, float amount) { return new Vector2(MathHelper.CatmullRom(v1.X, v2.X, v3.X, v4.X, amount), MathHelper.CatmullRom(v1.Y, v2.Y, v3.Y, v4.Y, amount)); }

Having added a number of additional points to the list of track points, we now need to create the vertices and indices to trace a track through them. We add a vertex to the left and right of each track point and create two triangles to ill the space between each track section. We ind the side vector by taking the cross product of the forward vector (the vector between the next and the current position) and the up vector (0, 1, 0).

We create the vertices as follows. For the sake of brevity, I won't step through the code in pieces, as it is well commented: VertexPositionNormalTexture[] createVertices(float trackWidth, int textureRepetitions) { // Create 2 vertices for each track point nVertices = positions.Count * 2; [ 242 ]

Chapter 9 VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[nVertices]; int j = 0; trackLength = 0; for (int i = 0; i < positions.Count; i++) { // Find the index of the next position int next = wrapIndex(i + 1, positions.Count - 1); // Find the current and next positions on the path Vector3 position = new Vector3(positions[i].X, 0, positions[i].Y); Vector3 nextPosition = new Vector3(positions[next].X, 0, positions[next].Y); // Find the vector between the current and next position Vector3 forward = nextPosition - position; float length = forward.Length(); forward.Normalize(); // Find the side vector based on the forward and up vectors Vector3 side = -Vector3.Cross(forward, Vector3.Up) * trackWidth; // Create a vertex to the left and right of the current position vertices[j++] = new VertexPositionNormalTexture(position - side, Vector3.Up, new Vector2(0, trackLength)); vertices[j++] = new VertexPositionNormalTexture(position + side, Vector3.Up, new Vector2(1, trackLength)); trackLength += length; } // Attach the end vertices to the beginning to close the loop vertices[vertices.Length - 1].Position = vertices[1].Position; vertices[vertices.Length - 2].Position = vertices[0].Position; // For each vertex... for (int i = 0; i < vertices.Length; i++) { // Bring the UV's Y coordinate back to the [0, 1] range vertices[i].TextureCoordinate.Y /= trackLength;

[ 243 ]

Animation // Tile the texture along the track vertices[i].TextureCoordinate.Y *= textureRepetitions; } return vertices; }

Next, we need to create the indices. All we need to do is create two triangles between each track position: int[] createIndices() { // Create indices nIndices = (positions.Count - 1) * 6; int[] indices = new int[nIndices]; int j = 0; // Create two triangles between every position for (int i = 0; i < positions.Count - 1; i++) { int i0 = i * 2; indices[j++] indices[j++] indices[j++] indices[j++] indices[j++] indices[j++]

= = = = = =

i0; i0 + i0 + i0 + i0 + i0 +

1; 2; 2; 1; 3;

} return indices; }

The createBuffers() function ties the vertex and index creation together: void createBuffers(float trackWidth, int textureRepetitions) { VertexPositionNormalTexture[] vertices = createVertices(trackWidth, textureRepetitions); // Create vertex buffer and set data vb = new VertexBuffer(graphicsDevice, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly);

[ 244 ]

Chapter 9 vb.SetData(vertices); int[] indices = createIndices(); // Create index buffer and set data ib = new IndexBuffer(graphicsDevice, IndexElementSize.ThirtyTwoBits, indices.Length, BufferUsage.WriteOnly); ib.SetData(indices); }

Drawing the track is very simple: public void Draw(Matrix View, Matrix Projection) { // Set effect parameters effect.World = Matrix.Identity; effect.View = View; effect.Projection = Projection; effect.Texture = texture; effect.TextureEnabled = true; // Set the vertex and index buffers to the graphics device graphicsDevice.SetVertexBuffer(vb); graphicsDevice.Indices = ib; // Apply the effect effect.CurrentTechnique.Passes[0].Apply(); // Draw the list of triangles graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, nVertices, 0, nIndices / 3); }

Let's now create a track in our game: RaceTrack track;

We initialize it as follows in the LoadContent() function: List trackPositions = new List() { new Vector2(-4000, 0), new Vector2(-4000, -4000), new Vector2(0, -4000), new Vector2(4000, -4000),

[ 245 ]

Animation new new new new new new new new new

Vector2(4000, -2000), Vector2(0, -2000), Vector2(-1000, 0), Vector2(0, 2000), Vector2(4000, 2000), Vector2(4000, 4000), Vector2(0, 4000), Vector2(-4000, 4000), Vector2(-4000, 0)

}; track = new RaceTrack(trackPositions, 25, 300, 30, GraphicsDevice, Content);

Finally, we draw the terrain as follows: track.Draw(camera.View, camera.Projection);

Moving a car along the track Now that we can create and render our race track, we are ready to add a car to our little racing game. First, we will need a way to determine where on the track the car is, based on a distance value. This function will be able to handle the car looping around the track, and will be able to interpolate between track positions:

[ 246 ]

Chapter 9 // Returns the position on the track the given distance from the start, // and the forward direction at that point public Vector2 TracePath(float distance, out Vector2 direction) { // Remove extra laps while (distance > trackLength) distance -= trackLength; int i = 0; while (true) { // Find the index of the next and last position int last = wrapIndex(i - 1, positions.Count - 1); int next = wrapIndex(i + 1, positions.Count - 1); // Find the distance between this position and the next direction = positions[next] - positions[i]; float length = direction.Length(); // // // // if {

If the length remaining is greater than the distance to the next position, keep looping. Otherwise, the final position is somewhere between the current and next position in the list (length < distance) distance -= length; i++; continue;

} // Find the direction from the last position to the current position Vector2 lastDirection = positions[i] - positions[last]; lastDirection.Normalize(); direction.Normalize(); // Determine how far the position is between the current and next // positions in the list float amt = distance / length;

[ 247 ]

Animation // Interpolate the last and current direction and current and // next position to find final direction and position direction = Vector2.Lerp(lastDirection, direction, amt); return Vector2.Lerp(positions[i], positions[next], amt); } }

Now we are ready to animate the car. First, we load the car as usual. In this case, the car is illing the second position in the list as the ground is loaded into the irst position. models.Add(new CModel(Content.Load("car"), Vector3.Zero, Vector3.Zero, Vector3.One, GraphicsDevice));

We'll attach a chase camera to the car: camera = new ChaseCamera(new Vector3(0, 250, 700), new Vector3(0, 250, 0), new Vector3(0, MathHelper.Pi, 0), GraphicsDevice);

The camera will need to be updated at the very end of the Update() function: ((ChaseCamera)camera).Move(models[1].Position, models[1].Rotation); ((ChaseCamera)camera).Update();

We will also need to update the references in the Draw() function to treat the camera as a ChaseCamera instead of a FreeCamera: foreach (CModel model in models) if (camera.BoundingVolumeIsInView(model.BoundingSphere)) model.Draw(camera.View, camera.Projection, ((ChaseCamera)camera). Position); track.Draw(camera.View, camera.Projection);

To make things more interesting, we will simulate some simple velocity/acceleration physics on our car. We will need distance and speed values: float distance = 0; float speed = 0;

We will work with these values at the beginning of the Update() function: // Update the car speed for acceleration, braking, and friction if (Keyboard.GetState().IsKeyDown(Keys.Up)) speed += 1000 * (float)gameTime.ElapsedGameTime.TotalSeconds; else if (Keyboard.GetState().IsKeyDown(Keys.Down)) speed -= 2500 * (float)gameTime.ElapsedGameTime.TotalSeconds; else [ 248 ]

Chapter 9 speed -= 1500 * (float)gameTime.ElapsedGameTime.TotalSeconds; // Limit the speed min/max speed = MathHelper.Clamp(speed, 0, 2000); // Increase the distance based on speed distance += speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

Next, we get the position and direction of the car from the race track based on our distance value, and move the car accordingly: // Get position and direction of car from track class Vector2 direction; Vector2 trackPosition = track.TracePath(distance, out direction); // Convert direction vector to angle float rotation = (float)Math.Acos(direction.Y > 0 ? -direction.X : direction.X); if (direction.Y > 0) rotation += MathHelper.Pi; rotation += MathHelper.PiOver2; // Move and rotate car accordingly models[1].Position = new Vector3(trackPosition.X, 20, trackPosition.Y); models[1].Rotation = new Vector3(0, rotation, 0);

Running the game, you can drive the car using the up key to accelerate and the back key to brake. Also notice that with no input the car will slow down by itself due to friction.

[ 249 ]

Animation

Hierarchical animation Thus far, we have looked at animation of objects as a whole, but this is interesting only to a point. Next, we'll look at hierarchical animation—animation of individual parts of a model. We call it hierarchical animation because models are generally built as a hierarchy of pieces, where transforming one piece will also affect its children. For example, a windmill may have the following hierarchy:

If we were to rotate the Fan part of the hierarchy, you can see that each blade would also be rotated. Let's try this in our game. First, we load the windmill and an animation: models.Add(new CModel(Content.Load("windmill"), Vector3.Zero, Vector3.Zero, new Vector3(0.25f), GraphicsDevice)); anim = new ObjectAnimation(new Vector3(0, 875, 0), new Vector3(0, 875, 0), Vector3.Zero, new Vector3(0, 0, MathHelper.TwoPi), TimeSpan.FromSeconds(10), true);

This animation will simply rotate the fan blade in a circle every ten seconds. Now, all we need to do is link the animation to the Fan mesh in the Update() function, and the fan will spin independently of the tower. Because the fan is spinning, the blades will spin as well, as they are linked hierarchically to the Fan piece: models[0].Model.Meshes["Fan"].ParentBone.Transform = Matrix.CreateRotationZ(anim.Rotation.Z) * Matrix.CreateTranslation(anim.Position);

[ 250 ]

Chapter 9

We will revisit hierarchical animation when we get to skinned models, but for any "normal" (non-skinned) model—like those we have been using throughout the book—simply manipulating the bone transforms, like we did earlier, will be enough to perform most animations of individual pieces of a model.

Skinned animation The inal type of animation we will look at is skinned animation. Skinned animation animates the individual vertices of an object based on the object's skeleton. Each bone is assigned a number of vertices, which it will affect when moved. For each of those vertices, each bone is also assigned a weight determining how much inluence the bone will have on each vertex. This technique is especially useful for animating characters, animals, and so on, because it makes it easy to simulate a smooth, lowing, morphing skin with a skeleton inside. For example, it is easy to model an arm as being made up of a few bones, which will deform the skin when moved. This makes for a much more realistic effect than other approaches and makes animation easy because the animator only has to worry about animating the bones of a character—the skinning process will take care of animating each individual vertex. Skinned animation also commonly makes use of keyframed animation: animators will specify positions and orientations for each bone at speciic frames and allow the interpolation functions to handle the frames between the key frames. To make matters even simpler (building off of even more of what we have learned), the bones of a model are almost always modeled as a hierarchy, so transformations of one bone will be relected by its children. For example, moving a character's thigh will also move its lower leg and foot, without any extra animation needed. [ 251 ]

Animation

Before we can render skinned models, we need to be able to load them. XNA doesn't provide a content processor for the content pipeline to build and load a skinned model. Therefore, we will need to write our own. We will base this off of the skinned model sample available at apphub.com. To begin with, we need to add a few projects to our game's solution. We will need two new projects—a neutral class library to store classes that can be accessed by the content pipeline and the game, and the content pipeline extension itself. To add a new project, right-click on the solution in the Solution Explorer in Visual Studio (labeled Solution MyGame (two projects)) and click on Add New Project. We will do this twice—adding a Windows Game Library called SkinnedModel the irst time, and a Content Pipeline Extension Library called SkinnedModelPipeline the second time. We now need to link all of the projects together so that each project will have access to what they need to build. The game and content pipeline extension will need access to the SkinnedModel project, and the game's content project will need access to the content pipeline extension. In the Solution Explorer, irst right-click on References under the Content Pipeline Extension project (SkinnedModelPipeline) and under Projects in the pop-up window, choose SkinnedModel. Do the same for the MyGame project. Add a reference in the MyGameContent project to the SkinnedModelPipeline project. We will need a few classes in the SkinnedModel project for animation. First, we will need a somewhat familiar Keyframe class. This class stores the transformation of a single bone as a matrix, the index of the bone the keyframe stores the transformation of, and the time from the beginning of the animation of the keyframe: public class Keyframe { // Index of the bone this keyframe animates [ContentSerializer] public int Bone { get; private set; } // Time from the beginning of the animation of this keyframe [ContentSerializer] public TimeSpan Time { get; private set; } // Bone transform for this keyframe [ContentSerializer] public Matrix Transform { get; private set; } public Keyframe(int Bone, TimeSpan Time, Matrix Transform) { this.Bone = Bone; this.Time = Time; [ 252 ]

Chapter 9 this.Transform = Transform; } private Keyframe() { } }

The ContentSerializer attribute on the public properties and the blank constructor of this class will allow the content pipeline to serialize the class so that it can be recreated when loaded by the game. Next, the AnimationClip class stores all of the keyframes for all of the bones of an animation: public class AnimationClip { // Total length of the clip [ContentSerializer] public TimeSpan Duration { get; private set; } // List of keyframes for all bones, sorted by time [ContentSerializer] public List Keyframes { get; private set; } public AnimationClip(TimeSpan Duration, List Keyframes) { this.Duration = Duration; this.Keyframes = Keyframes; } private AnimationClip() { } }

Finally, the SkinningData class stores all of the data needed to load, animate, and draw a skinned model. First, it stores all of the animation clips a model may happen to contain. Next, it contains the matrix transformations for each bone (relative to their parent's bone) that will put the model in what is called its "bind pose"—the "default" pose of the model. The SkinningData class also contains the inverse matrices of the bind pose matrices, to convert back from the bind pose. Finally, this class stores the skeleton hierarchy of the model by storing the parent index of each bone: public class SkinningData { // Gets a collection of animation clips, stored by name [ContentSerializer] [ 253 ]

Animation public Dictionary AnimationClips { get; private set; } // Bind pose matrices for each bone in the skeleton, // relative to the parent bone. [ContentSerializer] public List BindPose { get; private set; } // Vertex to bonespace transforms for each bone in the skeleton. [ContentSerializer] public List InverseBindPose { get; private set; } // For each bone in the skeleton, stores the index of the parent bone. [ContentSerializer] public List SkeletonHierarchy { get; private set; } public SkinningData(Dictionary animationClips, List bindPose, List inverseBindPose, List skeletonHierarchy) { AnimationClips = animationClips; BindPose = bindPose; InverseBindPose = inverseBindPose; SkeletonHierarchy = skeletonHierarchy; } private SkinningData() { } }

Loading a skinned model In XNA, the content pipeline is responsible for processing content iles—models, textures, and so on—and converting them into a format that is ready to be loaded by the game. The content pipeline is a two stage process—irst, content is run through an importer that converts iles of the same type (.fbx and .x models, or .jpg and .png and .tga textures, for example) to the same intermediate format. This intermediate data is then further processed by a content processor. The content processor is responsible for taking the data provided by the importer and creating an object that can be serialized (saved to a ile) and loaded directly by the game's content manager. In this case, we will take the animation data that has been loaded by the importer and create a SkinningData object attached to the regular model output by the content processor.

[ 254 ]

Chapter 9

With these three classes, we are ready to write the content processor that will process the model ile. XNA's model importer will have already extracted the data from the model ile, so all we need to do is extract the data that we need and convert it to a format we can use. We will do this in the SkinnedModelPipeline project, with a processor called SkinnedModelProcessor: [DisplayName("Skinned Model Processor")] public class SkinnedModelProcessor : ModelProcessor { }

The Process() function is primarily responsible for converting the data we are given to the format we desire: public override ModelContent Process(NodeContent input, ContentProcessorContext context) { }

The irst step is to ind the skeleton of the model in the data we are given, by using the MeshHelper.FindSkeleton() function. We then extract the bones from that skeleton using the same class' FlattenSkeleton() function: // Find the skeleton. BoneContent skeleton = MeshHelper.FindSkeleton(input); // Read the bind pose and skeleton hierarchy data. IList bones = MeshHelper.FlattenSkeleton(skeleton);

Next, we will extract the bind post matrix transformations from the bones as well as the hierarchy of the parent bone indices: List bindPose = new List(); List inverseBindPose = new List(); List skeletonHierarchy = new List(); // Extract the bind pose transform, inverse bind pose transform, // and parent bone index of each bone in order foreach (BoneContent bone in bones) { bindPose.Add(bone.Transform); inverseBindPose.Add(Matrix.Invert(bone.AbsoluteTransform)); skeletonHierarchy.Add(bones.IndexOf(bone.Parent as BoneContent)); }

[ 255 ]

Animation

The next step is to process all of the animations in the model. We will do this with a function called ProcessAnimations(), which we will write in a moment: // Convert animation data to our runtime format. Dictionary animationClips; animationClips = ProcessAnimations(skeleton.Animations, bones);

Once we have processed all of this data, we call the base.Process() function so that the Process() function of the default ModelProcessor can process the rest of the data contained in the model (including the bone weights). Then we create an instance of the SkinningData class, ill it with all of the data we collected, and set it as the model's tag: // Chain to the base ModelProcessor class so it can convert the model data. ModelContent model = base.Process(input, context); // Store our custom animation data in the Tag property of the model. model.Tag = new SkinningData(animationClips, bindPose, inverseBindPose, skeletonHierarchy); return model;

The content importer will have provided the animations of the model as an AnimationContentDictionary object, so the ProcessAnimations() function's job is to convert that object into our AnimationClip object. We do this by irst building a dictionary of bone names and indices. Next, we process all of the individual animations in the dictionary. Finally, we add each animation to a dictionary of animation clips. The table of bone names and indices is needed by the ProcessAnimation() function to convert between bone names and indices when processing a single animation: static Dictionary ProcessAnimations( AnimationContentDictionary animations, IList bones) { // Build up a table mapping bone names to indices. Dictionary boneMap = new Dictionary(); for (int i = 0; i < bones.Count; i++) boneMap.Add(bones[i].Name, i); Dictionary animationClips = new Dictionary(); // Convert each animation [ 256 ]

Chapter 9 foreach (KeyValuePair animation in animations) { AnimationClip processed = ProcessAnimation(animation.Value, boneMap); animationClips.Add(animation.Key, processed); } return animationClips; }

The ProcessAnimation() function is responsible for taking a single animation from the AnimationContentDictionary (stored as an AnimationContent object) and converting it into an AnimationClip, illed with keyframes. It does this by looking at each animation channel in the animation. An animation channel contains all of the key frames for one bone of a model. This function, then, needs to determine what bone the animation channel controls, and extract all of the key frames in the animation channel. Finally, having loaded all of the key frames for each bone, the ProcessAnimation() function sorts all of the key frames by their time in the animation using a comparison function: static AnimationClip ProcessAnimation(AnimationContent animation, Dictionary boneMap) { List keyframes = new List(); // For each input animation channel. foreach (KeyValuePair channel in animation.Channels) { // Look up what bone this channel is controlling. int boneIndex = boneMap[channel.Key]; // Convert the keyframe data. foreach (AnimationKeyframe keyframe in channel.Value) keyframes.Add(new Keyframe(boneIndex, keyframe.Time, keyframe.Transform)); } // Sort the merged keyframes by time. keyframes.Sort(CompareKeyframeTimes); return new AnimationClip(animation.Duration, keyframes); }

[ 257 ]

Animation static int CompareKeyframeTimes(Keyframe a, Keyframe b) { return a.Time.CompareTo(b.Time); }

We can now build a model using our content processor and load it into the game. We'll use the dude.fbx model from the skinning sample (also included in the code download). To build the model with the correct content processor, we need to tell XNA to use the Skinned Model Processor by right-clicking on the model in the Solution Explorer, opening its properties, and setting the Content Processor property to the previously mentioned value:

In order to render a skinned model, we need to use an Effect that will perform the vertex transformations based on bone transformations. XNA provides a built-in effect called SkinnedEffect that can do this for us. Let's create a new class that will handle skinned models for us: public class SkinnedModel { Model model; GraphicsDevice graphicsDevice; ContentManager content; SkinningData skinningData; public Vector3 Position, Rotation, Scale;

[ 258 ]

Chapter 9 public Model Model { get { return model; } } public SkinnedModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice GraphicsDevice, ContentManager Content) { this.model = Model; this.graphicsDevice = GraphicsDevice; this.content = Content; this.Position = Position; this.Rotation = Rotation; this.Scale = Scale; this.skinningData = model.Tag as SkinningData; setNewEffect(); } }

The setNewEffect() function changes the effect used by the model to a SkinnedEffect and copies the texture, diffuse color, and so on from the old effect to the new effect: void setNewEffect() { foreach (ModelMesh mesh in model.Meshes) { foreach (ModelMeshPart part in mesh.MeshParts) { SkinnedEffect newEffect = new SkinnedEffect(graphicsDevice); BasicEffect oldEffect = ((BasicEffect)part.Effect); newEffect.EnableDefaultLighting(); newEffect.SpecularColor = Color.Black.ToVector3(); newEffect.AmbientLightColor = oldEffect.AmbientLightColor; newEffect.DiffuseColor = oldEffect.DiffuseColor; newEffect.Texture = oldEffect.Texture; part.Effect = newEffect; } } }

[ 259 ]

Animation

Finally, the Draw() function draws the model as usual: public void Draw(Matrix View, Matrix Projection, Vector3 CameraPosition) { Matrix world = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position); foreach (ModelMesh mesh in model.Meshes) { foreach (SkinnedEffect effect in mesh.Effects) { effect.World = world; effect.View = View; effect.Projection = Projection; } mesh.Draw(); } }

Right now, this class may not seem complicated enough to be warranted, but we will see in a moment why we created a separate SkinnedModel class. We can test it at this most basic level right now, by creating an instance of this class in our game: SkinnedModel skinnedModel;

We initialize it in the LoadContent() function just like a CModel: skinnedModel = new SkinnedModel(Content.Load("dude"), Vector3.Zero,new Vector3(0, MathHelper.Pi, 0), new Vector3(10), GraphicsDevice, Content);

We also draw it just like a CModel: skinnedModel.Draw(camera.View, camera.Projection, ((FreeCamera)camera).Position);

Running the game, we should see the model in its bind pose. The model will be facing backwards without an animation playing if you use the dude.fbx model. This will change when an animation plays.

[ 260 ]

Chapter 9

Playing a skinned animation In order to play a skinned animation, we need to, for each frame, ind the transformations to be applied to each bone according to the current keyframe of the animation. The Effect will then take those transformations and use them to move the vertices of our model. We will use XNA's SkinnedEffect to perform the second function. However, to perform the irst function of determining the current keyframe and determining what the bone transformations should be, we will create a new class—AnimationPlayer. public class AnimationPlayer { SkinningData skinningData; // The currently playing clip, if there is one public AnimationClip CurrentClip { get; private set; } // Whether the current animation has finished public bool Done { get; private set; } // Timing values TimeSpan startTime, endTime, currentTime; bool loop; int currentKeyframe; // Transforms public Matrix[] BoneTransforms { get; private set; } public Matrix[] WorldTransforms { get; private set; } public Matrix[] SkinTransforms { get; private set; } [ 261 ]

Animation public AnimationPlayer(SkinningData skinningData) { this.skinningData = skinningData; BoneTransforms = new Matrix[skinningData.BindPose.Count]; WorldTransforms = new Matrix[skinningData.BindPose.Count]; SkinTransforms = new Matrix[skinningData.BindPose.Count]; } }

The AnimationPlayer class will be responsible for playing back animation clips. It would be nice if all of the separate animations included in a model were stored as individual animation clips, but unfortunately, many modeling packages export only a single AnimationClip, with all of the animations placed together in a sequence. For this reason, we want to be able to playback only a speciic portion of an AnimationClip. To accommodate this, we will provide a number of overloads for the StartClip() function, which is responsible for resetting the animation player and getting ready to play a given AnimationClip: // Starts playing the entirety of the given clip public void StartClip(string clip, bool loop) { AnimationClip clipVal = skinningData.AnimationClips[clip]; StartClip(clip, TimeSpan.FromSeconds(0), clipVal.Duration, loop); } // Plays a specific portion of the given clip, from one frame // index to another public void StartClip(string clip, int startFrame, int endFrame, bool loop) { AnimationClip clipVal = skinningData.AnimationClips[clip]; StartClip(clip, clipVal.Keyframes[startFrame].Time, clipVal.Keyframes[endFrame].Time, loop); } // Plays a specific portion of the given clip, from one time // to another public void StartClip(string clip, TimeSpan StartTime, TimeSpan EndTime, bool loop) { CurrentClip = skinningData.AnimationClips[clip]; currentTime = TimeSpan.FromSeconds(0);

[ 262 ]

Chapter 9 currentKeyframe = 0; Done = false; this.startTime = StartTime; this.endTime = EndTime; this.loop = loop; // Copy the bind pose to the bone transforms array to reset the // animation skinningData.BindPose.CopyTo(BoneTransforms, 0); }

The Update() function increases the time value, then updates the three arrays of transformations: public void Update(TimeSpan time, Matrix rootTransform) { if (CurrentClip == null || Done) return; currentTime += time; updateBoneTransforms(); updateWorldTransforms(rootTransform); updateSkinTransforms(); }

First, we update the bone transforms: // Helper used by the Update method to refresh the BoneTransforms data void updateBoneTransforms() { // If the current time has passed the end of the animation... while (currentTime >= (endTime - startTime)) { // If we are looping, reduce the time until we are // back in the animation's time frame if (loop) { currentTime -= (endTime - startTime); currentKeyframe = 0; skinningData.BindPose.CopyTo(BoneTransforms, 0); } // Otherwise, clamp to the end of the animation else { Done = true; [ 263 ]

Animation currentTime = endTime; break; } } // Read keyframe matrices IList keyframes = CurrentClip.Keyframes; // Read keyframes until we have found the latest frame before // the current time while (currentKeyframe < keyframes.Count) { Keyframe keyframe = keyframes[currentKeyframe]; // Stop when we've read up to the current time position. if (keyframe.Time > currentTime + startTime) break; // Use this keyframe. BoneTransforms[keyframe.Bone] = keyframe.Transform; currentKeyframe++; } }

Next, we update the world transforms. This will give us the world space transformation of each bone, including its parent transform: // Helper used by the Update method to refresh the WorldTransforms data void updateWorldTransforms(Matrix rootTransform) { // Root bone WorldTransforms[0] = BoneTransforms[0] * rootTransform; // For each child bone... for (int bone = 1; bone < WorldTransforms.Length; bone++) { // Add the transform of the parent bone int parentBone = skinningData.SkeletonHierarchy[bone]; WorldTransforms[bone] = BoneTransforms[bone] * WorldTransforms[parentBone]; } } [ 264 ]

Chapter 9

Finally, we can update the skin transforms. These are the matrices actually used to render the model: // Helper used by the Update method to refresh the SkinTransforms data void updateSkinTransforms() { for (int bone = 0; bone < SkinTransforms.Length; bone++) SkinTransforms[bone] = skinningData.InverseBindPose[bone] * WorldTransforms[bone]; }

We can now update the SkinnedModel class to use an AnimationPlayer to play animations: public SkinnedAnimationPlayer Player { get; private set; }

We initialize this player in the constructor: this.skinningData = model.Tag as SkinningData; Player = new SkinnedAnimationPlayer(skinningData);

Next, we add an Update() function that will update the player and perform the world matrix calculation: public void Update(GameTime gameTime) { Matrix world = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position); Player.Update(gameTime.ElapsedGameTime, world); }

Finally, we can update the draw function to set the skin transforms from the animation player to the SkinnedEffect attached to each MeshPart: public void Draw(Matrix View, Matrix Projection, Vector3 CameraPosition) { foreach (ModelMesh mesh in model.Meshes) { foreach (SkinnedEffect effect in mesh.Effects) { effect.SetBoneTransforms(Player.SkinTransforms); effect.View = View;

[ 265 ]

Animation effect.Projection = Projection; } mesh.Draw(); } }

Back in the Game1 class, we must be sure to call the skinned model's update function: skinnedModel.Update(gameTime);

Finally, we can play this character's walking animation by calling the StartClip() function of the AnimationPlayer of the SkinnedModel: skinnedModel.Player.StartClip("Take 001", true);

Changing an animation's play speed With the SkinnedModel class set up the way it is, it would be very easy to change the speed at which its animations are played. All we need to do is lie to the AnimationPlayer about how much time has elapsed between frames. First, we'll add a PlaySpeed property: public float PlaySpeed = 1.0f;

We can then modify the Update() function to use a modiied elapsed time when updating the AnimationPlayer: TimeSpan elapsedTime = TimeSpan.FromSeconds( gameTime.ElapsedGameTime.TotalSeconds * PlaySpeed); Player.Update(elapsedTime, world); [ 266 ]

Chapter 9

We could make our character walk in slow motion, then, by changing the PlaySpeed property: skinnedModel.PlaySpeed = 0.5f;

Model attachments A very useful trick when animating models, or when drawing models in general, is attaching models to certain parts of other models. In the next example, we will attach a gun model to the hand of our "dude" model. The irst step is to ind the index of the bone controlling the hand. We will store that index in a variable. We will have to load our model as a regular Model instead of the CModel, so that we can set the world matrix directly: Model gun; int handIndex;

We ind the index of the hand through the Model class of the underlying SkinnedModel, while loading the gun model: gun = Content.Load("mp5"); foreach(ModelMesh mesh in gun.Meshes) foreach (BasicEffect effect in mesh.Effects) { effect.Texture = Content.Load("mp5_tex"); effect.TextureEnabled = true; effect.EnableDefaultLighting(); } handIndex = skinnedModel.Model.Bones["R_Hand"].Index;

We can now draw the gun model as follows, applying both a transformation to make the gun model lineup with the character's hand and the world transformation of the character's hand itself: foreach (ModelMesh mesh in gun.Meshes) { foreach (BasicEffect effect in mesh.Effects) { Matrix gunTransform = Matrix.CreateScale(0.65f) * Matrix.CreateFromYawPitchRoll(-MathHelper.PiOver2, 0, MathHelper.Pi); effect.World = gunTransform * skinnedModel.Player.WorldTransforms[handIndex]; [ 267 ]

Animation effect.View = camera.View; effect.Projection = camera.Projection; } mesh.Draw(); }

Summary Having completed this chapter, you have learned about several different types of animation—you have learned how to animate objects as a whole with some very simple position and rotation interpolation and interpolation over a curve. You have also learned how to create a race track from a curve and how to move a car along that path and track. You have also learned about hierarchical animation—the animation of an object's skeleton and submeshes. You also learned about "keyframed" animation, and how to create more complex animations using it. Finally, you learned about "skinned" animation: animation of the individual vertices of a model according to the model's skeleton's transformations.

[ 268 ]

Index Symbols 3D coordinate system 10

A AddParticle() function 172 advanced materials 206-208 A key 24 AlphaTestValue parameter 158 ambient lighting 56, 57 amt argument 238 animation keyframed animation 233, 234 object animation 231 play speed, changing 266 skinned animation 251, 252 skinned animation, playing 261 Update() function 266 animation channel 257 AnimationClip class 253 AnimationContentDictionary object 256 AnimationPlayer class, creating 261, 262 anisotropic iltering 55 arc-ball camera about 31 ArcBall Camera class 32, 33 LoadContent() method 34 ArcBall Camera class 32, 33 aspect ratio 15

B base.Draw() 224 base.Process() function 256 BasicEffect 15, 66

BasicTexture parameter 52 Billboarding 149 Billboards drawing 153-156 non-rotating 163-166 BillboardSystem 196, 198 billboard system adding, to render clouds 200-203 BillboardSystem class about 156, 159, 164 creating 150, 152 black and white post processor 212, 213 BoundingFrustum class 29 BoundingSphere member variable 27 bounding spheres calculating, for models 27, 28 boundingSphere value 28 bounding volume 27 buildBoundingSphere() 45

C CachedEffect variable 45 camera arc-ball camera 31, 34 chase camera 34-36 types 31, 34 upgrading, to free camera 24-27 Camera base class 21 Camera class creating 21, 22 Catmull-Rom interpolation about 237 amt argument 238 KeyframedObjectAnimation class, modifying 238

wrap() function 238 cellSize variable 184 chase camera about 34 ChaseCamera class, code 35, 36 ChaseCamera class code 35, 36 circular queue 170-173 clipped 15 clipping plane 139 clouds creating, with spherical billboarding 162 CModel class 18, 21, 27, 45, 61, 140, 205, 206 constructor function 171 ContentManager class 13 Content Processor property 258 ContentSerializer attribute 253 coordinate system 10 CopyEffect parameter 47 createBuffers() function 244 CreateLookAt() function 22 CreateLookAt() static function 14 CreatePerspectiveFieldOfView() function 14, 15 cube map 127 cube mapping about 127 relections, simulating 131-133 SkySphere class, creating 127-130 curve interpolation 237 Custom Model class creating 18-20 cylindrical billboarding 162

D depth bias 108 depth buffer 157 depth of ield effect about 218, 220 base.Draw() 224 Draw() function 220, 221 GaussianBlur class 221 LoadContent() function 220, 222, 224 depth values storing 81-83

detail texture adding, to terrain 193, 194 Draw() function 194 effect parameters 194 lerp() function 194 LoadContent() method 194 Terrain class 194 diffuse colors 56 directional lighting 57 D key 24 Draw() call 167 Draw() function 14, 16, 30, 47, 94, 114, 130, 154, 167, 174, 178, 189, 192, 194, 197, 199, 201, 209, 220, 221, 222, 224, 227, 248, 260 Draw() method 20, 23, 156, 160 drawBillboards() function 166 drawDepthNormalMap() function 90 draw function 207, 265 drawing model 14, 15, 17

E effect 15, 41, 46 Effect class 65 Effect instance 209 effect parameters 46, 153, 187, 189, 194, 217

F First-Person-Shooter (FPS) games 24 FlattenSkeleton() function 255 fog 117 frame 14 FreeCamera class 24, 156

G Game1 class 16, 143, 222 Game class 13 Gaussian blur 111 GaussianBlur class 216, 221 Gaussian blur post processor about 214 effect parameter 217 GaussianBlur class 216

[ 270 ]

gaussianFn() function 216 Gaussian function 217 Gaussian function 217 generateFrustum() function 29 generateParticles() function 164, 171 getHeights() function 183 glow post processor effect about 225 Draw() function 227 GraphicsDevice 15 GraphicsDevice.Clear() function 14 grass value 198

H heightmap about 181 cellSize variable 184 Draw() function 189 effect parameters 187, 189 getHeights() function 183 heights[,] array 184 LoadContent() method 189 new Terrain class, creating 182 normals 186 Projection parameters 189 terrain, building from 182 TerrainEffect.fx effect 187 TextureTiling parameter 187, 188 View parameters 189 heights[,] array 184 heights array 195 hierarchical animation about 231, 250 animation, loading 250 Update() function 250 windmill, loading 250 High Level Shader Language. See HLSL HLSL about 41 ambient lighting 56, 57 diffuse colors 56 lambertian directional lighting 57-59 material class, creating to store effect parameters 65-67 phong specular highlights 60, 62, 65

point light, implementing with 69-73 shader, assigning to model 44, 46 simple effect, creating 49-52 spot light, implementing with 74, 75 starting 42, 43 texture mapping 52 texture sampling 54, 55

I index buffer 150 interpolatePositions() function 241 IRenderable interface 202

K Keyframe class 252 keyframed animation about 231, 233 ObjectAnimation class 233 Update() function 235 Update code 237 uses 234 KeyframedObjectAnimation class, modifying 238

L lambertian directional lighting 57-60 Lambertian lighting 57, 81 lerp() function 144, 194 LightEffect.fx effect 121 light map creating 83-86 used, for drawing models 86, 87 LightMaterial class 122 lights multiple lights, drawing 77-79 Load() function 13 LoadContent() function 26, 93, 130, 203, 211, 220, 222, 224, 245, 260 LoadContent() method 13, 20, 23, 34, 66, 143, 189, 194 LoadContent function 50, 196 loading model 12, 13 local 11

[ 271 ]

M Material 65 material class about 70, 96 creating, to store effect parameters 65 matrices 10, 11 Matrix class 14, 22 mesh 11 meshes 13 MeshHelper.FindSkeleton() function 255 MeshTag class 45, 56, 206 method PreDraw 146 Mip mapping 55 model adding, to content project 12 bounding spheres, calculating 27, 28 drawing 14-16 drawing, with light map 86, 87 loading 12 shader, assigning to 44 model attachments 267, 268 Model class 13 modelMaterial 67 ModelMeshPart 44 modelTransformations array 15 MouseState member variable 26 Move() function 24 mul function 51 multiple lights drawing 77-79 multitexturing about 181, 190 pixel shader, updating 191 texture parameters, modifying 190, 191 values, setting in Game1 class 192 values, setting to effect 192

N normalize() function 59 normal mapping 118-123 normal maps about 120 generating, Photoshop used 123-126

normal values storing 81-83

O object animation about 231 ObjectAnimation class 231 Update() function 232 ObjectAnimation class 231, 233 ObjectAnimationFrame class 234 object space 11 origin 10

P particles 168, 169 particle smoke 179 ParticleSystem class 177 Photoshop using, for normal map generation 123-126 pitch values 24 pixel shader 41, 72 point light about 69 implementing, with HLSL 70-73 LightAttenuation value 70 LightFalloff value 70 pixel shader 72 POSITION0 semantics 51 position 14 post processing effect 205, 208, 209, 211 PostProcessor class 214, 218 PPLight.fx function 92 PreDraw method 146 prelighting about 80 process 80 PrelightingRenderer 88 prelighting renderer creating 88-92 using 93 PrelightingRenderer class 103, 113 prepareMainPass() function 92 Process() function 255 ProcessAnimation() function 256, 257

[ 272 ]

ProcessAnimations() function 256 projection matrix 11 Projection parameters 189 projective texturing about 95-100 difference 96 effect, creating 98 orthographic matrix 98 texture, sampling from 99

R Race Track building, from curve 240, 241 car, moving 246, 247 createBuffers() function 244 Draw() function 248 indices, creating 244 interpolatePositions() function 241 LoadContent() function 245 RaceTrack class 240 relective water effect creating 137-147 region growing 181 RenderCapture class 211 render clouds billboard system, adding 200-203 Right vectors 153 root 11 Rotate() method 24 rotation, transformations 10

S sampler 53 saturate() function 59 scaling (changing size), transformations 10 SetClipPlane() function 139 SetClipPlane function 202 SetEffectParameters() 66 setEffectParameters() function 167 setEffectParameters() method 162 SetModelEffect() function 50, 207 SetModelMaterial() function 207 setNewEffect() function 259

shader effects about 117 cube mapping 117, 127 fog 117 normal mapping 117, 118 water effect 117 shaders about 41 assigning, to model 44-48 pixel shader 41 vertex shader 41 shadows, VSM generating 115, 116 shadow mapping about 95, 100 depth comparison, performing 107, 108, 109 depth texture, projecting onto scene 105, 106, 107 PrelightingRenderer class, updating 103, 104 scene, rendering as depth texture 101 shader, writing 102, 103 Side parameters 166 simple effect creating 48-51 S key 24 skinned animation about 231, 251 AnimationClip class 253 AnimationPlayer class, creating 261, 262 ContentSerializer attribute 253 draw function 265 playing 261 SkinnedModel class, updating 265 SkinnedModelPipeline 252 SkinningData class 253 skin transforms, updating 265 StartClip() function, calling 266 Update() function 263 Update() function, adding 265 world transforms, updating 264 skinned model animation channel 257 AnimationContentDictionary object 256

[ 273 ]

base.Process() function 256 Content Processor property 258 Draw() function 260 FlattenSkeleton() function 255 LoadContent() function 260 loading 254 MeshHelper.FindSkeleton() function 255 Process() function 255 ProcessAnimation() function 256, 257 ProcessAnimations() function 256 rendering 258 setNewEffect() function 259 SkinnedModel class 260 SkinnedModelPipeline project 255 SkinningData class 256 SkinningData object 254 SkinnedModel class 260, 266 SkinnedModel class, updating 265 SkinnedModelPipeline 252 SkinnedModelPipeline project 255 SkinningData class 253, 256 SkinningData object 254 SkySphere class creating 127-130 spaceship simulator 37, 38 specular highlights 60-65 spherical billboarding about 162 clouds, creating with 162 spot light about 74 implementing, with HLSL 74, 75 new effect, creating 75 StartClip() function 262 StartClip() function, calling 266 strobing 193

T Tag properties 44 target 14 target camera creating 22-24 TargetCamera class 24 Terragen about 133

downloading 134 general render settings 134-136 sky boxes, rendering 133-136 terrain BillboardSystem 198 detail texture, adding 193, 194 plants, placing 195-198 Terrain class 188, 194 texture mapping 52-54 texture parameter, multitexturing modifying 190, 191 texture sampling 54, 55 TextureTiling parameter 187, 188 transformations about 10 rotation 10 scaling (changing size) 10 translation (movement) 10 types 10 transforms array 13 translation (movement), transformations 10

U Update() function 23, 25, 156, 173, 232, 235, 250, 263, 265, 266 Update() method 23, 26 Update code 237 Up parameters 166 up vector 14, 153 UV coordinates 53

V Variance Shadow Maps. See VSM vertex 150 vertex buffer 150 VertexPositionTexture vertex type 154 vertex shader 41 VertexShaderInput struct 175 VertexShaderOutput 59 VertexShaderOutput struct 51, 52, 202 vertices 12 view frustum culling 29 view matrix 11 View parameters 189

[ 274 ]

VSM about 110 beneits 110 depth texture, blurring 111-114 shadows, generating 115, 116 soft shadows, implementing 110

X

W

Z-axis 10

XNA game project setting up 7-9

Z

WaterEffect class 146 W key 24 world matrix 11 world space 11 wrap() function 238

[ 275 ]