Thursday, December 18, 2008

Polygons, Shading and Skeletons

The vast majority of 3-D objects created for computer games are made up of polygons. A polygon is an area defined by lines. To have a polygon, you must have at least three lines. The lines connect a series of coordinates in the three-dimensional "space" the computer creates. The point where the lines connect is known as a vertex. Each vertex has X, Y and Z coordinates.
• X determines the position relative to right or left in the virtual space
• Y determines the position relative to top or bottom in the virtual space
• Z determines the position relative to front or back in the virtual space Once each polygon has a set of vertices to define its shape, it needs information that tells it what to look like. There are four common ways to do this:
• Flat shading
• Gouraud shading
• Phong shading
• Texture mapping Flat shading simply assigns a single color to a polygon. It is very simple and fast, but makes the object look artificial. Gouraud shading is more involved. Colors are assigned to each vertex and then are blended across the face of the polygon. Since each vertex is typically associated with at least three distinct polygons, this makes the object look natural instead of faceted. Look at this example: You will notice that the ball with Gouraud shading appears much smoother than the flat shaded one. But look closely at the outlines of the two balls. That is where you can tell that both balls have the exact same number of polygons! A more complex version of shading, Phong, is rarely used in games. Whereas Gouraud shading interpolates colors by averaging between the vertices, Phong shading averages each pixel based on the colors of the pixels adjacent to it. Another common technique for determining the appearance of a polygon is to use texture mapping. Think of texture mapping as wrapping a present. Each side of the box you are wrapping is a blank polygon. You could paint the box, but it would be very difficult to make it match all the designs on the wrapping paper. However, if you take the wrapping paper and tightly cover the box with it, you have completely transformed the box with just a little effort. Texture mapping works the same way. Mapping requires the use of another image. Essentially, this other image is stretched over the object like a skin. Most video game consoles and computer graphics adapters contain a special chip and dedicated memory that store the special images used for texture mapping and apply them to each polygon on the fly. This allows games such as Portal Runner to have incredibly detailed 3-D environments that you can interact with in real time. The characters in a game have skeletons. Similar to our own skeleton, this is a hidden series of objects that connect with and move in relation to each other. Using a technique called parenting, a target object (the child) is assigned to another object (the parent). Every time the parent object moves, the child object will follow according to the attributes assigned to it. A complete hierarchy can be created with objects that have children and parents. Here's an example for a human character: Once the skeleton is created and all of the parenting controls put in place, the character is animated. Probably the most popular method of character animation relies on inverse kinematics. This technique moves the child object to where the animator wants it, causing the parent object and all other attached objects to follow. Another method that is popular for games is motion capture, which uses a suit of sensors on a real person to transmit a series of coordinates to a computer system. The coordinates are mapped to the skeleton of a game character and translated into fluid, realistic motion. Each character's range of motion is programmed into the game. Here's a typical sequence of events:
• You press a button on the controller to make the character move forward.
• The button completes a circuit, and the controller sends the resulting data to the console.
• The controller chip in the console processes the data and forwards it to the game application logic.
• The game logic determines what the appropriate action at that point in the game is (move the character forward).
• The game logic analyzes all factors involved in making the movement (shadows, collision models, change of viewing angle).
• The game logic sends the new coordinates for the character's skeleton, and all other changes, to the rendering engine.
• The rendering engine renders the scene with new polygons for each affected object, redrawing the scene about sixty times each second.
• You see the character move forward.

No comments: