头文件:
-
-
-
- #pragma once
- #include <d3dx9.h>
- #pragma comment (lib, "d3d9.lib")
- #pragma comment (lib, "d3dx9.lib")
- enum RENDERTYPE
- {
- RT_INDEX = 0,
- RT_ANIME = 1,
- };
- class D3DMesh
- {
- public:
- D3DMesh(LPDIRECT3DDEVICE9 _pd3dDevice);
- ~D3DMesh(void);
-
- public:
- HRESULT loadMesh(LPCTSTR _pSrcFile);
- VOID drawMesh(INT _mode, INT _parameter);
- VOID setPosition(INT _index, FLOAT _x, FLOAT _y, FLOAT _z);
- VOID setZoom(INT _index, FLOAT _zoomX, FLOAT _zoomY, FLOAT _zoomZ);
- VOID setRotation(INT _index, FLOAT _rotationX, FLOAT _rotationY, FLOAT _rotationZ);
- VOID move(INT _index, FLOAT _x, FLOAT _y, FLOAT _z);
-
- private:
- VOID removePathFromFileName(LPSTR fullPath, LPWSTR fileName);
- VOID autoExpand();
- VOID drawIndex(INT _parameter);
- VOID drawAnime(INT _parameter);
- VOID setTransForm(INT _mode);
-
- private:
- LPDIRECT3DDEVICE9 m_pd3dDevice;
- struct MESH
- {
- LPD3DXMESH pMesh;
- D3DMATERIAL9* pMeshMaterials;
- LPDIRECT3DTEXTURE9* pMeshTextures;
- DWORD dwNumMaterials;
- D3DXVECTOR3 vCenter;
- FLOAT fX;
- FLOAT fY;
- FLOAT fZ;
- FLOAT fZoomX;
- FLOAT fZoomY;
- FLOAT fZoomZ;
- FLOAT fRotationX;
- FLOAT fRotationY;
- FLOAT fRotationZ;
- D3DXMATRIX matTranslate;
- D3DXMATRIX matScaling;
- D3DXMATRIX matRotationZ;
- D3DXMATRIX matRotationX;
- D3DXMATRIX matRotationY;
- };
- MESH *m_mesh;
- MESH *m_meshTemp;
- INT m_iMaxIndex;
- INT m_iCurrentIndex;
- INT m_iDelay;
- #define ZOOM (1.0f/100.0f)
- }
源代码:
-
-
-
- #include "D3DMesh.h"
-
-
-
- D3DMesh::D3DMesh(LPDIRECT3DDEVICE9 _pd3dDevice)
- {
- m_pd3dDevice = _pd3dDevice;
- m_iMaxIndex = 0;
- m_iCurrentIndex = 0;
- m_iDelay = 0;
- m_mesh = new MESH[m_iMaxIndex+1];
- ZeroMemory(m_mesh,(m_iMaxIndex+1)*sizeof(MESH));
- m_mesh[0].fZoomX = 1.0f;
- m_mesh[0].fZoomY = 1.0f;
- m_mesh[0].fZoomZ = 1.0f;
-
- D3DXMatrixTranslation(&m_mesh[0].matTranslate, 0, 0, 0);
- D3DXMatrixScaling(&m_mesh[0].matScaling, 1.0f,1.0f, 1.0f);
- D3DXMatrixRotationZ(&m_mesh[0].matRotationZ, 0);
- D3DXMatrixRotationX(&m_mesh[0].matRotationX, 0);
- D3DXMatrixRotationY(&m_mesh[0].matRotationY, 0);
- }
- D3DMesh::~D3DMesh(void)
- {
- }
-
-
-
-
- VOID D3DMesh::autoExpand()
- {
- m_meshTemp = new MESH[m_iMaxIndex+2];
- ZeroMemory(m_meshTemp,(m_iMaxIndex+2)*sizeof(MESH));
- m_meshTemp[m_iMaxIndex+1].fZoomX = 1.0f;
- m_meshTemp[m_iMaxIndex+1].fZoomY = 1.0f;
- m_meshTemp[m_iMaxIndex+1].fZoomZ = 1.0f;
-
- D3DXMatrixTranslation(&m_meshTemp[m_iMaxIndex+1].matTranslate, 0, 0, 0);
- D3DXMatrixScaling(&m_meshTemp[m_iMaxIndex+1].matScaling, 1.0f,1.0f, 1.0f);
- D3DXMatrixRotationZ(&m_meshTemp[m_iMaxIndex+1].matRotationZ, 0);
- D3DXMatrixRotationX(&m_meshTemp[m_iMaxIndex+1].matRotationX, 0);
- D3DXMatrixRotationY(&m_meshTemp[m_iMaxIndex+1].matRotationY, 0);
- memcpy(m_meshTemp,m_mesh, (m_iMaxIndex+1)*sizeof(MESH));
- delete m_mesh;
- m_mesh = m_meshTemp;
- m_meshTemp = NULL;
- }
-
-
-
-
- HRESULT D3DMesh::loadMesh(LPCTSTR _pSrcFile)
- {
-
- autoExpand();
-
- LPD3DXBUFFER pD3DXMtrlBuffer;
-
- if( FAILED( D3DXLoadMeshFromX( _pSrcFile, D3DXMESH_MANAGED,
- m_pd3dDevice, NULL,
- &pD3DXMtrlBuffer, NULL, &m_mesh[m_iMaxIndex].dwNumMaterials,
- &m_mesh[m_iMaxIndex].pMesh ) ) )
- {
- MessageBox(NULL, _pSrcFile, TEXT("以下模型加载失败"), MB_OK);
- return E_FAIL;
- }
-
- D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
- m_mesh[m_iMaxIndex].pMeshMaterials =
- new D3DMATERIAL9[m_mesh[m_iMaxIndex].dwNumMaterials];
- if( m_mesh[m_iMaxIndex].pMeshMaterials == NULL )
- return E_FAIL;
- m_mesh[m_iMaxIndex].pMeshTextures =
- new LPDIRECT3DTEXTURE9[m_mesh[m_iMaxIndex].dwNumMaterials];
- if( m_mesh[m_iMaxIndex].pMeshTextures == NULL )
- return E_FAIL;
-
- for( DWORD i=0; i<m_mesh[m_iMaxIndex].dwNumMaterials; i++ )
- {
-
- m_mesh[m_iMaxIndex].pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
-
- m_mesh[m_iMaxIndex].pMeshMaterials[i].Ambient =
- m_mesh[m_iMaxIndex].pMeshMaterials[i].Diffuse;
- m_mesh[m_iMaxIndex].pMeshTextures[i] = NULL;
- if( d3dxMaterials[i].pTextureFilename != NULL &&
- strlen(d3dxMaterials[i].pTextureFilename) > 0 )
- {
-
- WCHAR filename[256];
- removePathFromFileName(d3dxMaterials[i].pTextureFilename, filename);
- if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, filename,
- &m_mesh[m_iMaxIndex].pMeshTextures[i] ) ) )
- {
- MessageBox(NULL, TEXT("纹理加载失败"), TEXT("失败"), MB_OK);
- }
- }
- }
-
- pD3DXMtrlBuffer->Release();
- m_iMaxIndex++;
- return S_OK;
- }
-
-
-
-
- VOID D3DMesh::removePathFromFileName(LPSTR fullPath, LPWSTR fileName)
- {
-
- WCHAR wszBuf[MAX_PATH];
- MultiByteToWideChar( CP_ACP, 0, fullPath, -1, wszBuf, MAX_PATH );
- wszBuf[MAX_PATH-1] = L'\0';
- WCHAR* wszFullPath = wszBuf;
-
- LPCTSTR pch = wcsrchr(wszBuf,'\\');
- if (pch)
- {
- lstrcpy(fileName, TEXT("Mesh\\"));
- lstrcat(fileName, ++pch);
- }
- else
- {
- lstrcpy(fileName, TEXT("Mesh\\"));
- lstrcat(fileName, wszFullPath);
- }
- }
-
-
-
-
- VOID D3DMesh::setPosition(INT _index, FLOAT _x, FLOAT _y, FLOAT _z)
- {
- m_mesh[_index].fX = _x;
- m_mesh[_index].fY = _y;
- m_mesh[_index].fZ = _z;
- D3DXMatrixTranslation(&m_mesh[_index].matTranslate,
- m_mesh[_index].fX, m_mesh[_index].fY, m_mesh[_index].fZ);
- }
-
-
-
-
- VOID D3DMesh::move(INT _index, FLOAT _x, FLOAT _y, FLOAT _z)
- {
- m_mesh[_index].fX += _x;
- m_mesh[_index].fY += _y;
- m_mesh[_index].fZ += _z;
- D3DXMatrixTranslation(&m_mesh[_index].matTranslate,
- m_mesh[_index].fX, m_mesh[_index].fY, m_mesh[_index].fZ);
- }
-
-
-
-
- VOID D3DMesh::setZoom(INT _index, FLOAT _zoomX, FLOAT _zoomY, FLOAT _zoomZ)
- {
- m_mesh[_index].fZoomX = _zoomX;
- m_mesh[_index].fZoomY = _zoomY;
- m_mesh[_index].fZoomZ = _zoomZ;
- D3DXMatrixScaling(&m_mesh[_index].matScaling,
- m_mesh[_index].fZoomX, m_mesh[_index].fZoomY, m_mesh[_index].fZoomZ);
- }
-
-
-
-
- VOID D3DMesh::setRotation(INT _index, FLOAT _rotationX, FLOAT _rotationY,
- FLOAT _rotationZ)
- {
- m_mesh[_index].fRotationX = _rotationX;
- m_mesh[_index].fRotationY = _rotationY;
- m_mesh[_index].fRotationZ = _rotationZ;
- _rotationX = _rotationX * D3DX_PI /180.0f;
- _rotationY = _rotationY * D3DX_PI /180.0f;
- _rotationZ = _rotationZ * D3DX_PI /180.0f;
- D3DXMatrixRotationX(&m_mesh[_index].matRotationX, _rotationX);
- D3DXMatrixRotationY(&m_mesh[_index].matRotationY, _rotationY);
- D3DXMatrixRotationZ(&m_mesh[_index].matRotationZ, _rotationZ);
- }
-
-
-
-
- VOID D3DMesh::setTransForm(INT _mode)
- {
- D3DXMATRIX matWorld;
- if( _mode < 0 )
- {
- D3DXMATRIX matTranslate;
- D3DXMATRIX matScla;
- D3DXMATRIX matRotationZ;
- D3DXMATRIX matRotationX;
- D3DXMATRIX matRotationY;
-
- D3DXMatrixScaling(&matScla, ZOOM, ZOOM, ZOOM);
- D3DXMatrixTranslation(&matTranslate, 0, 0, 0);
- D3DXMatrixRotationZ(&matRotationZ, 0);
- D3DXMatrixRotationX(&matRotationX, 0);
- D3DXMatrixRotationY(&matRotationY, 0);
-
- D3DXMatrixMultiply(&matWorld, &matScla, &matRotationZ);
- D3DXMatrixMultiply(&matWorld, &matWorld, &matRotationX);
- D3DXMatrixMultiply(&matWorld, &matWorld, &matRotationY);
- D3DXMatrixMultiply(&matWorld, &matWorld, &matTranslate);
- }
- else
- {
- D3DXMATRIX matScla;
- D3DXMatrixScaling(&matScla, ZOOM, ZOOM, ZOOM);
- D3DXMatrixMultiply(&matWorld, &m_mesh[_mode].matScaling,
- &m_mesh[_mode].matRotationX);
- D3DXMatrixMultiply(&matWorld, &matWorld, &m_mesh[_mode].matRotationY);
- D3DXMatrixMultiply(&matWorld, &matWorld, &m_mesh[_mode].matRotationZ);
- D3DXMatrixMultiply(&matWorld, &matWorld, &m_mesh[_mode].matTranslate);
- D3DXMatrixMultiply(&matWorld, &matWorld, &matScla);
- }
- m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
- }
-
-
-
-
- VOID D3DMesh::drawMesh(INT _mode, INT _parameter)
- {
- switch(_mode)
- {
- case RT_INDEX:
- drawIndex(_parameter);
- break;
- case RT_ANIME:
- drawAnime(_parameter);
- break;
- }
- }
-
-
-
-
- VOID D3DMesh::drawIndex(INT _index)
- {
- for( DWORD i=0; i<m_mesh[_index].dwNumMaterials; i++ )
- {
-
- m_pd3dDevice->SetMaterial( &m_mesh[_index].pMeshMaterials[i] );
- m_pd3dDevice->SetTexture( 0, m_mesh[_index].pMeshTextures[i] );
-
- setTransForm(_index);
- m_mesh[_index].pMesh->DrawSubset( i );
- }
- }
-
-
-
-
- VOID D3DMesh::drawAnime(INT _delay)
- {
- if (m_iDelay < _delay)
- m_iDelay++;
- else
- {
- if (m_iCurrentIndex < m_iMaxIndex-1)
- {
- m_iCurrentIndex++;
- }
- else
- {
- m_iCurrentIndex = 0;
- }
- m_iDelay = 0;
- }
- for( DWORD i=0; i<m_mesh[m_iCurrentIndex].dwNumMaterials; i++ )
- {
-
- m_pd3dDevice->SetMaterial( &m_mesh[m_iCurrentIndex].pMeshMaterials[i] );
- m_pd3dDevice->SetTexture( 0, m_mesh[m_iCurrentIndex].pMeshTextures[i] );
-
- setTransForm(m_iCurrentIndex);
- m_mesh[m_iCurrentIndex].pMesh->DrawSubset( i );
- }
- }
(秩名) |