Anatomy of a Game - Part 3

So what are the different elements that go into making a 3D game? Let’s start by familiarizing you some terms used in 3D programming, so that when you’re working on your game, you’ll have an idea of what’s going on.

Game Camera

One of the first concepts you’ll see when working with 3D graphics is the game. Camera, you say? What is a camera doing in a game? The principle is quite simple, and is not required for 2D games. In 2D games, your “world” is bound by the X and Y axes only. So your view of the game is a top-down view of the game grid. In 3D, however, you have another axis, the Z axis. So how do you position your view of the game world? That’s what the game camera does for you.

In simplest terms, the game camera is the hypothetical camera that someone is holding and through which they are filming the action in your game. What you see on your screen is what the camera sees. So imagine you have a ball sitting on a table and someone is holding a camera and is filming that ball while moving around it. What you end up seeing on the TV later is the ball from every direction as the camera is rotating around it.

It’s the same thing in 3D graphics. A game camera has a position in the world just as anything else has a position. So in 3D graphics, the camera position is a value of x, y, and z that tells the game where your camera is positioned in the world. That means the camera can have objects behind it and you are not going to see them. The other important value that you need to specify is a position in the world for the camera to look at. If you don’t specify that value, then how is the camera going to know where to point once it’s placed in the world? So a camera can be positioned at, say, 0,0,100 and be looking at 0,0,0. This means it is positioned 100 units away from the origin of the world and is pointing toward the origin. So if you have an object that is at 0,0,50, you will see it. If there is an object at 0,0,200, you won’t see it because it’s behind the camera. You can do all kinds of cool zooming and rotation effects by moving the camera position in the world every frame! Turn your code into a director and a camera man and go wild!

The camera has other settings, such as the Up direction, the field of view, near and far planes, and so on. Once you start working with 3D graphics, all those things will be very clear and easy to understand. I just wanted to explain the purpose and need for having a camera in a 3D game.

Vertex

Since we started talking about 3D graphics, I’ve been mentioning that everything in the world has a position x,y,z. In its simplest form, a vertex is a position in the world. More specifically, a vertex is a data structure that you declare and use as the building block of your entire world. So to draw a square on the screen, for example, you would specify four vertices that define the four coordinates of that square. A vertex has a position element in it that is of type Vector3. That position element is where you store the values for the x,y, and z for a particular point in space.

A vertex can also have other information in it. In fact, you can define your own vertex structure and include as much data in it as you need. A typical simple vertex structure, though, would contain a Vector3 position (x,y,z) and a Color element. This means for every point in the world, you can specify its position and color. What does that color do exactly? Well, if you specify a different color for each of the four vertices you used to create a square, you can get an image that looks like this:

clip_image002

In this image, the vertices have the colors blue, green, red, and yellow. What the graphics card did for you was fade one color to the other in the way you see in the illustration. Had all vertices had the color black, the square would just be, well, all black. Pretty neat, eh? Ok…not too useful, but still neat!

DirectX and thus XNA Framework define a few popular vertex structures that have different elements besides position in them. VertexPositionColor is one that you can use to create something like the square above. The cool thing is that you can create your own if you find yourself needing something more custom. It’s pretty easy to do.

Mesh

Ok… so now that we learned what a vertex is and how it is the building block of anything in the world, it’s time we learned how we can build shapes using them. The square we talked about creating in the previous section is just a collection of four vertices that got together to create that square. If you’ve ever played “connect the dots” on paper before, you are already familiar with the idea of meshes.

A mesh is a collection of positions in the world that, when connected together, form a shape. The following diagram shows a simple cube mesh that is made up of eight distinct vertices in the 3D world:

clip_image004

This is a simple mesh, of course, and has only eight vertices. The more vertices you have, the more detailed and complex your mesh can be:

clip_image006 image

So as you can imagine, everything you see today in a game like Halo or Gears of War is composed of such meshes—all the characters, guns, vehicles, banners, terrain, buildings, and so on. To create such meshes—or models, as they are called —you use some drawing package such as 3D Studio Max, Maya, Softimage, and so on. These packages make it easy to draw such intricate models and export them in a format your game can load and transfer to a collection of vertices that your graphics card can render. The XNA Framework can draw meshes with the Model class, which is a group of several meshes.

Materials

So now that you have a cool model of a car or spaceship, how do you actually make it look all cool and colored? You see how uninteresting the model looks when it’s “naked” and not colored and polished. That’s where materials come into play.

Just as you might create a model out of clay and then paint it to give it color and detail, you do the same in 3D graphics. You apply a material to a model that specifies the color of that model or part of the model. Materials also include information such as how much light they reflect, whether they are plastic or wood-like in appearance, and so on—just as there are different materials in the real world that have specific properties. Plastic tends to be very shiny and reflects a lot of light; wood is not generally shiny and doesn’t reflect light as much. Leather has a very distinct look to it, and so does metal. You get the idea.

You can define a material for your model that gives it a unique look in the world. Armor on a futuristic soldier, for example, could have a material that is very reflective. To add more detail, a material can have a texture. A texture is an image that is overlaid on a model to give it more detail. Think of it like a decal that you put on a toy airplane model to give it a more realistic look. A texture can be an image of grass that you apply to your terrain to make it look more real, or an image of a suit that you put on a model of a person to give them clothes. The texture will be affected by the other properties you have on your material that affect how much light it reflects, and so on.

Materials are usually created by your graphics package along with something like Photoshop to create the textures. You map that material on your model and then export the entire model and load it in your game. The end result will be a mesh object along with its related set of materials that may or may not include textures. The Gears of War model above will look quite different when you apply materials to it:

Big difference eh? When you start messing with this aspect of game development, you really start to develop a huge appreciation for artists.

Move on to part 4

Thanks for reading! I'd love to hear your thoughts, feel free to leave a comment below. Don't forget to subscribe to my RSS Feed!

One Response to “Anatomy of a Game - Part 3”

  1. Smaktard Says:

    Not seeing the image of the GoW model with the materials added, just so you know that.

    Great little write up on those terms. Helped me to clear a couple concepts up in my head.

    Good Job!

Leave a Reply

The page's WebCounter count says that you are visitor number Visitor Counter by Digits
FireStats icon Powered by FireStats