1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __C_OCTREE_H_INCLUDED__
#define __C_OCTREE_H_INCLUDED__
#include "SViewFrustum.h"
#include "S3DVertex.h"
#include "aabbox3d.h"
#include "irrArray.h"
#include "CMeshBuffer.h"
/**
Flags for Octree
*/
//! use meshbuffer for drawing, enables VBO usage
#define OCTREE_USE_HARDWARE false
//! use visibility information together with VBOs
#define OCTREE_USE_VISIBILITY true
//! use bounding box or frustum for calculate polys
#define OCTREE_BOX_BASED true
//! bypass full invisible/visible test
#define OCTREE_PARENTTEST
namespace irr
{
//! template octree.
/** T must be a vertex type which has a member
called .Pos, which is a core::vertex3df position. */
template <class T>
class Octree
{
public:
struct SMeshChunk : public scene::CMeshBuffer<T>
{
SMeshChunk ()
: scene::CMeshBuffer<T>(), MaterialId(0)
{
scene::CMeshBuffer<T>::grab();
}
virtual ~SMeshChunk ()
{
//removeAllHardwareBuffers
}
s32 MaterialId;
};
struct SIndexChunk
{
core::array<u16> Indices;
s32 MaterialId;
};
struct SIndexData
{
u16* Indices;
s32 CurrentSize;
s32 MaxSize;
};
//! Constructor
Octree(const core::array<SMeshChunk>& meshes, s32 minimalPolysPerNode=128) :
IndexData(0), IndexDataCount(meshes.size()), NodeCount(0)
{
IndexData = new SIndexData[IndexDataCount];
// construct array of all indices
core::array<SIndexChunk>* indexChunks = new core::array<SIndexChunk>;
indexChunks->reallocate(meshes.size());
for (u32 i=0; i!=meshes.size(); ++i)
{
IndexData[i].CurrentSize = 0;
IndexData[i].MaxSize = meshes[i].Indices.size();
IndexData[i].Indices = new u16[IndexData[i].MaxSize];
indexChunks->push_back(SIndexChunk());
SIndexChunk& tic = indexChunks->getLast();
tic.MaterialId = meshes[i].MaterialId;
tic.Indices = meshes[i].Indices;
}
// create tree
Root = new OctreeNode(NodeCount, 0, meshes, indexChunks, minimalPolysPerNode);
}
//! returns all ids of polygons partially or fully enclosed
//! by this bounding box.
void calculatePolys(const core::aabbox3d<f32>& box)
{
for (u32 i=0; i!=IndexDataCount; ++i)
IndexData[i].CurrentSize = 0;
Root->getPolys(box, IndexData, 0);
}
//! returns all ids of polygons partially or fully enclosed
//! by a view frustum.
void calculatePolys(const scene::SViewFrustum& frustum)
{
for (u32 i=0; i!=IndexDataCount; ++i)
IndexData[i].CurrentSize = 0;
Root->getPolys(frustum, IndexData, 0);
}
const SIndexData* getIndexData() const
{
return IndexData;
}
u32 getIndexDataCount() const
{
return IndexDataCount;
}
u32 getNodeCount() const
{
return NodeCount;
}
//! for debug purposes only, collects the bounding boxes of the tree
void getBoundingBoxes(const core::aabbox3d<f32>& box,
core::array< const core::aabbox3d<f32>* >&outBoxes) const
{
Root->getBoundingBoxes(box, outBoxes);
}
//! destructor
~Octree()
{
for (u32 i=0; i<IndexDataCount; ++i)
delete [] IndexData[i].Indices;
delete [] IndexData;
delete Root;
}
private:
// private inner class
class OctreeNode
{
public:
// constructor
OctreeNode(u32& nodeCount, u32 currentdepth,
const core::array<SMeshChunk>& allmeshdata,
core::array<SIndexChunk>* indices,
s32 minimalPolysPerNode) : IndexData(0),
Depth(currentdepth+1)
{
++nodeCount;
u32 i; // new ISO for scoping problem with different compilers
for (i=0; i!=8; ++i)
Children[i] = 0;
if (indices->empty())
{
delete indices;
return;
}
bool found = false;
// find first point for bounding box
for (i=0; i<indices->size(); ++i)
{
if (!(*indices)[i].Indices.empty())
{
Box.reset(allmeshdata[i].Vertices[(*indices)[i].Indices[0]].Pos);
found = true;
break;
}
}
if (!found)
{
delete indices;
return;
}
s32 totalPrimitives = 0;
// now lets calculate our bounding box
for (i=0; i<indices->size(); ++i)
{
totalPrimitives += (*indices)[i].Indices.size();
for (u32 j=0; j<(*indices)[i].Indices.size(); ++j)
Box.addInternalPoint(allmeshdata[i].Vertices[(*indices)[i].Indices[j]].Pos);
}
core::vector3df middle = Box.getCenter();
core::vector3df edges[8];
Box.getEdges(edges);
// calculate all children
core::aabbox3d<f32> box;
core::array<u16> keepIndices;
if (totalPrimitives > minimalPolysPerNode && !Box.isEmpty())
for (u32 ch=0; ch!=8; ++ch)
{
box.reset(middle);
box.addInternalPoint(edges[ch]);
// create indices for child
bool added = false;
core::array<SIndexChunk>* cindexChunks = new core::array<SIndexChunk>;
cindexChunks->reallocate(allmeshdata.size());
for (i=0; i<allmeshdata.size(); ++i)
{
cindexChunks->push_back(SIndexChunk());
SIndexChunk& tic = cindexChunks->getLast();
tic.MaterialId = allmeshdata[i].MaterialId;
for (u32 t=0; t<(*indices)[i].Indices.size(); t+=3)
{
if (box.isPointInside(allmeshdata[i].Vertices[(*indices)[i].Indices[t]].Pos) &&
box.isPointInside(allmeshdata[i].Vertices[(*indices)[i].Indices[t+1]].Pos) &&
box.isPointInside(allmeshdata[i].Vertices[(*indices)[i].Indices[t+2]].Pos))
{
tic.Indices.push_back((*indices)[i].Indices[t]);
tic.Indices.push_back((*indices)[i].Indices[t+1]);
tic.Indices.push_back((*indices)[i].Indices[t+2]);
added = true;
}
else
{
keepIndices.push_back((*indices)[i].Indices[t]);
keepIndices.push_back((*indices)[i].Indices[t+1]);
keepIndices.push_back((*indices)[i].Indices[t+2]);
}
}
(*indices)[i].Indices.set_used(keepIndices.size());
memcpy( (*indices)[i].Indices.pointer(), keepIndices.pointer(), keepIndices.size()*sizeof(u16));
keepIndices.set_used(0);
}
if (added)
Children[ch] = new OctreeNode(nodeCount, Depth,
allmeshdata, cindexChunks, minimalPolysPerNode);
else
delete cindexChunks;
} // end for all possible children
IndexData = indices;
}
// destructor
~OctreeNode()
{
delete IndexData;
for (u32 i=0; i<8; ++i)
delete Children[i];
}
// returns all ids of polygons partially or full enclosed
// by this bounding box.
void getPolys(const core::aabbox3d<f32>& box, SIndexData* idxdata, u32 parentTest ) const
{
#if defined (OCTREE_PARENTTEST )
// if not full inside
if ( parentTest != 2 )
{
// partially inside ?
if (!Box.intersectsWithBox(box))
return;
// fully inside ?
parentTest = Box.isFullInside(box)?2:1;
}
#else
if (Box.intersectsWithBox(box))
#endif
{
const u32 cnt = IndexData->size();
u32 i; // new ISO for scoping problem in some compilers
for (i=0; i<cnt; ++i)
{
const s32 idxcnt = (*IndexData)[i].Indices.size();
if (idxcnt)
{
memcpy(&idxdata[i].Indices[idxdata[i].CurrentSize],
&(*IndexData)[i].Indices[0], idxcnt * sizeof(s16));
idxdata[i].CurrentSize += idxcnt;
}
}
for (i=0; i!=8; ++i)
if (Children[i])
Children[i]->getPolys(box, idxdata,parentTest);
}
}
// returns all ids of polygons partially or full enclosed
// by the view frustum.
void getPolys(const scene::SViewFrustum& frustum, SIndexData* idxdata,u32 parentTest) const
{
u32 i; // new ISO for scoping problem in some compilers
// if parent is fully inside, no further check for the children is needed
#if defined (OCTREE_PARENTTEST )
if ( parentTest != 2 )
#endif
{
#if defined (OCTREE_PARENTTEST )
parentTest = 2;
#endif
for (i=0; i!=scene::SViewFrustum::VF_PLANE_COUNT; ++i)
{
core::EIntersectionRelation3D r = Box.classifyPlaneRelation(frustum.planes[i]);
if ( r == core::ISREL3D_FRONT )
return;
#if defined (OCTREE_PARENTTEST )
if ( r == core::ISREL3D_CLIPPED )
parentTest = 1; // must still check children
#endif
}
}
const u32 cnt = IndexData->size();
for (i=0; i!=cnt; ++i)
{
s32 idxcnt = (*IndexData)[i].Indices.size();
if (idxcnt)
{
memcpy(&idxdata[i].Indices[idxdata[i].CurrentSize],
&(*IndexData)[i].Indices[0], idxcnt * sizeof(s16));
idxdata[i].CurrentSize += idxcnt;
}
}
for (i=0; i!=8; ++i)
if (Children[i])
Children[i]->getPolys(frustum, idxdata,parentTest);
}
//! for debug purposes only, collects the bounding boxes of the node
void getBoundingBoxes(const core::aabbox3d<f32>& box,
core::array< const core::aabbox3d<f32>* >&outBoxes) const
{
if (Box.intersectsWithBox(box))
{
outBoxes.push_back(&Box);
for (u32 i=0; i!=8; ++i)
if (Children[i])
Children[i]->getBoundingBoxes(box, outBoxes);
}
}
private:
core::aabbox3df Box;
core::array<SIndexChunk>* IndexData;
OctreeNode* Children[8];
u32 Depth;
};
OctreeNode* Root;
SIndexData* IndexData;
u32 IndexDataCount;
u32 NodeCount;
};
} // end namespace
#endif
|