D3D is a very cumbersome tool, although Microsoft has simplified as much as possible, but in order to achieve an effect, you still have to do a lot of work, including the assignment of a variety of complex structures; these assignments are important, it Directly related to the final effect of the program.
This part is a summary of my beginner D3D.
First, get a D3D device:
LPDIRECT3DDEVICE9 PD3DDEVICE;
LPDIRECT3D9 PD3D = Direct3dcreate9 (D3D_SDK_Version);
D3DPresent_Parameters D3DPP;
ZeromeMory (& D3DPP, SIZEOF (D3DPP));
D3DPP.Windowed = true;
D3dpp.swapeffect = D3DSWAPEFFECT_DISCARD;
D3dpp.backbufferformat = D3DFMT_UNKNOWN;
PD3D-> CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, HWND,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
& D3DPP, & PD3DDEVICE);
PD3DDEvice is the D3D device we need. This device is written for a window application. If you want a full-screen D3D program, you must change the D3DPP.Windowed This property is set to false. As for the meaning of other parameters, after you still install DXSDK, read it carefully in the content of the help document, which is the most authoritative information, and exercise your English reading, the level 4 has not learned English at the same time. And programming, one fell in two, but first, we will admire one sentence, calm, do not try to destroy the display. The basically the default parameters used in the examples above, it is enough to build your first D3D program.
Second, vertex information:
About 3D knowledge I want everyone to have some, in many games, the reaction is quite obvious; for example, CS is a very good FPS game, it is also very good in 3D processing, but we can still see the characters. The corner of the corner; Daxu's "Xuanyuan Sword 4" is also like this. This is the true face - triangle of 3D graphics. A 3D object is composed of n polygons (in fact the triangle), the more the number of polygons, the more smooth objects, the more rough, the more rough. However, more polygons will result in, the amount of calculation is increased, and the performance is lowered. This requires us to better balance the relationship between quality and quality. I personally prefer optimization of performance, like people in WOW, in fact, its number of polygon is not high, on the one hand to meet performance requirements, on the one hand, by some light and shadow effects and algorithms to compensate for quality decline. It seems to be far ....
A triangle is composed of three vertices in 3D space. Due to the 3D space, there is also a point, line and other elements. So the vertex can be considered the most basic element in D3D. In D3D, the information contained in the vertex is much more; but not all information is used each time. So, we generally customize the vertex structure in the program. In a program, there can be many vertex structures.
Struct Customvertex
{
Float X, Y, Z;
Dowrd color;
}
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
Struct CustomvertEx1
{
D3DXVECTOR3 POSITION;
Float Tu, TV;
}
#define D3DFVF_CUSTOMVERTEX1 (D3DFVF_XYZ | D3DFVF_TEX1)
Struct CustomvertEX2
{
D3DXVECTOR3 POSITION;
D3DXVECTOR3 NORMAL;
}
#define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ | D3DFVF_NORMAL)
Several vertex structures defined above can also be combined together to define a new vertex structure. It should be noted that each custom vertex structure should have a D3DFVF corresponding to the corresponding, and the vertex structure will be described. Otherwise, D3D will not know what the vertex you define is meaning. D3DFVF_XYZ represents the coordinates of the vertex, D3DFVF_DIFFUSE represents the diffuse color of the vertex, D3DFVF_TEX1 represents the map coordinates of the vertex, and D3DFVF_NORMAL represents the normal vector of the vertex. Position is a must, even the coordinates are not known to us. Color stores the color information of the vertex, represented by a 32 bit of value, just like 0xffff0000 represents opaque red.
Third, basic graphics (primitive):
In the drawing software, there is a basic graphic; D3D also has basic graphics, which only need to provide vertex information, and can be quickly plotted by hardware through hardware. Including a Point List, a LIST STRIP, Triangle List, Triangle Strip, Triangle Fan.
Example: Description If you draw 6 separate vertices.
CustomvertEX Vertices [] =
{
{-5.0, -5.0, 0.0},
{0.0, 5.0, 0.0},
{5.0, -5.0, 0.0},
{10.0, 5.0, 0.0},
{15.0, -5.0, 0.0},
{20.0, 5.0, 0.0}
}
PD3DDEVICE-> DrawPrimitive (D3DPT_PointList, 0, 6);
There is a specific description of each element in the help document.
Fourth, Matrix:
Hahaha, finally waited for this chapter. In fact, I have already started can't wait for a chapter. Matrix is always then Cool! "Hi, Neo. Wake Up ..." is in a beautiful memory, take the DVD to take it over again.
OK, I am back. The matrix is an integral part of D3D. If you want to say "nonsense, which is not important!" So, it is the most important part. Without matrix, your object has no space for survival, is it important? In addition, the matrix is not very understanding for the new contact D3D, of course, if your line is very good, I certainly don't say, I have spit it out of this year.
Do you still remember the left hand coordinate system? I have already forgotten my graduation, I will read it. Here is just telling you that the left-hand coordinate system for the computer screen, the positive direction of the X-axis is right, the positive direction of the Y-axis is on the positive direction of the z-axis is to point to the screen.
There are 3 matrices that need to be set in D3D.
I, World Matrix (WORLD MATRIX)
The location offset of the world matrix control object in the 3D world, the deflection angle. Here is to introduce a new concept, viewport, viewport (translation seems to be very soil, but everyone translated). Later, discuss it, first tell you that the size of the viewport is equal to your program form size. The origin of the world matrix (0.0, 0.0, 0.0) is the center of the viewport. Setting a world matrix for an object to specify its location and deflection in 3D space. D3DXMAMATRIX MATWORLD;
D3DXMAMATRIXIDENTITY (& MATWORL);
PD3DDEVICE-> SetTransform (D3DTS_WORLD, & MATWORLD);
The position of the selected object in the 3D space is the origin, and there is no deflection. In order to set the offset and rotation of the object, we will define two auxiliary matrices.
D3DXMATRIX Matrotate; // rotation
D3DXMATRIXIDENTITY (& Matrotate);
D3DXMATRIXROTATIONAXIS (& Matrotate, Axisx, Axisy, Fangle); // Turn a certain angle in a direction
D3DXMatrixmultiply (& MatWorld, & Matworld, & Matrotate);
D3DXMAMATRIX MATTRANS; // Translation
D3DXMAMATRIXIDENTITY (& Mattrans);
D3DXMatrixTranslation (& Mattrans, fposx, fposy, fposz);
D3DXMatrixmultiply (& MatWorld, & MatWorld, & Mattrans);
The MatWorld obtained by multiplying above the above two matrices is the world matrix of the object of the offset.
II, observation matrix (View Matrix)
The observation matrix is clear that what is the place, where is it, which direction is above. Very simple. In D3D, everyone should be used to think and calculate with vector.
D3DVector3 VEYEPT (fposx, fposy, fposz);
D3DVector3 VLOOKATPT (fposx, fposy, fposz);
D3DVector3 Vupvec (fx, fy, fz);
D3DXMAMATRIX MATVIEW;
D3DXMAMATRIXIDENTITY (& Matview);
D3DXMatrixlookatlh (& Matview, & VeyEPT, & VLOOKATPT, & VUPVEC);
PD3DDEVICE-> SetTransform (D3DTS_VIEW, & MATVIEW);
It seems that there is nothing to say about this, and the earth people know.
Last night, take a shower, sleep, tomorrow is writing.