📄 xfilemanager.cpp
字号:
// XFileManager.cpp: implementation of the CXFileManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "XFileManager.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CXFileManager::CXFileManager()
{
Init();
}
CXFileManager::~CXFileManager()
{
Destroy();
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
void CXFileManager::Init()
{
m_pSysMemMesh = NULL;
m_pLocalMesh = NULL;
m_dwNumMaterials = 0L;
m_pMaterials = NULL;
m_pTextures = NULL;
m_bUseMaterials = TRUE;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
void CXFileManager::Destroy()
{
InvalidateDeviceObjects();
for( UINT i=0; i<m_dwNumMaterials; i++ )
SAFE_RELEASE( m_pTextures[i] );
SAFE_DELETE_ARRAY( m_pTextures );
SAFE_DELETE_ARRAY( m_pMaterials );
SAFE_RELEASE( m_pSysMemMesh );
m_dwNumMaterials = 0L;
}
HRESULT CXFileManager::Create( const char* szFileName )
{
LPD3DXBUFFER pAdjacencyBuffer = NULL;
LPD3DXBUFFER pMtrlBuffer = NULL;
HRESULT hr;
// Load the mesh
if( FAILED( hr = D3DXLoadMeshFromX( szFileName, D3DXMESH_SYSTEMMEM, g_pApp->GetD3dDevice(),
&pAdjacencyBuffer, &pMtrlBuffer, NULL,
&m_dwNumMaterials, &m_pSysMemMesh ) ) )
{
return hr;
}
// Optimize the mesh for performance
if( FAILED( hr = m_pSysMemMesh->OptimizeInplace(
D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
{
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return hr;
}
// Get material info for the mesh
// Get the array of materials out of the buffer
if( pMtrlBuffer && m_dwNumMaterials > 0 )
{
// Allocate memory for the materials and textures
D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
m_pMaterials = new D3DMATERIAL9[m_dwNumMaterials];
if( m_pMaterials == NULL )
{
hr = E_OUTOFMEMORY;
goto LEnd;
}
m_pTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];
if( m_pTextures == NULL )
{
hr = E_OUTOFMEMORY;
goto LEnd;
}
// Copy each material and create its texture
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
// Copy the material
m_pMaterials[i] = d3dxMtrls[i].MatD3D;
m_pTextures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
if( FAILED( D3DXCreateTextureFromFile( g_pApp->GetD3dDevice(), d3dxMtrls[i].pTextureFilename,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
hr = S_OK;
// g_pApp->GetD3dDevice()
LEnd:
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return hr;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CXFileManager::SetFVF( LPDIRECT3DDEVICE9 pd3dDevice, DWORD dwFVF )
{
LPD3DXMESH pTempSysMemMesh = NULL;
LPD3DXMESH pTempLocalMesh = NULL;
if( m_pSysMemMesh )
{
if( FAILED( m_pSysMemMesh->CloneMeshFVF( D3DXMESH_SYSTEMMEM, dwFVF,
pd3dDevice, &pTempSysMemMesh ) ) )
return E_FAIL;
}
if( m_pLocalMesh )
{
if( FAILED( m_pLocalMesh->CloneMeshFVF( 0L, dwFVF, pd3dDevice,
&pTempLocalMesh ) ) )
{
SAFE_RELEASE( pTempSysMemMesh );
return E_FAIL;
}
}
SAFE_RELEASE( m_pSysMemMesh );
SAFE_RELEASE( m_pLocalMesh );
if( pTempSysMemMesh ) m_pSysMemMesh = pTempSysMemMesh;
if( pTempLocalMesh ) m_pLocalMesh = pTempLocalMesh;
// Compute normals in case the meshes have them
if( m_pSysMemMesh )
D3DXComputeNormals( m_pSysMemMesh, NULL );
if( m_pLocalMesh )
D3DXComputeNormals( m_pLocalMesh, NULL );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CXFileManager::SetVertexDecl
// Desc: Convert the mesh to the format specified by the given vertex
// declarations.
//-----------------------------------------------------------------------------
HRESULT CXFileManager::SetVertexDecl( LPDIRECT3DDEVICE9 pd3dDevice, D3DVERTEXELEMENT9 *pDecl )
{
LPD3DXMESH pTempSysMemMesh = NULL;
LPD3DXMESH pTempLocalMesh = NULL;
if( m_pSysMemMesh )
{
if( FAILED( m_pSysMemMesh->CloneMesh( D3DXMESH_SYSTEMMEM, pDecl,
pd3dDevice, &pTempSysMemMesh ) ) )
return E_FAIL;
}
if( m_pLocalMesh )
{
if( FAILED( m_pLocalMesh->CloneMesh( 0L, pDecl, pd3dDevice,
&pTempLocalMesh ) ) )
{
SAFE_RELEASE( pTempSysMemMesh );
return E_FAIL;
}
}
SAFE_RELEASE( m_pSysMemMesh );
SAFE_RELEASE( m_pLocalMesh );
if( pTempSysMemMesh )
{
m_pSysMemMesh = pTempSysMemMesh;
// Compute normals in case the meshes have them
D3DXComputeNormals( m_pSysMemMesh, NULL );
}
if( pTempLocalMesh )
{
m_pLocalMesh = pTempLocalMesh;
// Compute normals in case the meshes have them
D3DXComputeNormals( m_pLocalMesh, NULL );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CXFileManager::RestoreDeviceObjects( LPDIRECT3DDEVICE9 pd3dDevice )
{
if( NULL == m_pSysMemMesh )
return E_FAIL;
// Make a local memory version of the mesh. Note: because we are passing in
// no flags, the default behavior is to clone into local memory.
if( FAILED( m_pSysMemMesh->CloneMeshFVF( D3DXMESH_MANAGED, m_pSysMemMesh->GetFVF(),
pd3dDevice, &m_pLocalMesh ) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CXFileManager::InvalidateDeviceObjects()
{
SAFE_RELEASE( m_pLocalMesh );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CXFileManager::Render( LPDIRECT3DDEVICE9 pd3dDevice, bool bDrawOpaqueSubsets,
bool bDrawAlphaSubsets )
{
// if( NULL == m_pLocalMesh )
// return E_FAIL;
//
// // Frist, draw the subsets without alpha
// if( bDrawOpaqueSubsets )
// {
// for( DWORD i=0; i<m_dwNumMaterials; i++ )
// {
// if( m_bUseMaterials )
// {
// if( m_pMaterials[i].Diffuse.a < 1.0f )
// continue;
// pd3dDevice->SetMaterial( &m_pMaterials[i] );
// pd3dDevice->SetTexture( 0, m_pTextures[i] );
// }
// m_pLocalMesh->DrawSubset( i );
// }
// }
//
// // Then, draw the subsets with alpha
// if( bDrawAlphaSubsets && m_bUseMaterials )
// {
// for( DWORD i=0; i<m_dwNumMaterials; i++ )
// {
// if( m_pMaterials[i].Diffuse.a == 1.0f )
// continue;
//
// // Set the material and texture
// pd3dDevice->SetMaterial( &m_pMaterials[i] );
// pd3dDevice->SetTexture( 0, m_pTextures[i] );
// m_pLocalMesh->DrawSubset( i );
// }
// }
//
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CXFileManager::Render( BOOL bWireFrame )
{
// if( NULL == m_pLocalMesh )
// return E_FAIL;
//
//
// for( DWORD i=0; i<m_dwNumMaterials; i++ )
// {
//
// g_pApp->GetD3dDevice()->SetMaterial( &m_pMaterials[i] );
// g_pApp->GetD3dDevice()->SetTexture( 0, m_pTextures[i] );
//
// m_pLocalMesh->DrawSubset( i );
// }
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -