Commit 65cfbeb2 authored by hybrid's avatar hybrid

Cleanup and some speed improvements.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@1440 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 7b4d76de
This diff is collapsed.
...@@ -20,7 +20,7 @@ namespace scene ...@@ -20,7 +20,7 @@ namespace scene
//! constructor //! constructor
COctTreeSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id, COctTreeSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
s32 minimalPolysPerNode=128); s32 minimalPolysPerNode=512);
//! destructor //! destructor
virtual ~COctTreeSceneNode(); virtual ~COctTreeSceneNode();
...@@ -73,7 +73,6 @@ namespace scene ...@@ -73,7 +73,6 @@ namespace scene
video::E_VERTEX_TYPE vertexType; video::E_VERTEX_TYPE vertexType;
core::array< video::SMaterial > Materials; core::array< video::SMaterial > Materials;
//IMesh* Mesh;
core::stringc MeshName; core::stringc MeshName;
s32 MinimalPolysPerNode; s32 MinimalPolysPerNode;
s32 PassCount; s32 PassCount;
......
...@@ -39,13 +39,13 @@ private: ...@@ -39,13 +39,13 @@ private:
{ {
SOctTreeNode() SOctTreeNode()
{ {
for (s32 i=0; i<8; ++i) for (u32 i=0; i!=8; ++i)
Child[i] = 0; Child[i] = 0;
} }
~SOctTreeNode() ~SOctTreeNode()
{ {
for (s32 i=0; i<8; ++i) for (u32 i=0; i!=8; ++i)
delete Child[i]; delete Child[i];
} }
...@@ -57,9 +57,10 @@ private: ...@@ -57,9 +57,10 @@ private:
void constructOctTree(SOctTreeNode* node); void constructOctTree(SOctTreeNode* node);
void deleteEmptyNodes(SOctTreeNode* node); void deleteEmptyNodes(SOctTreeNode* node);
void getTrianglesFromOctTree(SOctTreeNode* node, s32& trianglesWritten, s32 maximumSize, void getTrianglesFromOctTree(SOctTreeNode* node, s32& trianglesWritten,
const core::aabbox3d<f32>& box, const core::matrix4* transform, s32 maximumSize, const core::aabbox3d<f32>& box,
core::triangle3df* triangles) const; const core::matrix4* transform,
core::triangle3df* triangles) const;
SOctTreeNode* Root; SOctTreeNode* Root;
s32 NodeCount; s32 NodeCount;
......
...@@ -9,20 +9,18 @@ ...@@ -9,20 +9,18 @@
#include "S3DVertex.h" #include "S3DVertex.h"
#include "aabbox3d.h" #include "aabbox3d.h"
#include "irrArray.h" #include "irrArray.h"
#include "irrString.h"
namespace irr namespace irr
{ {
//! template octtree. T must be a vertex type which has a member //! template octtree.
//! called .Pos, which is a core::vertex3df position. /** T must be a vertex type which has a member
called .Pos, which is a core::vertex3df position. */
template <class T> template <class T>
class OctTree class OctTree
{ {
public: public:
u32 nodeCount;
struct SMeshChunk struct SMeshChunk
{ {
core::array<T> Vertices; core::array<T> Vertices;
...@@ -44,45 +42,38 @@ public: ...@@ -44,45 +42,38 @@ public:
}; };
//! Constructor
//! constructor OctTree(const core::array<SMeshChunk>& meshes, s32 minimalPolysPerNode=128) :
OctTree(const core::array<SMeshChunk>& meshes, s32 minimalPolysPerNode=128) IndexData(0), IndexDataCount(meshes.size()), NodeCount(0)
{ {
nodeCount = 0;
IndexDataCount = meshes.size();
IndexData = new SIndexData[IndexDataCount]; IndexData = new SIndexData[IndexDataCount];
// construct array of all indices // construct array of all indices
core::array<SIndexChunk>* indexChunks = new core::array<SIndexChunk>; core::array<SIndexChunk>* indexChunks = new core::array<SIndexChunk>;
SIndexChunk ic; indexChunks->reallocate(meshes.size());
for (u32 i=0; i!=meshes.size(); ++i)
for (u32 i=0; i<meshes.size(); ++i)
{ {
IndexData[i].CurrentSize = 0; IndexData[i].CurrentSize = 0;
IndexData[i].MaxSize = meshes[i].Indices.size(); IndexData[i].MaxSize = meshes[i].Indices.size();
IndexData[i].Indices = new u16[IndexData[i].MaxSize]; IndexData[i].Indices = new u16[IndexData[i].MaxSize];
ic.MaterialId = meshes[i].MaterialId; indexChunks->push_back(SIndexChunk());
indexChunks->push_back(ic); SIndexChunk& tic = indexChunks->getLast();
SIndexChunk& tic = (*indexChunks)[i]; tic.MaterialId = meshes[i].MaterialId;
tic.Indices = meshes[i].Indices;
for (u32 t=0; t<meshes[i].Indices.size(); ++t)
tic.Indices.push_back(meshes[i].Indices[t]);
} }
// create tree // create tree
Root = new OctTreeNode(NodeCount, 0, meshes, indexChunks, minimalPolysPerNode);
Root = new OctTreeNode(nodeCount, 0, meshes, indexChunks, minimalPolysPerNode);
} }
//! returns all ids of polygons partially or fully enclosed //! returns all ids of polygons partially or fully enclosed
//! by this bounding box. //! by this bounding box.
void calculatePolys(const core::aabbox3d<f32>& box) void calculatePolys(const core::aabbox3d<f32>& box)
{ {
for (u32 i=0; i<IndexDataCount; ++i) for (u32 i=0; i!=IndexDataCount; ++i)
IndexData[i].CurrentSize = 0; IndexData[i].CurrentSize = 0;
Root->getPolys(box, IndexData, 0); Root->getPolys(box, IndexData, 0);
...@@ -92,28 +83,32 @@ public: ...@@ -92,28 +83,32 @@ public:
//! by a view frustum. //! by a view frustum.
void calculatePolys(const scene::SViewFrustum& frustum) void calculatePolys(const scene::SViewFrustum& frustum)
{ {
for (u32 i=0; i<IndexDataCount; ++i) for (u32 i=0; i!=IndexDataCount; ++i)
IndexData[i].CurrentSize = 0; IndexData[i].CurrentSize = 0;
Root->getPolys(frustum, IndexData, 0); Root->getPolys(frustum, IndexData, 0);
} }
const SIndexData* getIndexData() const
SIndexData* getIndexData()
{ {
return IndexData; return IndexData;
} }
u32 getIndexDataCount() u32 getIndexDataCount() const
{ {
return IndexDataCount; return IndexDataCount;
} }
// for debug purposes only, renders the bounding boxes of the tree u32 getNodeCount() const
void renderBoundingBoxes(const core::aabbox3d<f32>& box,
core::array< core::aabbox3d<f32> >&outBoxes)
{ {
Root->renderBoundingBoxes(box, outBoxes); 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 //! destructor
...@@ -127,7 +122,6 @@ public: ...@@ -127,7 +122,6 @@ public:
} }
private: private:
// private inner class // private inner class
class OctTreeNode class OctTreeNode
{ {
...@@ -144,7 +138,7 @@ private: ...@@ -144,7 +138,7 @@ private:
u32 i; // new ISO for scoping problem with different compilers u32 i; // new ISO for scoping problem with different compilers
for (i=0; i<8; ++i) for (i=0; i!=8; ++i)
Children[i] = 0; Children[i] = 0;
if (indices->empty()) if (indices->empty())
...@@ -192,23 +186,20 @@ private: ...@@ -192,23 +186,20 @@ private:
core::array<u16> keepIndices; core::array<u16> keepIndices;
if (totalPrimitives > minimalPolysPerNode && !Box.isEmpty()) if (totalPrimitives > minimalPolysPerNode && !Box.isEmpty())
for (s32 ch=0; ch<8; ++ch) for (u32 ch=0; ch!=8; ++ch)
{ {
box.reset(middle); box.reset(middle);
box.addInternalPoint(edges[ch]); box.addInternalPoint(edges[ch]);
// create indices for child // create indices for child
core::array<SIndexChunk>* cindexChunks = new core::array<SIndexChunk>;
bool added = false; bool added = false;
core::array<SIndexChunk>* cindexChunks = new core::array<SIndexChunk>;
cindexChunks->reallocate(allmeshdata.size());
for (i=0; i<allmeshdata.size(); ++i) for (i=0; i<allmeshdata.size(); ++i)
{ {
SIndexChunk ic; cindexChunks->push_back(SIndexChunk());
ic.MaterialId = allmeshdata[i].MaterialId; SIndexChunk& tic = cindexChunks->getLast();
cindexChunks->push_back(ic); tic.MaterialId = allmeshdata[i].MaterialId;
SIndexChunk& tic = (*cindexChunks)[i];
for (u32 t=0; t<(*indices)[i].Indices.size(); t+=3) for (u32 t=0; t<(*indices)[i].Indices.size(); t+=3)
{ {
...@@ -246,8 +237,6 @@ private: ...@@ -246,8 +237,6 @@ private:
IndexData = indices; IndexData = indices;
} }
// destructor // destructor
~OctTreeNode() ~OctTreeNode()
{ {
...@@ -257,8 +246,6 @@ private: ...@@ -257,8 +246,6 @@ private:
delete Children[i]; delete Children[i];
} }
// returns all ids of polygons partially or full enclosed // returns all ids of polygons partially or full enclosed
// by this bounding box. // by this bounding box.
void getPolys(const core::aabbox3d<f32>& box, SIndexData* idxdata, u32 parentTest ) const void getPolys(const core::aabbox3d<f32>& box, SIndexData* idxdata, u32 parentTest ) const
...@@ -267,22 +254,21 @@ private: ...@@ -267,22 +254,21 @@ private:
if ( parentTest != 2 ) if ( parentTest != 2 )
{ {
// partially inside ? // partially inside ?
parentTest = (u32) Box.intersectsWithBox(box); if (!Box.intersectsWithBox(box))
if ( 0 == parentTest )
return; return;
// fully inside ? // fully inside ?
parentTest+= Box.isFullInside(box); parentTest = Box.isFullInside(box)?2:1;
} }
//if (Box.intersectsWithBox(box)) //if (Box.intersectsWithBox(box))
{ {
u32 cnt = IndexData->size(); const u32 cnt = IndexData->size();
u32 i; // new ISO for scoping problem in some compilers u32 i; // new ISO for scoping problem in some compilers
for (i=0; i<cnt; ++i) for (i=0; i<cnt; ++i)
{ {
s32 idxcnt = (*IndexData)[i].Indices.size(); const s32 idxcnt = (*IndexData)[i].Indices.size();
if (idxcnt) if (idxcnt)
{ {
...@@ -292,19 +278,17 @@ private: ...@@ -292,19 +278,17 @@ private:
} }
} }
for (i=0; i<8; ++i) for (i=0; i!=8; ++i)
if (Children[i]) if (Children[i])
Children[i]->getPolys(box, idxdata,parentTest); Children[i]->getPolys(box, idxdata,parentTest);
} }
} }
// returns all ids of polygons partially or full enclosed // returns all ids of polygons partially or full enclosed
// by the view frustum. // by the view frustum.
void getPolys(const scene::SViewFrustum& frustum, SIndexData* idxdata,u32 parentTest) const void getPolys(const scene::SViewFrustum& frustum, SIndexData* idxdata,u32 parentTest) const
{ {
s32 i; // new ISO for scoping problem in some compilers u32 i; // new ISO for scoping problem in some compilers
// not fully inside // not fully inside
//if ( parentTest != 2 ) //if ( parentTest != 2 )
...@@ -312,25 +296,27 @@ private: ...@@ -312,25 +296,27 @@ private:
core::vector3df edges[8]; core::vector3df edges[8];
Box.getEdges(edges); Box.getEdges(edges);
for (i=0; i<scene::SViewFrustum::VF_PLANE_COUNT; ++i) for (i=0; i!=scene::SViewFrustum::VF_PLANE_COUNT; ++i)
{ {
bool boxInFrustum=false; bool boxInFrustum=false;
for (int j=0; j<8; ++j) for (u32 j=0; j!=8; ++j)
{
if (frustum.planes[i].classifyPointRelation(edges[j]) != core::ISREL3D_FRONT) if (frustum.planes[i].classifyPointRelation(edges[j]) != core::ISREL3D_FRONT)
{ {
boxInFrustum=true; boxInFrustum=true;
break; break;
} }
}
if (!boxInFrustum) // all edges outside if (!boxInFrustum) // all edges outside
return; return;
} }
} }
s32 cnt = IndexData->size(); const u32 cnt = IndexData->size();
for (i=0; i<cnt; ++i) for (i=0; i!=cnt; ++i)
{ {
s32 idxcnt = (*IndexData)[i].Indices.size(); s32 idxcnt = (*IndexData)[i].Indices.size();
...@@ -342,23 +328,22 @@ private: ...@@ -342,23 +328,22 @@ private:
} }
} }
for (i=0; i<8; ++i) for (i=0; i!=8; ++i)
if (Children[i]) if (Children[i])
Children[i]->getPolys(frustum, idxdata,parentTest); Children[i]->getPolys(frustum, idxdata,parentTest);
} }
//! for debug purposes only, collects the bounding boxes of the node
void getBoundingBoxes(const core::aabbox3d<f32>& box,
void renderBoundingBoxes(const core::aabbox3d<f32>& box, core::array< const core::aabbox3d<f32>* >&outBoxes) const
core::array< core::aabbox3d<f32> >&outBoxes)
{ {
if (Box.intersectsWithBox(box)) if (Box.intersectsWithBox(box))
{ {
outBoxes.push_back(Box); outBoxes.push_back(&Box);
for (u32 i=0; i<8; ++i) for (u32 i=0; i!=8; ++i)
if (Children[i]) if (Children[i])
Children[i]->renderBoundingBoxes(box, outBoxes); Children[i]->getBoundingBoxes(box, outBoxes);
} }
} }
...@@ -373,6 +358,7 @@ private: ...@@ -373,6 +358,7 @@ private:
OctTreeNode* Root; OctTreeNode* Root;
SIndexData* IndexData; SIndexData* IndexData;
u32 IndexDataCount; u32 IndexDataCount;
u32 NodeCount;
}; };
} // end namespace } // end namespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment