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