XNA--3D Coordinate Systems and Projections

     The XNA Framework works, by default, in a right-handed coordinate system (which, it’s worth noting, is different from DirectX’s default). This means that negative values for the z axis are visible, and the more negative they are for a given object, the farther the object is from the screen. Positive values are not shown, unless you change your camera position.

     Now that you understand 3D coordinate systems, the next step to explore ishow you can map 3D objects from this system to your computer (or Xbox 360) 2D screen.
     Fortunately, XNA does all the hard mathematical work for this mapping, but you still need to understand theconcept of projections and how they apply to XNA to issue the basic instructions forhow to present the objects on the screen.
     Similarly to other gaming libraries, XNA supports two different types of projections:

      Perspective projection: The most common type of projection, perspective projection takes the z distance into account and adjusts the objects accordingly. This projection makes objects appear smaller when far from the screen. Depending on the position, the objects also appear deformed, as in the real world. For example, the sides of a cube that are closer to the screen seem bigger than the farther ones.

XNA--3D Coordinate Systems and Projections_第1张图片

     Orthogonal projection: In this type of projection, the z component is just ignored, and the objects don’t get bigger when closer to the screen or smaller when they are farther away. This projection is mostly used for 2D games (which may use “fake” 3D, just to put some sprites over others), to create head-up displays (HUDs, which are 2D interface elements such as life indicators, windows, and so on) or simpler 3D games.

Vertices and Primitives:

      vertices: are represented solely by their 3D coordinates (which are mapped to theVector3 data type in XNA), but in XNA, they includeextra information—such as color, texture, and normal vector information—depending on the vertex format used.

    

      Along with the vertices’ position and additional data, when creating 3D objects, you also need to specify how XNA will connect these vertices, according to different drawing primitives

      Drawing primitives: are one of the three basic geometric primitives (points, lines, and triangles) used by XNA to render a collection of vertices. Depending on theprimitive type chosen, the same set of vertices (known as the vertex buffer or vertex stream) will be rendered differently.

      The triangle is used as a base to create any other 2D or 3D objects. This is becausea primitive defined with only three points is guaranteed to be in a single plane and to be convex. (A line connecting any two points inside a triangle is always fully inside the triangle, which doesn’t happen in some figures with four vertices.)These characteristics are the key to performing the fastest rendering possible by the graphics cards, which always use triangles as the base rendering primitives.
     So, for example, if you want to draw a square on the screen, you’ll use two triangles. If you want to create a cube, you’ll use 12 triangles (2 for each facet).

     In XNA, the graphics device object has a method namedDrawPrimitives that is used to draw a vertex buffer according to a specific primitive type, defined by the PrimitiveType enumeration:

      PointList: Each vertex is rendered isolated from the others, so you can see a list of floating points.

     LineList: The vertices are rendered in pairs, with lines connecting each pair. This call fails if you do not pass a vertex buffer with an even number of vertices.

     LineStrip: All the vertices in the buffer are rendered as a single, connected line. This can be useful when debugging, because this primitive type allows you to see a wireframe image of your objects, regardless of the number of vertices

      TriangleList: The vertices are rendered in groups of three, as isolated triangles. This provides you with the greatest flexibility when rendering complex scenes, but there’s the drawback of having duplicated vertices if you want to draw connected triangles.

      TriangleStrip: You use this primitive type when drawing connected triangles. It’s more efficient for rendering scenes, because you don’t need to repeat the duplicated vertices. Every new vertex (after the first two) added to the buffer creates a new triangle, using the last two vertices.

      TriangleFan:In this primitive, all the triangles share a common vertex—the first one in the buffer—and each new vertex added creates a new triangle, using the first vertex and the last one defined.

XNA--3D Coordinate Systems and Projections_第2张图片XNA--3D Coordinate Systems and Projections_第3张图片

XNA--3D Coordinate Systems and Projections_第4张图片XNA--3D Coordinate Systems and Projections_第5张图片


Vectors, Matrices, and 3D Transformations

      Vectors:Vector3 is the most commonly used vector in 3D games, and some of its most important methods are as follows:
                    Vector3.Distance: Given two points, returns a float representing the distance between them.
                    Vector3.Add and Vector3.Subtract: Add and subtract two vectors. You can also use the common plus (+) and minus (-) signs to perform addition and subtraction operations on Vector3.
                    Vector3.Multiply and Vector3.Divide: Multiply and divide two vectors, or a vector by a float value.

                    Vector3.Clamp: Constrains the vector components into a given range—useful when defining lights or matrices’ values that support only values within a given range.
                    Vector3.Lerp: Calculates the linear interpolation between two vectors.
                    Vector3.SmoothStep: Interpolates two vectors according to a float given as a weight value.

                    Vector.Zero for an empty vector,Vector3.Up for the (0, 1, 0) vector,Vector3.Right for the (1, 0, 0) vector

      Matrices:are the basis for defining rotation, scaling, and translation of an object in the 3D world. Because matrices are used todefine any 3D transformations, they are also used to define the operations needed to simulate the projections and totransform the 3D scene according to the camera position and facing direction.

       Suppose you want to move a triangle up the y axis, as shown in Figure:

XNA--3D Coordinate Systems and Projections_第6张图片XNA--3D Coordinate Systems and Projections_第7张图片--->

XNA--3D Coordinate Systems and Projections_第8张图片

        So, what’s the big deal about using matrices? One of the biggest benefits is that you can perform complex operations by multiplying their corresponding transformation matrices. You can then apply the resulting matrix over each vertex on the 3D model, so you can perform all operations on the model by multiplying its vertices for only one matrix, instead of calculating each transformation for each vertex. For example, usually you will need to rotate, scale, and translate an object to position it in your 3D scene, and then perform new operations according to the object’s movement around the scene.

      Through the Matrix class, many matrix operations are available, such as the following:
            • Matrix.CreateRotationX, Matrix.CreateRotationY, and Matrix.CreateRotationZ: Each of these creates a rotation matrix for each of the axes.
            • Matrix.Translation: Creates a translation matrix (one or more axes).
            • Matrix.Scale: Creates a scale matrix (one or more axes).
            • Matrix.CreateLookAt: Creates a view matrix used to position the camera, by setting the 3D position of the camera, the 3D position it is facing, and which direction is “up” for the camera.
            • Matrix.CreatePerspectiveFieldOfView: Creates a projection matrix that uses a perspective view, by setting the angle of viewing (field of view), the aspect ratio (the ratio used to map the 3D projection to screen coordinates, usually the width of the screen divided by the height of the screen), and the near and far planes, which limit which part of the 3D scene is drawn. See Figure to better understand these concepts. Similarly, you have two extra methods, CreatePerspectiveOffCenter and CreatePerspective, which create matrices for perspective projection using different parameters.

            • Matrix.CreateOrthographic: Creates a matrix used in orthogonal, or orthographic, projection. This method receives the width, height, and near and far planes that define the orthographic projection, and has a similar method named CreateOrthographicOffCenter, which creates the orthogonal projection matrix where the center of the 3D scene does not map to the center of the screen.

           The aspect ratio is usually defined as device. Viewport.Width / device.Viewport.Height.

你可能感兴趣的:(vector,Matrix,transformation,Primitive,translation,orthogonal)