Browse Source

First pass at a scene graph representation

Getty Ritter 7 years ago
parent
commit
44668a07e2
3 changed files with 32 additions and 0 deletions
  1. 1 0
      therm_model/Cargo.toml
  2. 29 0
      therm_model/src/graph.rs
  3. 2 0
      therm_model/src/lib.rs

+ 1 - 0
therm_model/Cargo.toml

@@ -6,4 +6,5 @@ authors = ["Getty Ritter <gdritter@galois.com>"]
 [dependencies]
 glium = "*"
 image = "*"
+cgmath = "*"
 therm_util = { path = "../therm_util" }

+ 29 - 0
therm_model/src/graph.rs

@@ -0,0 +1,29 @@
+use cgmath::{Matrix4,SquareMatrix,Vector3,Vector4};
+use model::Model;
+
+pub enum SceneGraph {
+    Translate(f32, f32, f32, Box<SceneGraph>),
+    Model(Model),
+    Many(Vec<SceneGraph>),
+}
+
+impl SceneGraph {
+    pub fn traverse<F>(&self, callback: &mut F)
+        where F: FnMut(&Model, &[[f32;4];4]) -> ()
+    {
+        self.go(callback, Matrix4::identity() * 0.01);
+    }
+
+    fn go<F>(&self, callback: &mut F, mat: Matrix4<f32>)
+        where F: FnMut(&Model, &[[f32;4];4]) -> ()
+    {
+        match self {
+            &SceneGraph::Model(ref m) => callback(m, &mat.into()),
+            &SceneGraph::Many(ref graphs) => for g in graphs {
+                g.go(callback, mat);
+            },
+            &SceneGraph::Translate(x, y, z, ref rs) =>
+                rs.go(callback, mat * Matrix4::from_translation(Vector3::new(x, y, z))),
+        }
+    }
+}

+ 2 - 0
therm_model/src/lib.rs

@@ -1,9 +1,11 @@
 #[macro_use]
 extern crate glium;
 extern crate image;
+extern crate cgmath;
 extern crate therm_util;
 
 pub mod model;
+pub mod graph;
 
 #[cfg(test)]
 mod tests {