Grid Model Intermediate 2

Source: Internet
Author: User

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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.