graph.rs 852 B

1234567891011121314151617181920212223242526272829
  1. use cgmath::{Matrix4,SquareMatrix,Vector3,Vector4};
  2. use model::Model;
  3. pub enum SceneGraph {
  4. Translate(f32, f32, f32, Box<SceneGraph>),
  5. Model(Model),
  6. Many(Vec<SceneGraph>),
  7. }
  8. impl SceneGraph {
  9. pub fn traverse<F>(&self, callback: &mut F)
  10. where F: FnMut(&Model, &[[f32;4];4]) -> ()
  11. {
  12. self.go(callback, Matrix4::identity() * 0.01);
  13. }
  14. fn go<F>(&self, callback: &mut F, mat: Matrix4<f32>)
  15. where F: FnMut(&Model, &[[f32;4];4]) -> ()
  16. {
  17. match self {
  18. &SceneGraph::Model(ref m) => callback(m, &mat.into()),
  19. &SceneGraph::Many(ref graphs) => for g in graphs {
  20. g.go(callback, mat);
  21. },
  22. &SceneGraph::Translate(x, y, z, ref rs) =>
  23. rs.go(callback, mat * Matrix4::from_translation(Vector3::new(x, y, z))),
  24. }
  25. }
  26. }