npc.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:2k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "npc.h"
- CNpc::CNpc(LPDIRECT3DDEVICE9 d3dd,LPSTR str)
- {
- //pos=poss;
- m_pD3DDevice=d3dd;
- LPD3DXBUFFER pBuffer;
- D3DXMATERIAL * pMat;
- //LPD3DXNpc pNpc = NULL;
- D3DXLoadMeshFromX(str,D3DXMESH_SYSTEMMEM,m_pD3DDevice,NULL,&pBuffer,NULL,&m_dwNumOfMaterials,&m_pNpc);
- 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();
- //pNpc->CloneNpcFVF(D3DXNpc_MANAGED, MESH_D3DFVF_CUSTOMVERTEX, m_pD3DDevice, &m_pMesh);
- //SafeRelease(pMesh);
- DWORD * adjacencyInfo=new DWORD[m_pNpc->GetNumFaces()*3];
- m_pNpc->GenerateAdjacency(0.0f,adjacencyInfo);
- DWORD* optimizedAdjacencyInfo=new DWORD[m_pNpc->GetNumFaces()*3];
- m_pNpc->OptimizeInplace(D3DXMESHOPT_ATTRSORT|D3DXMESHOPT_COMPACT|D3DXMESHOPT_VERTEXCACHE,adjacencyInfo,optimizedAdjacencyInfo,0,0);
- D3DXComputeNormals(m_pNpc,NULL);
- }
- CNpc::~CNpc()
- {
- 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_pNpc->Release();
- }
- void CNpc::RenderNpc()
- {
- for(DWORD i=0;i<m_dwNumOfMaterials;i++)
- {
- m_pD3DDevice->SetMaterial(&m_pMaterial[i]);
- m_pD3DDevice->SetTexture(0,m_pTexture[i]);
- m_pNpc->DrawSubset(i);
- }
- }
- void CNpc::Translate()
- {
- D3DXMATRIX matWorld,matRotation;
- D3DXMatrixTranslation(&matRotation,pos.x,pos.y,pos.z);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matRotation);
- }
- void CNpc::Translation(float x,float y,float z,float a,float k)
- {
- float aa=D3DX_PI/180*a;
- D3DXMATRIX matRotate,matTrans ,matScal;
- D3DXMatrixTranslation(&matTrans,x,y,z);
- D3DXMatrixRotationY(&matRotate,aa);
- D3DXMatrixScaling(&matScal,k,k,k);
- D3DXMatrixMultiply(&matWorld,&matRotate,&matScal);
- D3DXMatrixMultiply(&matWorld,&matWorld,&matTrans);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
- }