22
33use 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
88pub 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
3333pub 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
4040impl 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
5656impl 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