e2rcore/interface/
i_component.rs

1extern crate pretty_env_logger;
2
3use std::any::Any;
4use std::collections::HashMap;
5
6use interface::i_renderobj;
7
8use implement::render::renderdevice_gl;
9
10pub trait IComponent: IComponentClone {
11    fn as_any( & self ) -> & Any;
12}
13
14pub trait IComponentClone {
15    fn clone_box( & self ) -> Box< IComponent >;
16}
17
18impl< T > IComponentClone for T where T: 'static + IComponent + Clone {
19    fn clone_box( & self ) -> Box< IComponent > {
20        Box::new( self.clone() )
21    }
22}
23
24impl Clone for Box< IComponent > {
25    fn clone( & self ) -> Box< IComponent > {
26        self.clone_box()
27    }
28}
29
30#[derive(Clone)]
31pub struct ComponentRenderBuffer {
32    pub _data_dict: HashMap< i_renderobj::BuffDataType, Vec<f32> >,
33    // pub _render_prim_type: i_renderobj::RenderObjType,
34}
35
36impl IComponent for ComponentRenderBuffer {
37    fn as_any( & self ) -> & Any {
38        self
39    }
40}
41
42impl ComponentRenderBuffer {
43    /// # this dumps the data to render device
44    pub fn flush_into_render_device( & self, rd: & mut i_renderobj::RenderDevice ) -> Result< (), & 'static str > {
45        trace!("flushing into render device" );
46        rd.store_buff_data( & self._data_dict )?;
47        Ok( () )
48    }
49}
50
51#[derive(Clone)]
52pub struct ComponentRenderUniform {
53    /// # stores the uniforms values
54    pub _data_dict_vf: HashMap< String, Vec<f32> >,
55    pub _data_dict_mat4f: HashMap< String, Vec<f32> >,
56    pub _data_dict_mat3f: HashMap< String, Vec<f32> >,
57    /// # this maps an id to multiple uniforms
58    pub _data_uniform_group: HashMap< u64, Vec< String > >,
59}
60
61impl Default for ComponentRenderUniform {
62    fn default() -> ComponentRenderUniform {
63        ComponentRenderUniform {
64            _data_dict_vf: HashMap::new(),
65            _data_dict_mat4f: HashMap::new(),
66            _data_dict_mat3f: HashMap::new(),
67            _data_uniform_group: HashMap::new(),
68        }
69    }
70}
71
72impl IComponent for ComponentRenderUniform {
73    fn as_any( & self ) -> & Any {
74        self
75    }
76}
77
78impl ComponentRenderUniform {
79    /// # this dumps the data to uniform manager
80    pub fn flush_into_uniform_collection( & self, shader_program: i64, uc: & mut renderdevice_gl::RenderUniformCollection ) -> Result< (), & 'static str > {
81        trace!("flushing into uniform collection" );
82        for ( ref k, ref v ) in self._data_dict_vf.iter() {
83            uc.set_uniform_f( shader_program as _, (*k).as_str(), renderdevice_gl::UniformType::VEC, &v[..] );
84        }
85        for ( ref k, ref v ) in self._data_dict_mat4f.iter() {
86            uc.set_uniform_f( shader_program as _, (*k).as_str(), renderdevice_gl::UniformType::MAT4, &v[..] );
87        }
88        for ( ref k, ref v ) in self._data_dict_mat3f.iter() {
89            uc.set_uniform_f( shader_program as _, (*k).as_str(), renderdevice_gl::UniformType::MAT3, &v[..] );
90        }
91        
92        for ( ref k, ref v ) in self._data_uniform_group.iter() {
93            trace!("uniform group: {}, length: {}.", **k, (**v).len() );
94            uc.set_group( shader_program as _, **k, (**v).clone() ).is_ok();            
95        }
96
97        Ok( () )
98    }
99}
100
101/// # command for resetting draw group content
102#[derive(Clone)]
103pub struct ComponentDrawGroupClear {
104    pub _group_id: usize,
105}
106
107impl IComponent for ComponentDrawGroupClear {
108    fn as_any( & self ) -> & Any {
109        self
110    }
111}
112
113#[derive(Clone)]
114pub struct ComponentDrawGroupDependentObjects {
115    pub _group_id: usize,
116    pub _obj_ids: Vec< usize >,
117}
118
119impl IComponent for ComponentDrawGroupDependentObjects {
120    fn as_any( & self ) -> & Any {
121        self
122    }
123}
124
125#[derive(Clone)]
126pub struct ComponentDrawGroupBind {
127    pub _group_id: usize,
128}
129
130impl IComponent for ComponentDrawGroupBind {
131    fn as_any( & self ) -> & Any {
132        self
133    }
134}
135
136#[derive(Clone)]
137pub struct ComponentDrawGroupDependentUniforms {
138    pub _group_id: usize,
139    pub _uniform_ids: Vec< u64 >,
140}
141
142impl IComponent for ComponentDrawGroupDependentUniforms {
143    fn as_any( & self ) -> & Any {
144        self
145    }
146}
147
148#[derive(Clone)]
149pub struct ComponentDrawGroupDispatch {
150    pub _group_id: usize,
151}
152
153impl IComponent for ComponentDrawGroupDispatch {
154    fn as_any( & self ) -> & Any {
155        self
156    }
157}
158