在DirectX3D程式中載入3DMAX的模型(尾碼為.X的檔案)

來源:互聯網
上載者:User

 要想從3DMAX中匯出設計好的模型,通常需要自己用3DS Max SDK來建立自己的匯出器, 而微軟提供了一種匯出格式.X,這種格式主要用於DirectX9, Directx10及以後都不支援這種格式。

 

第一步: 匯出模型

首先我們需要將模型從3DMAX中匯出來,這裡推薦Pandasoft  - 將下載的外掛程式放到3DS plugins目錄下。這樣你就可以在3DMAX中將模型儲存成X類型的檔案。

 

第二步:載入

我們使用D3DX的Mesh對象和介面。

 

LPD3DXBUFFER materialBuffer;
DWORD        numMaterials;           
LPD3DXMESH   mesh;

HRESULT hr=D3DXLoadMeshFromX(

                             filename,

                             D3DXMESH_SYSTEMMEM,
                             gD3dDevice,

                             NULL,
                             &materialBuffer,

                             NULL,

                             &numMaterials,
                             &mesh

                            );

D3DXMATERIAL* d3dxMaterials       = (D3DXMATERIAL*)materialBuffer->GetBufferPointer();

D3DMATERIAL9 *meshMaterials       = new D3DMATERIAL9[numMaterials];
LPDIRECT3DTEXTURE9 *meshTextures  = new LPDIRECT3DTEXTURE9[numMaterials];

 

第三步:渲染

for (DWORD i=0; i<m_numMaterials; i++)
{
  gD3dDevice->SetMaterial(&meshMaterials[i]);
  gD3dDevice->SetTexture(0,meshTextures[i]);
  m_mesh->DrawSubset( i );
}

for (DWORD i=0; i<m_numMaterials; i++)
{

     // 匯出Material
     meshMaterials[i] = d3dxMaterials[i].MatD3D;

     // 設定ambient color 

     meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
        
     // 如果有Texture存在,則建立

     meshTextures[i] = NULL;
     if (d3dxMaterials[i].pTextureFilename)
         D3DXCreateTextureFromFile(

                                   gD3dDevice,

                                   d3dxMaterials[i].pTextureFilename,    

                                   &meshTextures[i]

                                  )

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.