raSystem  1.0 bata
raOctree.cpp
Go to the documentation of this file.
1 #include "..\include\raMain.h"
2 namespace System
3 {
4  raOctree::raOctree(raVector3& bbMin, raVector3& bbMax, raOctree* pParent):
5  m_bbMin(bbMin), m_bbMax(bbMax)
6  {
7  //Rückwärts durchlaufen, damit Punkte hinten zuerst entfernt werden
8  for (int n = pParent->m_Points.GetSize() - 3; n >= 0; n -= 3)
9  {
10  if(
11  IsPointInOctant(pParent->m_Points[n ]) &&
12  IsPointInOctant(pParent->m_Points[n + 1]) &&
13  IsPointInOctant(pParent->m_Points[n + 2]) )
14  {
15  m_Points.Add(pParent->m_Points[n]);
16  m_Points.Add(pParent->m_Points[n + 1]);
17  m_Points.Add(pParent->m_Points[n + 2]);
18  pParent->m_Points.Remove(n + 2);
19  pParent->m_Points.Remove(n + 1);
20  pParent->m_Points.Remove(n);
21  }
22  }
23 
24  if((((float)m_bbMax.x - (float)m_bbMin.x) > 1) && (m_Points.GetSize() > 0))
25  {
26  CreateChildNodes();
27  }
28  else
29  m_pChildren = NULL;
30  }
31 
32  void raOctree::CreateChildNodes()
33  {
34  m_pChildren = new raOctree*[8];
35  int i = 0;
36  for(int x = -1; x <= 1; x+=2)
37  {
38  for(int y = -1; y <= 1; y+=2)
39  {
40  for(int z = -1; z <= 1; z+=2)
41  {
42  raVector3 bbMin, bbMax;
43 
44  bbMin.x = 0.25f * x * (((float)m_bbMax.x - (float)m_bbMin.x)
45  + ((float)m_bbMax.x + 3 * (float)m_bbMin.x));
46  bbMin.y = 0.25f * y * (((float)m_bbMax.y - (float)m_bbMin.y)
47  + ((float)m_bbMax.y + 3 * (float)m_bbMin.y));
48  bbMin.z = 0.25f * z * (((float)m_bbMax.z - (float)m_bbMin.z)
49  + ((float)m_bbMax.z + 3 * (float)m_bbMin.z));
50 
51  bbMax.x = 0.25f * x * (((float)m_bbMax.x - (float)m_bbMin.x)
52  + (3 * (float)m_bbMax.x + (float)m_bbMin.x));
53  bbMax.y = 0.25f * y * (((float)m_bbMax.y - (float)m_bbMin.y)
54  + (3 * (float)m_bbMax.y + (float)m_bbMin.y));
55  bbMax.z = 0.25f * z * (((float)m_bbMax.z - (float)m_bbMin.z)
56  + (3 * (float)m_bbMax.z + (float)m_bbMin.z));
57 
58  m_pChildren[i++] = new raOctree(bbMin, bbMax, this);
59  }
60  }
61  }
62  }
63 
64  bool raOctree::IsPointInOctant(raVector3 p)
65  {
66  return ( (p.x >= m_bbMin.x && p.x <= m_bbMax.x) &&
67  (p.y >= m_bbMin.y && p.y <= m_bbMax.y) &&
68  (p.z >= m_bbMin.z && p.z <= m_bbMax.z));
69  }
70 
72  {
73  if(m_pChildren)
74  {
75  for(int i = 0; i < 8; i++)
76  {
77  delete m_pChildren[i];
78  }
79  delete[] m_pChildren;
80  }
81  }
82 
83  bool raOctree::Intersects(const raVector3* pRayPos,
84  const raVector3* pRayDir,
85  float* pDist)
86  {
87  if(! D3DXBoxBoundProbe((D3DXVECTOR3*)&m_bbMin, (D3DXVECTOR3*)&m_bbMax, (D3DXVECTOR3*)pRayPos, (D3DXVECTOR3*)pRayDir))
88  return false;
89 
90  bool bIntersects = false;
91  float dist = FLT_MAX;
92  float mindist = FLT_MAX;
93 
94  //Zuerst die eigenen Dreiecke untersuchen
95  float u = 0, v = 0; //u, v nicht verwendet
96  for (int n = 0; n < m_Points.GetSize(); n += 3)
97  {
98  if(D3DXIntersectTri((D3DXVECTOR3 *)&m_Points[n], (D3DXVECTOR3 *)&m_Points[n + 1], (D3DXVECTOR3 *)&m_Points[n + 2],
99  (D3DXVECTOR3 *)pRayPos, (D3DXVECTOR3 *)pRayDir, &u, &v, &dist))
100  {
101  bIntersects = true;
102  if(dist < mindist)
103  {
104  mindist = dist;
105  }
106  }
107  }
108 
109  //Jetzt ggf. die Kinder
110  if(m_pChildren)
111  {
112  for(int i = 0; i < 8; i++)
113  {
114  if (m_pChildren[i]->Intersects(pRayPos, pRayDir, &dist))
115  {
116  bIntersects = true;
117  if(dist < mindist)
118  {
119  mindist = dist;
120  }
121  }
122  }
123  }
124  *pDist = mindist;
125  return bIntersects;
126  }
127 };
HRESULT Remove(raInt nIndex)
Definition: raArray.h:216
raFloat y
Definition: raVector3.h:13
raFloat x
Definition: raVector3.h:12
raOctree(const raEntity< VertexType, bIndexed, IndexType > *pEntity)
Definition: raOctree.h:27
raFloat z
Definition: raVector3.h:14
~raOctree(void)
Definition: raOctree.cpp:71
virtual bool Intersects(const raVector3 *pRayPos, const raVector3 *pRayDir, float *pDist)
Definition: raOctree.cpp:83
HRESULT Add(const TYPE &value)
Definition: raArray.h:119
raInt GetSize() const
Definition: raArray.h:20