Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cModelX.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cModelX_H
7 #define _INC_cModelX_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cModel.h"
13 #include "../Resource/cXResourceFile.h"
14 #include "../DX/cDXMesh.h"
18 #include "GrayCore/include/cBits.h"
19 
20 namespace Gray3D
21 {
23  {
26 #define cModelXFrameName(a,b,c) ModelX_FrameName_##a,
27 #include "cModelXFrameNames.tbl"
28 #undef cModelXFrameName
30  };
31 
33  {
36 #define cModelXFrameName(a,b,c) ModelX_FrameMask_##a = _1BITMASK(ModelX_FrameName_##a),
37 #include "cModelXFrameNames.tbl"
38 #undef cModelXFrameName
39  };
40 
41  typedef WORD ModelX_FrameMask_t; // what frames does this hierarchy have ?
42 
43  class GRAY3D_LINK DECLSPEC_UUID("{a6342e06-8899-44c0-ab3a-596bc92b25df}") cModelXHeader
44  {
49  public:
50  enum { VERSION = 2 };
53 
54  DWORD m_nVersion; //!< cModelXHeader::VERSION
55  cBounds3f m_BBox; //!< pre-calculate a full model bbox. (before any animation)
56  cSpheref m_BSphere; //!< pre-calculate a full model tight bsphere. (before any animation)
57  int m_nVerticesTotal; //!< Count total for the hierarchical model.
58  int m_nFacesTotal; //!< Count total for the hierarchical model.
59 
60  public:
61  void InitXFileHeader()
62  {
63  m_nVersion = 0; // unknown version.
64  m_nVerticesTotal = 0;
65  m_nFacesTotal = 0;
66  }
67  };
68 
69  class GRAY3D_LINK cModelXEffectParam
70  {
75 
76  public:
77  cString m_sName; //!< param name
78 
79  // Variant
80  enum _D3DXEFFECTDEFAULTTYPE m_eType; //!< param value type. D3DXEDT_STRING, DWORD, FLOATS (or array of such based on size of m_Value) VARTYPE_DWORD, VARTYPE_ArrayT
81  cHeapBlock m_Value; //!< binary blob of data for param value. D3DXEFFECTDEFAULT
82  };
83 
84  class GRAY3D_LINK cModelXMaterial
85  {
92  // UVVelocity
93  public:
94  cStringF m_sTextureFilename; //!< + normal, bump map, gloss map, etc ?
95  cStringF m_sEffectFilename;
96  cArrayStruct<cModelXEffectParam> m_aDefaults; //!< set default params for m_sEffectFilename
97  };
98 
99  class GRAY3D_LINK cModelXMeshCont
100  : public _D3DXMESHCONTAINER
101  {
107 
108  public:
109  cStringA m_sName; //!< stores the name of this mesh.
110  cBounds3f m_BBoxMesh; //!< pre-transformed. local mesh coord units.
111  cSpheref m_BSphereMesh; //!< pre-transformed. local mesh coord units.
112  cArrayStruct<cModelXMaterial> m_aMaterials; //!< materials and effects on the material. (NumMaterials) for (pMaterials and pEffects)
113 
114  static const _D3DVERTEXELEMENT9 k_VertDeclX[5];
115 
116  public:
117  cModelXMeshCont(const ATOMCHAR_t* pszName);
118  virtual ~cModelXMeshCont();
119 
120  virtual HRESULT DisposeThis();
121 
122  void InitAdjacency(const DWORD *pAdjacencyInit, DWORD dwNumFaces);
123  HRESULT InitTangents(DWORD dwOpt, IDirect3DDevice9* pDXDev);
124  void InitMaterials(const _D3DXMATERIAL* pMaterials2, DWORD dwNumMaterials);
125  void InitEffectInstances(const _D3DXEFFECTINSTANCE* pEffects2, DWORD dwNumMaterials);
126 
127  DWORD ComputeMeshBoundingBox(cBounds3f& tBox) const;
128  DWORD ComputeMeshBoundingSphere(const cMatrix4x4f& CombinedTransformationMatrix, cSphereWork& bsphere) const;
129  DWORD ComputePointCloud(const cMatrix4x4f& CombinedTransformationMatrix, cArrayStruct<cVector3f>& cloud);
130 
131  HRESULT GetIndexedFaceMesh(_Inout_ DWORD& rdwFaceToFind, OUT cTriangle3f& tri) const;
132 
133  DWORD get_NumMaterials() const
134  {
135  return this->NumMaterials;
136  }
137  HRESULT GetMaterialTexName(int iMaterial, cStringF& sFileName) const;
138 
139  cModelXMeshCont* get_NextMeshCont() const
140  {
142  return static_cast<cModelXMeshCont*>(this->pNextMeshContainer);
143  }
144 
145  ID3DXMesh* get_Mesh() const
146  {
148  ASSERT(MeshData.Type == D3DXMESHTYPE_MESH);
149  return MeshData.pMesh;
150  }
151 
152  void put_MeshX(ID3DXMesh* pMesh)
153  {
154  if (MeshData.pMesh == pMesh)
155  return;
156  if (MeshData.pMesh != nullptr)
157  {
158  MeshData.pMesh->Release();
159  }
160  MeshData.pMesh = pMesh;
161  MeshData.Type = D3DXMESHTYPE_MESH;
162  if (pMesh != nullptr)
163  {
164  pMesh->AddRef();
165  }
166  }
167 
168  HRESULT CountIntersections(const cVector3f& v1, const cVector3f& vDir, float* pfDistHit);
169  };
170 
171  class GRAY3D_LINK cModelXFrame
172  : public _D3DXFRAME // has a transform matrix.
173  , public cHeapObject
174  {
181 
182  public:
183  cStringA m_sName;
184  ModelX_FrameMask_t m_uFrameFlags; //!< inside, outside,... flags for meshes in this frame
185  ModelX_FrameMask_t m_uChildrenFlags; //!< all frame children flags concatenated. (NOT sibs)
186 
187  cMatrix4x4f m_mCombinedTransformation; //!< multiply up all my parent chains matrices. how to transform into full model coords. NOT valid if animated
188  cBounds3f m_BBoxFrame; //!< combined model space bbox. NOT valid if animated.
189  cSpheref m_BSphereFrame; //!< combined model space bsphere. NOT valid if animated.
190 
191  public:
192  CHEAPOBJECT_IMPL;
193  DECLARE_HEAP_ALIGNED_ALLOC(cModelXFrame);
194 
195  cModelXFrame(const ATOMCHAR_t* pszFrameName);
196  virtual ~cModelXFrame();
197 
198  cModelXFrame* get_FrameSibling() const
199  {
201  return static_cast<cModelXFrame*>(this->pFrameSibling);
202  }
203  cModelXFrame* get_FrameChild0() const
204  {
206  return static_cast<cModelXFrame*>(this->pFrameFirstChild);
207  }
208 
209  cModelXMeshCont* get_MeshCont0() const
210  {
212  return static_cast<cModelXMeshCont*>(this->pMeshContainer);
213  }
214 
215  DWORD get_MeshContFaces(bool bChildFrames, bool bSiblings) const;
216  HRESULT GetIndexedFaceFrame(DWORD dwFaceToFind, cTriangle3f& tri, const cMatrix4x4f** ppmTransform, bool bCheckSiblings) const;
217 
218  DWORD ComputeFrameBoundingBox(cBounds3f& bres, bool bSibsAndChildren) const;
219  DWORD ComputeFrameBoundingSphere(cSphereWork& bsphere, bool bSibsAndChildren) const;
220  DWORD ComputePointCloud(const cMatrix4x4f* pParentMatrix, cArrayStruct<cVector3f>& cloud);
221 
222  cModelXMeshCont* FindMeshCont(const ATOMCHAR_t* pszMeshContName, bool bSiblings) const;
223  cModelXFrame* FindFrame(const ATOMCHAR_t* pszFrameName, bool bSiblings) const;
224 
225  bool GetHitTest(float& fDistHit, const cVector3f& vo, const cVector3f& vd, const cMatrix4x4f& mWorldRoot);
226  int CountIntersections(const cVector3f& vStart, const cVector3f& vDir, ModelX_FrameMask_t uFrameMask);
227 
228  void UpdateCombinedTransformationMatrixs(const cMatrix4x4f* pParentMatrix);
229  ModelX_FrameMask_t UpdateChildrenFlags();
230  };
231 
232  class GRAY3D_LINK cModelX
233  : public cXResourceFile
234  , public ID3DXAllocateHierarchy
235  , public ID3DXLoadUserData
236  {
243 
244  public:
245  static const char* k_szFrameNames[ModelX_FrameName_QTY + 1]; // ModelX_FrameName_TYPE
246 
247  cModelXHeader m_ModelXHeader; //!< for new (compiled) formats
249  cModelXFrame* m_pFrameRoot; //!< created by CreateFrame() (a list)
250 
251 #ifdef USE_DXX
252  // Optionally - Multiple set of animations that may run on this model. ID3DXAnimationSet
253  cIUnkPtr<ID3DXAnimationController> m_pAnimController; //!< created only for models that can animate.
254 #endif
255 
256  protected:
257  bool m_bXHeaderValid; //!< m_ModelXHeader is valid ? read or computed.
258  ModelX_FrameMask_t m_uTotalFlags; //!< m_uFrameFlags | m_uChildrenFlags for all frames
259 
260  public:
261  cModelX(const FILECHAR_t* pszFileName);
262  virtual ~cModelX();
263 
264  virtual HRESULT LoadMeshHierarchy(const FILECHAR_t* pstrMeshXFilePath, IDirect3DDevice9* pd3dDevice);
265  virtual HRESULT DeleteDeviceObjects();
266 
267  HRESULT FrameMove(float fElapsedTime);
268 
269  // public helpers
270  cModelXFrame* get_FrameRoot() const
271  {
273  return m_pFrameRoot;
274  }
275  ModelX_FrameMask_t get_TotalFlags() const
276  {
277  return m_uTotalFlags;
278  }
279 
280  void UpdateXHeader()
281  {
282  m_bXHeaderValid = true;
283  m_ModelXHeader.m_nVerticesTotal = ComputeBoundingSphere(m_ModelXHeader.m_BSphere);
284  ComputeBoundingBox(m_ModelXHeader.m_BBox);
285  }
286 
287  const cBounds3f& get_BBox()
288  {
289  if (!m_bXHeaderValid)
290  {
291  UpdateXHeader();
292  }
293  return m_ModelXHeader.m_BBox;
294  }
295 
296  const cSpheref& get_BSphere()
297  {
298  if (!m_bXHeaderValid)
299  {
300  UpdateXHeader();
301  }
302  return m_ModelXHeader.m_BSphere;
303  }
304 
305  int ComputeBoundingBox(cBounds3f& bres);
306  int ComputeBoundingSphere(cSpheref& bsphere);
307 
308  cModelXFrame* FindFrame(const ATOMCHAR_t* pszFrameName) const;
309  void DestroyFrameX(cModelXFrame* pFrame);
310 
312  STDMETHOD(CreateFrame)(THIS_ const char* pszName, _D3DXFRAME** ppNewFrame);
313  STDMETHOD(CreateMeshContainer)(THIS_
314  const char* pszName,
315  const _D3DXMESHDATA *pMeshData,
316  const _D3DXMATERIAL *pMaterials,
317  const _D3DXEFFECTINSTANCE *pEffectInstances,
318  DWORD NumMaterials,
319  const DWORD *pAdjacency,
320  ID3DXSkinInfo* /* pSkinInfo*/,
321  _D3DXMESHCONTAINER** ppNewMeshCont);
322  STDMETHOD(DestroyFrame)(THIS_ _D3DXFRAME* pFrameToFree);
323  STDMETHOD(DestroyMeshContainer)(THIS_ _D3DXMESHCONTAINER* pMeshContBase);
324 
326  STDMETHOD(LoadTopLevelData)(ID3DXFileData* pData);
327  STDMETHOD(LoadFrameChildData)(_D3DXFRAME* pF, ID3DXFileData* pData)
328  {
329  UNREFERENCED_PARAMETER(pF);
330  UNREFERENCED_PARAMETER(pData);
331  return S_OK;
332  }
333  STDMETHOD(LoadMeshChildData)(_D3DXMESHCONTAINER* pMC, ID3DXFileData* pData)
334  {
335  UNREFERENCED_PARAMETER(pMC);
336  UNREFERENCED_PARAMETER(pData);
337  return S_OK;
338  }
339 
340  UNITTEST_FRIEND(cModelX);
341  };
342 
343  typedef cRefPtr<cModelX> cModelXPtr;
344 };
345 
346 #endif // _INC_cModelX_H
#define GRAY3D_LINK
Definition: Gray3D.h:15
#define DECLSPEC_UUID(x)
Definition: IUnknown.h:19
Definition: Gray3D.cpp:12
ModelX_FrameName_TYPE
Definition: cModelX.h:23
@ ModelX_FrameName_QTY
Definition: cModelX.h:29
WORD ModelX_FrameMask_t
Definition: cModelX.h:41
ModelX_FrameMask_TYPE_
Definition: cModelX.h:33