Author: strongcoding
Mailbox: StrongCoding@qq.comDX group: 130302441
If the text is incorrect, please let us know. Thank you very much. Come on with me.
Next, let's take a look at how to load a common. X file using the knowledge of Grid Model Intermediate 1 and just list the core code:
I. key ideas for implementation:
1. Load the. X file
2. extract materials and textures from the. X file
3. Release Material pointer
4. Mesh optimization
5. Release the adjacent information pointer.
6. Batch Rendering
2. Follow the above process to write it together (note that it is pseudo code ):
1. Load the. X file
Id3dxbuffer * adjbuffer = 0; // stores the adjacent information.
Id3dxbuffer * mtrlbuffer = 0; // used to describe the material
DWORD nummaterials = 0; // records the number of materials
Id3dxmesh * pmesh = NULL;
HR =D3dxloadmeshfromx (
"bigship1.x", D3DXMESH_VB_MANAGED, pD3DDevice, &adjBuffer, &mtrlBuffer , 0, &NumMaterials, &pMesh );
2. extract materials and textures from the. X file
If (mtrlbuffer! = 0 & nummtrls! = 0) {d3dxmaterial * mtrls = (d3dxmaterial *) mtrlbuffer-> getbufferpointer (); For (INT I = 0; I <nummtrls; I ++) {// The matd3d property doesn't have an ambient value set // when its loaded, so set it now: // when matd3d is loaded, there is no environmental value, so before loading it, you need to specify mtrls [I]. matd3d. ambient = mtrls [I]. matd3d. diffuse; // Save the ith material // Save the material information in the global variable mtrls. push_back (mtrls [I]. matd3d); // check if the ith material has an asso Ciative texture // check whether this material has a corresponding texture if (mtrls [I]. ptexturefilename! = 0) {// Yes, load the texture for the ith subset // OK, load the texture data from the subset, extract the source from the texture file, and use idirect3dtexture9 * Tex = 0; d3dxcreatetexturefromfile (device, mtrls [I]. ptexturefilename, & Tex
); // Save the loaded texturetextures. push_back (Tex);} else {// no texture for the ith subset // If texture data is not involved in this material number, place 0 in the current position textures. push_back (0 );}}}
3. Release Material pointer
ID3DXMaterial_Temp->Realease();
4. Mesh optimization
hr = Mesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT |D3DXMESHOPT_COMPACT |D3DXMESHOPT_VERTEXCACHE,(DWORD*)adjBuffer->GetBufferPointer(), 0,
0,
0
);
5. Release the adjacent information pointer.
ID3DXBuffer_Temp->Realease();
6. Batch Rendering
for(int i = 0; i < Mtrls.size(); i++){Device->SetMaterial( &Mtrls[i] );Device->SetTexture(0, Textures[i]);Mesh->DrawSubset(i);}