Skip to content

Commit

Permalink
Merge #241
Browse files Browse the repository at this point in the history
241: Example to load and display a STL file r=kvark a=vishpat



Co-authored-by: Vishal Patil <[email protected]>
  • Loading branch information
bors[bot] and Vishal Patil authored Feb 11, 2021
2 parents 046346c + e75a06d commit 07e47da
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ quick-error = "1.2"
rodio = { version = "0.8", optional = true }
mint = "0.5"
vec_map = "0.8"
stlv = "0.1.3"

# OpenGL
gfx_device_gl = { version = "0.16.2", optional = true }
Expand Down Expand Up @@ -86,6 +87,9 @@ name = "anim"
[[example]]
name = "text"

[[example]]
name = "stl"

[[example]]
name = "aviator"
path = "examples/aviator/main.rs"
Expand Down
64 changes: 64 additions & 0 deletions examples/stl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
extern crate three;
extern crate mint;
extern crate froggy;

use std::env;
use three::{Geometry, Object};
use mint::Point3;
use froggy::WeakPointer;

fn main() {
let mut args = env::args();
let path = args.nth(1).expect("Please provide STL file path");
let mut vertices = vec!();

match stlv::parser::load_file(path.as_str()) {
Ok(model) => {
for triangle in (*model).iter() {
let stl_vertices = triangle.vertices();
vertices.push(Point3 {
x: stl_vertices[0].get_x(),
y: stl_vertices[0].get_y(),
z: stl_vertices[0].get_z(),
});
vertices.push(Point3 {
x: stl_vertices[1].get_x(),
y: stl_vertices[1].get_y(),
z: stl_vertices[1].get_z(),
});
vertices.push(Point3 {
x: stl_vertices[2].get_x(),
y: stl_vertices[2].get_y(),
z: stl_vertices[2].get_z(),
});
}
}
_ => panic!("Failed to parse the STL file {}", path),
}

let geometry = Geometry::with_vertices(vertices);

// Upload the triangle data to the GPU.
let mut window = three::Window::new("Loading STL...");

// Create multiple meshes with the same GPU data and material.
let material = three::material::Wireframe{color: 0xff0000};

let mesh = window.factory.mesh(geometry, material);
window.scene.add(&mesh);

let cam = window.factory.perspective_camera(60.0, 1.0 .. 1000.0);
let mut controls = three::controls::Orbit::builder(&cam)
.position([0.0, 2.0, -5.0])
.target([0.0, 0.0, 0.0])
.build();

let dir_light = window.factory.directional_light(0xffffff, 0.9);
dir_light.look_at([15.0, 35.0, 35.0], [0.0, 0.0, 2.0], None);
window.scene.add(&dir_light);

while window.update() && !window.input.hit(three::KEY_ESCAPE) {
controls.update(&window.input);
window.render(&cam);
}
}

0 comments on commit 07e47da

Please sign in to comment.