diff --git a/src/main.rs b/src/main.rs index 1e5da77..1a357b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,15 @@ enum Rule { struct RuleStep { // The geometry generated at this step geom: Mesh, - // The next rule to run on this geometry + // The next rule to run on this geometry. If EmptyRule, then stop + // here (and 'xform' is irrelevant). rule: Box, // The transformation to apply to any results of 'rule' (if // applicable) xform: Mat4, } -fn test_rule(_v: Vec) -> Vec { +fn cube_thing_rule(_v: Vec) -> Vec { let mesh = MeshBuilder::new().cube().build().unwrap(); @@ -40,7 +41,7 @@ fn test_rule(_v: Vec) -> Vec { let m: Mat4 = rot * Matrix4::from_scale(0.5) * Matrix4::from_translation(vec3(6.0, 0.0, 0.0)); - let r = Rule::Recurse(test_rule); + let r = Rule::Recurse(cube_thing_rule); let mut m2 = mesh.clone(); m2.apply_transformation(m); RuleStep { geom: m2, rule: Box::new(r), xform: m } @@ -55,7 +56,11 @@ fn test_rule(_v: Vec) -> Vec { // TODO: Do I want to make 'geom' shared somehow, maybe with Rc? I // could end up having a lot of identical geometry that need not be -// duplicated until it is transformed into the global space +// duplicated until it is transformed into the global space. +// +// This might produce bigger gains if I rewrite rule_to_mesh so that +// rather than repeatedly transforming meshes, it stacks +// transformations and then applies them all at once. fn rule_to_mesh(rule: &Rule, iters_left: u32) -> (Mesh, u32) { @@ -90,28 +95,6 @@ fn rule_to_mesh(rule: &Rule, iters_left: u32) -> (Mesh, u32) { (mesh, nodes) } -// This isn't kosher: -//type Rule = fn (Vec) -> (Mesh, Vec<(Mesh, Box)>); - -fn mesh_builder_example() -> Result { - let indices: Vec = vec![0, 1, 2, - 0, 2, 3, - 0, 3, 1]; - let positions: Vec = vec![0.0, 0.0, 0.0, - 1.0, 0.0, -0.5, - -1.0, 0.0, -0.5, - 0.0, 0.0, 1.0]; - let mesh = MeshBuilder::new(). - with_indices(indices). - with_positions(positions). - build()?; - - assert_eq!(mesh.no_faces(), 3); - assert_eq!(mesh.no_vertices(), 4); - - Ok(mesh) -} - fn print_vector(v: &Vec4) -> String { return format!("{},{},{},{}", v.x, v.y, v.z, v.w); } @@ -139,38 +122,9 @@ fn main() { // Export the bounding box to an obj file std::fs::write("foo.obj", mesh.parse_as_obj()).unwrap(); - // Try some vector stuff: + let r = Rule::Recurse(cube_thing_rule); - let m: Mat4 = Matrix4::from_translation(vec3(5.0, 0.0, 0.0)); - println!("translation: "); - print_matrix(&m); - /*print_vector(&m.x); - print_vector(&m.y); - print_vector(&m.z); - print_vector(&m.w);*/ - - let m2: Mat4 = Matrix4::from_scale(2.0); - println!("scale: "); - print_matrix(&m2); - let m3 = m * m2; - println!("translation * scale: "); - print_matrix(&m3); - let m4 = m2 * m; - println!("scale * translation: "); - print_matrix(&m4); - - let mut mesh2 = mesh_builder_example().unwrap(); - std::fs::write("foo2.obj", mesh2.parse_as_obj()).unwrap(); - - mesh2.apply_transformation(m); - - mesh2.append(&mesh); - - std::fs::write("foo3.obj", mesh2.parse_as_obj()).unwrap(); - - let r = Rule::Recurse(test_rule); - - let max_iters = 4; + let max_iters = 2; println!("Running rules..."); let (cubemesh, nodes) = rule_to_mesh(&r, max_iters); println!("Collected {} nodes, produced {} faces, {} vertices",