Mesh.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:3k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Mesh.h"
- CMesh::CMesh(LPDIRECT3DDEVICE9 d3dd,LPSTR str)
- { m_pD3DDevice=d3dd;
- LPD3DXBUFFER pBuffer;
- D3DXMATERIAL * pMat;
- LPD3DXMESH pMesh = NULL;
- D3DXLoadMeshFromX(str,D3DXMESH_SYSTEMMEM,m_pD3DDevice,NULL,&pBuffer,NULL,&m_dwNumOfMaterials,&m_pMesh);
- pMat=(D3DXMATERIAL*) pBuffer->GetBufferPointer();
- m_pMaterial=new D3DMATERIAL9 [m_dwNumOfMaterials];
- m_pTexture=new LPDIRECT3DTEXTURE9 [m_dwNumOfMaterials];
- for(DWORD i=0;i<m_dwNumOfMaterials;i++)
- {
- m_pMaterial[i]=pMat[i].MatD3D;
- m_pMaterial[i].Ambient=m_pMaterial[i].Diffuse;
- D3DXCreateTextureFromFile(m_pD3DDevice,pMat[i].pTextureFilename,&m_pTexture[i]);
- }
- pBuffer->Release();
- //pMesh->CloneMeshFVF(D3DXMESH_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, m_pD3DDevice, &m_pMesh);
- //SafeRelease(pMesh);
- /*DWORD * adjacencyInfo=new DWORD[m_pMesh->GetNumFaces()*3];
- m_pMesh->GenerateAdjacency(0.0f,adjacencyInfo);
- DWORD* optimizedAdjacencyInfo=new DWORD[m_pMesh->GetNumFaces()*3];
- m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_DONOTSPLIT,adjacencyInfo,optimizedAdjacencyInfo,0,0);*/
- D3DXComputeNormals(m_pMesh,NULL);
- }
- CMesh::~CMesh()
- {
- if(m_pMaterial!=NULL)
- {
- delete [] m_pMaterial;
- }
- if(m_pTexture!=NULL)
- {
- for(DWORD i=0;i<m_dwNumOfMaterials;i++)
- {
- if(m_pTexture[i]!=NULL)
- {
- m_pTexture[i]->Release();
- }
- }
- delete [] m_pTexture;
- }
- m_pMesh->Release();
- }
- void CMesh::RenderMesh()
- {// m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
- //m_pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
- // m_pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
- // m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,TRUE);
- for(DWORD i=0;i<m_dwNumOfMaterials;i++)
- {
- m_pD3DDevice->SetMaterial(&m_pMaterial[i]);
- m_pD3DDevice->SetTexture(0,m_pTexture[i]);
- m_pMesh->DrawSubset(i);
- }
- // m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,FALSE);
- }
- void CMesh::Translate(POSITION ppos)
- {
- D3DXMATRIX matRotation,matTranslate;
- D3DXMatrixRotationY(&matRotation,-D3DX_PI/180*(ppos.a+90.0f));
- D3DXMatrixTranslation(&matTranslate,ppos.x,ppos.y+65.0f,ppos.z);
- D3DXMatrixMultiply(&matWorld,&matRotation,&matTranslate);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
- }
- void CMesh::Translation(NPOSITION ppos)
- {
- D3DXMATRIX matRotation,matTranslate;
- D3DXMatrixRotationY(&matRotation,-D3DX_PI/180*(ppos.a+90.0f));
- D3DXMatrixTranslation(&matTranslate,ppos.x,ppos.y+65.0f,ppos.z);
- D3DXMatrixMultiply(&matWorld,&matRotation,&matTranslate);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
- }