e2rcore/implement/render/
camera.rs

1extern crate pretty_env_logger;
2extern crate mazth;
3
4use std::collections::HashMap;
5use std::vec::Vec;
6use std::any::Any;
7
8use interface::i_renderobj;
9use interface::i_ele;
10use interface::i_component;
11
12use self::mazth::mat::*;
13use implement::math;
14
15#[derive(Clone, PartialEq, Eq, Hash)]
16pub enum PropKey {
17    Fov,
18    Aspect,
19    Near,
20    Far,
21    Pos,
22    Up,
23    Focus,
24}
25
26#[derive(Clone)]
27pub enum PropVal {
28    Fov(f32),
29    Aspect(f32),
30    Near(f32),
31    Far(f32),
32    Pos(Mat3x1< f32 >),
33    Up(Mat3x1< f32 >),
34    Focus(Mat3x1< f32 >),
35}
36
37#[derive(Clone)]
38pub struct Cam {
39    /// # helper id for the camera
40    pub _id: u64,
41
42    pub _proj_xform: Mat4< f32 >,
43    /// # The following generates the projection matrix
44    pub _fov: f32,
45    pub _aspect: f32,
46    pub _near: f32,
47    pub _far: f32,
48
49    pub _view_xform: Mat4< f32 >,
50    /// # The following generates the view matrix
51    pub _pos: Mat3x1< f32 >,
52    pub _pos_orig: Mat3x1< f32 >,
53    pub _up: Mat3x1< f32 >,
54    pub _focus: Mat3x1< f32 >,
55}
56
57impl Cam {
58    pub fn init( id: u64, fov: f32, aspect: f32, near: f32, far: f32, pos: Mat3x1< f32 >, focus: Mat3x1< f32 >, up: Mat3x1< f32 > ) -> Cam {
59        Cam {
60            _id: id,
61            _fov: fov,
62            _aspect: aspect,
63            _near: near,
64            _far: far,
65            _pos: pos,
66            _pos_orig: pos,
67            _up: up,
68            _focus: focus,
69            _proj_xform: math::util::perspective( fov, aspect, near, far ),
70            _view_xform: math::util::look_at( pos, focus, up ),
71        }
72    }
73    pub fn update_pos( & mut self, pos: Mat3x1< f32 >, focus: Mat3x1< f32 > ) {
74        self._pos = pos;
75        self._focus = focus;
76        self._view_xform = math::util::look_at( pos, self._focus, self._up );
77    }
78}
79
80impl i_ele::IObjImpl for Cam {
81    fn as_any( & self ) -> & Any {
82        self
83    }
84    fn update_components( & mut self, components: & mut Vec< Box< i_component::IComponent > > ) -> Result< (), & 'static str > {
85
86        //store uniform data
87        {
88            let model_transform = Mat4::<f32> { _val: [ 1f32, 0f32, 0f32, 0f32,
89                                                                   0f32, 1f32, 0f32, 0f32,
90                                                                   0f32, 0f32, 1f32, 0f32,
91                                                                   0f32, 0f32, 0f32, 1f32 ],
92                                                            _is_row_major: true };
93
94            let mvp_transform = self._proj_xform.mul( &self._view_xform ).unwrap().mul( &model_transform ).unwrap();
95            let model_view_transform = self._view_xform.mul( &model_transform ).unwrap();
96            let normal_inv_transpose = model_view_transform.submat_mat3().inverse().unwrap().transpose();
97
98            trace!( "mv: {:?}", model_view_transform );
99            trace!( "mv submat3: {:?}", model_view_transform.submat_mat3() );
100            trace!( "mvp: {:?}", mvp_transform );
101            trace!( "normal matrix: {:?}", normal_inv_transpose );
102
103            let data_map_mat4f : HashMap< String, Vec<f32> > =  [ ( String::from("ModelViewMatrix\0"), &model_view_transform._val[..] ),
104                                                                  ( String::from("ProjectionMatrix\0"), &self._proj_xform._val[..] ),
105                                                                  ( String::from("MVP\0"), &mvp_transform._val[..] ),
106            ].into_iter().map(|&( ref k, ref v)| ( k.clone(), v[..].to_vec() ) ).collect();
107            
108            let data_map_mat3f : HashMap< String, Vec<f32> > =  [ ( String::from("NormalMatrix\0"), &normal_inv_transpose._val[..] ),
109            ].into_iter().map(|&( ref k, ref v)| ( k.clone(), v[..].to_vec() ) ).collect();                                                              
110
111            let uniform_group_id = 1;
112            let data_group : HashMap< u64, Vec<String> > = [ ( uniform_group_id, [ String::from("ModelViewMatrix\0"), String::from("NormalMatrix\0"), //todo: add warning message on unmatched uniform name in uniform manager
113                                                                                   String::from("ProjectionMatrix\0"), String::from("MVP\0") ] ),
114            ].into_iter().map(|&( ref k, ref v)| ( k.clone(), v[..].to_vec() ) ).collect();
115            
116            let c = i_component::ComponentRenderUniform {
117                _data_dict_mat4f: data_map_mat4f,
118                _data_dict_mat3f: data_map_mat3f,
119                _data_uniform_group: data_group,
120                ..Default::default()
121            };
122            components.push( Box::new(c) );
123            trace!( "load into render buffer: uniform: camera" );
124        }
125        Ok( () )
126    }
127}
128
129impl i_renderobj::ObjPos for Cam {
130    fn get_pos( & self ) -> Mat3x1< f32 > {
131        self._pos
132    }
133}