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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With