2
2
3
3
use std:: ops:: Range ;
4
4
5
- use dotrix_math :: { perspective , Mat3 , Mat4 , Point3 , Quat , Rad , Vec3 } ;
5
+ use crate :: math :: { Mat3 , Mat4 , Quat , Vec3 } ;
6
6
7
7
/// Camera object and constructor
8
8
pub struct Camera {
@@ -24,22 +24,22 @@ impl Camera {
24
24
}
25
25
26
26
/// 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 {
28
28
Lens :: new ( fov, plane)
29
29
}
30
30
}
31
31
32
32
/// Projection matrix constructor
33
33
pub struct Lens {
34
34
/// Field of View (rad)
35
- pub fov : Rad < f32 > ,
35
+ pub fov : f32 ,
36
36
/// Near..Far plane
37
37
pub plane : Range < f32 > ,
38
38
}
39
39
40
40
impl Lens {
41
41
/// 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 {
43
43
Self {
44
44
fov : fov. into ( ) ,
45
45
plane,
@@ -49,14 +49,14 @@ impl Lens {
49
49
/// Returns projection matrix for the surface
50
50
pub fn proj ( & self , surface_width : u32 , surface_height : u32 ) -> Mat4 {
51
51
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 )
53
53
}
54
54
}
55
55
56
56
impl Default for Lens {
57
57
fn default ( ) -> Self {
58
58
Self {
59
- fov : Rad ( 1.1 ) ,
59
+ fov : 1.1 , // std::f32::consts::FRAC_PI_4
60
60
plane : 0.0625 ..524288.06 ,
61
61
}
62
62
}
@@ -80,50 +80,49 @@ impl View {
80
80
///
81
81
/// self.point is handled as camera position
82
82
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) ;
86
86
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 ;
91
91
92
92
mx
93
93
}
94
94
95
95
/// Return view matrix made from target
96
96
pub fn target ( & self , target : Vec3 ) -> Mat4 {
97
- self . target_up ( target, Vec3 :: unit_z ( ) )
97
+ self . target_up ( target, Vec3 :: Z )
98
98
}
99
99
100
100
/// Return view matrix made from target and up vector
101
101
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);
102
103
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 ) ,
105
106
up,
106
107
)
107
108
}
108
109
109
110
/// Return view matrix for camera flying around a target (self.point)
110
111
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 ;
114
113
let dz = distance * tilt. sin ( ) ;
115
114
let dxy = distance * tilt. cos ( ) ;
116
115
let dx = dxy * pan. cos ( ) ;
117
116
let dy = dxy * pan. sin ( ) ;
118
117
let position = Vec3 :: new ( target. x + dx, target. y + dy, target. z + dz) ;
119
118
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 ) ;
122
121
let camera_up = roll * camera_right. cross ( direction) ;
123
122
124
123
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 ) ,
127
126
camera_up,
128
127
)
129
128
}
0 commit comments