graph.rs 991 B

12345678910111213141516171819202122232425262728293031
  1. use cgmath::{Matrix4,SquareMatrix,Vector3};
  2. use model::Model;
  3. pub enum SceneGraph<'a> {
  4. Translate(f32, f32, f32, Box<SceneGraph<'a>>),
  5. Model(&'a Model),
  6. Many(Vec<SceneGraph<'a>>),
  7. }
  8. impl<'a> SceneGraph<'a> {
  9. /// Call the supplied callback on every element of a scene graph
  10. /// along with its calculated transform matrix
  11. pub fn traverse<F>(&self, callback: &mut F)
  12. where F: FnMut(&Model, &[[f32;4];4]) -> ()
  13. {
  14. self.go(callback, Matrix4::identity() * 0.01);
  15. }
  16. fn go<F>(&self, callback: &mut F, mat: Matrix4<f32>)
  17. where F: FnMut(&'a Model, &[[f32;4];4]) -> ()
  18. {
  19. match self {
  20. &SceneGraph::Model(ref m) => callback(m, &mat.into()),
  21. &SceneGraph::Many(ref graphs) => for g in graphs {
  22. g.go(callback, mat);
  23. },
  24. &SceneGraph::Translate(x, y, z, ref rs) =>
  25. rs.go(callback, mat * Matrix4::from_translation(Vector3::new(x, y, z))),
  26. }
  27. }
  28. }