Starting at the Beginning-Defining a Few Primitives


Before we venture into the working code of this game engine, we will start with a few of the low-level or primitive data structures that we will be using. The C# language has two ways in which data and associated methods may be defined. The first is a structure ( struct ), which is a value type that is allocated on the stack rather than the managed heap. The structure types do not have the power and flexibility found in the class type, but they are more efficient for small data units such as the primitives that form the foundation of the game engine.

The first primitive is Vector3 , a simple structure consisting of three single-precision floating-point variables (X, Y, and Z). This simple structure forms the very foundation of much of the 3D rendering and physical dynamics within the game engine. We will use this type to describe everything from each point in three-dimensional space as well as the speed, acceleration, and forces along each axis for an object. The three-dimensional system that we will use in this game engine is defined with X positive moving to the right of the reference point, Y positive moving up from the reference point, and Z positive moving forward from the reference point. The majority of the code found in this book will use the vector class provided with Microsoft with DirectX. There will be an example in Chapter 10 that employs our own implementation of the class for a dynamics library usable by either DirectX or OpenGL. The C# code to represent this struc ture is shown in Listing 1 “1.

Listing 1.1: Vector3 C# Definition
start example
 public struct Vector3  {  public float X = 0.0;  public float Y = 0.0;  public float Z = 0.0;  } 
end example
 
Note

The units used in this game engine will all be in the English system. All distances will be measured in feet.

The Vector3 values can be referenced to two basic coordinate systems. The first system is called the world coordinate system. This system is centered at a point in the world database chosen by the game author. For the purpose of the game engine developed in this book, the origin of the world coordinate system will be at the southwest corner of the terrain model. With this origin, the X coordinate is positive moving east and the Z coordinate is positive moving north. The other coordinate system is referred to as the local coordinate system, or body coordinate system. For the terrain model, there is no difference between the two systems. For every other object, the local coordinate system is used in the definitions of the vertices that make up the object. As you will see in Chapter 3, both of these coordinate systems will be transformed to screen coordinates (two-dimensional coordinates) in order to draw properly on the screen.

The second primitive, Attitude , is a variation on Vector3 that describes the rotation around each of these axes. You will often see these referred to as Euler angles. For the sake of efficiency, all angles ( rotations ) are stored in radians. This is the format required by trigonometric math functions. If we use degrees in developing our game engine, it might be a bit more understandable to us as programmers, but not worth the computational cost required to convert each angle every time we build a transformation matrix.

There are a few terms related to the Attitude primitive you need to know before we move on. Pitch is the rotation around the X-axis with positive values in the clockwise direction when looking from the origin. Yaw is the rotation around the Y-axis with positive values in the counterclockwise direction when looking from the origin. Roll is the rotation around the Z-axis with positive values in the clockwise direction. It may seem strange that yaw angles are defined a bit differently from the other two. This is done in order to match up with compass heading angles. As you may know, the compass is defined with zero degrees as north, with angles increasing in the clockwise direction. In our system, that would only be true while looking toward the axis origin. Since all of our rotations are defined as looking along the given axis from the origin, we must rotate in the opposite direction for yaw to match compass angles. The C# code to represent this structure is shown in Listing 1 “2.

Listing 1.2: Attitude C# Definition
start example
 public struct Attitude  {   public float Pitch = 0.0;   public float Yaw = 0.0;   public float Roll = 0.0;  } 
end example
 

The third and last primitive that I will define for now is Vertex . This structure is an extension of the Vector3 structure that includes information used in texture mapping. A vertex defines a point in three-dimensional space that is part of a definition of a three-dimensional object. You will see later that a mesh (3D lingo for the data describing a three-dimensional shape) is made up of a list of vertices with additional information about how they are connected and the textures that cover them. The first component in the structure is a Vector3 component called Point that holds the position in three-dimensional space for the vertex. The next two components (TU and TV) are the two-dimensional coordinates within a texture map that corresponds to a particular vertex s position within the texture map. The texture map is simply a bitmap that describes how the pixels at and between vertices will be rendered. If any of this seems confusing, rest assured that we will discuss this in much greater detail in Chapter 3 as we look at the rendering pipeline. We will not need to develop these vertex structures ourselves though. Microsoft has kindly provided all of the basic vertex structures for us in managed DirectX. The C# code to represent this structure is shown in Listing 1 “3.

Listing 1.3: Vertex C# Definition
start example
 public struct Vertex  {   public Vector3 Point;   public Vector3 Normal;   public Color Diffuse;   public Color Specular;   public float TU = 0.0;   public float TV = 0.0;  } 
end example
 

This is the first and most basic version of the Vertex structure. Later in the book, you will encounter more complex versions of this structure tailored for specific purposes. These other versions will support features such as multitexturing as well as nontextured surfaces.




Introduction to 3D Game Engine Design Using DirectX 9 and C#
Introduction to 3D Game Engine Design Using DirectX 9 and C#
ISBN: 1590590813
EAN: 2147483647
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net