www.pudn.com > notWow.rar > Model.h
#pragma once class M2Model; struct ModelAttachment; struct ModelColor; struct ModelTransparency; struct ModelCamera ; struct ModelLight; struct CharDetails; struct CharModelDetails; #includeusing namespace std; #include "Bone.h" #include "Math.h" #include "TexMgr.h" #include "M2Block.h" #include "AnimMgr.h" #include "Enums.h" #include "Particle.h" #include "Render.h" #include "Effect.h" #include "ResLoader.h" extern TexturesManager g_TexMgr; extern ResLoader g_ResLdr; #include #include struct CharDetails { unsigned int skinColor; unsigned int faceType; unsigned int hairColor; unsigned int hairStyle; unsigned int facialHair; unsigned int facialColor; unsigned int maxFacialColor; unsigned int maxHairStyle, maxHairColor, maxSkinColor, maxFaceType, maxFacialHair; unsigned int race, gender; unsigned int useNPC; bool showUnderwear, showEars, showHair, showFacialHair, showFeet; int equipment[NUM_CHAR_SLOTS]; int geosets[16]; }; struct CharModelDetails { bool closeRHand; bool closeLHand; bool isChar; bool isMounted; void Reset() { closeRHand = false; closeLHand = false; isChar = false; isMounted = false; } }; // the Interface of Model class IModel { protected: Effect m_Effect; MODELTYPE m_Type; LPDIRECT3DDEVICE9 m_pd3dDevice; D3DXVECTOR3 m_vMax,m_vMin,m_vCenter; float m_fRadius; public: IModel() : m_pd3dDevice(NULL) { } // Init() : creates objects in memory and managed-pool // it should be called in OnCreateDevice virtual bool Init(char* pFile,int Size,MODELTYPE Type,LPDIRECT3DDEVICE9 pd3dDevice) = 0; // Free() : release and free the resource created in Init() // it should be called in OnDestoryDevice virtual void Free() = 0; // Reset() : build objects in default-pool(in AGP or Vedio Memory),when the device is lost // these objects should recreate // it should be called in OnResetDevice virtual bool Reset() = 0; // Lost() : release and free the objects created in Reset() when device is lost // it should be called in OnLostDevice virtual void Lost() = 0; // Update() : get the some information to ready for rending // it could be called in OnFrameMove (in OnFrameRender also) virtual void Update(D3DXMATRIX* pmatViewProj) = 0; // Render() : render model // it should be called during the BeginScene() and EndScene() virtual bool Render(float CameraDist = 0.0f,bool bFar = false) = 0; // Move(): is similar with Update() virtual void Move(float x,float y,float z,int Dir) = 0; // Dtr virtual ~IModel(){} Effect* GetEffect() { return &m_Effect; } void GetBoundBox(float x,float y,float z,int Dir,D3DXVECTOR3* pMin,D3DXVECTOR3* pMax); void GetBoundBox(D3DXVECTOR3* pMin,D3DXVECTOR3* pMax) { *pMin = m_vMin; *pMax = m_vMax; } MODELTYPE GetType() { return m_Type; } }; class XModel : public IModel { protected: ID3DXPMesh** m_ppPMeshes; ID3DXMesh* m_pMesh; int m_nPMeshes; int m_nTextures; TextureID* m_pTexturesID; D3DXMATERIAL* m_pMtrls; bool Init(char *M2File,int Size); public: XModel() : m_pTexturesID(NULL),m_pMtrls(NULL),m_pMesh(NULL), m_ppPMeshes(NULL),m_nPMeshes(0) { } ~XModel() { } bool Init(char* pFile,int Size,MODELTYPE Type,LPDIRECT3DDEVICE9 pd3dDevice); void Free(); bool Reset(); void Lost(); void Update(D3DXMATRIX* pmatViewProj); bool Render(float CameraDist = 0.0f,bool bFar = false); void Move(float x,float y,float z,int Dir); }; class M2Model : public IModel { protected: LPDIRECT3DVERTEXBUFFER9 m_pVB; LPDIRECT3DINDEXBUFFER9 m_pIB; int m_nVertices; // the count of vertices in a view int m_nIndices; // the count of indices in a view TextureID* m_pTexturesID; int m_nTextures; bool m_bBuiltIn; ModelAttachment* m_pAttachments; int m_nAttachments; int m_AttachmentLookup[40]; ModelColor* m_pColors; int m_nColors; ModelTransparency* m_pTransparencies; int m_nTransparencies; ModelRenderPass* m_pRenderPass; int m_nRenderPass; ModelLight* m_pLights; int m_nLights; int* m_pGlobalSequences; int m_nGlobalSequences; bool* m_pShowGeosets; D3DXVECTOR3* m_pBoundVertices; int m_nBoundVertices; uint16* m_pBoundIndices; int m_nBoundIndices; // CharModelDetails m_CharModelDetails; // ModelCamera cam; ParticleSystem* m_pPS; int m_nPS; BonesContainer* m_pBC; int m_Anim; int m_Time; bool InitCommom(char *M2File,int Size); bool InitStatic(char *M2File,int Size); bool InitVB(char *M2File,int Size); public: M2Model() : m_pVB(NULL),m_pIB(NULL),m_pTexturesID(NULL), m_pGlobalSequences(NULL),m_pPS(NULL),m_pBoundVertices(NULL),m_pBoundIndices(NULL), m_pShowGeosets(NULL),m_pAttachments(NULL),m_pLights(NULL), m_pColors(NULL),m_pTransparencies(NULL),m_pBC(NULL),m_pRenderPass(NULL), m_nTextures(0) { } ~M2Model() { } bool Init(char* pFile,int Size,MODELTYPE Type,LPDIRECT3DDEVICE9 pd3dDevice); void Free(); bool Reset(); void Lost(); void Update(D3DXMATRIX* pmatViewProj); bool Render(float CameraDist = 0.0f,bool bFar = false); void Move(float x,float y,float z,int Dir); void SetDetails(char Geosets[]); void SetTexture(TextureID TexID[]); vector GetAnimation(ACTION Act) { return m_pBC->GetAnimationByAct(Act); } void SetAnimation(int Anim,int &Time); bool NewLoop(int Anim,int Time); D3DXMATRIX* SetupAttachment(int id); protected: friend struct ModelAttachment; friend class ModelRenderPass; friend class ParticleSystem; struct MODELVBVERTEX { D3DXVECTOR3 Pos; FLOAT Blend1; FLOAT Blend2; FLOAT Blend3; DWORD Indices; D3DXVECTOR3 Normal; D3DXVECTOR2 Texcoord; const static DWORD FVF = (D3DFVF_XYZB4 | D3DFVF_LASTBETA_UBYTE4 | D3DFVF_NORMAL | D3DFVF_TEX1); }; struct MODELSTATICVERTEX { D3DXVECTOR3 Pos; D3DXVECTOR3 Normal; D3DXVECTOR2 Texcoord; const static DWORD FVF = (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1); }; }; struct ModelColor { Animated m_Color; AnimatedShort m_Opacity; void Init(char* M2File, ModelColorDef &mcd, int *global) { m_Color.init(mcd.color, M2File, global); m_Opacity.init(mcd.opacity, M2File, global); } }; struct ModelTransparency { AnimatedShort m_Trans; void Init(char* M2File, ModelTransDef &mtd, int *global) { m_Trans.init(mtd.trans, M2File, global); } }; struct ModelAttachment { int m_ID; D3DXVECTOR3 m_vPos; int m_Bone; M2Model* m_pModel; D3DXMATRIX m_Mat; void Init(ModelAttachmentDef &mad,M2Model* pModel) { m_vPos = fixCoordSystem(mad.pos); m_Bone = mad.bone; m_ID = mad.id; m_pModel = pModel; } D3DXMATRIX* Setup() { m_Mat = *(m_pModel->m_pBC->GetBones()[m_Bone].GetMatrix()); D3DXMATRIX tm; D3DXMatrixTranslation(&tm,m_vPos.x,m_vPos.y,m_vPos.z); m_Mat = tm * m_Mat; return &m_Mat; } void setupParticle() { /* Matrix m = model->bones[bone].mat; m.transpose(); glMultMatrixf(m); glTranslatef(pos.x, pos.y, pos.z); */ } }; struct TextureAnimation { // there are problem about m_Rot; Animated m_Trans, m_Scale; Animated m_Rot; D3DXVECTOR3 m_vTrans,m_vScale; D3DXQUATERNION m_qRot; D3DXMATRIX m_Mat; void Init(char* M2File, ModelTexAnimDef &mta, int *global) { m_Trans.init(mta.trans, M2File, global); m_Rot.init(mta.rot, M2File, global); m_Scale.init(mta.scale, M2File, global); } D3DXMATRIX* Setup(int anim, int time) { if (m_Trans.used) m_vTrans = m_Trans.getValue(anim, time); if (m_Rot.used) m_qRot = m_Rot.getValue(anim, time); if (m_Scale.used) m_vScale = m_Scale.getValue(anim, time); D3DXMatrixTransformation(&m_Mat,NULL,NULL,&m_vScale,NULL,&m_qRot,&m_vTrans); return &m_Mat; } }; struct ModelLight { private: int m_Type, m_Parent; D3DXVECTOR3 m_vPos, m_vTPos, m_vDir, m_vTDir; Animated m_DiffColor, m_AmbColor; Animated m_DiffIntensity, m_AmbIntensity; public: D3DXVECTOR4 m_vAmbColor; D3DXVECTOR4 m_vDiffColor; // D3DXVECTOR4 m_ void Init(char* M2File, ModelLightDef &mld, int *global) { m_vTPos = m_vPos = fixCoordSystem(mld.pos); m_vTDir = m_vDir = D3DXVECTOR3(0,1,0); // no idea m_Type = mld.type; m_Parent = mld.bone; m_AmbColor.init(mld.ambColor, M2File, global); m_AmbIntensity.init(mld.ambIntensity, M2File, global); m_DiffColor.init(mld.color, M2File, global); m_DiffIntensity.init(mld.intensity, M2File, global); } /* void setup(int time) { m_AmbColor = D3DXVECTOR4(ambColor.getValue(0, time) * ambIntensity.getValue(0, time), 1.0f); m_DiffColor = D3DXVECTOR4(diffColor.getValue(0, time) * diffIntensity.getValue(0, time), 1.0f); D3DXVECTOR4 p; if (type==0) { // directional p = D3DXVECTOR4(tdir, 0.0f); } else { // point p = D3DXVECTOR4(tpos, 1.0f); } }*/ }; /* struct ModelCamera { bool ok; Vec3D pos, target; float nearclip, farclip, fov; Animated tPos, tTarget; Animated rot; void init(MPQFile &f, ModelCameraDef &mcd, int *global); void setup(int time=0); ModelCamera():ok(false) {} }; */