📄 x_model.cpp
字号:
#include "x_model.h"
#include "math.h"
//-----------------------------------------------------------------------------
// Name: CalcFileObjectSizeCB()
// Desc: Callback used to calculate the radius of a sphere that encloses all
// the meshes in the file.
//-----------------------------------------------------------------------------
BOOL CalcFileObjectSizeCB( CD3DFileObject* pObject, D3DMATRIX* pmat,
VOID* pContext )
{
FLOAT* pfRadius = (FLOAT*)pContext;
D3DVERTEX* pVertices;
DWORD dwNumVertices;
if( SUCCEEDED( pObject->GetMeshGeometry( &pVertices, &dwNumVertices,
NULL, NULL ) ) )
{
for( DWORD i=0; i<dwNumVertices; i++ )
{
FLOAT x = pVertices[i].x;
FLOAT y = pVertices[i].y;
FLOAT z = pVertices[i].z;
FLOAT mx = x*pmat->_11 + y*pmat->_21 + z*pmat->_31 + pmat->_41;
FLOAT my = x*pmat->_12 + y*pmat->_22 + z*pmat->_32 + pmat->_42;
FLOAT mz = x*pmat->_13 + y*pmat->_23 + z*pmat->_33 + pmat->_43;
// Store the largest r (radius) for any point in the mesh
FLOAT r = sqrtf( mx*mx + my*my + mz*mz );
if( r > (*pfRadius) )
(*pfRadius) = r;
}
}
// Keep enumerating file objects
return FALSE;
}
X_MODEL::X_MODEL()
{
m_pObj = NULL;
m_fObjRadius = 0.0f;
m_fEyeDistance = 0.0f;
m_bCull = FALSE;
m_bFlat = FALSE;
m_bWire = FALSE;
m_fInitYaw = 0;
m_fInitPitch = 0;
m_fInitRoll = 0;
m_fYaw = 0;
m_fPitch = 0;
m_fRoll = 0;
m_fScaleX = 0;
m_fScaleY = 0;
m_fScaleZ = 0;
m_Position = D3DXVECTOR3( 0.0f , 0.0f , 0.0f);
m_boInitFlag = FALSE;
}
HRESULT X_MODEL::LoadXFile(char *strFileName)
{
CD3DFile *pObject = new CD3DFile();
if( FAILED( pObject->Load( strFileName ) ) )
{
MessageBox( NULL, TEXT("Error loading specified X file"),
TEXT("Runner"), MB_OK|MB_ICONERROR );
return E_FAIL;
}
SAFE_DELETE(m_pObj);
m_pObj = pObject;
m_fObjRadius = 0.0f;
m_pObj->EnumObjects( CalcFileObjectSizeCB , NULL , (void*)&m_fObjRadius );
return S_OK;
}
VOID X_MODEL::SetOwnView()
{
D3DVECTOR vEyePt = D3DVECTOR( 0.0f, 0.0f, 0.0f );
D3DVECTOR vLookatPt = D3DVECTOR( 0.0f, 0.0f, 0.0f );
D3DVECTOR vUpVec = D3DVECTOR( 0.0f, 1.0f, 0.0f );
vEyePt.z += m_fObjRadius * 3.0f + m_fObjRadius * m_fEyeDistance;
// Set the view matrix
D3DMATRIX matView;
D3DUtil_SetViewMatrix( matView, vEyePt, vLookatPt, vUpVec );
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_VIEW, &matView );
D3DVIEWPORT7 vp;
g_pd3dDevice->GetViewport(&vp);
float fAspect = ((FLOAT)vp.dwHeight) / vp.dwWidth;
D3DMATRIX matProj;
D3DUtil_SetProjectionMatrix( matProj, g_PI / 4, fAspect, 1.0f , m_fObjRadius * 10.0f );
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_PROJECTION, &matProj );
}
VOID X_MODEL::SetupMatrices()
{
D3DXMATRIX mat;
if(m_boInitFlag)
{
D3DXMATRIX scale;
D3DXMatrixScaling(&scale , m_fScaleX , m_fScaleY , m_fScaleZ);
D3DXMATRIX T;
D3DXMATRIX R;
D3DXMatrixRotationYawPitchRoll(&R , D3DXToRadian(m_fInitYaw) , D3DXToRadian(m_fInitPitch) , D3DXToRadian(m_fInitRoll));
D3DXMATRIX matYaw;
D3DXMatrixRotationYawPitchRoll(&matYaw , D3DXToRadian(m_fYaw) , 0.0f , 0.0f);
D3DXMATRIX matPitch;
D3DXMatrixRotationYawPitchRoll(&matPitch , 0.0f , D3DXToRadian(m_fPitch) , 0.0f);
D3DXMATRIX matRoll;
D3DXMatrixRotationYawPitchRoll(&matRoll , 0.0f , 0.0f , D3DXToRadian(m_fRoll));
D3DXMatrixTranslation(&T , m_Position.x , m_Position.y , m_Position.z);
mat = scale * R * matYaw * matPitch * matRoll * T;
}
else
{
D3DXMatrixIdentity(&mat);
}
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_WORLD , mat );
}
VOID X_MODEL::MoveRelative(D3DVECTOR &Dir)
{
D3DXMATRIX R;
D3DXMatrixRotationYawPitchRoll(&R , D3DXToRadian(m_fInitYaw) , D3DXToRadian(m_fInitPitch) , D3DXToRadian(m_fInitRoll));
D3DXMATRIX matYaw;
D3DXMatrixRotationYawPitchRoll(&matYaw , D3DXToRadian(m_fYaw) , 0.0f , 0.0f);
D3DXMATRIX matPitch;
D3DXMatrixRotationYawPitchRoll(&matPitch , 0.0f , D3DXToRadian(m_fPitch) , 0.0f);
D3DXMATRIX matRoll;
D3DXMatrixRotationYawPitchRoll(&matRoll , 0.0f , 0.0f , D3DXToRadian(m_fRoll));
R = R * matYaw * matPitch * matRoll;
D3DXVec3TransformCoord((D3DXVECTOR3*)&Dir , (D3DXVECTOR3*)&Dir , &R);
m_Position+=Dir;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -