user406009
user406009

Reputation:

Optimize scene graph

I have a standard scene graph written in opengl with c++.

My scene graph has nodes and shapes.

Nodes are matrices, and they draw all of their children after applying their matrix.

void Node::draw(Affine3f amatrix) const
{
   amatrix = amatrix * matrix;
   for (Drawable* child : childern)
   {
      child->draw(amatrix);
   }
}

Shapes are simply packaged vbos, they take the matrix from the draw call, set it as the uniform modelview matrix, and then draw the vbo.

void Shape::draw(Affine3f mat) const
{
   renderer.setModelView(mat);
   myVertices.draw();
}

I love this design, it is very simple and flexible. But, it is very inefficient, with tons of CPU side matrix multiplications and tons of draw calls.

My question is:

How can I optimize this design, removing both unneeded matrix multiplications and unnecessary draw calls?

Like not recalculating matrices each draw(only calculate changed) and uniting the shapes so that they can be drawn with one call.

Some more information:

Upvotes: 3

Views: 1072

Answers (2)

Ben Ruijl
Ben Ruijl

Reputation: 5143

Are you drawing every element in the tree, even if it isn't visible? If so, you should check out octrees to filter invisible nodes.

You could also try and do most matrix computations in the shaders, by passing them as variables. I also see that your matrices are affine, but maybe you still do an expensive inverse calculation in your implementation. If that's the case, you can check my tutorial to see how to make it cheap.

Upvotes: 2

Jim Buck
Jim Buck

Reputation: 20734

For one thing, I would pass a const & for the incoming matrices. You are passing by value, and if you have some draw functions that don't end up needing to do anything special with the matrix, it's a lot of unnecessary copying.

If you want to prevent matrix calculations if a matrix hasn't changed, you will need to have a "dirty" flag to determine whether or not a matrix's value changed since you last used it. RenderWare did something like this with its matrix stuff.

Otherwise, like in the comment, without seeing your overall design, there's nothing inherently wrong with what you have.

Upvotes: 4

Related Questions