Jump to content

  • Log In with Google      Sign In   
  • Create Account

Octree for object culling


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 newtechnology   Members   -  Reputation: 790

Like
0Likes
Like

Posted 18 April 2014 - 07:01 AM

I want to use octree for object culling. My idea is as follows:


struct Node 

{

   bool Visible;

   AABB Box;

   bool IsLeaf;

   Node* Children[8];

};

 

//called only once for objects at init time

 

Node** Octree::AddObject(AABB box, Node* parent)

{

     if (parent->IsLeaf) 

       return &Node;

   

    if (IntersectAABBAABB(parent->box, box))

    {

          for (int i=0; i < 8; +++i)

          { 

 

                   if  (IntersectAABBAABB(parent->Children[i]->box, box))

                           AddObject(box, parent);

          }

 

    }

 

}

 

//update is called everyframe

void Octree::Update()

{  

    if (IntersectAABBFrustum(parent->box, frustum)

   {  

            parent->visible = true;

           for (int i = 0; i < 8; ++i)

              {

                //move down the tree 

               .....

 

                //check for box against frustum

                  .. 

                     //.if visible

                       

                     //set visible bool to true

                   ...

                     // check it's children and do samething

                 ...

                   //else if false, set its all children's visible bool to false without checking

                   

              }

   } else

    {

           parent->visible = false; 

            //set all children's visible bool to false without checking

     }

 

}

 

//get node for this object

 

Object* house;

 

Node** houseNode = Octree.AddObject(house.box);

 

if (*houseNode->Visible)

//draw

 

 

I need help, do you see any problems? what should I do when object is inbetween Node AABBs ?

Please give me suggestions/improvements for octree or any other data structure object culling.

I do my terrain culling in hull shader.

 

 

Also I got another problem as boxes are not in same space, I think transforming them to world space would result into wrong assumptions about object being in a particular node because AABB in world space does not tightly surround the object.


Edited by newtechnology, 18 April 2014 - 07:05 AM.


Sponsor:

#2 xexuxjy   Members   -  Reputation: 653

Like
0Likes
Like

Posted 22 April 2014 - 07:49 AM

general approach when objects are shared between node aabb's is to move them up to the parent until they fit into an appropriately sized box. If you find most of your objects are large enough to sit near the top of your tree then you'll lose a lot of benefits of having the octree.



#3 Waterlimon   Crossbones+   -  Reputation: 2644

Like
0Likes
Like

Posted 22 April 2014 - 10:24 AM

You can also try placing the objects using one of their corners instead of the center, but then you need to check 4 adjacent octree nodes to find out if an object occupies a spot instead of only a single one (because the corner is in one cell, but the object itself can occupy the 3 adjacent cells, or in other words each corner of the object occupies one octree cell)

 

This should prevent objects being lifted to the root when they are at a bad spot, however youll probably need to add some shortcuts to make accessing adjacent octree cells not kill performance.

 

The idea is the same as placing objects of side X into a uniform grid of cell size X*X based on their top left (lets say) corner, which means the object itself occupies the cell where the top left corner is but also the one to the right, down and diagonally down-right.


o3o


#4 froop   Members   -  Reputation: 636

Like
0Likes
Like

Posted 22 April 2014 - 05:18 PM

tiny improvement... instead of the isLeaf variable you could just check if children == null.

 

Edit: sorry, was wrong.


Edited by froop, 22 April 2014 - 05:21 PM.


#5 Alundra   Members   -  Reputation: 932

Like
0Likes
Like

Posted 23 April 2014 - 10:03 AM

The real question about octree is how achieve the ocree correct when you have object who move ?

You can't regenerate all the octree each frame, a listener is needed to know when object need to check for a change ?



#6 clb   Members   -  Reputation: 1795

Like
2Likes
Like

Posted 23 April 2014 - 04:43 PM

A big thing that strikes me here is the variable bool Node::Visible. There should not be such cache variable or stored data of a visible node. It might be the case for now that you make only single camera queries for rendering purposes, but later on you could have game logic-specific code making queries to the same acceleration structure, i.e. using the structure to do raycasting queries, or "give me objects inside this bounding sphere/box", and so on. It doesn't make sense to hardcode the Octree to contain state pertaining to the world camera.

 

Instead, I'd make a separate QueryResult object, which contains a vector/hash table of collected visible nodes. Then, when you are about to render:

 

QueryResult visibleObjects;
octree.FindVisibleObjects(cameraFrustum, &visibleObjects)
sort(visibleObjects, sceneObjectsSortPredicate); 
for (object in visibleObjects)
   render(object);

 

This collecting pass of scene objects allows you to sort, which an important optimization, i.e. it gives the ability to sort front-to-back for depth, and sort to group objects that use the same material/shader/texture, and so on.

 

At minimum, if you need to keep that boolean, strongly document the semantics that the bool Node::Visible refers to either the most recent query (effectively making it a cache for the most recent visibility query operation) or rename it to bool Node::VisibleToMainCamera; to signify that the boolean refers to whether the node is visible to the certain specific god camera that the player views the scene through.

 

Then, I'd replace Node *children[8]; with a Node *children, which I'd allocate with a children = new Node[8]; statement. Also, remove the IsLeaf boolean and replace it with a function IsLeaf() const { return children == nullptr; }

 

Also, Octree::AddObject does 9 calls to a generic IntersectAABBAABB function. That is a bit excessive, and instead I'd do something like:

 

Octree::AddObject(Node *node, Object *object, const AABB &objectAABB)
{
   vec center = node->CenterPoint();
   if (objectAABB.maxX < center.x)
   {
     // left half
     if (objectAABB.maxY < center.y)
     {
       // top half
       // test z ...
     }
     else if (objectAABB.minY > center.y)
    {
       // bottom half
       // test z...
    }
   }
   else if (objectAABB.minX > center.x)
   {
     // test y and z.
   }
}
 
Octree::AddObject(Node *node, Object *object, const AABB &objectAABB)
{
  return AddObject(node, object, ComputeObjectAABB(object));
}
 

 

this form makes one efficient AABB intersection test to place the object AABB in the proper octant, and manage the case where it straddles multiple octants. As was suggested above, there are different ways to handle this:

  • support having objects up the tree, and not just in leaves. Objects will be placed in bottommost node they fully fit
  • add to multiple children
  • add to fixed child e.g. with the top-left rule that was presented above. Then when querying, see the top-left neighbors.
  • make the octree what is known as a "loose octree" - there the neighboring octree nodes overlap in volume.

There is no single best way to handle the straddling issue, but they are a bit of "what works best" decision in my experience.


Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#7 newtechnology   Members   -  Reputation: 790

Like
0Likes
Like

Posted 24 April 2014 - 01:45 AM

A big thing that strikes me here is the variable bool Node::Visible. There should not be such cache variable or stored data of a visible node. It might be the case for now that you make only single camera queries for rendering purposes, but later on you could have game logic-specific code making queries to the same acceleration structure, i.e. using the structure to do raycasting queries, or "give me objects inside this bounding sphere/box", and so on. It doesn't make sense to hardcode the Octree to contain state pertaining to the world camera.

 

Instead, I'd make a separate QueryResult object, which contains a vector/hash table of collected visible nodes. Then, when you are about to render:

QueryResult visibleObjects;
octree.FindVisibleObjects(cameraFrustum, &visibleObjects)
sort(visibleObjects, sceneObjectsSortPredicate); 
for (object in visibleObjects)
   render(object);

This collecting pass of scene objects allows you to sort, which an important optimization, i.e. it gives the ability to sort front-to-back for depth, and sort to group objects that use the same material/shader/texture, and so on.

 

At minimum, if you need to keep that boolean, strongly document the semantics that the bool Node::Visible refers to either the most recent query (effectively making it a cache for the most recent visibility query operation) or rename it to bool Node::VisibleToMainCamera; to signify that the boolean refers to whether the node is visible to the certain specific god camera that the player views the scene through.

 

Then, I'd replace Node *children[8]; with a Node *children, which I'd allocate with a children = new Node[8]; statement. Also, remove the IsLeaf boolean and replace it with a function IsLeaf() const { return children == nullptr; }

 

Also, Octree::AddObject does 9 calls to a generic IntersectAABBAABB function. That is a bit excessive, and instead I'd do something like:

Octree::AddObject(Node *node, Object *object, const AABB &objectAABB)
{
   vec center = node->CenterPoint();
   if (objectAABB.maxX < center.x)
   {
     // left half
     if (objectAABB.maxY < center.y)
     {
       // top half
       // test z ...
     }
     else if (objectAABB.minY > center.y)
    {
       // bottom half
       // test z...
    }
   }
   else if (objectAABB.minX > center.x)
   {
     // test y and z.
   }
}
 
Octree::AddObject(Node *node, Object *object, const AABB &objectAABB)
{
  return AddObject(node, object, ComputeObjectAABB(object));
}
 

this form makes one efficient AABB intersection test to place the object AABB in the proper octant, and manage the case where it straddles multiple octants. As was suggested above, there are different ways to handle this:

  • support having objects up the tree, and not just in leaves. Objects will be placed in bottommost node they fully fit
  • add to multiple children
  • add to fixed child e.g. with the top-left rule that was presented above. Then when querying, see the top-left neighbors.
  • make the octree what is known as a "loose octree" - there the neighboring octree nodes overlap in volume.

There is no single best way to handle the straddling issue, but they are a bit of "what works best" decision in my experience.

 

 

Thanks for this, I'll change it.

 

 

EDIT: I've another question,

My octree does a TriangleAABB intersection to find out which triangle lies in which node,

I've about 125000 triangles. By how much time will this octree increase my resource build up time? (I've not yet tested octree.)

void Octree::BuildOctree(OctreeNode* parent, const std::vector<UINT>& indices)
{
size_t triCount = indices.size() / 3;
 
if(triCount < 10000) 
{
parent->IsLeaf = true;
parent->Indices = indices;
}
else
{
parent->IsLeaf = false;
 
XNA::AxisAlignedBox subbox[8];
parent->Subdivide(subbox);
 
for(int i = 0; i < 8; ++i)
{

parent->Children[i] = new OctreeNode();
parent->Children[i]->Bounds = subbox[i];
 
// Find triangles that intersect this node's bounding box.
std::vector<UINT> intersectedTriangleIndices;
for(size_t j = 0; j < triCount; ++j)
{
UINT i0 = indices[j*3+0];
UINT i1 = indices[j*3+1];
UINT i2 = indices[j*3+2];
 
XMVECTOR v0 = XMLoadFloat3(&mVertices[i0]);
XMVECTOR v1 = XMLoadFloat3(&mVertices[i1]);
XMVECTOR v2 = XMLoadFloat3(&mVertices[i2]);
 
if(XNA::IntersectTriangleAxisAlignedBox(v0, v1, v2, &subbox[i]))
{
intersectedTriangleIndices.push_back(i0);
intersectedTriangleIndices.push_back(i1);
intersectedTriangleIndices.push_back(i2);
}
}
 

BuildOctree(parent->Children[i], intersectedTriangleIndices);
}
}
}

