Skip to content

Instantly share code, notes, and snippets.

@eguven
Created July 13, 2018 12:58
Show Gist options
  • Save eguven/fdc8971b3e70ccda680d6f7e860c59a2 to your computer and use it in GitHub Desktop.
Save eguven/fdc8971b3e70ccda680d6f7e860c59a2 to your computer and use it in GitHub Desktop.
Playing with boxes, rust, kiss3d
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_wireframe(window: &mut Window) {
let axis_length = 32.0;
let axis_translation = axis_length / 2.0;
let mut container = window.add_cube(32.0, 32.0, 32.0);
container.set_local_translation(Translation::<_, nalgebra::U3>::new(
axis_translation,
axis_translation,
axis_translation,
));
container.set_points_size(1.0);
container.set_lines_width(1.0);
container.set_surface_rendering_activation(false);
}
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
}
use std::{thread, time};
fn sleep(millis: u64) {
let duration = time::Duration::from_millis(millis);
thread::sleep(duration);
}
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("dazCubes on coordinate system (kiss3d)");
add_wireframe(&mut window);
let font = Font::new(&Path::new("/Library/Fonts/STIXGeneral.otf"), 60);
let mut camera = ArcBall::new(Point3::new(60.0, 32.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;
sleep(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment