?? d3dfile.cpp
字號:
//-----------------------------------------------------------------------------
// File: D3DFile.cpp
//
// Desc: Support code for loading DirectX .X files.
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <tchar.h>
#include <stdio.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dxfile.h>
#include <rmxfguid.h>
#include <rmxftmpl.h>
#include "D3DFile.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DMesh::CD3DMesh( TCHAR* strName )
{
_tcsncpy( m_strName, strName, sizeof(m_strName) / sizeof(TCHAR) );
m_strName[sizeof(m_strName) / sizeof(TCHAR) - 1] = _T('\0');
m_pSysMemMesh = NULL;
m_pLocalMesh = NULL;
m_dwNumMaterials = 0L;
m_pMaterials = NULL;
m_pTextures = NULL;
m_bUseMaterials = TRUE;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DMesh::~CD3DMesh()
{
Destroy();
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strFilename )
{
TCHAR strPath[MAX_PATH];
LPD3DXBUFFER pAdjacencyBuffer = NULL;
LPD3DXBUFFER pMtrlBuffer = NULL;
HRESULT hr;
// Find the path for the file, and convert it to ANSI (for the D3DX API)
DXUtil_FindMediaFileCb( strPath, sizeof(strPath), strFilename );
// Load the mesh
if( FAILED( hr = D3DXLoadMeshFromX( strPath, D3DXMESH_SYSTEMMEM, pd3dDevice,
&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 )
{
TCHAR strTexture[MAX_PATH];
TCHAR strTextureTemp[MAX_PATH];
DXUtil_ConvertAnsiStringToGenericCb( strTextureTemp, d3dxMtrls[i].pTextureFilename, sizeof(strTextureTemp) );
DXUtil_FindMediaFileCb( strTexture, sizeof(strTexture), strTextureTemp );
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexture,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
hr = S_OK;
LEnd:
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return hr;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice,
LPDIRECTXFILEDATA pFileData )
{
LPD3DXBUFFER pMtrlBuffer = NULL;
LPD3DXBUFFER pAdjacencyBuffer = NULL;
HRESULT hr;
// Load the mesh from the DXFILEDATA object
if( FAILED( hr = D3DXLoadMeshFromXof( pFileData, D3DXMESH_SYSTEMMEM, pd3dDevice,
&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 )
{
TCHAR strTexture[MAX_PATH];
TCHAR strTextureTemp[MAX_PATH];
DXUtil_ConvertAnsiStringToGenericCb( strTextureTemp, d3dxMtrls[i].pTextureFilename, sizeof(strTextureTemp) );
DXUtil_FindMediaFileCb( strTexture, sizeof(strTexture), strTextureTemp );
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexture,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
hr = S_OK;
LEnd:
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return hr;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::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:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::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( 0L, m_pSysMemMesh->GetFVF(),
pd3dDevice, &m_pLocalMesh ) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::InvalidateDeviceObjects()
{
SAFE_RELEASE( m_pLocalMesh );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::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;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::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:
//-----------------------------------------------------------------------------
CD3DFrame::CD3DFrame( TCHAR* strName )
{
_tcsncpy( m_strName, strName, sizeof(m_strName) / sizeof(TCHAR) );
m_strName[sizeof(m_strName) / sizeof(TCHAR) - 1] = _T('\0');
D3DXMatrixIdentity( &m_mat );
m_pMesh = NULL;
m_pChild = NULL;
m_pNext = NULL;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DFrame::~CD3DFrame()
{
SAFE_DELETE( m_pChild );
SAFE_DELETE( m_pNext );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
bool CD3DFrame::EnumMeshes( bool (*EnumMeshCB)(CD3DMesh*,void*),
void* pContext )
{
if( m_pMesh )
EnumMeshCB( m_pMesh, pContext );
if( m_pChild )
m_pChild->EnumMeshes( EnumMeshCB, pContext );
if( m_pNext )
m_pNext->EnumMeshes( EnumMeshCB, pContext );
return TRUE;
}
//-----------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -