Skip to content

Commit cd57571

Browse files
committed
Backport old camera implementation
1 parent 1d75ef1 commit cd57571

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub use utils::Id;
4848

4949
/// World
5050
pub mod world;
51-
pub use world::{Entity, World};
51+
pub use world::{Camera, Entity, World};
5252

5353
/// Window API and input events
5454
pub mod window;

src/world.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
mod camera;
12
mod storage;
23

34
use std::sync::{Arc, Condvar, Mutex};
45
use std::{any::TypeId, collections::HashMap, marker::PhantomData};
56

67
use crate::recursive;
78
use crate::utils::{Id, Lock, TypeLock};
9+
pub use camera::{Camera, Lens, View};
810
pub use storage::{Entity, IntoEntity};
911

1012
#[derive(Default, Debug, Eq, PartialEq)]

src/world/camera.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::ops::Range;
44

5-
use dotrix_math::{perspective, Mat3, Mat4, Point3, Quat, Rad, Vec3};
5+
use crate::math::{Mat3, Mat4, Quat, Vec3};
66

77
/// Camera object and constructor
88
pub struct Camera {
@@ -24,22 +24,22 @@ impl Camera {
2424
}
2525

2626
/// Returns projection matrix constructor
27-
pub fn lens(fov: impl Into<Rad<f32>>, plane: Range<f32>) -> Lens {
27+
pub fn lens(fov: impl Into<f32>, plane: Range<f32>) -> Lens {
2828
Lens::new(fov, plane)
2929
}
3030
}
3131

3232
/// Projection matrix constructor
3333
pub struct Lens {
3434
/// Field of View (rad)
35-
pub fov: Rad<f32>,
35+
pub fov: f32,
3636
/// Near..Far plane
3737
pub plane: Range<f32>,
3838
}
3939

4040
impl Lens {
4141
/// Returns new instance of projection matrix constructor
42-
pub fn new(fov: impl Into<Rad<f32>>, plane: Range<f32>) -> Self {
42+
pub fn new(fov: impl Into<f32>, plane: Range<f32>) -> Self {
4343
Self {
4444
fov: fov.into(),
4545
plane,
@@ -49,14 +49,14 @@ impl Lens {
4949
/// Returns projection matrix for the surface
5050
pub fn proj(&self, surface_width: u32, surface_height: u32) -> Mat4 {
5151
let aspect_ratio = surface_width as f32 / surface_height as f32;
52-
perspective(self.fov, aspect_ratio, self.plane.start, self.plane.end)
52+
Mat4::perspective_rh(self.fov, aspect_ratio, self.plane.start, self.plane.end)
5353
}
5454
}
5555

5656
impl Default for Lens {
5757
fn default() -> Self {
5858
Self {
59-
fov: Rad(1.1),
59+
fov: 1.1, // std::f32::consts::FRAC_PI_4
6060
plane: 0.0625..524288.06,
6161
}
6262
}
@@ -80,50 +80,49 @@ impl View {
8080
///
8181
/// self.point is handled as camera position
8282
pub fn rotate(self, pitch: f32, yaw: f32, roll: f32) -> Mat4 {
83-
let rx = Mat3::from_angle_x(Rad(roll));
84-
let ry = Mat3::from_angle_y(Rad(pitch));
85-
let rz = Mat3::from_angle_z(Rad(yaw));
83+
let rx = Mat3::from_rotation_x(roll);
84+
let ry = Mat3::from_rotation_y(pitch);
85+
let rz = Mat3::from_rotation_z(yaw);
8686

87-
let mut mx = Mat4::from(rx * ry * rz);
88-
mx.w.x = self.point.x;
89-
mx.w.y = self.point.y;
90-
mx.w.z = self.point.z;
87+
let mut mx = Mat4::from_mat3(rx * ry * rz);
88+
mx.w_axis.x = self.point.x;
89+
mx.w_axis.y = self.point.y;
90+
mx.w_axis.z = self.point.z;
9191

9292
mx
9393
}
9494

9595
/// Return view matrix made from target
9696
pub fn target(&self, target: Vec3) -> Mat4 {
97-
self.target_up(target, Vec3::unit_z())
97+
self.target_up(target, Vec3::Z)
9898
}
9999

100100
/// Return view matrix made from target and up vector
101101
pub fn target_up(&self, target: Vec3, up: Vec3) -> Mat4 {
102+
// let view = Mat4::look_at_rh(Vec3::new(1.5f32, -5.0, 3.0), Vec3::ZERO, Vec3::Z);
102103
Mat4::look_at_rh(
103-
Point3::new(self.point.x, self.point.y, self.point.z),
104-
Point3::new(target.x, target.y, target.z),
104+
Vec3::new(self.point.x, self.point.y, self.point.z),
105+
Vec3::new(target.x, target.y, target.z),
105106
up,
106107
)
107108
}
108109

109110
/// Return view matrix for camera flying around a target (self.point)
110111
pub fn follow(self, distance: f32, pan: f32, tilt: f32, roll: f32) -> Mat4 {
111-
use dotrix_math::{InnerSpace, Rotation3};
112-
113-
let target = &self.point;
112+
let target = self.point;
114113
let dz = distance * tilt.sin();
115114
let dxy = distance * tilt.cos();
116115
let dx = dxy * pan.cos();
117116
let dy = dxy * pan.sin();
118117
let position = Vec3::new(target.x + dx, target.y + dy, target.z + dz);
119118
let direction = (target - position).normalize();
120-
let roll = Quat::from_axis_angle(direction, Rad(roll));
121-
let camera_right = direction.cross(Vec3::unit_z());
119+
let roll = Quat::from_axis_angle(direction, roll);
120+
let camera_right = direction.cross(Vec3::Z);
122121
let camera_up = roll * camera_right.cross(direction);
123122

124123
Mat4::look_at_rh(
125-
Point3::new(position.x, position.y, position.z),
126-
Point3::new(target.x, target.y, target.z),
124+
Vec3::new(position.x, position.y, position.z),
125+
Vec3::new(target.x, target.y, target.z),
127126
camera_up,
128127
)
129128
}

0 commit comments

Comments
 (0)