raSystem  1.0 bata
raEntity.h
Go to the documentation of this file.
1 #pragma warning( disable : 4127 )
2 
3  template<class VertexType, bool bIndexed = true, typename IndexType = UINT16>
4  class raEntity : public raVisual
5  {
6  friend class raOctree;
7  public:
9  raMaterial* pMaterial, bool bAlpha = false, bool bAdditive = false,
10  UINT numMaterials = 1);
11  /*virtual */~raEntity();
12 
13  virtual bool CreateMesh();
14  virtual bool DestroyMesh();
15 
16  virtual bool RenderMesh(LPCSTR techniqueName = "");
17 
18  virtual bool Intersects(const raVector3* pRayPos,
19  const raVector3* pRayDir,
20  float* pDist);
21 
22  virtual ID3D11Buffer* GetVertexBuffer(){ return m_pVB;}
23  virtual ID3D11Buffer* GetIndexBuffer() { return m_pIB;}
24  virtual UINT GetStrideSize(){return sizeof(VertexType);}
25  virtual D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveTopology(UINT n)
26  {
27  return (D3D11_PRIMITIVE_TOPOLOGY) m_pSubsets[n].PrimitiveType;
28  }
29  virtual SDKMESH_SUBSET* GetSubset(UINT n){ return &m_pSubsets[n];}
30  virtual UINT GetNumSubsets(){ return m_numSubsets;}
31 
32  protected:
33  ID3D11Buffer* m_pVB;
34  ID3D11Buffer* m_pIB;
35 
36  DWORD m_nVertices;
37  DWORD m_nIndices;
38 
39  VertexType* m_pVertices;
40  IndexType* m_pIndices;
41 
43 
44  virtual void SetupVertices() = 0;
45  virtual void SetupIndices(){ };
46 
47  virtual void CreateVertexBuffer();
48  virtual void CreateIndexBuffer();
49  virtual void DeleteTempData();
50 
51  virtual const D3D11_INPUT_ELEMENT_DESC* GetVertexLayout()
52  {
53  return VertexType::GetVertexLayout();
54  }
55 
56  virtual UINT GetNumElements()
57  {
58  return VertexType::GetNumElements();
59  }
60 
61  virtual DXGI_FORMAT GetIndexBufferFormat()
62  {
63  if(sizeof(IndexType) == 2)
64  return DXGI_FORMAT_R16_UINT;
65  else
66  return DXGI_FORMAT_R32_UINT;
67  }
68 
69  virtual void ComputeBoundingBox();
70  };
71 
72  template<class VertexType, bool bIndexed, typename IndexType>
75  raMaterial* pMaterial, bool bAlpha, bool bAdditive,
76  UINT numMaterials):
77  raVisual(dx, bAlpha, bAdditive)
78  {
79  m_numMaterials = numMaterials;
80  if (numMaterials > 0)
81  {
82  m_pMaterials = new raMaterial*[numMaterials];
83  m_pMaterials[0] = pMaterial;
84 
85  m_numSubsets = 1;
87  for(UINT n = 0; n < m_numSubsets; n++)
88  {
89  m_pSubsets[n].IndexStart = 0;
90  m_pSubsets[n].IndexCount = 0;
91  m_pSubsets[n].VertexStart = 0;
92  m_pSubsets[n].VertexCount = 0;
93  m_pSubsets[n].MaterialID = 0;
95  D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
96  }
97  }
98  m_nVertices = 0;
99  m_nIndices = 0;
100 
101  m_pVertices = NULL;
102  m_pIndices = NULL;
103 
104  m_pVB = NULL;
105  m_pIB = NULL;
106  m_OctreeRoot = NULL;
107  }
108 
109  template<class VertexType, bool bIndexed, typename IndexType>
111  {
114 
115  return true;
116  }
117  template<class VertexType, bool bIndexed, typename IndexType>
119  {
120  SetupVertices();
121 
122  if(m_pSubsets)
123  {
124  m_pSubsets[0].VertexStart = 0;
126  }
127 
128  D3D11_BUFFER_DESC bufferDesc;
129  bufferDesc.ByteWidth = m_nVertices * GetStrideSize();
130  bufferDesc.Usage = D3D11_USAGE_DEFAULT;
131  bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
132  bufferDesc.CPUAccessFlags = 0;
133  bufferDesc.MiscFlags = 0;
134 
135  D3D11_SUBRESOURCE_DATA InitData;
136  InitData.pSysMem = m_pVertices;
137  InitData.SysMemPitch = 0;
138  InitData.SysMemSlicePitch = 0;
139 
140  m_dx->GetDevice()->CreateBuffer(&bufferDesc, &InitData, &m_pVB);
141  }
142 
143  template<class VertexType, bool bIndexed, typename IndexType>
145  {
146  if(!bIndexed) return;
147  SetupIndices();
148 
149  //Index Buffer
150  D3D11_BUFFER_DESC bufferDesc;
151  bufferDesc.ByteWidth = m_nIndices * sizeof(IndexType);
152  bufferDesc.Usage = D3D11_USAGE_DEFAULT;
153  bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
154  bufferDesc.CPUAccessFlags = 0;
155  bufferDesc.MiscFlags = 0;
156 
157  D3D11_SUBRESOURCE_DATA InitData;
158  InitData.pSysMem = m_pIndices;
159  m_dx->GetDevice()->CreateBuffer( &bufferDesc, &InitData, &m_pIB);
160  }
161 
162  template<class VertexType, bool bIndexed, typename IndexType>
164  {
165  DeleteTempData();
167  }
168 
169  template<class VertexType, bool bIndexed, typename IndexType>
171  {
174 
176  m_OctreeRoot = new raOctree(this);
177  return true;
178  }
179 
180  template<class VertexType, bool bIndexed, typename IndexType>
182  {
184  if(bIndexed) SAFE_DELETE_ARRAY(m_pIndices);
185  }
186 
187  template<class VertexType, bool bIndexed, typename IndexType>
189  {
190  UINT Strides[1];
191  UINT Offsets[1];
192  Strides[0] = GetStrideSize();
193  Offsets[0] = 0;
194  m_dx->GetImmediateContext()->IASetVertexBuffers(0, 1, &m_pVB, Strides, Offsets );
195 
196  if(bIndexed)
197  m_dx->GetImmediateContext()->IASetIndexBuffer(m_pIB, GetIndexBufferFormat(), 0);
198 
199  for (DWORD n = 0; n < m_numMaterials; n++)
200  {
201  UINT indexStart;
202  UINT indexCount;
203 
204  if(m_pSubsets)
205  {
206  indexStart = (UINT)m_pSubsets[n].IndexStart;
207  indexCount = (UINT)m_pSubsets[n].IndexCount;
209  m_dx->GetImmediateContext()->IASetPrimitiveTopology(GetPrimitiveTopology(n));
210  }
211  else
212  {
213  assert(false);
214  indexStart = 0;
215  indexCount = m_nIndices;
216  m_pMaterials[0]->Setup();
217  m_dx->GetImmediateContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
218  }
219 
220  ID3DX11EffectTechnique* pTechnique =
221  m_pMaterials[n]->GetEffectTechnique(techniqueName);
222 
223  if(pTechnique)
224  {
225  if(pTechnique->IsValid())
226  {
227  D3DX11_TECHNIQUE_DESC techDesc;
228  pTechnique->GetDesc(&techDesc);
229 
230  for(UINT p = 0; p < techDesc.Passes; ++p )
231  {
232  pTechnique->GetPassByIndex(p)->Apply(0, m_dx->GetImmediateContext());
233  if(bIndexed)
234  m_dx->GetImmediateContext()->DrawIndexed(indexCount, indexStart, 0);
235  else
236  m_dx->GetImmediateContext()->Draw(m_nVertices, 0);
237  }
238  }
239  }
240  }
241  return true;
242  }
243  template<class VertexType, bool bIndexed, typename IndexType>
245  {
246  for(UINT i = 0; i < m_nVertices; i++)
247  {
248  if(m_pVertices[i].position.x < m_bbMin.x)
249  m_bbMin.x = m_pVertices[i].position.x;
250  if(m_pVertices[i].position.y < m_bbMin.y)
251  m_bbMin.y = m_pVertices[i].position.y;
252  if(m_pVertices[i].position.z < m_bbMin.z)
253  m_bbMin.z = m_pVertices[i].position.z;
254 
255  if(m_pVertices[i].position.x > m_bbMax.x)
256  m_bbMax.x = m_pVertices[i].position.x;
257  if(m_pVertices[i].position.y > m_bbMax.y)
258  m_bbMax.y = m_pVertices[i].position.y;
259  if(m_pVertices[i].position.z > m_bbMax.z)
260  m_bbMax.z = m_pVertices[i].position.z;
261  }
262  }
263  template<class VertexType, bool bIndexed, typename IndexType>
265  const raVector3* pRayDir,
266  float* pDist)
267  {
268  if(*pRayDir == raVector3(0, 0, 0))return false;
269 
270  if(!m_OctreeRoot)
271  {
272  bool bIntersects = false;
273  float mindist = FLT_MAX;
274  float u = 0, v = 0, dist = FLT_MAX; //u, v nicht verwendet
275 
276  for (UINT64 idx = 0; idx < m_nIndices; idx += 3)
277  {
278  raVector3 p[3];
279  for(int i = 0; i < 3; i++)
280  p[i] = ((VertexType*)m_pVertices +
281  m_pIndices[idx + i])->position;
282 
283  if(D3DXIntersectTri((D3DXVECTOR3*)&p[0], (D3DXVECTOR3*)&p[1], (D3DXVECTOR3*)&p[2],
284  (D3DXVECTOR3*)pRayPos,(D3DXVECTOR3*) pRayDir, &u, &v, &dist))
285  {
286  bIntersects = true;
287  if(dist < mindist)
288  {
289  mindist = dist;
290  }
291  }
292  }
293  *pDist = mindist;
294  return bIntersects;
295  }
296  else
297  {
298  return m_OctreeRoot->Intersects(pRayPos, pRayDir, pDist);
299  }
300  }
UINT64 VertexStart
Definition: raSDKmesh.h:143
DWORD m_nVertices
Definition: raEntity.h:36
~raEntity()
Definition: raEntity.h:163
raFloat y
Definition: raVector3.h:13
virtual void CreateVertexBuffer()
Definition: raEntity.h:118
virtual UINT GetNumElements()
Definition: raEntity.h:56
raFloat x
Definition: raVector3.h:12
raVector3 m_bbMax
Definition: raVisual.h:60
virtual DXGI_FORMAT GetIndexBufferFormat()
Definition: raEntity.h:61
raVector3 m_bbMin
Definition: raVisual.h:59
ID3D11Buffer * m_pVB
Definition: raEntity.h:33
ID3D11DeviceContext * GetImmediateContext(void)
Definition: raDirectX.h:31
#define SAFE_DELETE_ARRAY(p)
Definition: d3dxGlobal.h:25
IndexType * m_pIndices
Definition: raEntity.h:40
raOctree * m_OctreeRoot
Definition: raEntity.h:42
VertexType * m_pVertices
Definition: raEntity.h:39
UINT64 IndexCount
Definition: raSDKmesh.h:142
interface ID3DX11EffectTechnique ID3DX11EffectTechnique
ID3D11Device * GetDevice(void)
Definition: raDirectX.h:29
virtual void ComputeBoundingBox()
Definition: raEntity.h:244
virtual void CreateIndexBuffer()
Definition: raEntity.h:144
virtual ID3D11Buffer * GetVertexBuffer()
Definition: raEntity.h:22
raFloat z
Definition: raVector3.h:14
UINT64 VertexCount
Definition: raSDKmesh.h:144
DWORD m_numSubsets
Definition: raVisual.h:120
friend class raOctree
Definition: raEntity.h:6
virtual bool DestroyMesh()
Definition: raEntity.h:110
DWORD m_nIndices
Definition: raEntity.h:37
virtual void Setup()
Definition: raMaterial.cpp:160
virtual bool Intersects(const raVector3 *pRayPos, const raVector3 *pRayDir, float *pDist)
Definition: raOctree.cpp:83
SDKMESH_SUBSET * m_pSubsets
Definition: raVisual.h:121
virtual ID3D11Buffer * GetIndexBuffer()
Definition: raEntity.h:23
virtual SDKMESH_SUBSET * GetSubset(UINT n)
Definition: raEntity.h:29
UINT64 IndexStart
Definition: raSDKmesh.h:141
virtual void DeleteTempData()
Definition: raEntity.h:181
virtual UINT GetStrideSize()
Definition: raEntity.h:24
raMaterial ** m_pMaterials
Definition: raVisual.h:97
virtual UINT GetNumSubsets()
Definition: raEntity.h:30
virtual const D3D11_INPUT_ELEMENT_DESC * GetVertexLayout()
Definition: raEntity.h:51
raEntity(raSmartPointer< raDirectX >, raMaterial *pMaterial, bool bAlpha=false, bool bAdditive=false, UINT numMaterials=1)
Definition: raEntity.h:73
#define SAFE_RELEASE(p)
Definition: d3dxGlobal.h:22
virtual D3D11_PRIMITIVE_TOPOLOGY GetPrimitiveTopology(UINT n)
Definition: raEntity.h:25
UINT PrimitiveType
Definition: raSDKmesh.h:140
ID3D11Buffer * m_pIB
Definition: raEntity.h:34
virtual void SetupVertices()=0
raSmartPointer< raDirectX > m_dx
Definition: raVisual.h:99
virtual bool CreateMesh()
Definition: raEntity.h:170
DWORD m_numMaterials
Definition: raVisual.h:96
ID3DX11EffectTechnique * GetEffectTechnique(LPCSTR techniqueName="")
Definition: raMaterial.h:31
virtual bool RenderMesh(LPCSTR techniqueName="")
Definition: raEntity.h:188
#define SAFE_DELETE(p)
Definition: d3dxGlobal.h:26
virtual void SetupIndices()
Definition: raEntity.h:45
virtual bool Intersects(const raVector3 *pRayPos, const raVector3 *pRayDir, float *pDist)
Definition: raEntity.h:264