Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgmath version 0.18 and automatic arcball camera placement #19

Merged
merged 5 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ exclude = [
]

[dependencies]
cgmath = "0.17.0"
cgmath = "0.18"

2 changes: 1 addition & 1 deletion examples/curve_geometry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ authors = ["Will Usher <[email protected]>"]
[dependencies]
embree = { path = "../../" }
support = { path = "../support" }
cgmath = "0.17.0"
cgmath = "0.18.0"

2 changes: 1 addition & 1 deletion examples/instancing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ authors = ["Will Usher <[email protected]>"]
[dependencies]
embree = { path = "../../" }
support = { path = "../support" }
cgmath = "0.17.0"
cgmath = "0.18.0"

2 changes: 1 addition & 1 deletion examples/obj_ao_parallel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Will Usher <[email protected]>"]
[dependencies]
embree = { path = "../../" }
support = { path = "../support" }
cgmath = "0.17.0"
cgmath = "0.18.0"
tobj = "0.1.6"
rayon = "1.3"
rand = "0.7"
12 changes: 10 additions & 2 deletions examples/obj_ao_parallel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use cgmath::{InnerSpace, Matrix3, Point2, Vector2, Vector3, Vector4};
use embree::{Device, Geometry, IntersectContext, Ray, RayHit, Scene, TriangleMesh};
use rand::prelude::*;
use rayon::prelude::*;
use support::Camera;
use support::{Camera, AABB};