PS: my resource build up time is 2 minutes (it was faster on my old laptop)


Edited by newtechnology, 24 April 2014 - 02:11 AM.


#8 clb   Members   -  Reputation: 1795

Like
0Likes
Like

Posted 07 May 2014 - 01:53 PM

Hmm, if it takes 2 minutes to build an octree that consists of 125000 triangles, that sounds very slow. Some observations from the code you posted:

 

for(int i = 0; i < 8; ++i)
{
parent->Children[i] = new OctreeNode();
...
}

 

As a micro-optimization, you could just refactor so that you can do something like

 

parent->Children[i] = new OctreeNode[8];
 
That reduces the number of operator new() calls by eight-fold.
 
Also, the subdivision and termination logic is potentially very problematic. It might be that the subdivision never terminates, since that code only stops if there are less than 10k triangles in a one node. Think of a pathological case where you have 10k triangles that are *identical* in their positions. Then they always go in the exact same children since they overlap, and the loop will infinitely recurse until it runs out of memory.
 
Another potential problem is that you place triangles that overlap to duplicate nodes, but never check if that is a win. For example, you could be subdividing a node with 10k triangles, and end up cloning those triangles so that the resulting child nodes each contain a large portion of them (think e.g. all 8 nodes carrying 5000 triangles, which is 40k triangles total, even when the parent had only 10k). The code should have some heuristic to decide if subdivision is more trouble than it is worth, and then decide to stop if so.
 
Third, the code tests each triangle against eight aabbs at each level. It would be better to combine those checks: avoid doing a general triangle-aabb test, and instead do just a series of triangle-plane tests (one for +x, +y and +z facing planes), and use that info to find which octant a triangle belongs in. That saves a lot of redundant work.

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#9 Waterlimon   Crossbones+   -  Reputation: 2644

Like
0Likes
Like

Posted 07 May 2014 - 01:58 PM

Allocate the octree nodes from a pool.


o3o





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS