DirectX Graphical Interface Guide (7)

zhaozj2021-02-08  216

Guide 6: Using the Mesh Model

Complex geometry often uses a 3-D modeling software to construct a model and saved as a file. An example is the .x file format. Microsoft Direct3D uses the Mesh object to load these objects from the file. The Mesh object is slightly complicated, but the function containing the Microsoft Direct3DX enables the application Mesh object to become simple. The Meshed routine introduces the topic of Mesh and showing how to load, demonstrate, and uninstall a Mesh object.

This guide uses the following steps to explain how to load, demonstrate, and uninstall a Mesh object:

· Step 1: Load a Mesh object

· Step 2: Demonstrate a Mesh object

· Step 3: Uninstall a Mesh object

Note: The path to the Methes sample program is:

(SDK root) / Samples / MultiMedia / Direct3D / Tutorials / Tut06_Meshes.

Note: Example code for this project is almost exactly the same as the Lights project in addition to the sample code in the Meshes project. The "Using the Mesh Model" guide is only focused on the unique code of the Mesh object, and does not repeat the work of setting Microsoft Direct3D, processing Microsoft Windows messages, demonstrations, or cleaning. For information on these work, see: Guide 1: Create a device.

This guide uses a custom vertex and a vertex buffer to display geometric objects. For more information on selecting a custom vertex format and performs vertex buffering, see: Guide 2: Demo the vertices.

This guide uses a matrix to geometric transform. For more information on matrices and transformations, see: Guide 3: Use matrices.

This guide uses texture to cover the surface of the MESH model. For more information on loading and using textures, see: Guide 5: Use texture mapping.

Step 1: Load a Mesh object

Before using, the Microsoft Direct3D application must first load a Mesh object. The Meshes routine loads a tiger's Mesh model by calling InIgEMetry, a program-defined function, of course, after the required Direct3D object already loaded.

A Mesh object requires a material buffer to save all materials and textures that will be used. So this function initially defines a material buffer, as shown by the following code segment:

LPD3DXBUFFER PD3DXMTRLBUFFER;

The following code segments use the D3DxLoadMethFromx function to load the Mesh object.

// load the mesh from the specified file.

IF (Failed (D3DXLoadMeshfromx ("Tiger.x", D3DXMESH_SYSTEMMEM,

g_pd3ddevice, null,

& pd3dxmtrlBuffer, & g_dwnummaterials,

& g_pmesh)))))))))))))))))))

Return E_FAIL;

The first parameter accepted by D3DXLoadMeshFromx is a pointer to string tells Microsoft Direct3D to load files. This routine reads a tiger's MESH model from Tiger.x.

The second parameter notifies how Direct3D creates a Mesh object. This example uses a D3DXMESH_SYSTEMMEM tag, which is equal to specifying D3DxMesh_vb_systemmem with d3dxmesh_ib_systemmem, which tells Direct3D to put the index buffer of the Mesh object in the system memory.

The third parameter is a pointer to the Direct 3D device that will be used to draw a Mesh object.

The fourth parameter is a pointer to the id3Dxbuffer object. This object is loaded with information about the neighborhoods adjacent to each side. This information is not required in this routine, so this parameter is set to NULL. The fifth parameter also achieves a pointer to id3dxbuffer. After the function is executed, this object will be filled in the D3DXMATERIAL structure used by the Mesh object.

The sixth parameter is a pointer. After the function is executed, the number of D3DXMATERIAL structures placed in the PPMATERIALS queue is returned.

The seventh parameter is an address of a Mesh object pointer, returns the loaded Mesh object.

After loading this Mesh object and related material information, you need to decompose material properties and texture names from the material buffer.

This MESH routine first needs to get the material buffer pointer to handle these things. The following code segment uses the ID3DXBuffer :: getBufferPointer function to get this pointer.

D3DXMATERIAL * D3DXMATERIALS =

(D3DXMATERIAL *) PD3DXMTRLBUFFER-> getBufferPointer ();

The following code segment creates a new MESH and texture objects based on the maximum number of materials in the Mesh object.

g_pmeshmaterials = new d3dmaterial8 [g_dwnummaterials];

g_pmeshtextures = new lpdirect3dtexture8 [g_dwnummaterials];

The following steps must be performed for the materials in each MESH object.

The first step is to copy material, as shown in the following code segment.

g_pmeshmaterials [i] = d3dxmares [i] .matd3d;

The second step is to set the environment of the material value, see the following code segment.

g_pmeshmaterials [i] .Ambient = g_pmeshmaterials [i] .diffuse;

The final step is to create textures for the material, as the code segment below.

// CREATE The texture.

IF (Failed (D3DxCreateTextureFromfile (g_pd3ddevice,

D3DXMATERIALS [i] .ptextureFileName,

& g_pmeshtextures [i]))))))

g_pmeshtextures [i] = null;

}

After loading each material, you have finished using this material buffer, you must call iUnknown :: release to release it.

PD3DXMTRLBUFFER-> Release ();

Now, Mesh objects, together with their corresponding materials and textures have been loaded. This Mesh object is ready to deliver to the screen, see the second step: demonstrate a Mesh object.

Step 2: Demonstrate a Mesh object

In the first step, the Mesh object has been presented to be presented. This object is divided into several subsets per material loaded by the Mesh object. To draw each subset, this Mesh object should be drawn in a loop. The first step of the loop is to set the material for each subset, as shown in the following code segment:

g_pd3ddevice-> setmaaterial (& g_pmeshmaterials [i]);

The second step of the loop is to set the texture for each subset, as shown in the following code segment.

g_pd3ddevice-> settexture (0, g_pmeshtextures [i]);

After setting the material quality and texture, the subset is drawn by the ID3DXBasemesh :: DrawSubset function, as shown below.

g_pmesh-> drawsubset (i);

DrawSubset functions have a DWORD parameter to specify which subset of the Mesh object is drawn. This routine is added to each of the values ​​of this parameter. After using the Mesh object, important things are to completely remove this Mesh object out of memory, see Step 3: Uninstall a Mesh object.

Step 3: Uninstall a Mesh object

Before any Microsoft Direct3D program, it is necessary to decise all DirectX objects it use and make the pointer to them. The MESH object used in this routine also needs to be deconsive. When it receives a WM_DESTROY message, the Meshes routine calls Cleanup, a user-defined function to handle this matter.

The following code segment deletes the material queue.

IF (g_pmeshmaterials)

delete [] g_pmeshmaterials;

The following code deconstruct each loaded separate texture and delete the texture queue.

IF (g_pmeshtextures)

{

For (DWORD I = 0; i

{

IF (g_pmeshtextures [i])

g_pmeshtextures [i] -> Release ();

}

delete [] g_pmeshtextures;

The following code segment deconstructs the Mesh object.

Delete the mesh object

IF (g_pmesh)

g_pmesh-> release ();

This guide has explained how to load and draw a Mesh object. This is the last guide of this area. If you need to know how a typical Direc3D application is written, see: "SDK: DirectX Graphics C / C Samples".

(DirectX Graphical Interface Guide (1) ~ (7) The full text is pasted, DATA 2002. 8. 1)

转载请注明原文地址:https://www.9cbs.com/read-2862.html

New Post(0)