/// Function to sample a point inside a 2D disk
pub fn concentric_sample_disk(u: Point2<f32>) -> Point2<f32> {
Expand Down Expand Up @@ -222,7 +222,7 @@ fn main() {
// Load the obj
let (models, _) = tobj::load_obj(&Path::new(&args[1])).unwrap();
let mut tri_geoms = Vec::new();

let mut aabb = AABB::default();
for m in models.iter() {
let mesh = &m.mesh;
println!(
Expand All @@ -237,6 +237,11 @@ fn main() {
let mut verts = tris.vertex_buffer.map();
let mut tris = tris.index_buffer.map();
for i in 0..mesh.positions.len() / 3 {
aabb = aabb.union_vec(&Vector3::new(
mesh.positions[i * 3],
mesh.positions[i * 3 + 1],
mesh.positions[i * 3 + 2],
));
verts[i] = Vector4::new(
mesh.positions[i * 3],
mesh.positions[i * 3 + 1],
Expand All @@ -257,7 +262,9 @@ fn main() {
tri_geom.commit();
tri_geoms.push(tri_geom);
}
display = display.aabb(aabb);

println!("Commit the scene ... ");
let mut scene = Scene::new(&device);
let mut mesh_ids = Vec::with_capacity(models.len());
for g in tri_geoms.drain(0..) {
Expand Down Expand Up @@ -285,6 +292,7 @@ fn main() {
let mut spp = 0;
let mut img = Vec::new();

println!("Rendering launched ... ");
display.run(|image, camera_pose, _| {
for p in image.iter_mut() {
*p = 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/obj_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ authors = ["Will Usher <[email protected]>"]
[dependencies]
embree = { path = "../../" }
support = { path = "../support" }
cgmath = "0.17.0"
cgmath = "0.18.0"
tobj = "0.1.6"

9 changes: 8 additions & 1 deletion examples/obj_viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;

use cgmath::{InnerSpace, Vector3, Vector4};
use embree::{Device, Geometry, IntersectContext, Ray, RayHit, Scene, TriangleMesh};
use support::Camera;
use support::{Camera, AABB};

fn main() {
let mut display = support::Display::new(512, 512, "OBJ Viewer");
Expand All @@ -19,6 +19,7 @@ fn main() {
let (models, _) = tobj::load_obj(&Path::new(&args[1])).unwrap();
let mut tri_geoms = Vec::new();

let mut aabb = AABB::default();
for m in models.iter() {
let mesh = &m.mesh;
println!(
Expand All @@ -33,6 +34,11 @@ fn main() {
let mut verts = tris.vertex_buffer.map();
let mut tris = tris.index_buffer.map();
for i in 0..mesh.positions.len() / 3 {
aabb = aabb.union_vec(&Vector3::new(
mesh.positions[i * 3],
mesh.positions[i * 3 + 1],
mesh.positions[i * 3 + 2],
));
verts[i] = Vector4::new(
mesh.positions[i * 3],
mesh.positions[i * 3 + 1],
Expand All @@ -53,6 +59,7 @@ fn main() {
tri_geom.commit();
tri_geoms.push(tri_geom);
}
display = display.aabb(aabb);

let mut scene = Scene::new(&device);
let mut mesh_ids = Vec::with_capacity(models.len());
Expand Down
4 changes: 2 additions & 2 deletions examples/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authors = ["Will Usher <[email protected]>"]

[dependencies]
image = "0.22.0"
arcball = "1.0.0"
cgmath = "0.17.0"
arcball = "1.1.0"
cgmath = "0.18.0"
clock_ticks = "0.1.1"

[dependencies.glium]
Expand Down
50 changes: 50 additions & 0 deletions examples/support/src/aabb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use cgmath::InnerSpace;
use std::f32;
use vec_max;
use vec_min;
use Vector3;

#[derive(Clone, Debug)]
pub struct AABB {
pub p_min: Vector3,
pub p_max: Vector3,
}

impl Default for AABB {
fn default() -> Self {
Self {
p_min: Vector3::new(std::f32::MAX, std::f32::MAX, std::f32::MAX),
p_max: Vector3::new(std::f32::MIN, std::f32::MIN, std::f32::MIN),
}
}
}

impl AABB {
pub fn is_valid(&self) -> bool {
self.p_max.x >= self.p_min.x && self.p_max.y >= self.p_min.y && self.p_max.z >= self.p_min.z
}

pub fn union_aabb(&self, b: &AABB) -> AABB {
AABB {
p_min: vec_min(&self.p_min, &b.p_min),
p_max: vec_max(&self.p_max, &b.p_max),
}
}

pub fn union_vec(&self, v: &Vector3) -> AABB {
AABB {
p_min: vec_min(&self.p_min, v),
p_max: vec_max(&self.p_max, v),
}
}

#[inline]
pub fn size(&self) -> Vector3 {
self.p_max - self.p_min
}

#[inline]
pub fn center(&self) -> Vector3 {
self.size() * 0.5 + self.p_min
}
}
39 changes: 30 additions & 9 deletions examples/support/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use arcball::ArcballCamera;
use cgmath::InnerSpace;
use cgmath::{Matrix4, SquareMatrix, Vector2, Vector3, Vector4};
use clock_ticks;
use glium::glutin::{
Expand All @@ -8,12 +9,14 @@ use glium::texture::RawImage2d;
use glium::Texture2d;
use glium::{self, glutin, Surface};
use image::RgbImage;
use AABB;

/// Manager to display the rendered image in an interactive window.
pub struct Display {
window_dims: (u32, u32),
event_loop: glutin::EventsLoop,
display: glium::Display,
aabb: Option<AABB>,
}

#[derive(Debug)]
Expand All @@ -38,10 +41,17 @@ impl Display {
let display = glium::Display::new(window_builder, context_builder, &event_loop).unwrap();
Display {
window_dims: (w, h),
event_loop: event_loop,
display: display,
event_loop,
display,
aabb: None,
}
}

pub fn aabb(mut self, aabb: AABB) -> Display {
self.aabb = Some(aabb);
self
}

/// The function passed should render and update the image to be displayed in the window,
/// optionally using the camera pose information passed.
pub fn run<F>(&mut self, mut render: F)
Expand All @@ -50,12 +60,23 @@ impl Display {
{
let mut embree_target = RgbImage::new(self.window_dims.0, self.window_dims.1);

let mut arcball_camera = ArcballCamera::new(
Vector3::new(0.0, 0.0, 0.0),
1.0,
[self.window_dims.0 as f32, self.window_dims.1 as f32],
);
arcball_camera.zoom(-50.0, 0.16);
let mut arcball_camera = if let Some(aabb) = &self.aabb {
let mut arcball = ArcballCamera::new(
aabb.center(),
aabb.size().magnitude() / 2.0,
[self.window_dims.0 as f32, self.window_dims.1 as f32],
);
arcball.zoom(-10.0, 0.16);
arcball
} else {
let mut arcball = ArcballCamera::new(
Vector3::new(0.0, 0.0, 0.0),
0.1,
[self.window_dims.0 as f32, self.window_dims.1 as f32],
);
arcball.zoom(-50.0, 0.16);
arcball
};
arcball_camera.rotate(
Vector2::new(
self.window_dims.0 as f32 / 2.0,
Expand Down Expand Up @@ -94,7 +115,7 @@ impl Display {
(prev.x - position.x) as f32,
(position.y - prev.y) as f32,
);
arcball_camera.pan(mouse_delta, 0.16);
arcball_camera.pan(mouse_delta * 0.16);
}
prev_mouse = Some(position);
}
Expand Down
10 changes: 10 additions & 0 deletions examples/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ type Vector2 = cgmath::Vector2<f32>;
type Vector3 = cgmath::Vector3<f32>;
type Vector4 = cgmath::Vector4<f32>;

pub mod aabb;
pub mod camera;
pub mod display;

pub use aabb::AABB;
pub use camera::Camera;
pub use display::Display;

Expand All @@ -27,3 +29,11 @@ pub fn clamp<T: PartialOrd>(x: T, min: T, max: T) -> T {
x
}
}

fn vec_min(v1: &Vector3, v2: &Vector3) -> Vector3 {
Vector3::new(v1.x.min(v2.x), v1.y.min(v2.y), v1.z.min(v2.z))
}

fn vec_max(v1: &Vector3, v2: &Vector3) -> Vector3 {
Vector3::new(v1.x.max(v2.x), v1.y.max(v2.y), v1.z.max(v2.z))
}
2 changes: 1 addition & 1 deletion examples/triangle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ authors = ["Will Usher <[email protected]>"]
[dependencies]
embree = { path = "../../" }
support = { path = "../support" }
cgmath = "0.17.0"
cgmath = "0.18.0"

2 changes: 1 addition & 1 deletion examples/triangle_geometry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ authors = ["Will Usher <[email protected]>"]
[dependencies]
embree = { path = "../../" }
support = { path = "../support" }
cgmath = "0.17.0"
cgmath = "0.18.0"