Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_render_gstos 5 #define tools_sg_render_gstos 6 7 #include "gstos" 8 #include "render_action" 9 10 #include "gstos_add" 11 12 namespace tools { 13 namespace sg { 14 15 class render_gstos : public gstos { 16 typedef gstos parent; 17 public: 18 virtual void visit(gstos_add& a_visitor,draw_type a_style) = 0; 19 protected: //gstos 20 virtual unsigned int create_gsto(std::ostream&,sg::render_manager& a_mgr) { 21 std::vector<float> gsto_data; 22 23 gstos_add _add; 24 visit(_add,draw_points); 25 append(gsto_data,_add.m_xyzs); 26 m_gstos_points_sz = _add.m_xyzs.size(); 27 28 _add.clear(); 29 visit(_add,draw_lines); 30 append(gsto_data,_add.m_xyzs); 31 m_gstos_lines_sz = _add.m_xyzs.size(); 32 33 _add.clear(); 34 visit(_add,draw_filled); 35 append(gsto_data,_add.m_xyzs); 36 m_gstos_tris_sz = _add.m_xyzs.size(); 37 append(gsto_data,_add.m_nms); 38 m_gstos_nms_sz = _add.m_nms.size(); 39 40 if(gsto_data.empty()) return 0; 41 42 return a_mgr.create_gsto_from_data(gsto_data); 43 } 44 public: //node 45 bool gstos_render(render_action& a_action) { 46 const state& state = a_action.state(); 47 48 bool draw_edges = false; 49 if(state.m_draw_type==draw_filled) draw_edges = state.m_GL_LIGHTING?false:true; 50 51 if(state.m_use_gsto) { 52 unsigned int _id = get_gsto_id(a_action.out(),a_action.render_manager()); 53 if(_id) { 54 bufpos pxyzs = 0; 55 bufpos plines = pxyzs+m_gstos_points_sz*sizeof(float); 56 bufpos ptris = plines+m_gstos_lines_sz*sizeof(float); 57 bufpos pnms = ptris+m_gstos_tris_sz*sizeof(float); 58 59 a_action.begin_gsto(_id); 60 if(draw_edges) { 61 //a_action.set_lighting(false); //NOTE : we should do that if style==draw_fill ! 62 a_action.color4f(0,0,0,1); //if lighten, then rendered grey. 63 a_action.line_width(1); 64 65 a_action.draw_gsto_v(gl::lines(),m_gstos_lines_sz/3,plines); 66 67 //pushes back the filled polygons to avoid z-fighting with lines 68 a_action.set_polygon_offset(true); 69 70 a_action.color4f(state.m_color); 71 a_action.line_width(state.m_line_width); 72 //a_action.set_lighting(state.m_GL_LIGHTING); 73 } 74 if(state.m_draw_type==draw_points) { 75 a_action.draw_gsto_v(gl::points(),m_gstos_points_sz/3,pxyzs); 76 } else if(state.m_draw_type==draw_lines) { 77 a_action.draw_gsto_v(gl::lines(),m_gstos_lines_sz/3,plines); 78 } else { 79 a_action.draw_gsto_vn(gl::triangles(),m_gstos_tris_sz/3,ptris,pnms); 80 } 81 if(draw_edges) a_action.set_polygon_offset(state.m_GL_POLYGON_OFFSET_FILL); 82 a_action.end_gsto(); 83 return true; 84 85 } else { //!_id 86 // use immediate rendering. 87 } 88 89 } else { 90 clean_gstos(&a_action.render_manager()); 91 } 92 return false; 93 } 94 bool gstos_render_no_style(render_action& a_action) { //for sg::markers. 95 const state& state = a_action.state(); 96 if(state.m_use_gsto) { 97 unsigned int _id = get_gsto_id(a_action.out(),a_action.render_manager()); 98 if(_id) { 99 bufpos pxyzs = 0; 100 bufpos plines = pxyzs+m_gstos_points_sz*sizeof(float); 101 bufpos ptris = plines+m_gstos_lines_sz*sizeof(float); 102 bufpos pnms = ptris+m_gstos_tris_sz*sizeof(float); 103 104 a_action.begin_gsto(_id); 105 a_action.draw_gsto_v(gl::points(),m_gstos_points_sz/3,pxyzs); 106 a_action.draw_gsto_v(gl::lines(),m_gstos_lines_sz/3,plines); 107 a_action.draw_gsto_vn(gl::triangles(),m_gstos_tris_sz/3,ptris,pnms); 108 a_action.end_gsto(); 109 return true; 110 111 } else { //!_id 112 // use immediate rendering. 113 } 114 115 } else { 116 clean_gstos(&a_action.render_manager()); 117 } 118 return false; 119 } 120 public: 121 render_gstos():parent(),m_gstos_points_sz(0),m_gstos_lines_sz(0),m_gstos_tris_sz(0),m_gstos_nms_sz(0) {} 122 virtual ~render_gstos() {} 123 public: 124 render_gstos(const render_gstos& a_from):parent(a_from),m_gstos_points_sz(0),m_gstos_lines_sz(0),m_gstos_tris_sz(0),m_gstos_nms_sz(0) {} 125 render_gstos& operator=(const render_gstos& a_from){ 126 parent::operator=(a_from); 127 m_gstos_points_sz = 0; 128 m_gstos_lines_sz = 0; 129 m_gstos_tris_sz = 0; 130 m_gstos_nms_sz = 0; 131 return *this; 132 } 133 protected: 134 size_t m_gstos_points_sz; 135 size_t m_gstos_lines_sz; 136 size_t m_gstos_tris_sz; 137 size_t m_gstos_nms_sz; 138 }; 139 140 }} 141 142 #endif