g4tools  5.4.0
render_action
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_render_action
5 #define tools_sg_render_action
6 
7 #include "matrix_action"
8 
9 #include "../glprims"
10 #include "../vdata"
11 #include "../colorf"
12 
13 namespace tools {
14 namespace sg {
15  class render_manager;
16 }}
17 
18 
19 namespace tools {
20 namespace sg {
21 
22 typedef size_t bufpos;
23 
24 class render_action : public matrix_action {
26 public:
27  typedef unsigned int gstoid;
28 public:
29  virtual void load_proj_matrix(const mat4f&) = 0;
30  virtual void load_model_matrix(const mat4f&) = 0;
31 public:
32  virtual void draw_vertex_array(gl::mode_t,size_t,const float*) = 0;
33  virtual void draw_vertex_array_xy(gl::mode_t,size_t,const float*) = 0;
34  virtual void draw_vertex_color_array(gl::mode_t,size_t,const float*,const float*) = 0;
35  virtual void draw_vertex_normal_array(gl::mode_t,size_t,const float*,const float*) = 0;
36  virtual void draw_vertex_color_normal_array(gl::mode_t,size_t,const float*,const float*,const float*) = 0;
37 
41  virtual void draw_vertex_array_texture(gl::mode_t,size_t,const float*,gstoid,const float*) = 0;
42  virtual void draw_vertex_normal_array_texture(gl::mode_t,size_t,const float*,const float*,gstoid,const float*) = 0;
43 
47  virtual void begin_gsto(gstoid) = 0;
48  virtual void draw_gsto_v(gl::mode_t,size_t,bufpos) = 0;
49  virtual void draw_gsto_vc(gl::mode_t,size_t,bufpos,bufpos) = 0;
50  virtual void draw_gsto_vn(gl::mode_t,size_t,bufpos,bufpos) = 0;
51  virtual void draw_gsto_vcn(gl::mode_t,size_t,bufpos,bufpos,bufpos) = 0;
52  virtual void end_gsto() = 0;
56 
57  virtual void clear_color(float,float,float,float) = 0;
58  virtual void color4f(float,float,float,float) = 0;
59  virtual void line_width(float) = 0;
60  virtual void point_size(float) = 0;
61  virtual void set_polygon_offset(bool) = 0;
62  virtual void set_winding(winding_type) = 0;
63  virtual void set_shade_model(shade_type) = 0;
64  virtual void set_cull_face(bool) = 0;
65  virtual void set_point_smooth(bool) = 0;
66  virtual void set_line_smooth(bool) = 0;
67  virtual void normal(float,float,float) = 0;
68  virtual void set_depth_test(bool) = 0;
69  virtual unsigned int max_lights() = 0;
70  virtual void enable_light(unsigned int,
71  float,float,float, //directrion
72  float,float,float,float) = 0; //RGBA
73  virtual void set_lighting(bool) = 0;
74  virtual void set_blend(bool) = 0;
75  virtual void restore_state(unsigned int) = 0;
76 
77  virtual sg::render_manager& render_manager() = 0; //sg:: is needed.
78 public:
79  render_action(std::ostream& a_out,unsigned int a_ww,unsigned int a_wh)
80  :parent(a_out,a_ww,a_wh){}
81  virtual ~render_action(){}
82 public:
84  :parent(a_from)
85  {}
87  parent::operator=(a_from);
88  return *this;
89  }
90 public:
94  }
96  const sg::state& _state = state();
97  load_proj_matrix(_state.m_proj);
98  load_model_matrix(_state.m_model);
99  }
100 
101  void clear_color(const colorf& a_color){
102  clear_color(a_color[0],a_color[1],a_color[2],a_color[3]);
103  }
104  void color4f(const colorf& a_color){
105  color4f(a_color[0],a_color[1],a_color[2],a_color[3]);
106  }
107 
108  void enable_light(unsigned int a_light,
109  const vec3f& a_dir,
110  const colorf& a_col) {
111  enable_light(a_light,
112  a_dir[0],a_dir[1],a_dir[2],
113  a_col[0],a_col[1],a_col[2],1);
114  }
115 
116  void draw_vertex_array(gl::mode_t a_mode,const std::vector<float>& a_xyzs){
117  const float* _xyzs = vec_data<float>(a_xyzs);
118  draw_vertex_array(a_mode,a_xyzs.size(),_xyzs);
119  }
120 
121  void draw_vertex_array_xy(gl::mode_t a_mode,const std::vector<float>& a_xys){
122  const float* _xys = vec_data<float>(a_xys);
123  draw_vertex_array_xy(a_mode,a_xys.size(),_xys);
124  }
125 
127  const std::vector<float>& a_xyzs,
128  const std::vector<float>& a_rgbas){
129  const float* _xyzs = vec_data<float>(a_xyzs);
130  const float* _rgbas = vec_data<float>(a_rgbas);
131  draw_vertex_color_array(a_mode,a_xyzs.size(),_xyzs,_rgbas);
132  }
133 
135  const std::vector<float>& a_xyzs,
136  const std::vector<float>& a_nms){
137  const float* _xyzs = vec_data<float>(a_xyzs);
138  const float* _nms = vec_data<float>(a_nms);
139  draw_vertex_normal_array(a_mode,a_xyzs.size(),_xyzs,_nms);
140  }
141 
143  const std::vector<float>& a_xyzs,
144  const std::vector<float>& a_rgbas,
145  const std::vector<float>& a_nms){
146  const float* _xyzs = vec_data<float>(a_xyzs);
147  const float* _rgbas = vec_data<float>(a_rgbas);
148  const float* _nms = vec_data<float>(a_nms);
149  draw_vertex_color_normal_array(a_mode,a_xyzs.size(),_xyzs,_rgbas,_nms);
150  }
151 
152  void normal(const vec3f& a_vec) {normal(a_vec[0],a_vec[1],a_vec[2]);}
153 
154 public:
155  // for sphere::visit template.
156  bool add_triangles_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms) {
157  draw_vertex_normal_array(gl::triangles(),a_floatn,a_xyzs,a_nms);
158  return true;
159  }
160  bool add_triangle_fan_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms) {
161  draw_vertex_normal_array(gl::triangle_fan(),a_floatn,a_xyzs,a_nms);
162  return true;
163  }
164  bool add_triangle_fan_normal_rgba(size_t a_floatn,const float* a_xyzs,float* a_nms,const float* a_rgbas) {
165  draw_vertex_color_normal_array(gl::triangle_fan(),a_floatn,a_xyzs,a_rgbas,a_nms);
166  return true;
167  }
168  bool add_triangle_strip_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms) {
169  draw_vertex_normal_array(gl::triangle_strip(),a_floatn,a_xyzs,a_nms);
170  return true;
171  }
172  bool add_triangle_strip_normal_rgba(size_t a_floatn,const float* a_xyzs,const float* a_nms,const float* a_rgbas) {
173  draw_vertex_color_normal_array(gl::triangle_strip(),a_floatn,a_xyzs,a_rgbas,a_nms);
174  return true;
175  }
176  bool add_lines(size_t a_floatn,const float* a_xyzs) {
177  draw_vertex_array(gl::lines(),a_floatn,a_xyzs);
178  return true;
179  }
180  bool add_line_loop(size_t a_floatn,const float* a_xyzs) {
181  draw_vertex_array(gl::line_loop(),a_floatn,a_xyzs);
182  return true;
183  }
184  bool add_line_strip(size_t a_floatn,const float* a_xyzs) {
185  draw_vertex_array(gl::line_strip(),a_floatn,a_xyzs);
186  return true;
187  }
188  bool add_points(size_t a_floatn,const float* a_xyzs) {
189  draw_vertex_array(gl::points(),a_floatn,a_xyzs);
190  return true;
191  }
192 
193 /*
194  bool add_triangles_texture(size_t a_floatn,const float* a_xyzs,gstoid a_tex,const float* a_texs){
195  draw_vertex_array_texture(gl::triangles(),a_floatn,a_xyzs,a_tex,a_texs);
196  return true;
197  }
198  bool add_triangle_fan_texture(size_t a_floatn,const float* a_xyzs,gstoid a_tex,const float* a_texs){
199  draw_vertex_array_texture(gl::triangle_fan(),a_floatn,a_xyzs,a_tex,a_texs);
200  return true;
201  }
202  bool add_triangle_strip_texture(size_t a_floatn,const float* a_xyzs,gstoid a_tex,const float* a_texs){
203  draw_vertex_array_texture(gl::triangle_strip(),a_floatn,a_xyzs,a_tex,a_texs);
204  return true;
205  }
206 */
207  bool add_triangle_fan_texture_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms,gstoid a_tex,const float* a_texs){
208  draw_vertex_normal_array_texture(gl::triangle_fan(),a_floatn,a_xyzs,a_nms,a_tex,a_texs);
209  return true;
210  }
211  bool add_triangle_strip_texture_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms,gstoid a_tex,const float* a_texs){
212  draw_vertex_normal_array_texture(gl::triangle_strip(),a_floatn,a_xyzs,a_nms,a_tex,a_texs);
213  return true;
214  }
215 
216  bool add_line_loop(const std::vector<float>& a_xyzs){
217  const float* _xyzs = vec_data<float>(a_xyzs);
218  draw_vertex_array(gl::line_loop(),a_xyzs.size(),_xyzs);
219  return true;
220  }
221 
222  bool add_line_strip(const std::vector<float>& a_xyzs){
223  const float* _xyzs = vec_data<float>(a_xyzs);
224  draw_vertex_array(gl::line_strip(),a_xyzs.size(),_xyzs);
225  return true;
226  }
227 
228 /*
232  bool add_lines(const std::vector<float>& a_xyzs){
233  const float* _xyzs = vec_data<float>(a_xyzs);
234  draw_vertex_array(gl::lines(),a_xyzs.size(),_xyzs);
235  return true;
236  }
237  bool add_triangles_normal(const std::vector<float>& a_xyzs,const std::vector<float>& a_nms){
238  if(a_xyzs.size()!=a_nms.size()) return false;
239  const float* _xyzs = vec_data<float>(a_xyzs);
240  const float* _nms = vec_data<float>(a_nms);
241  draw_vertex_normal_array(gl::triangles(),a_xyzs.size(),_xyzs,_nms);
242  return true;
243  }
244  bool project_point(const mat4f& a_model_matrix,const mat4f& a_projection_matrix,
245  float& a_x,float& a_y,float& a_z,float& a_w) {
246  //return project_point(a_x,a_y,a_z,a_w);
247  a_w = 1;
248  a_model_matrix.mul_4f(a_x,a_y,a_z,a_w);
249  a_projection_matrix.mul_4f(a_x,a_y,a_z,a_w);
250  if(a_w==0.0F) return false;
251  a_x /= a_w;
252  a_y /= a_w;
253  a_z /= a_w;
254  return true;
255  }
259 */
260 
261  bool add_triangle_strip_as_triangles(size_t a_floatn,const float* a_xyzs,const float* a_nms) { //used in sg::sphere, icosahedron_sphere.
262  // It appears that if glShadeModel is GL_FLAT, triangle strip does not look the same in "gsto mode" where it is rendered as triangles.
263  // We use this function for immediate rendering, in case we want the same rendering as "gsto mode".
264 
265  size_t num = a_floatn/3;
266  if(num<3) return false;
267  size_t nxyzs = (num-2)*3*3;
268 
269  std::vector<float> m_xyzs(nxyzs);
270  std::vector<float> m_nms(nxyzs);
271 
272  {float* pxyzs = vec_data<float>(m_xyzs);
273  float* pnms = vec_data<float>(m_nms);
274  gl::triangle_strip_to_triangles_nms(num,a_xyzs,a_nms,pxyzs,pnms);}
275 
276  return add_triangles_normal(nxyzs,vec_data<float>(m_xyzs),vec_data<float>(m_nms));
277  }
278 
279  void dump_vertex_array_xy(std::ostream& a_out,gl::mode_t /*a_mode*/,size_t a_floatn,const float* a_xys) {
280  size_t num = a_floatn/2;
281  if(!num) return;
282  a_out << "dump_vertex_array_xy : begin : " << num << std::endl;
283  for(size_t index=0;index<num;index++) {
284  a_out << "xy : " << index
285  << " " << a_xys[2*index]
286  << " " << a_xys[2*index+1]
287  << std::endl;
288  }
289  a_out << "dump_vertex_array_xy : end." << std::endl;
290  }
291 };
292 
293 }}
294 
295 #endif
tools::gl::triangle_strip_to_triangles_nms
void triangle_strip_to_triangles_nms(size_t a_npt, const float *a_xyzs, const float *a_nms, float *&a_pxyzs, float *&a_pnms)
Definition: glprims:307
tools::sg::render_action::add_line_strip
bool add_line_strip(const std::vector< float > &a_xyzs)
Definition: render_action:222
tools::sg::render_action::restore_state
virtual void restore_state(unsigned int)=0
tools::sg::render_action::set_line_smooth
virtual void set_line_smooth(bool)=0
tools::sg::render_action::draw_gsto_v
virtual void draw_gsto_v(gl::mode_t, size_t, bufpos)=0
tools::sg::render_action::draw_vertex_array
virtual void draw_vertex_array(gl::mode_t, size_t, const float *)=0
tools::sg::state
Definition: state:25
tools::sg::render_action::set_lighting
virtual void set_lighting(bool)=0
tools::sg::render_action::draw_vertex_color_normal_array
void draw_vertex_color_normal_array(gl::mode_t a_mode, const std::vector< float > &a_xyzs, const std::vector< float > &a_rgbas, const std::vector< float > &a_nms)
Definition: render_action:142
tools::sg::render_action::clear_color
virtual void clear_color(float, float, float, float)=0
tools::gl::triangles
mode_t triangles()
Definition: glprims:20
tools::sg::render_action::add_line_loop
bool add_line_loop(size_t a_floatn, const float *a_xyzs)
Definition: render_action:180
tools::sg::render_action::normal
void normal(const vec3f &a_vec)
Definition: render_action:152
tools::sg::render_action::add_line_loop
bool add_line_loop(const std::vector< float > &a_xyzs)
Definition: render_action:216
tools::sg::render_action::add_lines
bool add_lines(size_t a_floatn, const float *a_xyzs)
Definition: render_action:176
tools::sg::render_action::draw_gsto_vc
virtual void draw_gsto_vc(gl::mode_t, size_t, bufpos, bufpos)=0
tools::sg::render_action::draw_vertex_color_array
void draw_vertex_color_array(gl::mode_t a_mode, const std::vector< float > &a_xyzs, const std::vector< float > &a_rgbas)
Definition: render_action:126
tools::colorf
Definition: colorf:11
tools::sg::render_action::add_triangle_fan_normal
bool add_triangle_fan_normal(size_t a_floatn, const float *a_xyzs, const float *a_nms)
Definition: render_action:160
tools::sg::render_action::add_triangle_strip_normal_rgba
bool add_triangle_strip_normal_rgba(size_t a_floatn, const float *a_xyzs, const float *a_nms, const float *a_rgbas)
Definition: render_action:172
tools::gl::triangle_fan
mode_t triangle_fan()
Definition: glprims:22
tools::sg::render_action::draw_gsto_vcn
virtual void draw_gsto_vcn(gl::mode_t, size_t, bufpos, bufpos, bufpos)=0
tools::gl::lines
mode_t lines()
Definition: glprims:17
tools::sg::render_action::set_cull_face
virtual void set_cull_face(bool)=0
tools::sg::shade_type
shade_type
Definition: enums:197
tools::sg::render_action::draw_vertex_color_normal_array
virtual void draw_vertex_color_normal_array(gl::mode_t, size_t, const float *, const float *, const float *)=0
tools::sg::render_action::add_triangle_fan_normal_rgba
bool add_triangle_fan_normal_rgba(size_t a_floatn, const float *a_xyzs, float *a_nms, const float *a_rgbas)
Definition: render_action:164
tools::sg::render_action::set_winding
virtual void set_winding(winding_type)=0
tools::sg::render_action::add_triangle_strip_texture_normal
bool add_triangle_strip_texture_normal(size_t a_floatn, const float *a_xyzs, const float *a_nms, gstoid a_tex, const float *a_texs)
Definition: render_action:211
tools::sg::matrix_action
Definition: matrix_action:15
tools::gl::line_strip
mode_t line_strip()
Definition: glprims:19
tools::sg::render_action::add_line_strip
bool add_line_strip(size_t a_floatn, const float *a_xyzs)
Definition: render_action:184
tools::sg::render_action::color4f
virtual void color4f(float, float, float, float)=0
tools::sg::render_action::operator=
render_action & operator=(const render_action &a_from)
Definition: render_action:86
tools::sg::render_action::gstoid
unsigned int gstoid
Definition: render_action:27
tools::sg::render_action::set_blend
virtual void set_blend(bool)=0
tools::sg::render_action::enable_light
void enable_light(unsigned int a_light, const vec3f &a_dir, const colorf &a_col)
Definition: render_action:108
tools::sg::render_action::render_action
render_action(std::ostream &a_out, unsigned int a_ww, unsigned int a_wh)
Definition: render_action:79
tools::sg::render_action::set_point_smooth
virtual void set_point_smooth(bool)=0
tools::sg::render_action::add_triangle_strip_normal
bool add_triangle_strip_normal(size_t a_floatn, const float *a_xyzs, const float *a_nms)
Definition: render_action:168
tools::sg::render_action::add_triangles_normal
bool add_triangles_normal(size_t a_floatn, const float *a_xyzs, const float *a_nms)
Definition: render_action:156
matrix_action
tools::gl::triangle_strip
mode_t triangle_strip()
Definition: glprims:21
tools::sg::render_manager
Definition: render_manager:16
tools::sg::render_action::render_action
render_action(const render_action &a_from)
Definition: render_action:83
tools::sg::render_action::point_size
virtual void point_size(float)=0
tools::sg::render_action::draw_vertex_array
void draw_vertex_array(gl::mode_t a_mode, const std::vector< float > &a_xyzs)
Definition: render_action:116
tools::sg::render_action::enable_light
virtual void enable_light(unsigned int, float, float, float, float, float, float, float)=0
tools::sg::render_action::set_polygon_offset
virtual void set_polygon_offset(bool)=0
tools::gl::points
mode_t points()
Definition: glprims:16
tools::sg::render_action::render_manager
virtual sg::render_manager & render_manager()=0
tools::sg::render_action::load_proj_matrix
virtual void load_proj_matrix(const mat4f &)=0
tools::vec3f
Definition: vec3f:13
tools::sg::render_action
Definition: render_action:24
tools::sg::render_action::begin_gsto
virtual void begin_gsto(gstoid)=0
VBO /////////////////////////////////////////////////////////.
tools::gl::mode_t
unsigned char mode_t
Definition: glprims:14
tools::sg::render_action::draw_gsto_vn
virtual void draw_gsto_vn(gl::mode_t, size_t, bufpos, bufpos)=0
tools::sg::render_action::add_triangle_strip_as_triangles
bool add_triangle_strip_as_triangles(size_t a_floatn, const float *a_xyzs, const float *a_nms)
Definition: render_action:261
tools::sg::render_action::draw_vertex_color_array
virtual void draw_vertex_color_array(gl::mode_t, size_t, const float *, const float *)=0
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::state::m_proj
mat4f m_proj
Definition: state:244
tools::sg::render_action::normal
virtual void normal(float, float, float)=0
tools::sg::render_action::draw_vertex_array_xy
virtual void draw_vertex_array_xy(gl::mode_t, size_t, const float *)=0
tools::sg::matrix_action::m_identity
mat4f m_identity
Definition: matrix_action:129
tools::sg::render_action::set_shade_model
virtual void set_shade_model(shade_type)=0
tools::sg::winding_type
winding_type
Definition: enums:104
tools::sg::render_action::set_depth_test
virtual void set_depth_test(bool)=0
tools::sg::render_action::end_gsto
virtual void end_gsto()=0
tools::sg::state::m_model
mat4f m_model
Definition: state:245
tools::gl::line_loop
mode_t line_loop()
Definition: glprims:18
tools::mat4f
Definition: mat4f:12
tools::sg::render_action::load_matrices_from_state
void load_matrices_from_state()
Definition: render_action:95
tools::sg::render_action::clear_color
void clear_color(const colorf &a_color)
Definition: render_action:101
tools::sg::render_action::~render_action
virtual ~render_action()
Definition: render_action:81
tools::sg::render_action::max_lights
virtual unsigned int max_lights()=0
tools::sg::render_action::load_model_matrix
virtual void load_model_matrix(const mat4f &)=0
tools::sg::render_action::add_points
bool add_points(size_t a_floatn, const float *a_xyzs)
Definition: render_action:188
tools::sg::render_action::draw_vertex_normal_array
virtual void draw_vertex_normal_array(gl::mode_t, size_t, const float *, const float *)=0
tools::sg::bufpos
size_t bufpos
Definition: render_action:22
tools::sg::render_action::draw_vertex_normal_array_texture
virtual void draw_vertex_normal_array_texture(gl::mode_t, size_t, const float *, const float *, gstoid, const float *)=0
tools::sg::render_action::line_width
virtual void line_width(float)=0
tools::sg::states::state
const sg::state & state() const
Definition: states:76
tools::sg::render_action::draw_vertex_array_xy
void draw_vertex_array_xy(gl::mode_t a_mode, const std::vector< float > &a_xys)
Definition: render_action:121
tools::sg::render_action::add_triangle_fan_texture_normal
bool add_triangle_fan_texture_normal(size_t a_floatn, const float *a_xyzs, const float *a_nms, gstoid a_tex, const float *a_texs)
Definition: render_action:207
tools::sg::render_action::color4f
void color4f(const colorf &a_color)
Definition: render_action:104
tools::sg::render_action::dump_vertex_array_xy
void dump_vertex_array_xy(std::ostream &a_out, gl::mode_t, size_t a_floatn, const float *a_xys)
Definition: render_action:279
tools::sg::render_action::load_matrices_to_identity
void load_matrices_to_identity()
Definition: render_action:91
tools::sg::render_action::draw_vertex_array_texture
virtual void draw_vertex_array_texture(gl::mode_t, size_t, const float *, gstoid, const float *)=0
texture /////////////////////////////////////////////////////
tools::sg::render_action::draw_vertex_normal_array
void draw_vertex_normal_array(gl::mode_t a_mode, const std::vector< float > &a_xyzs, const std::vector< float > &a_nms)
Definition: render_action:134
TOOLS_ACTION_NO_COPY
#define TOOLS_ACTION_NO_COPY(a__class, a__sclass, a__parent)
Definition: action:64