Warning, /include/Geant4/tools/sg/render_action is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sg_render_action
0005 #define tools_sg_render_action
0006
0007 #include "matrix_action"
0008
0009 #include "../glprims"
0010 #include "../vdata"
0011 #include "../colorf"
0012
0013 namespace tools {
0014 namespace sg {
0015 class render_manager;
0016 }}
0017
0018
0019 namespace tools {
0020 namespace sg {
0021
0022 typedef size_t bufpos;
0023
0024 class render_action : public matrix_action {
0025 TOOLS_ACTION_NO_COPY(render_action,tools::sg::render_action,matrix_action)
0026 public:
0027 typedef unsigned int gstoid;
0028 public:
0029 virtual void load_proj_matrix(const mat4f&) = 0;
0030 virtual void load_model_matrix(const mat4f&) = 0;
0031 public:
0032 virtual void draw_vertex_array(gl::mode_t,size_t,const float*) = 0;
0033 virtual void draw_vertex_array_xy(gl::mode_t,size_t,const float*) = 0;
0034 virtual void draw_vertex_color_array(gl::mode_t,size_t,const float*,const float*) = 0;
0035 virtual void draw_vertex_normal_array(gl::mode_t,size_t,const float*,const float*) = 0;
0036 virtual void draw_vertex_color_normal_array(gl::mode_t,size_t,const float*,const float*,const float*) = 0;
0037
0038 /////////////////////////////////////////////////////////////////
0039 /// texture /////////////////////////////////////////////////////
0040 /////////////////////////////////////////////////////////////////
0041 virtual void draw_vertex_array_texture(gl::mode_t,size_t,const float*,gstoid,const float*) = 0;
0042 virtual void draw_vertex_normal_array_texture(gl::mode_t,size_t,const float*,const float*,gstoid,const float*) = 0;
0043
0044 /////////////////////////////////////////////////////////////////
0045 /// VBO /////////////////////////////////////////////////////////
0046 /////////////////////////////////////////////////////////////////
0047 virtual void begin_gsto(gstoid) = 0;
0048 virtual void draw_gsto_v(gl::mode_t,size_t,bufpos) = 0;
0049 virtual void draw_gsto_vc(gl::mode_t,size_t,bufpos,bufpos) = 0;
0050 virtual void draw_gsto_vn(gl::mode_t,size_t,bufpos,bufpos) = 0;
0051 virtual void draw_gsto_vcn(gl::mode_t,size_t,bufpos,bufpos,bufpos) = 0;
0052 virtual void end_gsto() = 0;
0053 /////////////////////////////////////////////////////////////////
0054 /////////////////////////////////////////////////////////////////
0055 /////////////////////////////////////////////////////////////////
0056
0057 virtual void clear_color(float,float,float,float) = 0;
0058 virtual void color4f(float,float,float,float) = 0;
0059 virtual void line_width(float) = 0;
0060 virtual void point_size(float) = 0;
0061 virtual void set_polygon_offset(bool) = 0;
0062 virtual void set_winding(winding_type) = 0;
0063 virtual void set_shade_model(shade_type) = 0;
0064 virtual void set_cull_face(bool) = 0;
0065 virtual void set_point_smooth(bool) = 0;
0066 virtual void set_line_smooth(bool) = 0;
0067 virtual void normal(float,float,float) = 0;
0068 virtual void set_depth_test(bool) = 0;
0069 virtual unsigned int max_lights() = 0;
0070 virtual void enable_light(unsigned int,
0071 float,float,float, //directrion
0072 float,float,float,float, //diffuse RGBA
0073 float,float,float,float) = 0; //ambient RGBA
0074 virtual void set_lighting(bool) = 0;
0075 virtual void set_blend(bool) = 0;
0076 virtual void restore_state(unsigned int) = 0;
0077
0078 virtual sg::render_manager& render_manager() = 0; //sg:: is needed.
0079 public:
0080 render_action(std::ostream& a_out,unsigned int a_ww,unsigned int a_wh)
0081 :parent(a_out,a_ww,a_wh)
0082 ,m_do_transparency(false)
0083 ,m_have_to_do_transparency(false)
0084 {}
0085 virtual ~render_action(){}
0086 public:
0087 render_action(const render_action& a_from)
0088 :parent(a_from)
0089 ,m_do_transparency(a_from.m_do_transparency)
0090 ,m_have_to_do_transparency(a_from.m_have_to_do_transparency)
0091 {}
0092 render_action& operator=(const render_action& a_from){
0093 parent::operator=(a_from);
0094 m_do_transparency = a_from.m_do_transparency;
0095 m_have_to_do_transparency = a_from.m_have_to_do_transparency;
0096 return *this;
0097 }
0098 public:
0099 void set_do_transparency(bool a_value) {m_do_transparency = a_value;}
0100 bool do_transparency() const {return m_do_transparency;}
0101 void set_have_to_do_transparency(bool a_value) {m_have_to_do_transparency = a_value;}
0102 bool have_to_do_transparency() const {return m_have_to_do_transparency;}
0103
0104 bool have_to_render() {
0105 bool transparent = state().m_color.a()!=1.0f?true:false;
0106 if(transparent && state().m_GL_BLEND) {
0107 if(m_do_transparency) return true;
0108 m_have_to_do_transparency = true;
0109 return false;
0110 }
0111 if(m_do_transparency) return false;
0112 return true;
0113 }
0114
0115 void load_matrices_to_identity() {
0116 load_proj_matrix(m_identity);
0117 load_model_matrix(m_identity);
0118 }
0119 void load_matrices_from_state() {
0120 const sg::state& _state = state();
0121 load_proj_matrix(_state.m_proj);
0122 load_model_matrix(_state.m_model);
0123 }
0124
0125 void clear_color(const colorf& a_color){
0126 clear_color(a_color[0],a_color[1],a_color[2],a_color[3]);
0127 }
0128 void color4f(const colorf& a_color){
0129 color4f(a_color[0],a_color[1],a_color[2],a_color[3]);
0130 }
0131
0132 void enable_light(unsigned int a_light,
0133 const vec3f& a_dir,
0134 const colorf& a_col,const colorf& a_ambient) {
0135 enable_light(a_light,
0136 a_dir[0],a_dir[1],a_dir[2],
0137 a_col[0],a_col[1],a_col[2],a_col[3],
0138 a_ambient[0],a_ambient[1],a_ambient[2],a_ambient[3]);
0139 }
0140
0141 void draw_vertex_array(gl::mode_t a_mode,const std::vector<float>& a_xyzs){
0142 const float* _xyzs = vec_data<float>(a_xyzs);
0143 draw_vertex_array(a_mode,a_xyzs.size(),_xyzs);
0144 }
0145
0146 void draw_vertex_array_xy(gl::mode_t a_mode,const std::vector<float>& a_xys){
0147 const float* _xys = vec_data<float>(a_xys);
0148 draw_vertex_array_xy(a_mode,a_xys.size(),_xys);
0149 }
0150
0151 void draw_vertex_color_array(gl::mode_t a_mode,
0152 const std::vector<float>& a_xyzs,
0153 const std::vector<float>& a_rgbas){
0154 const float* _xyzs = vec_data<float>(a_xyzs);
0155 const float* _rgbas = vec_data<float>(a_rgbas);
0156 draw_vertex_color_array(a_mode,a_xyzs.size(),_xyzs,_rgbas);
0157 }
0158
0159 void draw_vertex_normal_array(gl::mode_t a_mode,
0160 const std::vector<float>& a_xyzs,
0161 const std::vector<float>& a_nms){
0162 const float* _xyzs = vec_data<float>(a_xyzs);
0163 const float* _nms = vec_data<float>(a_nms);
0164 draw_vertex_normal_array(a_mode,a_xyzs.size(),_xyzs,_nms);
0165 }
0166
0167 void draw_vertex_color_normal_array(gl::mode_t a_mode,
0168 const std::vector<float>& a_xyzs,
0169 const std::vector<float>& a_rgbas,
0170 const std::vector<float>& a_nms){
0171 const float* _xyzs = vec_data<float>(a_xyzs);
0172 const float* _rgbas = vec_data<float>(a_rgbas);
0173 const float* _nms = vec_data<float>(a_nms);
0174 draw_vertex_color_normal_array(a_mode,a_xyzs.size(),_xyzs,_rgbas,_nms);
0175 }
0176
0177 void normal(const vec3f& a_vec) {normal(a_vec[0],a_vec[1],a_vec[2]);}
0178
0179 public:
0180 // for sphere::visit template.
0181 bool add_triangles_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms) {
0182 draw_vertex_normal_array(gl::triangles(),a_floatn,a_xyzs,a_nms);
0183 return true;
0184 }
0185 bool add_triangle_fan_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms) {
0186 draw_vertex_normal_array(gl::triangle_fan(),a_floatn,a_xyzs,a_nms);
0187 return true;
0188 }
0189 bool add_triangle_fan_normal_rgba(size_t a_floatn,const float* a_xyzs,float* a_nms,const float* a_rgbas) {
0190 draw_vertex_color_normal_array(gl::triangle_fan(),a_floatn,a_xyzs,a_rgbas,a_nms);
0191 return true;
0192 }
0193 bool add_triangle_strip_normal(size_t a_floatn,const float* a_xyzs,const float* a_nms) {
0194 draw_vertex_normal_array(gl::triangle_strip(),a_floatn,a_xyzs,a_nms);
0195 return true;
0196 }
0197 bool add_triangle_strip_normal_rgba(size_t a_floatn,const float* a_xyzs,const float* a_nms,const float* a_rgbas) {
0198 draw_vertex_color_normal_array(gl::triangle_strip(),a_floatn,a_xyzs,a_rgbas,a_nms);
0199 return true;
0200 }
0201 bool add_lines(size_t a_floatn,const float* a_xyzs) {
0202 draw_vertex_array(gl::lines(),a_floatn,a_xyzs);
0203 return true;
0204 }
0205 bool add_line_loop(size_t a_floatn,const float* a_xyzs) {
0206 draw_vertex_array(gl::line_loop(),a_floatn,a_xyzs);
0207 return true;
0208 }
0209 bool add_line_strip(size_t a_floatn,const float* a_xyzs) {
0210 draw_vertex_array(gl::line_strip(),a_floatn,a_xyzs);
0211 return true;
0212 }
0213 bool add_points(size_t a_floatn,const float* a_xyzs) {
0214 draw_vertex_array(gl::points(),a_floatn,a_xyzs);
0215 return true;
0216 }
0217
0218 /*
0219 bool add_triangles_texture(size_t a_floatn,const float* a_xyzs,gstoid a_tex,const float* a_texs){
0220 draw_vertex_array_texture(gl::triangles(),a_floatn,a_xyzs,a_tex,a_texs);
0221 return true;
0222 }
0223 bool add_triangle_fan_texture(size_t a_floatn,const float* a_xyzs,gstoid a_tex,const float* a_texs){
0224 draw_vertex_array_texture(gl::triangle_fan(),a_floatn,a_xyzs,a_tex,a_texs);
0225 return true;
0226 }
0227 bool add_triangle_strip_texture(size_t a_floatn,const float* a_xyzs,gstoid a_tex,const float* a_texs){
0228 draw_vertex_array_texture(gl::triangle_strip(),a_floatn,a_xyzs,a_tex,a_texs);
0229 return true;
0230 }
0231 */
0232 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){
0233 draw_vertex_normal_array_texture(gl::triangle_fan(),a_floatn,a_xyzs,a_nms,a_tex,a_texs);
0234 return true;
0235 }
0236 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){
0237 draw_vertex_normal_array_texture(gl::triangle_strip(),a_floatn,a_xyzs,a_nms,a_tex,a_texs);
0238 return true;
0239 }
0240
0241 bool add_line_loop(const std::vector<float>& a_xyzs){
0242 const float* _xyzs = vec_data<float>(a_xyzs);
0243 draw_vertex_array(gl::line_loop(),a_xyzs.size(),_xyzs);
0244 return true;
0245 }
0246
0247 bool add_line_strip(const std::vector<float>& a_xyzs){
0248 const float* _xyzs = vec_data<float>(a_xyzs);
0249 draw_vertex_array(gl::line_strip(),a_xyzs.size(),_xyzs);
0250 return true;
0251 }
0252
0253 /*
0254 /////////////////////////////////////////////////////////
0255 /// for sg::markers and gstos : /////////////////////////
0256 /////////////////////////////////////////////////////////
0257 bool add_lines(const std::vector<float>& a_xyzs){
0258 const float* _xyzs = vec_data<float>(a_xyzs);
0259 draw_vertex_array(gl::lines(),a_xyzs.size(),_xyzs);
0260 return true;
0261 }
0262 bool add_triangles_normal(const std::vector<float>& a_xyzs,const std::vector<float>& a_nms){
0263 if(a_xyzs.size()!=a_nms.size()) return false;
0264 const float* _xyzs = vec_data<float>(a_xyzs);
0265 const float* _nms = vec_data<float>(a_nms);
0266 draw_vertex_normal_array(gl::triangles(),a_xyzs.size(),_xyzs,_nms);
0267 return true;
0268 }
0269 bool project_point(const mat4f& a_model_matrix,const mat4f& a_projection_matrix,
0270 float& a_x,float& a_y,float& a_z,float& a_w) {
0271 //return project_point(a_x,a_y,a_z,a_w);
0272 a_w = 1;
0273 a_model_matrix.mul_4f(a_x,a_y,a_z,a_w);
0274 a_projection_matrix.mul_4f(a_x,a_y,a_z,a_w);
0275 if(a_w==0.0F) return false;
0276 a_x /= a_w;
0277 a_y /= a_w;
0278 a_z /= a_w;
0279 return true;
0280 }
0281 /////////////////////////////////////////////////////////
0282 /////////////////////////////////////////////////////////
0283 /////////////////////////////////////////////////////////
0284 */
0285
0286 bool add_triangle_strip_as_triangles(size_t a_floatn,const float* a_xyzs,const float* a_nms) { //used in sg::sphere, icosahedron_sphere.
0287 // It appears that if glShadeModel is GL_FLAT, triangle strip does not look the same in "gsto mode" where it is rendered as triangles.
0288 // We use this function for immediate rendering, in case we want the same rendering as "gsto mode".
0289
0290 size_t num = a_floatn/3;
0291 if(num<3) return false;
0292 size_t nxyzs = (num-2)*3*3;
0293
0294 std::vector<float> m_xyzs(nxyzs);
0295 std::vector<float> m_nms(nxyzs);
0296
0297 {float* pxyzs = vec_data<float>(m_xyzs);
0298 float* pnms = vec_data<float>(m_nms);
0299 gl::triangle_strip_to_triangles_nms(num,a_xyzs,a_nms,pxyzs,pnms);}
0300
0301 return add_triangles_normal(nxyzs,vec_data<float>(m_xyzs),vec_data<float>(m_nms));
0302 }
0303
0304 void dump_vertex_array_xy(std::ostream& a_out,gl::mode_t /*a_mode*/,size_t a_floatn,const float* a_xys) {
0305 size_t num = a_floatn/2;
0306 if(!num) return;
0307 a_out << "dump_vertex_array_xy : begin : " << num << std::endl;
0308 for(size_t index=0;index<num;index++) {
0309 a_out << "xy : " << index
0310 << " " << a_xys[2*index]
0311 << " " << a_xys[2*index+1]
0312 << std::endl;
0313 }
0314 a_out << "dump_vertex_array_xy : end." << std::endl;
0315 }
0316 protected:
0317 bool m_do_transparency;
0318 bool m_have_to_do_transparency;
0319 };
0320
0321 }}
0322
0323 #endif