📄 object3dclass.cpp
字号:
#include "Object3DClass.h"
// Constructor
Object3DClass::Object3DClass(void)
{
m_pMesh = NULL;
m_pMeshMaterials = NULL;
m_pMeshTextures = NULL;
m_pD3DXMtrlBuffer = NULL;
m_pd3dDevice = NULL;
}
// Destructor
Object3DClass::~Object3DClass(void)
{
try {
// Clean up memory
vCleanup();
}
catch( CException &ex )
{
throw ex;
}
}
//-----------------------------------------------------------------------------
// Name: Object3DClass::hLoad()
// Desc: Loads 3D data for the materials, geometry, and textures
//-----------------------------------------------------------------------------
HRESULT Object3DClass::hLoad( char *szName, LPDIRECT3DDEVICE9 p3d )
{
HRESULT hResult = S_OK;
int i, j;
char szFullPath[256];
D3DXMATERIAL* d3dxMaterials = NULL;
try {
// Store the object name for future reference
strcpy(m_szObjectName,szName);
// Set the IDirect3DDevice9 pointer to the internal one
m_pd3dDevice = p3d;
// Initialize number of materials to zero
m_dwNumMaterials = 0;
// Load Login Geometry
if( FAILED( D3DXLoadMeshFromX(
szName,
D3DXMESH_SYSTEMMEM,
m_pd3dDevice,
NULL,
&m_pD3DXMtrlBuffer,
NULL,
&m_dwNumMaterials,
&m_pMesh ) ) ) {
return(-1);
}
// Store how many faces this object uses
m_dwNumbFaces = m_pMesh->GetNumFaces();
// Set temp pointer for easier to read code
d3dxMaterials = (D3DXMATERIAL*)m_pD3DXMtrlBuffer->GetBufferPointer();
// Create the materials and textures
m_pMeshMaterials = new D3DMATERIAL9[m_dwNumMaterials];
m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials];
// Load in material information and textures
for( i = 0; i < (signed)m_dwNumMaterials ; i++ ) {
// Copy the material
m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
// Set the ambient color for the material (D3DX does not do this)
m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
if( d3dxMaterials[i].pTextureFilename != NULL ) {
//
// Parse out the texture name and remove the "garbage"
//
sprintf( szFullPath, "Data//3DObjects//%s", d3dxMaterials[i].pTextureFilename );
for( j = strlen( d3dxMaterials[i].pTextureFilename ); j >= 0; j-- ) {
if( d3dxMaterials[i].pTextureFilename[j] == 92 ) {
sprintf( szFullPath, "Data//3DObjects//%s", &d3dxMaterials[i].pTextureFilename[(j+1)] );
break;
}
}
// If the texture did not load, set it to NULL
if( FAILED( D3DXCreateTextureFromFile(
m_pd3dDevice,
szFullPath,
&m_pMeshTextures[i] ) ) ) {
m_pMeshTextures[i] = NULL;
}
}
// No texture to load, set it to NULL
else {
m_pMeshTextures[i] = NULL;
}
}
// Release the material buffer
SAFE_RELEASE(m_pD3DXMtrlBuffer);
}
catch(...) {
CException ex;
ex.vSetData("[Object3DClass::hLoad()] Failed",__LINE__);
throw ex;
}
return( hResult );
}
//-----------------------------------------------------------------------------
// Name: Object3DClass::vDisplayXYZ()
// Desc: Displays the 3D object at given cordinates and rotation
//-----------------------------------------------------------------------------
void Object3DClass::vDisplayXYZ(
float x, float y, float z,
float rx, float ry, float rz,
float sx, float sy, float sz )
{
D3DXMATRIX matWorld,matRotation,matTranslation,matScale;
int mat;
try {
// Set default translation
D3DXMatrixIdentity( &matTranslation );
// Scale
D3DXMatrixScaling( &matScale, sx, sy, sz );
D3DXMatrixMultiply( &matTranslation, &matScale, &matTranslation );
// Rotate on X
D3DXMatrixRotationX( &matRotation, rx/RADIAN_TO_DEGREES );
D3DXMatrixMultiply( &matTranslation, &matRotation, &matTranslation );
// Rotate on Y
D3DXMatrixRotationY( &matRotation, ry/RADIAN_TO_DEGREES );
D3DXMatrixMultiply( &matTranslation, &matRotation, &matTranslation );
// Rotate on Z
D3DXMatrixRotationZ( &matRotation, rz/RADIAN_TO_DEGREES );
D3DXMatrixMultiply( &matTranslation,&matRotation,&matTranslation );
// Move to X,Y,Z coordinates
matTranslation._41 = x;
matTranslation._42 = y;
matTranslation._43 = z;
// Set the matrix
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matTranslation );
// Display each material
for( mat = 0 ; mat < (signed)m_dwNumMaterials ; mat++ ) {
m_pd3dDevice->SetMaterial( &m_pMeshMaterials[mat] );
// Activate the appropriate texture
m_pd3dDevice->SetTexture( 0, m_pMeshTextures[mat] );
// Draw the mesh subset
m_pMesh->DrawSubset( mat );
// Turn off the texture
m_pd3dDevice->SetTexture( 0, NULL );
}
}
catch(...) {
CException ex;
ex.vSetData("[Object3DClass::vDisplayXYZ()] Failed",__LINE__);
throw ex;
}
}
//-----------------------------------------------------------------------------
// Name: Object3DClass::vCleanup()
// Desc: Releases all memory associated with the object
//-----------------------------------------------------------------------------
void Object3DClass::vCleanup(void)
{
try {
// Clear up materials
if( m_pMeshMaterials != NULL ) {
delete[] m_pMeshMaterials;
m_pMeshMaterials = NULL;
}
// Unload textures
if( m_pMeshTextures ) {
for( DWORD i = 0; i < m_dwNumMaterials; i++ ) {
if( m_pMeshTextures[i] )
m_pMeshTextures[i]->Release();
}
delete[] m_pMeshTextures;
m_pMeshTextures = NULL;
}
// Unload meshes
if( m_pMesh != NULL ) {
m_pMesh->Release();
m_pMesh = NULL;
}
}
catch(...) {
CException ex;
ex.vSetData("[Object3DClass::vCleanup] Failed",__LINE__);
throw ex;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -