openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.9k stars 2.55k forks source link

ofNode global matrix caching #4205

Open chuckleplant opened 9 years ago

chuckleplant commented 9 years ago

I went ahead and coded ofNode global matrix caching. Before PR-ing I wanted to discuss whether it's a good approach. You can see the changes here in a feature branch.

What this change does is computing the global matrix only if the local matrix changes. To do so every node keeps not only a parent ofNode* but also a set of children. This way we can flag all children as "dirty" whenever the node's matrix changes.

I've used a vector to make iteration faster, and a map to relate vector indices to the ofNode*. Tried first with an unordered_set<ofNode*> but access was slower.

Drawing, specially when we have a deep hierarchy of nodes is much faster than without caching. Updating (pan, tilt, etc) is a bit slower due to the flagging as dirty process.

I'll do a clean PR when it's polished!

bilderbuchi commented 9 years ago

ping @openframeworks/2d-3d

arturoc commented 9 years ago

isn't it easier to just keep a dirty flag for each node's local transformation? when a node needs it's global it just transverses the hierarchy checking if anything is dirty, if it is it recalculates the parent transform otherwise it uses the cached global. having so many collections to transverse seems kind of complicated for something that probably won't optimize a lot i think

chuckleplant commented 9 years ago

Yes you're right. No need to keep all children nodes listed. I'll give it a try.

However if we have one parent node with multiple children. Say all children are clean and parent is dirty. Then one children checks for global position and sees parent is dirty, updates parent (parent is now clean) and then updates itself. From then on all other children see the parent as clean when checking for global position.

I think we need to keep a set of all children nodes to notify they're in dirty state.

There is a big speed up for big hierarchies. I at least use ofNode for drawing geometry a lot and this would mean a nice boost. I'll try to upload numbers.