Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cDXMesh.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cDXMesh_H
7 #define _INC_cDXMesh_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cDXBuffer.h"
13 #include "UseDXX.h"
18 #include "GrayCore/include/cLogMgr.h" // DEBUG_ERR
22 
23 namespace Gray3D
24 {
26  {
30  public:
32 
33  public:
34  cDXMesh(ID3DXMesh* pMesh = nullptr)
35  : m_pMesh(pMesh)
36  {
37  }
38 
39  ID3DXMesh* get_Mesh() const
40  {
41  return m_pMesh;
42  }
43 
44  HRESULT OptimizeInplace(DWORD* pAdjacency);
45  HRESULT OptimizeInplace(ID3DXBuffer* pAdjacency);
46 
47  DWORD ComputeMeshBoundingBox(OUT cBounds3f& tBox) const;
48  DWORD ComputeMeshBoundingSphere(const cMatrix4x4f& CombinedTransformationMatrix, OUT cSphereWork& bsphere) const;
49 
50 #ifdef USE_DXX
51  HRESULT LoadMesh(const FILECHAR_t* pszMeshName, IDirect3DDevice9* pDevice, cIUnkPtr<ID3DXBuffer>& pAdjacency);
52  HRESULT LoadMeshOpt(const FILECHAR_t* pszMeshName, IDirect3DDevice9* pDevice);
53 #endif
54  };
55 
56  //**********************************************************************
57 
58  template <class _TYPE_VERT = cVector3f >
59  class GRAY3D_LINK cDXMeshVertex // : public cLockerT<>
60  {
65  public:
66  _TYPE_VERT* m_pVertex;
67  size_t m_nVertStride;
68  private:
69  ID3DXBaseMesh* m_pIMesh;
70  public:
71  cDXMeshVertex(ID3DXBaseMesh* pIMesh, DWORD dwFlags = D3DLOCK_READONLY)
72  : m_pVertex(nullptr)
73  , m_pIMesh(pIMesh)
74  {
77  ASSERT(m_pIMesh != nullptr);
78  m_nVertStride = m_pIMesh->GetNumBytesPerVertex();
79  Lock(dwFlags);
80  }
82  {
83  Unlock();
84  }
85  bool IsValid() const
86  {
87  return m_pVertex != nullptr;
88  }
89  HRESULT Lock(DWORD dwFlags = D3DLOCK_READONLY)
90  {
92  ASSERT(m_pIMesh != nullptr);
93  HRESULT hRes = m_pIMesh->LockVertexBuffer(dwFlags, reinterpret_cast<void**>(&m_pVertex));
94  if (FAILED(hRes))
95  {
96  DEBUG_ERR(("LockVertexBuffer ERR='%s'", LOGERR(hRes)));
97  m_pVertex = nullptr;
98  }
99  return hRes;
100  }
101  void Unlock()
102  {
103  if (m_pVertex != nullptr)
104  {
105  m_pVertex = nullptr;
106  ASSERT(m_pIMesh != nullptr);
107  m_pIMesh->UnlockVertexBuffer();
108  }
109  }
110  operator const _TYPE_VERT* () const
111  {
113  ASSERT(m_pVertex != nullptr);
114  return m_pVertex;
115  }
116  operator _TYPE_VERT* ()
117  {
118  ASSERT(m_pVertex != nullptr);
119  return m_pVertex;
120  }
121  _TYPE_VERT* operator -> () const
122  {
123  ASSERT(m_pVertex != nullptr); return(m_pVertex);
124  }
125 
126  _TYPE_VERT* GetVertPtr(int i) const
127  {
128  ASSERT(m_pVertex != nullptr);
129  ASSERT(m_nVertStride);
130  return (_TYPE_VERT*)(((BYTE*)m_pVertex) + (i * m_nVertStride));
131  }
132  cVector3f& GetVert(int i) const
133  {
135  return *((cVector3f*)GetVertPtr(i));
136  }
137  };
138 
140 
141  template <typename _TYPE_INDEX = WORD>
143  {
148  public:
149  _TYPE_INDEX* m_pIndex;
150  private:
151  ID3DXBaseMesh* m_pIMesh;
152  public:
153  cDXMeshIndex(ID3DXBaseMesh* pIMesh, DWORD dwFlags = D3DLOCK_READONLY)
154  : m_pIMesh(pIMesh)
155  {
158  ASSERT(pIMesh != nullptr);
159  HRESULT hRes = pIMesh->LockIndexBuffer(dwFlags,
160  reinterpret_cast<void**>(&m_pIndex));
161  if (FAILED(hRes))
162  {
163  DEBUG_ERR(("LockIndexBuffer FAIL ERR='%s'", LOGERR(hRes)));
164  m_pIndex = nullptr;
165  }
166  }
168  {
169  Unlock();
170  }
171  bool IsValid() const
172  {
173  return m_pIndex != nullptr ;
174  }
175  void Unlock()
176  {
177  if (m_pIndex != nullptr)
178  {
179  m_pIndex = nullptr;
180  ASSERT(m_pIMesh != nullptr);
181  m_pIMesh->UnlockIndexBuffer();
182  }
183  }
184  operator const _TYPE_INDEX* () const
185  {
186  ASSERT(m_pIndex != nullptr);
187  return m_pIndex;
188  }
189  operator _TYPE_INDEX* ()
190  {
191  ASSERT(m_pIndex != nullptr);
192  return m_pIndex;
193  }
194  };
195 
197 
199  {
202  public:
203  DWORD* m_pdwAttrib;
204  private:
205  ID3DXMesh* m_pIMesh;
206  public:
207  cDXMeshAttribute(ID3DXMesh* pIMesh, DWORD dwFlags = D3DLOCK_READONLY) :
208  m_pIMesh(pIMesh)
209  {
212  ASSERT(pIMesh != nullptr);
213  HRESULT hRes = pIMesh->LockAttributeBuffer(dwFlags, &m_pdwAttrib);
214  if (FAILED(hRes))
215  {
216  DEBUG_ERR(("LockAttributeBuffer FAIL ERR='%s'", LOGERR(hRes)));
217  m_pdwAttrib = nullptr;
218  }
219  }
221  {
222  Unlock();
223  }
224  bool IsValid() const
225  {
226  return(m_pdwAttrib != nullptr);
227  }
228  void Unlock()
229  {
230  if (m_pdwAttrib != nullptr)
231  {
232  m_pdwAttrib = nullptr;
233  ASSERT(m_pIMesh != nullptr);
234  m_pIMesh->UnlockAttributeBuffer();
235  }
236  }
237  operator const DWORD* () const
238  {
239  ASSERT(m_pdwAttrib != nullptr);
240  return m_pdwAttrib;
241  }
242  operator DWORD* ()
243  {
244  ASSERT(m_pdwAttrib != nullptr);
245  return m_pdwAttrib;
246  }
247  };
248 }
249 
250 #endif // _INC_cDXMesh_H
#define GRAY3D_LINK
Definition: Gray3D.h:15
#define FAILED(x)
Definition: HResult.h:30
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define D3DLOCK_READONLY
Definition: UseDX.h:115
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define LOGERR(hRes)
Used to supply "ERR='%s'".
Definition: cLogAppender.h:23
#define DEBUG_ERR(_x_)
Definition: cLogMgr.h:144
Definition: cDXMesh.h:199
DWORD * m_pdwAttrib
The locked pointer.
Definition: cDXMesh.h:203
~cDXMeshAttribute()
Definition: cDXMesh.h:220
bool IsValid() const
Definition: cDXMesh.h:224
cDXMeshAttribute(ID3DXMesh *pIMesh, DWORD dwFlags=0x00000010L)
Definition: cDXMesh.h:207
void Unlock()
Definition: cDXMesh.h:228
Definition: cDXMesh.h:143
_TYPE_INDEX * m_pIndex
The locked pointer.
Definition: cDXMesh.h:149
void Unlock()
Definition: cDXMesh.h:175
cDXMeshIndex(ID3DXBaseMesh *pIMesh, DWORD dwFlags=0x00000010L)
Definition: cDXMesh.h:153
~cDXMeshIndex()
Definition: cDXMesh.h:167
bool IsValid() const
Definition: cDXMesh.h:171
Definition: cDXMesh.h:60
cVector3f & GetVert(int i) const
Definition: cDXMesh.h:132
~cDXMeshVertex()
Definition: cDXMesh.h:81
void Unlock()
Definition: cDXMesh.h:101
_TYPE_VERT * m_pVertex
The locked pointer.
Definition: cDXMesh.h:66
cDXMeshVertex(ID3DXBaseMesh *pIMesh, DWORD dwFlags=0x00000010L)
Definition: cDXMesh.h:71
size_t m_nVertStride
size in bytes of each vertex. DWORD.
Definition: cDXMesh.h:67
HRESULT Lock(DWORD dwFlags=0x00000010L)
Definition: cDXMesh.h:89
bool IsValid() const
Definition: cDXMesh.h:85
_TYPE_VERT * GetVertPtr(int i) const
Definition: cDXMesh.h:126
Definition: cDXMesh.h:26
ID3DXMesh * get_Mesh() const
Definition: cDXMesh.h:39
cDXMesh(ID3DXMesh *pMesh=nullptr)
Definition: cDXMesh.h:34
cIUnkPtr< ID3DXMesh > m_pMesh
supports ID3DXMeshBase as well.
Definition: cDXMesh.h:31
Definition: cMatrix.h:194
Definition: cSphere.h:260
Definition: cVector.h:94
Definition: Gray3D.cpp:12
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22