ZFXMeshX.cpp
资源名称:Direct3D.rar [点击查看]
上传用户:junlon
上传日期:2022-01-05
资源大小:39075k
文件大小:2k
源码类别:
DirextX编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".zfxmeshx.h"
- ///////////////////CZFXMesh class implementation////////////////////////////////////////////////////
- CZFXMesh::CZFXMesh()
- {
- m_dwMtrl = 0;
- m_pMeshMtrl = NULL;
- m_pMesh = NULL;
- m_pTexture = NULL;
- }
- CZFXMesh::~CZFXMesh()
- {
- Release();
- }
- void CZFXMesh::Release()
- {
- SAFE_DELETE( m_pMeshMtrl );
- SAFE_RELEASE( m_pMesh );
- SAFE_RELEASE( m_pTexture );
- for(DWORD i=0;i<m_dwMtrl;i++)
- SAFE_RELEASE( m_ppTextureFromMesh[i] );
- }
- HRESULT CZFXMesh::LoadMeshFromFile( LPDIRECT3DDEVICE9 pd3dDevice, LPCTSTR strFileName, bool bTextureCreateFromMesh,
- DWORD options )
- {
- HRESULT hr;
- ID3DXBuffer* pMtrlBuffer;
- if( FAILED( hr = D3DXLoadMeshFromX( strFileName, options, pd3dDevice,
- NULL, &pMtrlBuffer, NULL, &m_dwMtrl, &m_pMesh )))
- return hr;
- D3DXMATERIAL* pMtrlInBuf = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
- m_pMeshMtrl = new D3DMATERIAL9[m_dwMtrl];
- for(DWORD i=0;i<m_dwMtrl;i++)
- {
- m_pMeshMtrl[i] = pMtrlInBuf[i].MatD3D;
- }
- if( (m_bTextureCreateFromMesh = bTextureCreateFromMesh ) == true )
- {
- m_ppTextureFromMesh = new LPDIRECT3DTEXTURE9[m_dwMtrl];
- for(DWORD i=0;i<m_dwMtrl;i++)
- {
- if( NULL != pMtrlInBuf[i].pTextureFilename )
- {
- D3DXCreateTextureFromFileA( pd3dDevice, pMtrlInBuf[i].pTextureFilename, &m_ppTextureFromMesh[i] );
- }
- else
- {
- m_ppTextureFromMesh[i] = NULL;
- }
- }
- }
- return hr;
- }
- HRESULT CZFXMesh::CreateTextureFromFile( LPDIRECT3DDEVICE9 pd3dDevice, LPCTSTR strFileName )
- {
- return D3DXCreateTextureFromFile( pd3dDevice, strFileName, &m_pTexture );
- }
- void CZFXMesh::OnRender( LPDIRECT3DDEVICE9 pd3dDevice )
- {
- for(DWORD i=0;i<m_dwMtrl;i++)
- {
- if( m_bTextureCreateFromMesh )
- pd3dDevice->SetTexture( 0, m_ppTextureFromMesh[i]);
- else
- pd3dDevice->SetTexture( 0, m_pTexture );
- pd3dDevice->SetMaterial( &m_pMeshMtrl[i] );
- m_pMesh->DrawSubset(i);
- }
- }
- ///////////////////CZFXMesh class implementation End////////////////////////////////////////////////////