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

add projection enum to camera #6

Merged
merged 1 commit into from
Jan 19, 2023
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
47 changes: 40 additions & 7 deletions src/scene/camera.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
use cgmath::*;
use gltf::camera::Projection;
use gltf::camera::Projection as GltfProjection;

/// Contains camera properties.
#[derive(Clone, Debug)]
pub struct Camera {
/// Transform matrix (also called world to camera matrix)
pub transform: Matrix4<f32>,

/// Angle in degree of field of view
pub fov: Rad<f32>,
/// Projection type and specific parameters
pub projection: Projection,

/// The distance to the far clipping plane.
///
/// For perspective projection, this may be infinite.
pub zfar: f32,

/// The distance to the near clipping plane.
pub znear: f32,
}

/// Camera projections
#[derive(Debug, Clone)]
pub enum Projection {
/// Perspective projection
Perspective {
/// Y-axis FOV, in radians
yfov: Rad<f32>,
/// Aspect ratio, if specified
aspect_ratio: Option<f32>,
},
/// Orthographic projection
Orthographic {
/// Projection scale
scale: Vector2<f32>,
},
}
impl Default for Projection {
fn default() -> Self {
Self::Perspective {
yfov: Rad(0.399),
aspect_ratio: None,
}
}
}

impl Camera {
/// Position of the camera.
pub fn position(&self) -> Vector3<f32> {
Expand Down Expand Up @@ -78,14 +105,20 @@ impl Camera {
..Default::default()
};
match gltf_cam.projection() {
Projection::Orthographic(ortho) => {
GltfProjection::Orthographic(ortho) => {
cam.projection = Projection::Orthographic {
scale: Vector2::new(ortho.xmag(), ortho.ymag()),
};
cam.zfar = ortho.zfar();
cam.znear = ortho.znear();
}
Projection::Perspective(pers) => {
GltfProjection::Perspective(pers) => {
cam.projection = Projection::Perspective {
yfov: Rad(pers.yfov()),
aspect_ratio: pers.aspect_ratio(),
};
cam.zfar = pers.zfar().unwrap_or(f32::INFINITY);
cam.znear = pers.znear();
cam.fov = Rad(pers.yfov());
}
};
cam
Expand All @@ -96,7 +129,7 @@ impl Default for Camera {
fn default() -> Self {
Camera {
transform: Zero::zero(),
fov: Rad(0.399),
projection: Projection::default(),
zfar: f32::INFINITY,
znear: 0.,
}
Expand Down
2 changes: 1 addition & 1 deletion src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod model;

use crate::utils::transform_to_matrix;
use crate::GltfData;
pub use camera::Camera;
pub use camera::{Camera, Projection};
pub use light::Light;
pub use model::{Material, Model};

Expand Down