This article by Harry _ Spider-Man original, reproduced please indicate the source! If you have any questions, please contact [email protected]
This time we continue to talk about Jim Adams old brother's RPG Programming Book Second Edition, chapter 4th, section Two: Getting down to Drawing. This super-long section last talked about the vertex section, which we'll talk about in the transition section.
Again, the headings of the sections in this section are listed below for your reference:
1. Using Vertices ( with vertices )
2. Flexible Vertex format ( flexible vertex format )
3. Using Vertex buffers ( with vertex caching )
4. Vertex Streams ( vertex stream )
5. Vertex Shaders ( vertex shader )
6. Transformations ( transform )
7. The World Transformation ( Global Transformation )
8. The View transformation ( Perspective transform )
9. The Projection transformation ( projection transform )
10. materials and Colors ( materials and Colors )
11. clearing the Viewport ( Clear viewport )
12. Beginning and ending a scene ( start and end scenes )
13. Rendering Polygons ( render polygon )
14. presenting the scene ( show scenes )
This issue is to be talked about theprojection transformation from transformations.
Original translation:
===============================================================================
2.4.6 Transformations ( transform )
By now, you have learned how to initialize the graphics system and create vertices. If you are working with a medium-size object, such as a polygon, these vertices are likely to be defined in a local space. If so, you pass these vertices through some transformations (world, perspective, and projection transformations) to ensure that they are in the right place when you render them. Each transformation needs to construct a special matrix that represents the appropriate position (or projection) value. The following sections will show you how to construct and use these three transformations; start with the world transformation.
2.4.7 The worldtransformation ( world Transformation )
Vertices defined in the local space need to be placed in their corresponding coordinates in the world space. For example, if you use a vertex to create a box (in a local space), and you make it appear at a certain point in the world, you apply a world transformation to it (2.12).
You use your old friend, D3DX Library, to help construct the world transformation matrix. In order to place an object, you need to construct three rotation matrices (one for each axis rotation), a translation matrix, and a scaling matrix:
D3dxmatrix Matworld;d3dxmatrix Matrotx,matroty, Matrotz;d3dxmatrix Mattrans;d3dxmatrix MatScale; Create therotation Matricesd3dxmatrixrotationx (&matrotx,xangle);D 3DXMatrixRotationY (&matroty,yangle);D 3DXMatrixRotationZ (&matrotz,zangle); Create thetranslation matrixd3dxmatrixtranslation (&mattrans,xpos, YPos, Zpos); Create the scalingmatrixd3dxmatrixscaling (&matscale,xscale, Yscale, Zscale);
Next, you combine all the matrices into one world matrix. They must be combined in this order: scaling, rotation around the x-axis, rotation around the y-axis, z-axis selection, and final panning.
Set matworld toidentityd3dxmatrixidentity (&matworld); Combine allmatrices to World transformation matrixd3dxmatrixmultiply (&matworld,&matworld, &matScale) ;D 3DXMatrixMultiply (&matworld,&matworld, &matrotx);D 3DXMatrixMultiply (&matworld,&matworld, &matroty);D 3DXMatrixMultiply (&matworld,&matworld, &matrotz);D 3DXMatrixMultiply (&matworld, &matworld, &mattrans);
You're almost done with the task. Now, you just tell Direct3D to use the world transformation you just created. You do this by using the following function:
Hresultidirect3ddevice9::settransform ( d3dtransformstatetype State, //D3dts_world CONST D3dmatrix *pmatrix); World Matrix to set
Note that the second argument is a pointer to a d3dmatrix struct, but thankfully you can use the D3dxmatrix object you constructed. Set the first parameter to D3dts_world to tell Direct3D that the matrix is used for the world transformation, and that any objects drawn after this will be adjusted by this matrix.
If you want to place more than one object in the world, you only need to construct a new world transformation matrix for each object (based on its location), and then call the SetTransform function again, but make sure that the object is drawn before entering the next world transformation.
2.4.8 The viewtransformation ( Perspective transform )
In the basic terminology, the function of the perspective transformation is much like a camera (called an observation point (Viewpoint)). By constructing a matrix that contains the offsets of the vertices in your world, you can arrange the entire scene around the observation points. All vertices must is oriented (using the view transformation) around the center of the world at the same relative position In which they is located around the viewpoint. (ask the great God!) This sentence will not translate! )
To create a perspective transformation, you create a matrix from the position and rotation angle of the observer, which is used in this order: pan, rotate around the z-axis, rotate around the y-axis, and then rotate around the x-axis. However, the trick here is if you use the opposite value of position and rotation angle. For example, if the observer is located in X=10, Y=0, z=-150 place, wondering if you use x=-10, y=0, z=150.
Here is the code to construct the perspective transformation matrix:
D3dxmatrix Matview;d3dxmatrix Matrotx,matroty, Matrotz;d3dxmatrix Mattrans; Create therotation matrices (opposite values) D3dxmatrixrotationx (&matrotx,-xangle);D 3DXMatrixRotationY (& Matroty,-yangle);D 3DXMatrixRotationZ (&matrotz,-zangle); Create thetranslation matrix (opposite values) d3dxmatrixtranslation (&mattrans,-xpos,-ypos,-zpos); Set matview toidentityd3dxmatrixidentity (&matview); Combine allmatrices into view transformation matrixd3dxmatrixmultiply (&matview,&matview, &mattrans);D 3DXMatrixMultiply (&matview,&matview, &matrotz);D 3DXMatrixMultiply (&matview,&matview, & Matroty);D 3DXMatrixMultiply (&matview,&matview, &matrotx);
In order for Direct3D to use the View transformation matrix you created, use the Idirect3ddevice9::settransform function again, and this time set the state parameter to D3dts_view:
g_pd3ddevice =pre-initialized device objectif (FAILED (G_pd3ddevice->settransform (D3dts_view,&matview))) { //Error occurred}
As you can see, it's easy to set the angle of view, but the tectonic view matrix is a problem. To make things easier, D3DX provides a function that only needs to be called once to establish the view transformation matrix:
D3DXMATRIX*D3DXMATRIXLOOKATLH ( d3dxmatrix *pout,//Output view transformation matrix CONST D3dxvector3 * Peye, //coordinates of Viewpoint const D3DXVECTOR3 *PAT, //coordinates at Target Const D3DXVECTOR3 * PUP); Up direction
At first glance, the D3DXMATRIXLOOKATLH function is a very unintelligible one. You can see the typical output matrix pointers, but what are the three D3dxvector3 objects? D3DXVECTOR3 is similar to the D3dxmatrix object except that it contains only 3 values (called X, y, and Z)--in the current case, three coordinate values. This D3dxvector3 object is called a vector object .
Peye represents the coordinates of the observer point, while Pat represents the coordinates of the target being looked at by the Observer. A pup is a vector that represents the upward direction of the observer point. In general, pups can be set to 0, 1, 0 (meaning the pup is forward along the y-axis), but since the observer can tilt (just as you tilt your head around), the upward direction can point in any direction, along any axis.
To use the D3DXMATRIXLOOKATLH function, you can use the following piece of code (assuming that the observer is located in Xpos, YPos, Zpos, and looking at the origin):
D3dxmatrix Matview;d3dxvector3 VECVP,VECTP, Vecup (0.0f, 1.0f, 0.0f); vecvp.x = Xpos;vecvp.y = Ypos;vecvp.z = ZPos;vecTP.x = Vectp.y =vectp.z = 0.0F;D3DXMATRIXLOOKATLH (&MATVIEW,&VECVP, &VECTP, &vecup);
2.4.9 The Projection transformation ( projection transform )
Finally, we have the projection transform, which converts the Direct3D (not transformed) to the 2-d coordinates (transformed) that are used to draw the graph onto the display. You can think of the projection transformation as a way to "flatten" the graphics into your display (2.13).
When dealing with projection transformations, there are many things that come into our field of view, such as the aspect ratio of the viewport (aspect ratio), the field of view, and the close and far clipping clipping range Ranges).
Cutting...... What? Some objects are too close to or too far away from the observation point when drawing a Direct3D, and you let the end of the day to cut off these parts (to speed up the program). In order to construct the projection matrix and define the area that the object is visible and thus not cut off, you use the D3DXMATRIXPERSPECTIVEFOVLH function:
D3DXMATRIX*D3DXMATRIXPERSPECTIVEFOVLH ( d3dxmatrix *pout, //Output matrix FLOAT Fovy,/ / Field of view, Inradians FLOAT Aspect, //Aspect ratio float Zn, //Z-value of Nearclipping plane FLOAT ZF); Z-value of far clipping plane
Attention
===============================================================================
The D3DXMATRIXPERSPECTIVEFOVLH function uses a left-handed coordinate system to establish a perspective transformation matrix (Perspective transformation matrix). If you are using the right-handed coordinate system, then use the D3dxmatrixperspectivefovrh function (it uses the same parameters as the left-hand coordinate system version).
===============================================================================
The Fovy parameter represents the width of the projected angle of view, so the larger the number, the more things you see. However, this is a double-edged sword, because if the value of the Fovy you use is too small or too large, then the sight you see will become crooked. The typical value of Fovy is D3dx_pi/4, which is One-fourth of Pi (since pi radians are equal to degrees).
The next important parameter is the aspect, which is the transverse aspect ratio of the viewing area. If you have a 400x400 pixel window, then the horizontal ratio is 1:1, or 1.0 (because it is a square). If your window is 400x200 (twice times the width of the height), then the transverse ratio is 2:1, or 2.0. To calculate this value, divide the width of the window by the height of the window:
float Aspect = (float) windowwidth/(float) windowheight;
The Zn and ZF parameters are the values of the near and far clipping plane (clipping planes), and the units are the same as the units you use to define the-I vertices. Typical values for the near and far clipping planes are 1.0 and 1000.0, respectively. These two values represent polygons that are less than 1.0 (and greater than 1000.0) away from the observer are not drawn. You might want to set ZF to a higher value in your project, if you want to draw objects that are more than 1000.0 units long.
After you have constructed the projection matrix, you use the Idirect3ddevice9::settransform function to set the projection transformation matrix, and this time set the state parameter to D3dts_projection:
g_pd3ddevice =pre-initialized device Objectd3dxmatrix matproj; Create theprojection Transformation Matrixd3dxmatrixperspectivefovlh (&MATPROJ,D3DX_PI/4, 1.0f, 1000.0f,); Set the ProjectionMatrix with Direct3dif (FAILED (G_pd3ddevice->settransform (d3dts_projection, &matproj )) { //Error occurred}
===============================================================================
Well, the sections on the transformation are finished. The content is not much, but it may seem a bit hard for everyone. In fact, the author in the perspective of transformation and projection transformation is not very good, especially the projection transformation part, at least should be the flat-truncated head (frustum) draw out Ah! This aspect of the content I hope that you refer to the "Dragon book" the second edition of the 6th chapter, where the more detailed and clearly described.
DirectX 9.0c Game Development Journal of RPG Programming self-learning log nine--drawing with DirectX graphics (drawing with DirectX graphic) (4th) (B)