📄 d3dfile.cpp
字号:
//-----------------------------------------------------------------------------
// File: D3DFile.cpp
//
// Desc: Support code for loading DirectX .X files.
//
// Copyright (c) 1997-2000 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <tchar.h>
#include <stdio.h>
#include <d3d8.h>
#include <d3dx8.h>
#include <dxfile.h>
#include <rmxfguid.h>
#include <rmxftmpl.h>
#include "D3DFile.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DMesh::CD3DMesh( TCHAR* strName )
{
_tcscpy( m_strName, strName );
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( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strFilename )
{
TCHAR strPath[MAX_PATH];
CHAR strPathANSI[MAX_PATH];
LPD3DXBUFFER pMtrlBuffer = NULL;
HRESULT hr;
// Find the path for the file, and convert it to ANSI (for the D3DX API)
DXUtil_FindMediaFile( strPath, strFilename );
DXUtil_ConvertGenericStringToAnsi( strPathANSI, strPath );
// Load the mesh
if( FAILED( hr = D3DXLoadMeshFromX( strPathANSI, D3DXMESH_SYSTEMMEM,
pd3dDevice, NULL, &pMtrlBuffer,
&m_dwNumMaterials, &m_pSysMemMesh ) ) )
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 D3DMATERIAL8[m_dwNumMaterials];
m_pTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
// Copy each material and create it's texture
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
// Copy the material
m_pMaterials[i] = d3dxMtrls[i].MatD3D;
m_pMaterials[i].Ambient = m_pMaterials[i].Diffuse;
m_pTextures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
TCHAR strTexture[MAX_PATH];
CHAR strTextureANSI[MAX_PATH];
DXUtil_ConvertGenericStringToAnsi( strTextureANSI, d3dxMtrls[i].pTextureFilename );
DXUtil_FindMediaFile( strTexture, strTextureANSI );
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexture,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
SAFE_RELEASE( pMtrlBuffer );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Create( LPDIRECT3DDEVICE8 pd3dDevice,
LPDIRECTXFILEDATA pFileData )
{
LPD3DXBUFFER pMtrlBuffer = NULL;
HRESULT hr;
// Load the mesh from the DXFILEDATA object
hr = D3DXLoadMeshFromXof( pFileData, D3DXMESH_SYSTEMMEM, pd3dDevice,
NULL, &pMtrlBuffer, &m_dwNumMaterials,
&m_pSysMemMesh );
if( FAILED(hr) )
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 D3DMATERIAL8[m_dwNumMaterials];
m_pTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
// 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_pMaterials[i].Ambient = m_pMaterials[i].Diffuse;
m_pTextures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
TCHAR strTexture[MAX_PATH];
CHAR strTextureANSI[MAX_PATH];
DXUtil_ConvertGenericStringToAnsi( strTextureANSI, d3dxMtrls[i].pTextureFilename );
DXUtil_FindMediaFile( strTexture, strTextureANSI );
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexture,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
SAFE_RELEASE( pMtrlBuffer );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::SetFVF( LPDIRECT3DDEVICE8 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 );
if( m_pLocalMesh )
D3DXComputeNormals( m_pLocalMesh );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::RestoreDeviceObjects( LPDIRECT3DDEVICE8 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( LPDIRECT3DDEVICE8 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 )
{
_tcscpy( m_strName, strName );
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:
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -