Created
February 11, 2018 01:05
-
-
Save eguven/1d72ebb286a0f8ce1141b68887f1e927 to your computer and use it in GitHub Desktop.
rendering cubes on a coordinate system from json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate kiss3d; | |
extern crate nalgebra; | |
extern crate rand; | |
extern crate serde; | |
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde_json; | |
use kiss3d::camera::ArcBall; | |
use kiss3d::scene::SceneNode; | |
use kiss3d::window::Window; | |
// use kiss3d::light::Light; // window.set_light(Light::StickToCamera); | |
use kiss3d::text::Font; | |
use nalgebra::{Point2, Point3}; | |
use nalgebra::geometry::Translation; | |
use std::fs::File; | |
use std::io::Read; | |
use std::path::Path; | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Cube { | |
x: f32, | |
y: f32, | |
z: f32, | |
wx: f32, | |
wy: f32, | |
wz: f32, | |
} | |
#[derive(Serialize, Deserialize, Debug)] | |
struct BinPack { | |
cubes: Vec<Cube>, | |
} | |
fn add_axes(window: &mut Window) { | |
let axis_length = 32.0; | |
let axis_translation = axis_length / 2.0; | |
let axis_thickness = 0.08; | |
let axis_rgb_r = 190.0 / 256.0; | |
let axis_rgb_g = 70.0 / 256.0; | |
let axis_rgb_b = 30.0 / 256.0; | |
let mut x_axis = window.add_cube(axis_length, axis_thickness, axis_thickness); | |
x_axis.set_color(axis_rgb_r, axis_rgb_g, axis_rgb_b); | |
x_axis.set_local_translation(Translation::<_, nalgebra::U3>::new( | |
axis_translation, | |
0.0, | |
0.0, | |
)); | |
let mut y_axis = window.add_cube(axis_thickness, axis_length, axis_thickness); | |
y_axis.set_color(axis_rgb_r, axis_rgb_g, axis_rgb_b); | |
y_axis.set_local_translation(Translation::<_, nalgebra::U3>::new( | |
0.0, | |
axis_translation, | |
0.0, | |
)); | |
let mut z_axis = window.add_cube(axis_thickness, axis_thickness, axis_length); | |
z_axis.set_color(axis_rgb_r, axis_rgb_g, axis_rgb_b); | |
z_axis.set_local_translation(Translation::<_, nalgebra::U3>::new( | |
0.0, | |
0.0, | |
axis_translation, | |
)); | |
} | |
fn add_floor_plane(window: &mut Window) { | |
let mut floor_plane = window.add_cube(64.0, 0.04, 64.0); | |
floor_plane.set_local_translation(Translation::<_, nalgebra::U3>::new(32.0, 0.02, 32.0)); | |
floor_plane.set_color(100.0 / 256.0, 100.0 / 256.0, 25.0 / 256.0); | |
} | |
fn add_cube(window: &mut Window, x: f32, y: f32, z: f32, wx: f32, wy: f32, wz: f32) -> SceneNode { | |
let mut cube = window.add_cube(wx, wy, wz); | |
cube.set_local_translation(Translation::<_, nalgebra::U3>::new( | |
x + wx / 2.0, | |
y + wy / 2.0, | |
z + wz / 2.0, | |
)); | |
cube.set_color(rand::random(), rand::random(), rand::random()); | |
cube | |
} | |
fn main() { | |
let mut file = File::open("binpack.json").expect("file not found: binpack.json"); | |
let mut data = String::new(); | |
file.read_to_string(&mut data).expect("could not read file"); | |
let binpack: BinPack = serde_json::from_str(&data).expect("error decoding json?"); | |
let mut window = Window::new("cubes on coordinate system (kiss3d)"); | |
add_axes(&mut window); | |
add_floor_plane(&mut window); | |
let font = Font::new(&Path::new("/Library/Fonts/STIXGeneral.otf"), 60); | |
let mut camera = ArcBall::new(Point3::new(60.0, 28.0, 60.0), Point3::origin()); | |
// camera.set_dist(16.0); | |
let mut counter = 0; | |
let mut cube_idx = 0; | |
while window.render_with_camera(&mut camera) { | |
window.draw_text( | |
&format!("{:?}", counter), | |
&Point2::origin(), | |
&font, | |
&Point3::new(1.0, 1.0, 1.0), | |
); | |
let cube = &binpack.cubes[cube_idx]; | |
add_cube( | |
&mut window, | |
cube.x, | |
cube.y, | |
cube.z, | |
cube.wx, | |
cube.wy, | |
cube.wz, | |
); | |
cube_idx += 1; | |
counter += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment