Skip to content

Commit

Permalink
Merge pull request #1 from sebcrozet/master
Browse files Browse the repository at this point in the history
Bump to master
  • Loading branch information
antonylsg authored May 9, 2020
2 parents aa07c44 + 0938c2e commit a75340d
Show file tree
Hide file tree
Showing 57 changed files with 533 additions and 369 deletions.
17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kiss3d"
version = "0.23.0"
version = "0.24.1"
authors = [ "Sébastien Crozet <[email protected]>" ]

description = "3D graphics engine for Rust."
Expand All @@ -9,6 +9,7 @@ readme = "README.md"
keywords = [ "3D", "graphics", "OpenGL", "KISS" ]
license = "BSD-3-Clause"
autoexamples = true
edition = "2018"

include = [
"src/**/*.rs",
Expand All @@ -28,22 +29,22 @@ name = "kiss3d"
path = "src/lib.rs"

[features]
conrod = [ "kiss3d_conrod" ]
conrod = [ "conrod_core" ]


[dependencies]
either = "1"
libc = "0.2"
bitflags = "1.2"
num-traits = "0.2"
nalgebra = "0.20"
ncollide3d = "0.22"
nalgebra = "0.21"
ncollide3d = "0.23"
image = "0.22"
serde = "1"
serde_derive = "1"
rusttype = { version = "0.8", features = [ "gpu_cache" ] }
instant = { version = "0.1", features = [ "stdweb" ]}
kiss3d_conrod = { version = "0.64", features = [ "stdweb" ], optional = true }
conrod_core = { version = "0.69", features = [ "stdweb" ], optional = true }

[target.'cfg(not(any(target_arch = "wasm32", target_arch = "asmjs")))'.dependencies]
gl = "0.14"
Expand All @@ -65,4 +66,8 @@ stdweb-derive = "0.5"

[dev-dependencies]
rand = "0.7"
ncollide2d = "0.22"
ncollide2d = "0.23"

[patch.crates-io]
ncollide2d = { path = "../ncollide/build/ncollide2d" }
ncollide3d = { path = "../ncollide/build/ncollide3d" }
2 changes: 2 additions & 0 deletions examples/wasm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ pub fn gui(ui: &mut conrod::UiCell, ids: &Ids, app: &mut DemoApp) {
const TITLE: &'static str = "All Widgets";
widget::Canvas::new()
.pad(MARGIN)
.align_bottom()
.h(ui.win_h / 2.0)
.scroll_kids_vertically()
// .color(conrod::Color::Rgba(0.5, 0.5, 0.5, 0.2))
.set(ids.canvas, ui);
Expand Down
12 changes: 6 additions & 6 deletions src/builtin/normals_material.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use camera::Camera;
use context::Context;
use light::Light;
use crate::camera::Camera;
use crate::context::Context;
use crate::light::Light;
use na::{Isometry3, Matrix3, Matrix4, Point3, Vector3};
use resource::Material;
use resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use scene::ObjectData;
use crate::resource::Material;
use crate::resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use crate::scene::ObjectData;

#[path = "../error.rs"]
mod error;
Expand Down
12 changes: 6 additions & 6 deletions src/builtin/object_material.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use camera::Camera;
use context::Context;
use light::Light;
use crate::camera::Camera;
use crate::context::Context;
use crate::light::Light;
use na::{Isometry3, Matrix3, Matrix4, Point2, Point3, Vector3};
use resource::Material;
use resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use scene::ObjectData;
use crate::resource::Material;
use crate::resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use crate::scene::ObjectData;

#[path = "../error.rs"]
mod error;
Expand Down
10 changes: 5 additions & 5 deletions src/builtin/planar_object_material.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use context::Context;
use crate::context::Context;
use na::{Isometry2, Matrix2, Matrix3, Point2, Point3, Vector2};
use planar_camera::PlanarCamera;
use resource::PlanarMaterial;
use resource::{Effect, PlanarMesh, ShaderAttribute, ShaderUniform};
use scene::PlanarObjectData;
use crate::planar_camera::PlanarCamera;
use crate::resource::PlanarMaterial;
use crate::resource::{Effect, PlanarMesh, ShaderAttribute, ShaderUniform};
use crate::scene::PlanarObjectData;

#[path = "../error.rs"]
mod error;
Expand Down
12 changes: 6 additions & 6 deletions src/builtin/uvs_material.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use camera::Camera;
use context::Context;
use light::Light;
use crate::camera::Camera;
use crate::context::Context;
use crate::light::Light;
use na::{Isometry3, Matrix3, Matrix4, Point2, Point3, Vector3};
use resource::Material;
use resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use scene::ObjectData;
use crate::resource::Material;
use crate::resource::{Effect, Mesh, ShaderAttribute, ShaderUniform};
use crate::scene::ObjectData;

#[path = "../error.rs"]
mod error;
Expand Down
97 changes: 42 additions & 55 deletions src/camera/arc_ball.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use camera::Camera;
use event::{Action, Key, Modifiers, MouseButton, WindowEvent};
use na::{self, Isometry3, Matrix4, Perspective3, Point3, Vector2, Vector3};
use resource::ShaderUniform;
use crate::camera::Camera;
use crate::event::{Action, Key, Modifiers, MouseButton, WindowEvent};
use na::{self, Isometry3, Matrix4, Perspective3, Point3, Unit, UnitQuaternion, Vector2, Vector3};
use crate::resource::ShaderUniform;
use std::f32;
use window::Canvas;
use crate::window::Canvas;

/// Arc-ball camera mode.
///
Expand Down Expand Up @@ -52,7 +52,7 @@ pub struct ArcBall {
proj_view: Matrix4<f32>,
inverse_proj_view: Matrix4<f32>,
last_cursor_pos: Vector2<f32>,
coord_system: CoordSystem,
coord_system: CoordSystemRh,
}

impl ArcBall {
Expand Down Expand Up @@ -92,7 +92,7 @@ impl ArcBall {
proj_view: na::zero(),
inverse_proj_view: na::zero(),
last_cursor_pos: na::zero(),
coord_system: CoordSystem::RightHandYUp,
coord_system: CoordSystemRh::from_up_axis(Vector3::y_axis()),
};

res.look_at(eye, at);
Expand Down Expand Up @@ -193,18 +193,11 @@ impl ArcBall {
/// Move and orient the camera such that it looks at a specific point.
pub fn look_at(&mut self, eye: Point3<f32>, at: Point3<f32>) {
let dist = (eye - at).norm();
let pitch;
let yaw;
match self.coord_system {
CoordSystem::RightHandYUp => {
pitch = ((eye.y - at.y) / dist).acos();
yaw = (eye.z - at.z).atan2(eye.x - at.x);
}
CoordSystem::RightHandZUp => {
pitch = ((eye.z - at.z) / dist).acos();
yaw = (at.y - eye.y).atan2(eye.x - at.x);
}
}

let view_eye = self.coord_system.rotation_to_y_up * eye;
let view_at = self.coord_system.rotation_to_y_up * at;
let pitch = ((view_eye.y - view_at.y) / dist).acos();
let yaw = (view_eye.z - view_at.z).atan2(view_eye.x - view_at.x);

self.at = at;
self.dist = dist;
Expand Down Expand Up @@ -306,7 +299,7 @@ impl ArcBall {
fn handle_right_button_displacement(&mut self, dpos: &Vector2<f32>) {
let eye = self.eye();
let dir = (self.at - eye).normalize();
let tangent = self.coord_system.up_axis().cross(&dir).normalize();
let tangent = self.coord_system.up_axis.cross(&dir).normalize();
let bitangent = dir.cross(&tangent);
let mult = self.dist / 1000.0;

Expand All @@ -327,18 +320,18 @@ impl ArcBall {
self.inverse_proj_view = self.proj_view.try_inverse().unwrap();
}

/// Sets the up vector of this camera.
/// Sets the up vector of this camera. Prefer using [`set_up_axis_dir`](#method.set_up_axis_dir)
/// if your up vector is already normalized.
#[inline]
pub fn set_up_axis(&mut self, up_axis: Vector3<f32>) {
let new_coord_system;
if up_axis == Vector3::y() {
new_coord_system = CoordSystem::RightHandYUp;
} else if up_axis == Vector3::z() {
new_coord_system = CoordSystem::RightHandZUp;
} else {
panic!("This up_axis is not supported: {:?}", up_axis);
}
if self.coord_system != new_coord_system {
self.set_up_axis_dir(Unit::new_normalize(up_axis));
}

/// Sets the up-axis direction of this camera.
#[inline]
pub fn set_up_axis_dir(&mut self, up_axis: Unit<Vector3<f32>>) {
if self.coord_system.up_axis != up_axis {
let new_coord_system = CoordSystemRh::from_up_axis(up_axis);
// Since setting the up axis changes the meaning of pitch and yaw
// angles, we need to recalculate them in order to preserve the eye
// position.
Expand All @@ -355,25 +348,15 @@ impl Camera for ArcBall {
}

fn view_transform(&self) -> Isometry3<f32> {
Isometry3::look_at_rh(&self.eye(), &self.at, &self.coord_system.up_axis())
Isometry3::look_at_rh(&self.eye(), &self.at, &self.coord_system.up_axis)
}

fn eye(&self) -> Point3<f32> {
let px = self.at.x + self.dist * self.yaw.cos() * self.pitch.sin();
let py;
let pz;
match self.coord_system {
CoordSystem::RightHandYUp => {
py = self.at.y + self.dist * self.pitch.cos();
pz = self.at.z + self.dist * self.yaw.sin() * self.pitch.sin();
}
CoordSystem::RightHandZUp => {
py = self.at.y - self.dist * self.yaw.sin() * self.pitch.sin();
pz = self.at.z + self.dist * self.pitch.cos();
}
}

Point3::new(px, py, pz)
let view_at = self.coord_system.rotation_to_y_up * self.at;
let px = view_at.x + self.dist * self.yaw.cos() * self.pitch.sin();
let py = view_at.y + self.dist * self.pitch.cos();
let pz = view_at.z + self.dist * self.yaw.sin() * self.pitch.sin();
self.coord_system.rotation_to_y_up.inverse() * Point3::new(px, py, pz)
}

fn handle_event(&mut self, canvas: &Canvas, event: &WindowEvent) {
Expand Down Expand Up @@ -439,18 +422,22 @@ impl Camera for ArcBall {
fn update(&mut self, _: &Canvas) {}
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum CoordSystem {
RightHandYUp,
RightHandZUp,
#[derive(Clone, Copy, Debug)]
struct CoordSystemRh {
up_axis: Unit<Vector3<f32>>,
rotation_to_y_up: UnitQuaternion<f32>,
}

impl CoordSystem {
impl CoordSystemRh {
#[inline]
fn up_axis(self) -> Vector3<f32> {
match self {
CoordSystem::RightHandYUp => Vector3::y(),
CoordSystem::RightHandZUp => Vector3::z(),
fn from_up_axis(up_axis: Unit<Vector3<f32>>) -> Self {
let rotation_to_y_up = UnitQuaternion::rotation_between_axis(&up_axis, &Vector3::y_axis())
.unwrap_or_else(|| {
UnitQuaternion::from_axis_angle(&Vector3::x_axis(), std::f32::consts::PI)
});
Self {
up_axis,
rotation_to_y_up,
}
}
}
6 changes: 3 additions & 3 deletions src/camera/camera.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use event::WindowEvent;
use crate::event::WindowEvent;
use na::{Isometry3, Matrix4, Point2, Point3, Point4, Vector2, Vector3};
use resource::ShaderUniform;
use window::Canvas;
use crate::resource::ShaderUniform;
use crate::window::Canvas;

/// Trait every camera must implement.
pub trait Camera {
Expand Down
Loading

0 comments on commit a75340d

Please sign in to comment.