Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_sg_gl2ps_action 4 #ifndef tools_sg_gl2ps_action 5 #define tools_sg_gl2ps_action 5 #define tools_sg_gl2ps_action 6 6 7 #include "gl2ps_manager" 7 #include "gl2ps_manager" 8 #include "../gl2ps" 8 #include "../gl2ps" 9 9 10 #include "render_action" 10 #include "render_action" 11 #include "primitive_visitor" 11 #include "primitive_visitor" 12 #include "../colorfs" 12 #include "../colorfs" 13 #include "../lina/plane" 13 #include "../lina/plane" 14 14 15 #include <cstdio> //FILE 15 #include <cstdio> //FILE 16 16 17 namespace tools { 17 namespace tools { 18 namespace sg { 18 namespace sg { 19 19 20 class gl2ps_action : public render_action { 20 class gl2ps_action : public render_action { 21 TOOLS_ACTION(gl2ps_action,tools::sg::gl2ps_a 21 TOOLS_ACTION(gl2ps_action,tools::sg::gl2ps_action,render_action) 22 private: 22 private: 23 gl2ps_action& get_me() {return *this;} 23 gl2ps_action& get_me() {return *this;} 24 24 25 static unsigned int _GL2PS_POINT() {retur 25 static unsigned int _GL2PS_POINT() {return 2;} 26 static unsigned int _GL2PS_LINE() {retur 26 static unsigned int _GL2PS_LINE() {return 3;} 27 static unsigned int _GL2PS_TRIANGLE() {retur 27 static unsigned int _GL2PS_TRIANGLE() {return 5;} 28 public: 28 public: 29 virtual void draw_vertex_array(gl::mode_t a_ 29 virtual void draw_vertex_array(gl::mode_t a_mode, 30 size_t a_floa 30 size_t a_floatn, 31 const float* 31 const float* a_xyzs){ 32 m_pv.add_primitive(a_mode,a_floatn,a_xyzs) 32 m_pv.add_primitive(a_mode,a_floatn,a_xyzs); 33 } 33 } 34 34 35 virtual void draw_vertex_array_xy(gl::mode_t 35 virtual void draw_vertex_array_xy(gl::mode_t a_mode, 36 size_t a_f 36 size_t a_floatn, 37 const floa 37 const float* a_xys){ 38 m_pv.add_primitive_xy(a_mode,a_floatn,a_xy 38 m_pv.add_primitive_xy(a_mode,a_floatn,a_xys); 39 } 39 } 40 40 41 virtual void draw_vertex_color_array(gl::mod 41 virtual void draw_vertex_color_array(gl::mode_t a_mode, 42 size_t 42 size_t a_floatn, 43 const f 43 const float* a_xyzs, 44 const f 44 const float* a_rgbas){ 45 m_pv.add_primitive_rgba(a_mode,a_floatn,a_ 45 m_pv.add_primitive_rgba(a_mode,a_floatn,a_xyzs,a_rgbas); 46 } 46 } 47 47 48 virtual void draw_vertex_normal_array(gl::mo 48 virtual void draw_vertex_normal_array(gl::mode_t a_mode, 49 size_t 49 size_t a_floatn, 50 const 50 const float* a_xyzs, 51 const 51 const float* a_nms){ 52 m_pv.add_primitive_normal(a_mode,a_floatn, 52 m_pv.add_primitive_normal(a_mode,a_floatn,a_xyzs,a_nms); 53 } 53 } 54 54 55 virtual void draw_vertex_color_normal_array( 55 virtual void draw_vertex_color_normal_array(gl::mode_t a_mode, 56 56 size_t a_floatn, 57 57 const float* a_xyzs, 58 58 const float* a_rgbas, 59 59 const float* a_nms){ 60 // We expect a_nms of size : 3*(a_floatn/3 60 // We expect a_nms of size : 3*(a_floatn/3) 61 // (then one normal per 3D point). 61 // (then one normal per 3D point). 62 m_pv.add_primitive_normal_rgba(a_mode,a_fl 62 m_pv.add_primitive_normal_rgba(a_mode,a_floatn,a_xyzs,a_nms,a_rgbas); 63 } 63 } 64 64 65 virtual void clear_color(float a_r,float a_g 65 virtual void clear_color(float a_r,float a_g,float a_b,float /*a_a*/){ 66 //a_a ? 66 //a_a ? 67 set_background(a_r,a_g,a_b); 67 set_background(a_r,a_g,a_b); 68 } 68 } 69 virtual void color4f(float a_r,float a_g,flo 69 virtual void color4f(float a_r,float a_g,float a_b,float a_a){ 70 m_color.set_value(a_r,a_g,a_b,a_a); 70 m_color.set_value(a_r,a_g,a_b,a_a); 71 } 71 } 72 virtual void line_width(float a_v){m_line_wi 72 virtual void line_width(float a_v){m_line_width = a_v;} 73 virtual void point_size(float a_v) {m_point_ 73 virtual void point_size(float a_v) {m_point_size = a_v;} 74 virtual void set_polygon_offset(bool a_v) {m 74 virtual void set_polygon_offset(bool a_v) {m_POLYGON_OFFSET_FILL = a_v;} 75 virtual void normal(float a_x,float a_y,floa 75 virtual void normal(float a_x,float a_y,float a_z) { 76 m_normal.set_value(a_x,a_y,a_z); 76 m_normal.set_value(a_x,a_y,a_z); 77 } 77 } 78 78 79 virtual void set_winding(winding_type a_v) { 79 virtual void set_winding(winding_type a_v) { 80 m_ccw = (a_v==winding_ccw?true:false); 80 m_ccw = (a_v==winding_ccw?true:false); 81 } 81 } 82 82 83 virtual void set_shade_model(shade_type a_v) 83 virtual void set_shade_model(shade_type a_v) { 84 if(a_v==shade_smooth) {} 84 if(a_v==shade_smooth) {} 85 else {} 85 else {} 86 } 86 } 87 87 88 virtual void set_depth_test(bool a_on) {m_DE 88 virtual void set_depth_test(bool a_on) {m_DEPTH_TEST = a_on;} 89 89 90 virtual void set_cull_face(bool a_on) {m_CUL 90 virtual void set_cull_face(bool a_on) {m_CULL_FACE = a_on;} 91 virtual void set_point_smooth(bool a_on) {m_ 91 virtual void set_point_smooth(bool a_on) {m_POINT_SMOOTH = a_on;} 92 virtual void set_line_smooth(bool a_on) {m_L 92 virtual void set_line_smooth(bool a_on) {m_LINE_SMOOTH = a_on;} 93 93 94 virtual void load_proj_matrix(const mat4f& a 94 virtual void load_proj_matrix(const mat4f& a_mtx) {m_proj = a_mtx;} 95 virtual void load_model_matrix(const mat4f& 95 virtual void load_model_matrix(const mat4f& a_mtx) { 96 m_model = a_mtx; 96 m_model = a_mtx; 97 set_normal_matrix(); 97 set_normal_matrix(); 98 } 98 } 99 99 100 virtual unsigned int max_lights() {return 10 100 virtual unsigned int max_lights() {return 1000;} 101 101 102 virtual void enable_light(unsigned int, 102 virtual void enable_light(unsigned int, 103 float a_dx,float a 103 float a_dx,float a_dy,float a_dz, 104 float a_r,float a_ 104 float a_r,float a_g,float a_b,float a_a, 105 float a_ar,float a 105 float a_ar,float a_ag,float a_ab,float a_aa){ 106 m_light_color.set_value(a_r,a_g,a_b,a_a); 106 m_light_color.set_value(a_r,a_g,a_b,a_a); 107 m_light_ambient.set_value(a_ar,a_ag,a_ab,a 107 m_light_ambient.set_value(a_ar,a_ag,a_ab,a_aa); 108 m_light_direction.set_value(a_dx,a_dy,a_dz 108 m_light_direction.set_value(a_dx,a_dy,a_dz); 109 m_light_direction.normalize(); 109 m_light_direction.normalize(); 110 m_light_on = true; 110 m_light_on = true; 111 } 111 } 112 112 113 virtual void set_lighting(bool a_on) {m_ligh 113 virtual void set_lighting(bool a_on) {m_light_on = a_on;} 114 virtual void set_blend(bool) {} 114 virtual void set_blend(bool) {} 115 115 116 virtual void restore_state(unsigned int /*a_ 116 virtual void restore_state(unsigned int /*a_ret_num_light*/) { 117 const sg::state& _state = state(); 117 const sg::state& _state = state(); 118 m_proj = _state.m_proj; 118 m_proj = _state.m_proj; 119 m_model = _state.m_model; 119 m_model = _state.m_model; 120 set_normal_matrix(); 120 set_normal_matrix(); 121 121 122 m_color = _state.m_color; 122 m_color = _state.m_color; 123 m_normal = _state.m_normal; 123 m_normal = _state.m_normal; 124 124 125 m_ccw = (_state.m_winding==winding_ccw?tru 125 m_ccw = (_state.m_winding==winding_ccw?true:false); 126 m_POLYGON_OFFSET_FILL = _state.m_GL_POLYGO 126 m_POLYGON_OFFSET_FILL = _state.m_GL_POLYGON_OFFSET_FILL; 127 m_CULL_FACE = _state.m_GL_CULL_FACE; 127 m_CULL_FACE = _state.m_GL_CULL_FACE; 128 m_POINT_SMOOTH = _state.m_GL_POINT_SMOOTH; 128 m_POINT_SMOOTH = _state.m_GL_POINT_SMOOTH; 129 m_LINE_SMOOTH = _state.m_GL_LINE_SMOOTH; 129 m_LINE_SMOOTH = _state.m_GL_LINE_SMOOTH; 130 m_point_size = _state.m_point_size; 130 m_point_size = _state.m_point_size; 131 m_line_width = _state.m_line_width; 131 m_line_width = _state.m_line_width; 132 m_light_on = _state.m_GL_LIGHTING; 132 m_light_on = _state.m_GL_LIGHTING; 133 m_DEPTH_TEST = _state.m_GL_DEPTH_TEST; 133 m_DEPTH_TEST = _state.m_GL_DEPTH_TEST; 134 } 134 } 135 135 136 //////////////////////////////////////////// 136 ///////////////////////////////////////////////////////////////// 137 /// texture //////////////////////////////// 137 /// texture ///////////////////////////////////////////////////// 138 //////////////////////////////////////////// 138 ///////////////////////////////////////////////////////////////// 139 virtual void draw_vertex_array_texture(gl::m 139 virtual void draw_vertex_array_texture(gl::mode_t,size_t a_xyzn,const float* a_xyzs, 140 gstoi 140 gstoid a_id,const float* a_tcs) { 141 img_byte img; 141 img_byte img; 142 if(!m_mgr.find(a_id,img)) return; 142 if(!m_mgr.find(a_id,img)) return; 143 m_pv.add_texture(m_out,a_xyzn,a_xyzs,img,a 143 m_pv.add_texture(m_out,a_xyzn,a_xyzs,img,a_tcs); 144 } 144 } 145 145 146 virtual void draw_vertex_normal_array_textur 146 virtual void draw_vertex_normal_array_texture(gl::mode_t a_mode, 147 size_ 147 size_t a_xyzn, 148 const 148 const float* a_xyzs, 149 const 149 const float* /*a_nms*/, 150 gstoi 150 gstoid a_id, 151 const 151 const float* a_tcs) { 152 draw_vertex_array_texture(a_mode,a_xyzn,a_ 152 draw_vertex_array_texture(a_mode,a_xyzn,a_xyzs,a_id,a_tcs); 153 } 153 } 154 154 155 //////////////////////////////////////////// 155 ///////////////////////////////////////////////////////////////// 156 /// VBO //////////////////////////////////// 156 /// VBO ///////////////////////////////////////////////////////// 157 //////////////////////////////////////////// 157 ///////////////////////////////////////////////////////////////// 158 virtual void begin_gsto(gstoid) {} 158 virtual void begin_gsto(gstoid) {} 159 virtual void draw_gsto_v(gl::mode_t,size_t,b 159 virtual void draw_gsto_v(gl::mode_t,size_t,bufpos){} 160 virtual void draw_gsto_vc(gl::mode_t,size_t, 160 virtual void draw_gsto_vc(gl::mode_t,size_t,bufpos,bufpos) {} 161 virtual void draw_gsto_vn(gl::mode_t,size_t, 161 virtual void draw_gsto_vn(gl::mode_t,size_t,bufpos,bufpos) {} 162 virtual void draw_gsto_vcn(gl::mode_t,size_t 162 virtual void draw_gsto_vcn(gl::mode_t,size_t,bufpos,bufpos,bufpos) {} 163 virtual void end_gsto() {} 163 virtual void end_gsto() {} 164 //////////////////////////////////////////// 164 ///////////////////////////////////////////////////////////////// 165 //////////////////////////////////////////// 165 ///////////////////////////////////////////////////////////////// 166 //////////////////////////////////////////// 166 ///////////////////////////////////////////////////////////////// 167 virtual sg::render_manager& render_manager() 167 virtual sg::render_manager& render_manager() {return m_mgr;} 168 public: 168 public: 169 gl2ps_action(gl2ps_manager& a_mgr,std::ostre 169 gl2ps_action(gl2ps_manager& a_mgr,std::ostream& a_out,unsigned int a_ww,unsigned int a_wh) 170 :parent(a_out,a_ww,a_wh) 170 :parent(a_out,a_ww,a_wh) 171 ,m_mgr(a_mgr) 171 ,m_mgr(a_mgr) 172 ,m_gl2ps_context(0) 172 ,m_gl2ps_context(0) 173 ,m_FILE(0) 173 ,m_FILE(0) 174 ,m_pv(get_me()) 174 ,m_pv(get_me()) 175 ,m_light_color(colorf_white()) 175 ,m_light_color(colorf_white()) 176 ,m_light_ambient(colorf_black()) 176 ,m_light_ambient(colorf_black()) 177 ,m_light_direction(vec3f(0,0,-1)) 177 ,m_light_direction(vec3f(0,0,-1)) 178 178 179 ,m_ccw(true) 179 ,m_ccw(true) 180 ,m_POLYGON_OFFSET_FILL(false) 180 ,m_POLYGON_OFFSET_FILL(false) 181 ,m_CULL_FACE(true) 181 ,m_CULL_FACE(true) 182 ,m_POINT_SMOOTH(false) 182 ,m_POINT_SMOOTH(false) 183 ,m_LINE_SMOOTH(false) 183 ,m_LINE_SMOOTH(false) 184 ,m_point_size(1) 184 ,m_point_size(1) 185 ,m_line_width(1) 185 ,m_line_width(1) 186 ,m_light_on(false) 186 ,m_light_on(false) 187 ,m_DEPTH_TEST(true) 187 ,m_DEPTH_TEST(true) 188 { 188 { 189 m_back[0] = 1; 189 m_back[0] = 1; 190 m_back[1] = 1; 190 m_back[1] = 1; 191 m_back[2] = 1; 191 m_back[2] = 1; 192 192 193 m_vp_mtx.set_identity(); 193 m_vp_mtx.set_identity(); 194 m_vp_mtx.mul_translate(float(m_ww)/2,float 194 m_vp_mtx.mul_translate(float(m_ww)/2,float(m_wh)/2,0); 195 m_vp_mtx.mul_scale(float(m_ww)/2,float(m_w 195 m_vp_mtx.mul_scale(float(m_ww)/2,float(m_wh)/2,1); 196 196 197 //m_zb.change_size(a_ww,a_wh); 197 //m_zb.change_size(a_ww,a_wh); 198 // m_zb.clear_color_buffer(0); 198 // m_zb.clear_color_buffer(0); 199 // m_zb.clear_depth_buffer(); 199 // m_zb.clear_depth_buffer(); 200 m_proj.set_identity(); 200 m_proj.set_identity(); 201 m_model.set_identity(); 201 m_model.set_identity(); 202 m_normal_matrix.set_identity(); 202 m_normal_matrix.set_identity(); 203 } 203 } 204 virtual ~gl2ps_action(){ 204 virtual ~gl2ps_action(){ 205 close(); 205 close(); 206 } 206 } 207 protected: 207 protected: 208 gl2ps_action(const gl2ps_action& a_from) 208 gl2ps_action(const gl2ps_action& a_from) 209 :parent(a_from) 209 :parent(a_from) 210 ,m_mgr(a_from.m_mgr) 210 ,m_mgr(a_from.m_mgr) 211 ,m_gl2ps_context(0) 211 ,m_gl2ps_context(0) 212 ,m_FILE(0) 212 ,m_FILE(0) 213 213 214 ,m_vp_mtx(a_from.m_vp_mtx) 214 ,m_vp_mtx(a_from.m_vp_mtx) 215 ,m_pv(a_from.m_pv) 215 ,m_pv(a_from.m_pv) 216 ,m_light_color(a_from.m_light_color) 216 ,m_light_color(a_from.m_light_color) 217 ,m_light_ambient(a_from.m_light_ambient) 217 ,m_light_ambient(a_from.m_light_ambient) 218 ,m_light_direction(a_from.m_light_direction) 218 ,m_light_direction(a_from.m_light_direction) 219 ,m_normal(a_from.m_normal) 219 ,m_normal(a_from.m_normal) 220 220 221 ,m_proj(a_from.m_proj) 221 ,m_proj(a_from.m_proj) 222 ,m_model(a_from.m_model) 222 ,m_model(a_from.m_model) 223 ,m_normal_matrix(a_from.m_normal_matrix) 223 ,m_normal_matrix(a_from.m_normal_matrix) 224 ,m_color(a_from.m_color) 224 ,m_color(a_from.m_color) 225 ,m_ccw(a_from.m_ccw) 225 ,m_ccw(a_from.m_ccw) 226 ,m_POLYGON_OFFSET_FILL(a_from.m_POLYGON_OFFS 226 ,m_POLYGON_OFFSET_FILL(a_from.m_POLYGON_OFFSET_FILL) 227 ,m_CULL_FACE(a_from.m_CULL_FACE) 227 ,m_CULL_FACE(a_from.m_CULL_FACE) 228 ,m_POINT_SMOOTH(a_from.m_POINT_SMOOTH) 228 ,m_POINT_SMOOTH(a_from.m_POINT_SMOOTH) 229 ,m_LINE_SMOOTH(a_from.m_LINE_SMOOTH) 229 ,m_LINE_SMOOTH(a_from.m_LINE_SMOOTH) 230 ,m_point_size(a_from.m_point_size) 230 ,m_point_size(a_from.m_point_size) 231 ,m_line_width(a_from.m_line_width) 231 ,m_line_width(a_from.m_line_width) 232 ,m_light_on(a_from.m_light_on) 232 ,m_light_on(a_from.m_light_on) 233 ,m_DEPTH_TEST(a_from.m_DEPTH_TEST) 233 ,m_DEPTH_TEST(a_from.m_DEPTH_TEST) 234 { 234 { 235 m_back[0] = a_from.m_back[0]; 235 m_back[0] = a_from.m_back[0]; 236 m_back[1] = a_from.m_back[1]; 236 m_back[1] = a_from.m_back[1]; 237 m_back[2] = a_from.m_back[2]; 237 m_back[2] = a_from.m_back[2]; 238 } 238 } 239 gl2ps_action& operator=(const gl2ps_action& 239 gl2ps_action& operator=(const gl2ps_action& a_from){ 240 parent::operator=(a_from); 240 parent::operator=(a_from); 241 if(&a_from==this) return *this; 241 if(&a_from==this) return *this; 242 close(); 242 close(); 243 m_back[0] = a_from.m_back[0]; 243 m_back[0] = a_from.m_back[0]; 244 m_back[1] = a_from.m_back[1]; 244 m_back[1] = a_from.m_back[1]; 245 m_back[2] = a_from.m_back[2]; 245 m_back[2] = a_from.m_back[2]; 246 246 247 m_vp_mtx = a_from.m_vp_mtx; 247 m_vp_mtx = a_from.m_vp_mtx; 248 m_pv = a_from.m_pv; 248 m_pv = a_from.m_pv; 249 m_light_color = a_from.m_light_color; 249 m_light_color = a_from.m_light_color; 250 m_light_ambient = a_from.m_light_ambient; 250 m_light_ambient = a_from.m_light_ambient; 251 m_light_direction = a_from.m_light_directi 251 m_light_direction = a_from.m_light_direction; 252 m_normal = a_from.m_normal; 252 m_normal = a_from.m_normal; 253 253 254 m_proj = a_from.m_proj; 254 m_proj = a_from.m_proj; 255 m_model = a_from.m_model; 255 m_model = a_from.m_model; 256 m_normal_matrix = a_from.m_normal_matrix; 256 m_normal_matrix = a_from.m_normal_matrix; 257 m_color = a_from.m_color; 257 m_color = a_from.m_color; 258 m_ccw = a_from.m_ccw; 258 m_ccw = a_from.m_ccw; 259 m_POLYGON_OFFSET_FILL = a_from.m_POLYGON_O 259 m_POLYGON_OFFSET_FILL = a_from.m_POLYGON_OFFSET_FILL; 260 m_CULL_FACE = a_from.m_CULL_FACE; 260 m_CULL_FACE = a_from.m_CULL_FACE; 261 m_POINT_SMOOTH = a_from.m_POINT_SMOOTH; 261 m_POINT_SMOOTH = a_from.m_POINT_SMOOTH; 262 m_LINE_SMOOTH = a_from.m_LINE_SMOOTH; 262 m_LINE_SMOOTH = a_from.m_LINE_SMOOTH; 263 m_point_size = a_from.m_point_size; 263 m_point_size = a_from.m_point_size; 264 m_line_width = a_from.m_line_width; 264 m_line_width = a_from.m_line_width; 265 m_light_on = a_from.m_light_on; 265 m_light_on = a_from.m_light_on; 266 m_DEPTH_TEST = a_from.m_DEPTH_TEST; 266 m_DEPTH_TEST = a_from.m_DEPTH_TEST; 267 return *this; 267 return *this; 268 } 268 } 269 public: 269 public: 270 bool open(const std::string& a_name,int a_fo 270 bool open(const std::string& a_name,int a_format = TOOLS_GL2PS_EPS) { 271 return open(a_name,a_format,-1,-1); 271 return open(a_name,a_format,-1,-1); 272 } 272 } 273 bool open(const std::string& a_name,int a_fo 273 bool open(const std::string& a_name,int a_format,int a_sort,int a_options) { 274 close(); 274 close(); 275 275 276 m_gl2ps_context = ::tools_gl2psCreateConte 276 m_gl2ps_context = ::tools_gl2psCreateContext(); 277 if(!m_gl2ps_context) { 277 if(!m_gl2ps_context) { 278 m_out << "tools::sg::gl2ps_action::open 278 m_out << "tools::sg::gl2ps_action::open :" 279 << " can't create gl2ps context." 279 << " can't create gl2ps context." 280 << std::endl; 280 << std::endl; 281 return false; 281 return false; 282 } 282 } 283 283 284 m_FILE = ::fopen(a_name.c_str(),"wb"); 284 m_FILE = ::fopen(a_name.c_str(),"wb"); 285 if(!m_FILE) { 285 if(!m_FILE) { 286 m_out << "tools::sg::gl2ps_action::open 286 m_out << "tools::sg::gl2ps_action::open :" 287 << " can't open file " << a_name < 287 << " can't open file " << a_name << "." 288 << std::endl; 288 << std::endl; 289 ::tools_gl2psDeleteContext(m_gl2ps_conte 289 ::tools_gl2psDeleteContext(m_gl2ps_context); 290 m_gl2ps_context = 0; 290 m_gl2ps_context = 0; 291 return false; 291 return false; 292 } 292 } 293 293 294 int sort = 0; 294 int sort = 0; 295 if(a_sort==(-1)) { 295 if(a_sort==(-1)) { 296 sort = TOOLS_GL2PS_BSP_SORT; 296 sort = TOOLS_GL2PS_BSP_SORT; 297 } else { 297 } else { 298 sort = a_sort; 298 sort = a_sort; 299 } 299 } 300 300 301 int options = 0; 301 int options = 0; 302 if(a_options==(-1)) { 302 if(a_options==(-1)) { 303 options = TOOLS_GL2PS_SILENT 303 options = TOOLS_GL2PS_SILENT 304 | TOOLS_GL2PS_OCCLUSION_CULL 304 | TOOLS_GL2PS_OCCLUSION_CULL 305 | TOOLS_GL2PS_BEST_ROOT 305 | TOOLS_GL2PS_BEST_ROOT 306 | TOOLS_GL2PS_DRAW_BACKGROUND; 306 | TOOLS_GL2PS_DRAW_BACKGROUND; 307 } else { 307 } else { 308 options = a_options; 308 options = a_options; 309 } 309 } 310 310 311 tools_GLint vp[4]; 311 tools_GLint vp[4]; 312 vp[0] = 0; 312 vp[0] = 0; 313 vp[1] = 0; 313 vp[1] = 0; 314 vp[2] = m_ww; 314 vp[2] = m_ww; 315 vp[3] = m_wh; 315 vp[3] = m_wh; 316 316 317 int bufsize = 0; 317 int bufsize = 0; 318 if(::tools_gl2psBeginPage(m_gl2ps_context, 318 if(::tools_gl2psBeginPage(m_gl2ps_context,"","tools_sg_write_gl2ps", 319 vp,a_format,sort,options, 319 vp,a_format,sort,options, 320 TOOLS_GL_RGBA,0, NULL,0,0 320 TOOLS_GL_RGBA,0, NULL,0,0,0,bufsize, 321 m_FILE,a_name.c_str())==T 321 m_FILE,a_name.c_str())==TOOLS_GL2PS_ERROR) { 322 m_out << "tools::sg::gl2ps_action::open 322 m_out << "tools::sg::gl2ps_action::open :" 323 << " tools_gl2psBeginPage() failed 323 << " tools_gl2psBeginPage() failed." 324 << std::endl; 324 << std::endl; 325 ::fclose(m_FILE); 325 ::fclose(m_FILE); 326 m_FILE = 0; 326 m_FILE = 0; 327 ::tools_gl2psDeleteContext(m_gl2ps_conte 327 ::tools_gl2psDeleteContext(m_gl2ps_context); 328 m_gl2ps_context = 0; 328 m_gl2ps_context = 0; 329 return false; 329 return false; 330 } 330 } 331 331 332 ::tools_gl2psSetBackgroundColor(m_gl2ps_co 332 ::tools_gl2psSetBackgroundColor(m_gl2ps_context,m_back[0],m_back[1],m_back[2]); 333 333 334 m_vp_mtx.set_identity(); 334 m_vp_mtx.set_identity(); 335 m_vp_mtx.mul_translate(float(m_ww)/2,float 335 m_vp_mtx.mul_translate(float(m_ww)/2,float(m_wh)/2,0); 336 m_vp_mtx.mul_scale(float(m_ww)/2,float(m_w 336 m_vp_mtx.mul_scale(float(m_ww)/2,float(m_wh)/2,1); 337 337 338 return true; 338 return true; 339 } 339 } 340 340 341 bool close() { 341 bool close() { 342 tools_GLint _status = 0; 342 tools_GLint _status = 0; 343 if(m_gl2ps_context) { 343 if(m_gl2ps_context) { 344 _status = ::tools_gl2psEndPage(m_gl2ps_c 344 _status = ::tools_gl2psEndPage(m_gl2ps_context); 345 } 345 } 346 if(m_FILE) { 346 if(m_FILE) { 347 ::fclose(m_FILE); 347 ::fclose(m_FILE); 348 m_FILE = 0; 348 m_FILE = 0; 349 } 349 } 350 if(m_gl2ps_context) { 350 if(m_gl2ps_context) { 351 ::tools_gl2psDeleteContext(m_gl2ps_conte 351 ::tools_gl2psDeleteContext(m_gl2ps_context); 352 m_gl2ps_context = 0; 352 m_gl2ps_context = 0; 353 } 353 } 354 if (_status == TOOLS_GL2PS_OVERFLOW) retur 354 if (_status == TOOLS_GL2PS_OVERFLOW) return false; 355 return true; 355 return true; 356 } 356 } 357 void set_background(float a_r,float a_g,floa 357 void set_background(float a_r,float a_g,float a_b) { 358 m_back[0] = a_r; 358 m_back[0] = a_r; 359 m_back[1] = a_g; 359 m_back[1] = a_g; 360 m_back[2] = a_b; 360 m_back[2] = a_b; 361 } 361 } 362 protected: 362 protected: 363 void set_normal_matrix() { 363 void set_normal_matrix() { 364 mat4f tmp(m_model); 364 mat4f tmp(m_model); 365 tmp.no_translate(); 365 tmp.no_translate(); 366 if(!tmp.invert(m_normal_matrix)) { 366 if(!tmp.invert(m_normal_matrix)) { 367 m_out << "tools::sg::gl2ps_action::set_n 367 m_out << "tools::sg::gl2ps_action::set_normal_matrix : can't invert model matrix." << std::endl; 368 } 368 } 369 m_normal_matrix.transpose(); 369 m_normal_matrix.transpose(); 370 } 370 } 371 371 372 bool project_point(float& a_x,float& a_y,flo 372 bool project_point(float& a_x,float& a_y,float& a_z,float& a_w) { 373 a_w = 1; 373 a_w = 1; 374 m_model.mul_4f(a_x,a_y,a_z,a_w); 374 m_model.mul_4f(a_x,a_y,a_z,a_w); 375 m_proj.mul_4f(a_x,a_y,a_z,a_w); 375 m_proj.mul_4f(a_x,a_y,a_z,a_w); 376 if(a_w==0) return false; 376 if(a_w==0) return false; 377 a_x /= a_w; 377 a_x /= a_w; 378 a_y /= a_w; 378 a_y /= a_w; 379 a_z /= a_w; 379 a_z /= a_w; 380 return true; 380 return true; 381 } 381 } 382 // bool project_normal(float& a_x,float& a_y, 382 // bool project_normal(float& a_x,float& a_y,float& a_z) { 383 // m_normal_matrix.mul_dir_3f(a_x,a_y,a_z); 383 // m_normal_matrix.mul_dir_3f(a_x,a_y,a_z); 384 // //m_proj.mul_dir_3f(a_x,a_y,a_z); //? 384 // //m_proj.mul_dir_3f(a_x,a_y,a_z); //? 385 // return true; 385 // return true; 386 // } 386 // } 387 387 388 static void set_vtx(tools_GL2PSvertex* a_vs, 388 static void set_vtx(tools_GL2PSvertex* a_vs,unsigned int a_index, 389 float a_x,float a_y,floa 389 float a_x,float a_y,float a_z, 390 float a_r,float a_g,floa 390 float a_r,float a_g,float a_b,float a_a){ 391 a_vs[a_index].xyz[0] = a_x; 391 a_vs[a_index].xyz[0] = a_x; 392 a_vs[a_index].xyz[1] = a_y; 392 a_vs[a_index].xyz[1] = a_y; 393 a_vs[a_index].xyz[2] = a_z; 393 a_vs[a_index].xyz[2] = a_z; 394 a_vs[a_index].rgba[0] = a_r; 394 a_vs[a_index].rgba[0] = a_r; 395 a_vs[a_index].rgba[1] = a_g; 395 a_vs[a_index].rgba[1] = a_g; 396 a_vs[a_index].rgba[2] = a_b; 396 a_vs[a_index].rgba[2] = a_b; 397 a_vs[a_index].rgba[3] = a_a; 397 a_vs[a_index].rgba[3] = a_a; 398 } 398 } 399 399 400 class primvis : public primitive_visitor { 400 class primvis : public primitive_visitor { 401 protected: 401 protected: 402 virtual bool project(float& a_x,float& a_y 402 virtual bool project(float& a_x,float& a_y,float& a_z,float& a_w) { 403 return m_this.project_point(a_x,a_y,a_z, 403 return m_this.project_point(a_x,a_y,a_z,a_w); 404 } 404 } 405 virtual bool add_point(float a_x,float a_y 405 virtual bool add_point(float a_x,float a_y,float a_z,float) { 406 if(!m_this.m_gl2ps_context) return false 406 if(!m_this.m_gl2ps_context) return false; 407 407 408 float r = m_this.m_color[0]; 408 float r = m_this.m_color[0]; 409 float g = m_this.m_color[1]; 409 float g = m_this.m_color[1]; 410 float b = m_this.m_color[2]; 410 float b = m_this.m_color[2]; 411 float a = m_this.m_color[3]; 411 float a = m_this.m_color[3]; 412 412 413 tools_GLint offset = 0; 413 tools_GLint offset = 0; 414 tools_GLfloat ofactor = 0; 414 tools_GLfloat ofactor = 0; 415 tools_GLfloat ounits = 0; 415 tools_GLfloat ounits = 0; 416 tools_GLushort pattern = 0; 416 tools_GLushort pattern = 0; 417 tools_GLint factor = 0; 417 tools_GLint factor = 0; 418 tools_GLfloat sz = m_this.m_point_size; 418 tools_GLfloat sz = m_this.m_point_size; 419 tools_GLint linecap = 0; 419 tools_GLint linecap = 0; 420 tools_GLint linejoin = 0; 420 tools_GLint linejoin = 0; 421 char boundary = 0; 421 char boundary = 0; 422 422 423 tools_GL2PSvertex vertices[1]; 423 tools_GL2PSvertex vertices[1]; 424 424 425 m_this.m_vp_mtx.mul_3f(a_x,a_y,a_z); 425 m_this.m_vp_mtx.mul_3f(a_x,a_y,a_z); 426 426 427 m_this.set_vtx(vertices,0, a_x,a_y,a_z, 427 m_this.set_vtx(vertices,0, a_x,a_y,a_z, r,g,b,a); 428 ::tools_gl2psAddPolyPrimitive(m_this.m_g 428 ::tools_gl2psAddPolyPrimitive(m_this.m_gl2ps_context,_GL2PS_POINT(),1,vertices,offset,ofactor,ounits,pattern,factor,sz,linecap,linejoin,boundary); 429 return true; 429 return true; 430 } 430 } 431 431 432 virtual bool add_point(float a_x,float a_y 432 virtual bool add_point(float a_x,float a_y,float a_z,float, 433 float a_r,float a_g 433 float a_r,float a_g,float a_b,float a_a) { 434 if(!m_this.m_gl2ps_context) return false 434 if(!m_this.m_gl2ps_context) return false; 435 435 436 tools_GLint offset = 0; 436 tools_GLint offset = 0; 437 tools_GLfloat ofactor = 0; 437 tools_GLfloat ofactor = 0; 438 tools_GLfloat ounits = 0; 438 tools_GLfloat ounits = 0; 439 tools_GLushort pattern = 0; 439 tools_GLushort pattern = 0; 440 tools_GLint factor = 0; 440 tools_GLint factor = 0; 441 tools_GLfloat sz = m_this.m_point_size; 441 tools_GLfloat sz = m_this.m_point_size; 442 tools_GLint linecap = 0; 442 tools_GLint linecap = 0; 443 tools_GLint linejoin = 0; 443 tools_GLint linejoin = 0; 444 char boundary = 0; 444 char boundary = 0; 445 445 446 tools_GL2PSvertex vertices[1]; 446 tools_GL2PSvertex vertices[1]; 447 447 448 m_this.m_vp_mtx.mul_3f(a_x,a_y,a_z); 448 m_this.m_vp_mtx.mul_3f(a_x,a_y,a_z); 449 449 450 m_this.set_vtx(vertices,0, a_x,a_y,a_z, 450 m_this.set_vtx(vertices,0, a_x,a_y,a_z, a_r,a_g,a_b,a_a); 451 451 452 ::tools_gl2psAddPolyPrimitive(m_this.m_g 452 ::tools_gl2psAddPolyPrimitive(m_this.m_gl2ps_context,_GL2PS_POINT(),1,vertices,offset,ofactor,ounits,pattern,factor,sz,linecap,linejoin,boundary); 453 return true; 453 return true; 454 } 454 } 455 455 456 virtual bool add_line(float a_bx,float a_b 456 virtual bool add_line(float a_bx,float a_by,float a_bz,float, 457 float a_ex,float a_e 457 float a_ex,float a_ey,float a_ez,float) { 458 if(!m_this.m_gl2ps_context) return false 458 if(!m_this.m_gl2ps_context) return false; 459 459 460 float r = m_this.m_color[0]; 460 float r = m_this.m_color[0]; 461 float g = m_this.m_color[1]; 461 float g = m_this.m_color[1]; 462 float b = m_this.m_color[2]; 462 float b = m_this.m_color[2]; 463 float a = m_this.m_color[3]; 463 float a = m_this.m_color[3]; 464 464 465 tools_GLint offset = 0; 465 tools_GLint offset = 0; 466 tools_GLfloat ofactor = 0; 466 tools_GLfloat ofactor = 0; 467 tools_GLfloat ounits = 0; 467 tools_GLfloat ounits = 0; 468 tools_GLushort pattern = 0; 468 tools_GLushort pattern = 0; 469 tools_GLint factor = 0; 469 tools_GLint factor = 0; 470 tools_GLfloat lwidth = m_this.m_line_wid 470 tools_GLfloat lwidth = m_this.m_line_width; 471 tools_GLint linecap = 0; 471 tools_GLint linecap = 0; 472 tools_GLint linejoin = 0; 472 tools_GLint linejoin = 0; 473 char boundary = 0; 473 char boundary = 0; 474 474 475 tools_GL2PSvertex vertices[2]; 475 tools_GL2PSvertex vertices[2]; 476 476 477 m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz); 477 m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz); 478 m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez); 478 m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez); 479 479 480 m_this.set_vtx(vertices,0, a_bx,a_by,a_b 480 m_this.set_vtx(vertices,0, a_bx,a_by,a_bz, r,g,b,a); 481 m_this.set_vtx(vertices,1, a_ex,a_ey,a_e 481 m_this.set_vtx(vertices,1, a_ex,a_ey,a_ez, r,g,b,a); 482 482 483 ::tools_gl2psAddPolyPrimitive(m_this.m_g 483 ::tools_gl2psAddPolyPrimitive(m_this.m_gl2ps_context,_GL2PS_LINE(),2,vertices,offset,ofactor,ounits,pattern,factor,lwidth,linecap,linejoin,boundary); 484 484 485 return true; 485 return true; 486 } 486 } 487 487 488 virtual bool add_line(float a_bx,float a_b 488 virtual bool add_line(float a_bx,float a_by,float a_bz,float, 489 float a_br,float a_b 489 float a_br,float a_bg,float a_bb,float a_ba, 490 float a_ex,float a_e 490 float a_ex,float a_ey,float a_ez,float, 491 float a_er,float a_e 491 float a_er,float a_eg,float a_eb,float a_ea){ 492 if(!m_this.m_gl2ps_context) return false 492 if(!m_this.m_gl2ps_context) return false; 493 493 494 tools_GLint offset = 0; 494 tools_GLint offset = 0; 495 tools_GLfloat ofactor = 0; 495 tools_GLfloat ofactor = 0; 496 tools_GLfloat ounits = 0; 496 tools_GLfloat ounits = 0; 497 tools_GLushort pattern = 0; 497 tools_GLushort pattern = 0; 498 tools_GLint factor = 0; 498 tools_GLint factor = 0; 499 tools_GLfloat lwidth = m_this.m_line_wid 499 tools_GLfloat lwidth = m_this.m_line_width; 500 tools_GLint linecap = 0; 500 tools_GLint linecap = 0; 501 tools_GLint linejoin = 0; 501 tools_GLint linejoin = 0; 502 char boundary = 0; 502 char boundary = 0; 503 503 504 tools_GL2PSvertex vertices[2]; 504 tools_GL2PSvertex vertices[2]; 505 505 506 m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz); 506 m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz); 507 m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez); 507 m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez); 508 508 509 m_this.set_vtx(vertices,0, a_bx,a_by,a_b 509 m_this.set_vtx(vertices,0, a_bx,a_by,a_bz, a_br,a_bg,a_bb,a_ba); 510 m_this.set_vtx(vertices,1, a_ex,a_ey,a_e 510 m_this.set_vtx(vertices,1, a_ex,a_ey,a_ez, a_er,a_eg,a_eb,a_ea); 511 511 512 //tools_gl2psAddPolyPrimitive(m_this.m_g 512 //tools_gl2psAddPolyPrimitive(m_this.m_gl2ps_context,_GL2PS_LINE(),2,vertices,0,pattern,factor,lwidth,0); 513 ::tools_gl2psAddPolyPrimitive(m_this.m_g 513 ::tools_gl2psAddPolyPrimitive(m_this.m_gl2ps_context,_GL2PS_LINE(),2,vertices,offset,ofactor,ounits,pattern,factor,lwidth,linecap,linejoin,boundary); 514 514 515 return true; 515 return true; 516 } 516 } 517 517 518 virtual bool add_triangle(float a_p1x,floa 518 virtual bool add_triangle(float a_p1x,float a_p1y,float a_p1z,float a_p1w, 519 float a_p2x,floa 519 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 520 float a_p3x,floa 520 float a_p3x,float a_p3y,float a_p3z,float a_p3w){ 521 float r = m_this.m_color[0]; 521 float r = m_this.m_color[0]; 522 float g = m_this.m_color[1]; 522 float g = m_this.m_color[1]; 523 float b = m_this.m_color[2]; 523 float b = m_this.m_color[2]; 524 float a = m_this.m_color[3]; 524 float a = m_this.m_color[3]; 525 return _add_triangle(a_p1x,a_p1y,a_p1z,a 525 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 526 m_this.m_normal.x() 526 m_this.m_normal.x(),m_this.m_normal.y(),m_this.m_normal.z(), 527 r,g,b,a, 527 r,g,b,a, 528 a_p2x,a_p2y,a_p2z,a 528 a_p2x,a_p2y,a_p2z,a_p2w, 529 m_this.m_normal.x() 529 m_this.m_normal.x(),m_this.m_normal.y(),m_this.m_normal.z(), 530 r,g,b,a, 530 r,g,b,a, 531 a_p3x,a_p3y,a_p3z,a 531 a_p3x,a_p3y,a_p3z,a_p3w, 532 m_this.m_normal.x() 532 m_this.m_normal.x(),m_this.m_normal.y(),m_this.m_normal.z(), 533 r,g,b,a); 533 r,g,b,a); 534 } 534 } 535 535 536 virtual bool add_triangle( 536 virtual bool add_triangle( 537 float a_p1x,float a_p1y,float a_p1z,floa 537 float a_p1x,float a_p1y,float a_p1z,float a_p1w, 538 float a_r1,float a_g1,float a_b1,float a 538 float a_r1,float a_g1,float a_b1,float a_a1, 539 float a_p2x,float a_p2y,float a_p2z,floa 539 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 540 float a_r2,float a_g2,float a_b2,float a 540 float a_r2,float a_g2,float a_b2,float a_a2, 541 float a_p3x,float a_p3y,float a_p3z,floa 541 float a_p3x,float a_p3y,float a_p3z,float a_p3w, 542 float a_r3,float a_g3,float a_b3,float a 542 float a_r3,float a_g3,float a_b3,float a_a3){ 543 543 544 return _add_triangle(a_p1x,a_p1y,a_p1z,a 544 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 545 m_this.m_normal.x() 545 m_this.m_normal.x(),m_this.m_normal.y(),m_this.m_normal.z(), 546 a_r1,a_g1,a_b1,a_a1 546 a_r1,a_g1,a_b1,a_a1, 547 a_p2x,a_p2y,a_p2z,a 547 a_p2x,a_p2y,a_p2z,a_p2w, 548 m_this.m_normal.x() 548 m_this.m_normal.x(),m_this.m_normal.y(),m_this.m_normal.z(), 549 a_r2,a_g2,a_b2,a_a2 549 a_r2,a_g2,a_b2,a_a2, 550 a_p3x,a_p3y,a_p3z,a 550 a_p3x,a_p3y,a_p3z,a_p3w, 551 m_this.m_normal.x() 551 m_this.m_normal.x(),m_this.m_normal.y(),m_this.m_normal.z(), 552 a_r3,a_g3,a_b3,a_a3 552 a_r3,a_g3,a_b3,a_a3); 553 } 553 } 554 554 555 virtual bool project_normal(float& /*a_x*/ 555 virtual bool project_normal(float& /*a_x*/,float& /*a_y*/,float& /*a_z*/) { 556 //return m_this.project_normal(a_x,a_y,a 556 //return m_this.project_normal(a_x,a_y,a_z); 557 return true; 557 return true; 558 } 558 } 559 virtual bool add_point_normal(float a_x,fl 559 virtual bool add_point_normal(float a_x,float a_y,float a_z,float a_w, 560 float /*a_nx 560 float /*a_nx*/,float /*a_ny*/,float /*a_nz*/) { 561 add_point(a_x,a_y,a_z,a_w); 561 add_point(a_x,a_y,a_z,a_w); 562 //m_this.m_points.add_normal(a_nx,a_ny,a_n 562 //m_this.m_points.add_normal(a_nx,a_ny,a_nz); 563 return true; 563 return true; 564 } 564 } 565 virtual bool add_point_normal(float a_x,fl 565 virtual bool add_point_normal(float a_x,float a_y,float a_z,float a_w, 566 float /*a_nx 566 float /*a_nx*/,float /*a_ny*/,float /*a_nz*/, 567 float a_r,fl 567 float a_r,float a_g,float a_b,float a_a) { 568 add_point(a_x,a_y,a_z,a_w,a_r,a_g,a_b,a_ 568 add_point(a_x,a_y,a_z,a_w,a_r,a_g,a_b,a_a); 569 //m_this.m_points.add_normal(a_nx,a_ny,a_n 569 //m_this.m_points.add_normal(a_nx,a_ny,a_nz); 570 return true; 570 return true; 571 } 571 } 572 virtual bool add_line_normal(float a_bx,fl 572 virtual bool add_line_normal(float a_bx,float a_by,float a_bz,float a_bw, 573 float /*a_bnx 573 float /*a_bnx*/,float /*a_bny*/,float /*a_bnz*/, 574 float a_ex,fl 574 float a_ex,float a_ey,float a_ez,float a_ew, 575 float /*a_enx 575 float /*a_enx*/,float /*a_eny*/,float /*a_enz*/) { 576 add_line(a_bx,a_by,a_bz,a_bw, a_ex,a_ey, 576 add_line(a_bx,a_by,a_bz,a_bw, a_ex,a_ey,a_ez,a_ew); 577 //m_this.m_lines.add_normal(a_bnx,a_bny,a_ 577 //m_this.m_lines.add_normal(a_bnx,a_bny,a_bnz); 578 //m_this.m_lines.add_normal(a_enx,a_eny,a_ 578 //m_this.m_lines.add_normal(a_enx,a_eny,a_enz); 579 return true; 579 return true; 580 } 580 } 581 virtual bool add_line_normal(float a_bx,fl 581 virtual bool add_line_normal(float a_bx,float a_by,float a_bz,float a_bw, 582 float /*a_bnx 582 float /*a_bnx*/,float /*a_bny*/,float /*a_bnz*/, 583 float a_br,fl 583 float a_br,float a_bg,float a_bb,float a_ba, 584 float a_ex,fl 584 float a_ex,float a_ey,float a_ez,float a_ew, 585 float /*a_enx 585 float /*a_enx*/,float /*a_eny*/,float /*a_enz*/, 586 float a_er,fl 586 float a_er,float a_eg,float a_eb,float a_ea){ 587 add_line(a_bx,a_by,a_bz,a_bw, a_br,a_bg, 587 add_line(a_bx,a_by,a_bz,a_bw, a_br,a_bg,a_bb,a_ba, a_ex,a_ey,a_ez,a_ew, a_er,a_eg,a_eb,a_ea); 588 //m_this.m_lines.add_normal(a_bnx,a_bny,a_ 588 //m_this.m_lines.add_normal(a_bnx,a_bny,a_bnz); 589 //m_this.m_lines.add_normal(a_enx,a_eny,a_ 589 //m_this.m_lines.add_normal(a_enx,a_eny,a_enz); 590 return true; 590 return true; 591 } 591 } 592 virtual bool add_triangle_normal( 592 virtual bool add_triangle_normal( 593 float a_p1x,float a_p1y,float a_p1z,floa 593 float a_p1x,float a_p1y,float a_p1z,float a_p1w, 594 float a_n1x,float a_n1y,float a_n1z, 594 float a_n1x,float a_n1y,float a_n1z, 595 float a_p2x,float a_p2y,float a_p2z,floa 595 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 596 float a_n2x,float a_n2y,float a_n2z, 596 float a_n2x,float a_n2y,float a_n2z, 597 float a_p3x,float a_p3y,float a_p3z,floa 597 float a_p3x,float a_p3y,float a_p3z,float a_p3w, 598 float a_n3x,float a_n3y,float a_n3z) { 598 float a_n3x,float a_n3y,float a_n3z) { 599 599 600 float r = m_this.m_color[0]; 600 float r = m_this.m_color[0]; 601 float g = m_this.m_color[1]; 601 float g = m_this.m_color[1]; 602 float b = m_this.m_color[2]; 602 float b = m_this.m_color[2]; 603 float a = m_this.m_color[3]; 603 float a = m_this.m_color[3]; 604 604 605 return _add_triangle(a_p1x,a_p1y,a_p1z,a 605 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 606 a_n1x,a_n1y,a_n1z, 606 a_n1x,a_n1y,a_n1z, 607 r,g,b,a, 607 r,g,b,a, 608 a_p2x,a_p2y,a_p2z,a 608 a_p2x,a_p2y,a_p2z,a_p2w, 609 a_n2x,a_n2y,a_n2z, 609 a_n2x,a_n2y,a_n2z, 610 r,g,b,a, 610 r,g,b,a, 611 a_p3x,a_p3y,a_p3z,a 611 a_p3x,a_p3y,a_p3z,a_p3w, 612 a_n3x,a_n3y,a_n3z, 612 a_n3x,a_n3y,a_n3z, 613 r,g,b,a); 613 r,g,b,a); 614 } 614 } 615 virtual bool add_triangle_normal( 615 virtual bool add_triangle_normal( 616 float a_p1x,float a_p1y,float a_p1z,floa 616 float a_p1x,float a_p1y,float a_p1z,float a_p1w, 617 float a_n1x,float a_n1y,float a_n1z, 617 float a_n1x,float a_n1y,float a_n1z, 618 float a_r1,float a_g1,float a_b1,float a 618 float a_r1,float a_g1,float a_b1,float a_a1, 619 float a_p2x,float a_p2y,float a_p2z,floa 619 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 620 float a_n2x,float a_n2y,float a_n2z, 620 float a_n2x,float a_n2y,float a_n2z, 621 float a_r2,float a_g2,float a_b2,float a 621 float a_r2,float a_g2,float a_b2,float a_a2, 622 float a_p3x,float a_p3y,float a_p3z,floa 622 float a_p3x,float a_p3y,float a_p3z,float a_p3w, 623 float a_n3x,float a_n3y,float a_n3z, 623 float a_n3x,float a_n3y,float a_n3z, 624 float a_r3,float a_g3,float a_b3,float a 624 float a_r3,float a_g3,float a_b3,float a_a3){ 625 return _add_triangle(a_p1x,a_p1y,a_p1z,a 625 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 626 a_n1x,a_n1y,a_n1z, 626 a_n1x,a_n1y,a_n1z, 627 a_r1,a_g1,a_b1,a_a1 627 a_r1,a_g1,a_b1,a_a1, 628 a_p2x,a_p2y,a_p2z,a 628 a_p2x,a_p2y,a_p2z,a_p2w, 629 a_n2x,a_n2y,a_n2z, 629 a_n2x,a_n2y,a_n2z, 630 a_r2,a_g2,a_b2,a_a2 630 a_r2,a_g2,a_b2,a_a2, 631 a_p3x,a_p3y,a_p3z,a 631 a_p3x,a_p3y,a_p3z,a_p3w, 632 a_n3x,a_n3y,a_n3z, 632 a_n3x,a_n3y,a_n3z, 633 a_r3,a_g3,a_b3,a_a3 633 a_r3,a_g3,a_b3,a_a3); 634 } 634 } 635 public: 635 public: 636 primvis(gl2ps_action& a_this):m_this(a_thi 636 primvis(gl2ps_action& a_this):m_this(a_this){} 637 virtual ~primvis(){} 637 virtual ~primvis(){} 638 public: 638 public: 639 primvis(const primvis& a_from) 639 primvis(const primvis& a_from) 640 :primitive_visitor(a_from) 640 :primitive_visitor(a_from) 641 ,m_this(a_from.m_this) 641 ,m_this(a_from.m_this) 642 {} 642 {} 643 primvis& operator=(const primvis& a_from){ 643 primvis& operator=(const primvis& a_from){ 644 primitive_visitor::operator=(a_from); 644 primitive_visitor::operator=(a_from); 645 return *this; 645 return *this; 646 } 646 } 647 protected: 647 protected: 648 bool _add_triangle(float a_p1x,float a_p1y 648 bool _add_triangle(float a_p1x,float a_p1y,float a_p1z,float /*a_p1w*/, 649 float a_n1x,float a_n1y 649 float a_n1x,float a_n1y,float a_n1z, 650 float a_r1,float a_g1,f 650 float a_r1,float a_g1,float a_b1,float a_a1, 651 float a_p2x,float a_p2y 651 float a_p2x,float a_p2y,float a_p2z,float /*a_p2w*/, 652 float a_n2x,float a_n2y 652 float a_n2x,float a_n2y,float a_n2z, 653 float a_r2,float a_g2,f 653 float a_r2,float a_g2,float a_b2,float a_a2, 654 float a_p3x,float a_p3y 654 float a_p3x,float a_p3y,float a_p3z,float /*a_p3w*/, 655 float a_n3x,float a_n3y 655 float a_n3x,float a_n3y,float a_n3z, 656 float a_r3,float a_g3,f 656 float a_r3,float a_g3,float a_b3,float a_a3) { 657 657 658 if(!m_this.m_gl2ps_context) return false 658 if(!m_this.m_gl2ps_context) return false; 659 659 660 float p1x = a_p1x;float p1y = a_p1y;floa 660 float p1x = a_p1x;float p1y = a_p1y;float p1z = a_p1z;//float p1w = a_p1w; 661 float p2x = a_p2x;float p2y = a_p2y;floa 661 float p2x = a_p2x;float p2y = a_p2y;float p2z = a_p2z;//float p2w = a_p2w; 662 float p3x = a_p3x;float p3y = a_p3y;floa 662 float p3x = a_p3x;float p3y = a_p3y;float p3z = a_p3z;//float p3w = a_p3w; 663 663 664 m_this.m_vp_mtx.mul_3f(p1x,p1y,p1z); 664 m_this.m_vp_mtx.mul_3f(p1x,p1y,p1z); 665 m_this.m_vp_mtx.mul_3f(p2x,p2y,p2z); 665 m_this.m_vp_mtx.mul_3f(p2x,p2y,p2z); 666 m_this.m_vp_mtx.mul_3f(p3x,p3y,p3z); 666 m_this.m_vp_mtx.mul_3f(p3x,p3y,p3z); 667 667 668 {plane<vec3f> pn( 668 {plane<vec3f> pn( 669 vec3f(p1x,p1y,p1z), 669 vec3f(p1x,p1y,p1z), 670 vec3f(p2x,p2y,p2z), 670 vec3f(p2x,p2y,p2z), 671 vec3f(p3x,p3y,p3z) 671 vec3f(p3x,p3y,p3z) 672 ); 672 ); 673 if(!pn.is_valid()) return true; 673 if(!pn.is_valid()) return true; 674 float C = pn.normal()[2]; 674 float C = pn.normal()[2]; 675 if(m_this.m_CULL_FACE){ 675 if(m_this.m_CULL_FACE){ 676 if(m_this.m_ccw) { 676 if(m_this.m_ccw) { 677 if(C<=0) return true; 677 if(C<=0) return true; 678 } else { 678 } else { 679 if(C>=0) return true; 679 if(C>=0) return true; 680 } 680 } 681 }} 681 }} 682 682 683 tools_GL2PSvertex vertices[3]; 683 tools_GL2PSvertex vertices[3]; 684 684 685 if(m_this.m_light_on) { // same logic a 685 if(m_this.m_light_on) { // same logic as toolx/wasm/webgl.js: 686 686 687 float nx = (a_n1x+a_n2x+a_n3x)/3.0f; 687 float nx = (a_n1x+a_n2x+a_n3x)/3.0f; 688 float ny = (a_n1y+a_n2y+a_n3y)/3.0f; 688 float ny = (a_n1y+a_n2y+a_n3y)/3.0f; 689 float nz = (a_n1z+a_n2z+a_n3z)/3.0f; 689 float nz = (a_n1z+a_n2z+a_n3z)/3.0f; 690 690 691 m_this.m_normal_matrix.mul_dir_3f(nx,n 691 m_this.m_normal_matrix.mul_dir_3f(nx,ny,nz); 692 vec3f _normal(nx,ny,nz);_normal.normal 692 vec3f _normal(nx,ny,nz);_normal.normalize(); 693 693 694 float _dot = _normal.dot(m_this.m_ligh 694 float _dot = _normal.dot(m_this.m_light_direction); 695 695 696 float _r = (a_r1+a_r2+a_r3)/3.0f; 696 float _r = (a_r1+a_r2+a_r3)/3.0f; 697 float _g = (a_g1+a_g2+a_g3)/3.0f; 697 float _g = (a_g1+a_g2+a_g3)/3.0f; 698 float _b = (a_b1+a_b2+a_b3)/3.0f; 698 float _b = (a_b1+a_b2+a_b3)/3.0f; 699 float _a = (a_a1+a_a2+a_a3)/3.0f; 699 float _a = (a_a1+a_a2+a_a3)/3.0f; 700 colorf a_color(_r,_g,_b,_a); 700 colorf a_color(_r,_g,_b,_a); 701 701 702 colorf frag_color = a_color; 702 colorf frag_color = a_color; 703 703 704 if(_dot<0.0) { 704 if(_dot<0.0) { 705 _dot *= -1.0; 705 _dot *= -1.0; 706 706 707 colorf _tmp = m_this.m_light_color; 707 colorf _tmp = m_this.m_light_color; 708 _tmp *= _dot; 708 _tmp *= _dot; 709 _tmp += m_this.m_light_ambient; 709 _tmp += m_this.m_light_ambient; 710 710 711 frag_color *= _tmp; 711 frag_color *= _tmp; 712 712 713 } else { 713 } else { 714 frag_color *= m_this.m_light_ambient 714 frag_color *= m_this.m_light_ambient; 715 } 715 } 716 716 717 frag_color.clamp(); 717 frag_color.clamp(); 718 frag_color.set_a(a_color.a()); 718 frag_color.set_a(a_color.a()); 719 719 720 float r = frag_color.r(); 720 float r = frag_color.r(); 721 float g = frag_color.g(); 721 float g = frag_color.g(); 722 float b = frag_color.b(); 722 float b = frag_color.b(); 723 float a = frag_color.a(); 723 float a = frag_color.a(); 724 724 725 m_this.set_vtx(vertices,0, p1x,p1y,p1z 725 m_this.set_vtx(vertices,0, p1x,p1y,p1z, r,g,b,a); 726 m_this.set_vtx(vertices,1, p2x,p2y,p2z 726 m_this.set_vtx(vertices,1, p2x,p2y,p2z, r,g,b,a); 727 m_this.set_vtx(vertices,2, p3x,p3y,p3z 727 m_this.set_vtx(vertices,2, p3x,p3y,p3z, r,g,b,a); 728 728 729 } else { 729 } else { 730 m_this.set_vtx(vertices,0, p1x,p1y,p1z 730 m_this.set_vtx(vertices,0, p1x,p1y,p1z, a_r1,a_g1,a_b1,a_a1); 731 m_this.set_vtx(vertices,1, p2x,p2y,p2z 731 m_this.set_vtx(vertices,1, p2x,p2y,p2z, a_r2,a_g2,a_b2,a_a2); 732 m_this.set_vtx(vertices,2, p3x,p3y,p3z 732 m_this.set_vtx(vertices,2, p3x,p3y,p3z, a_r3,a_g3,a_b3,a_a3); 733 733 734 } 734 } 735 735 736 tools_GLint offset = 0; 736 tools_GLint offset = 0; 737 tools_GLfloat ofactor = 0; 737 tools_GLfloat ofactor = 0; 738 tools_GLfloat ounits = 0; 738 tools_GLfloat ounits = 0; 739 tools_GLushort pattern = 0; 739 tools_GLushort pattern = 0; 740 tools_GLint factor = 0; 740 tools_GLint factor = 0; 741 tools_GLfloat lwidth = m_this.m_line_wid 741 tools_GLfloat lwidth = m_this.m_line_width; 742 tools_GLint linecap = 0; 742 tools_GLint linecap = 0; 743 tools_GLint linejoin = 0; 743 tools_GLint linejoin = 0; 744 char boundary = 0; 744 char boundary = 0; 745 745 746 ::tools_gl2psAddPolyPrimitive(m_this.m_g 746 ::tools_gl2psAddPolyPrimitive(m_this.m_gl2ps_context,_GL2PS_TRIANGLE(),3,vertices,offset,ofactor,ounits,pattern,factor,lwidth,linecap,linejoin,boundary); 747 747 748 return true; 748 return true; 749 } 749 } 750 protected: 750 protected: 751 gl2ps_action& m_this; 751 gl2ps_action& m_this; 752 }; 752 }; 753 753 754 protected: 754 protected: 755 gl2ps_manager& m_mgr; 755 gl2ps_manager& m_mgr; 756 tools_GL2PScontext* m_gl2ps_context; 756 tools_GL2PScontext* m_gl2ps_context; 757 757 758 FILE* m_FILE; 758 FILE* m_FILE; 759 float m_back[3]; 759 float m_back[3]; 760 mat4f m_vp_mtx; 760 mat4f m_vp_mtx; 761 primvis m_pv; 761 primvis m_pv; 762 762 763 colorf m_light_color; 763 colorf m_light_color; 764 colorf m_light_ambient; 764 colorf m_light_ambient; 765 vec3f m_light_direction; 765 vec3f m_light_direction; 766 vec3f m_normal; 766 vec3f m_normal; 767 767 768 // to be restored in restore_state() : 768 // to be restored in restore_state() : 769 mat4f m_proj; 769 mat4f m_proj; 770 mat4f m_model; 770 mat4f m_model; 771 mat4f m_normal_matrix; 771 mat4f m_normal_matrix; 772 colorf m_color; 772 colorf m_color; 773 bool m_ccw; 773 bool m_ccw; 774 bool m_POLYGON_OFFSET_FILL; 774 bool m_POLYGON_OFFSET_FILL; 775 bool m_CULL_FACE; 775 bool m_CULL_FACE; 776 bool m_POINT_SMOOTH; 776 bool m_POINT_SMOOTH; 777 bool m_LINE_SMOOTH; 777 bool m_LINE_SMOOTH; 778 float m_point_size; 778 float m_point_size; 779 float m_line_width; 779 float m_line_width; 780 bool m_light_on; 780 bool m_light_on; 781 bool m_DEPTH_TEST; 781 bool m_DEPTH_TEST; 782 }; 782 }; 783 783 784 inline bool gl2ps_s2format(const std::string& 784 inline bool gl2ps_s2format(const std::string& a_format,int& a_gl2ps_format) { 785 if(a_format=="gl2ps_eps") {a_gl2ps_format = 785 if(a_format=="gl2ps_eps") {a_gl2ps_format = TOOLS_GL2PS_EPS;return true;} 786 if(a_format=="gl2ps_ps") {a_gl2ps_format = 786 if(a_format=="gl2ps_ps") {a_gl2ps_format = TOOLS_GL2PS_PS; return true;} 787 if(a_format=="gl2ps_pdf") {a_gl2ps_format = 787 if(a_format=="gl2ps_pdf") {a_gl2ps_format = TOOLS_GL2PS_PDF;return true;} 788 if(a_format=="gl2ps_svg") {a_gl2ps_format = 788 if(a_format=="gl2ps_svg") {a_gl2ps_format = TOOLS_GL2PS_SVG;return true;} 789 if(a_format=="gl2ps_tex") {a_gl2ps_format = 789 if(a_format=="gl2ps_tex") {a_gl2ps_format = TOOLS_GL2PS_TEX;return true;} 790 if(a_format=="gl2ps_pgf") {a_gl2ps_format = 790 if(a_format=="gl2ps_pgf") {a_gl2ps_format = TOOLS_GL2PS_PGF;return true;} 791 a_gl2ps_format = TOOLS_GL2PS_PS; 791 a_gl2ps_format = TOOLS_GL2PS_PS; 792 return false; 792 return false; 793 } 793 } 794 794 795 inline bool gl2ps_s2sort(const std::string& a_ 795 inline bool gl2ps_s2sort(const std::string& a_sort,int& a_gl2ps_sort) { 796 if(a_sort=="NO_SORT") {a_gl2ps_sort = TO 796 if(a_sort=="NO_SORT") {a_gl2ps_sort = TOOLS_GL2PS_NO_SORT; return true;} 797 if(a_sort=="SIMPLE_SORT") {a_gl2ps_sort = TO 797 if(a_sort=="SIMPLE_SORT") {a_gl2ps_sort = TOOLS_GL2PS_SIMPLE_SORT;return true;} 798 if(a_sort=="BSP_SORT") {a_gl2ps_sort = TO 798 if(a_sort=="BSP_SORT") {a_gl2ps_sort = TOOLS_GL2PS_BSP_SORT; return true;} 799 a_gl2ps_sort = TOOLS_GL2PS_NO_SORT; 799 a_gl2ps_sort = TOOLS_GL2PS_NO_SORT; 800 return false; 800 return false; 801 } 801 } 802 802 803 }} 803 }} 804 804 805 #include "../words" 805 #include "../words" 806 #include "../forit" 806 #include "../forit" 807 #include "../touplow" 807 #include "../touplow" 808 #include "../sout" 808 #include "../sout" 809 809 810 namespace tools { 810 namespace tools { 811 namespace sg { 811 namespace sg { 812 812 813 inline bool gl2ps_s2options(const std::string& 813 inline bool gl2ps_s2options(const std::string& a_opts,int& a_gl2ps_opts) { 814 std::vector<std::string> opts; 814 std::vector<std::string> opts; 815 words(a_opts,"|",false,opts); 815 words(a_opts,"|",false,opts); 816 a_gl2ps_opts = 0; 816 a_gl2ps_opts = 0; 817 tools_vforit(std::string,opts,it) { 817 tools_vforit(std::string,opts,it) { 818 touppercase(*it); 818 touppercase(*it); 819 const std::string& item = *it; 819 const std::string& item = *it; 820 if(item=="NONE") a_gl 820 if(item=="NONE") a_gl2ps_opts |= TOOLS_GL2PS_NONE; 821 else if(item=="DRAW_BACKGROUND") a_gl 821 else if(item=="DRAW_BACKGROUND") a_gl2ps_opts |= TOOLS_GL2PS_DRAW_BACKGROUND; 822 else if(item=="SIMPLE_LINE_OFFSET") a_gl 822 else if(item=="SIMPLE_LINE_OFFSET") a_gl2ps_opts |= TOOLS_GL2PS_SIMPLE_LINE_OFFSET; 823 else if(item=="SILENT") a_gl 823 else if(item=="SILENT") a_gl2ps_opts |= TOOLS_GL2PS_SILENT; 824 else if(item=="BEST_ROOT") a_gl 824 else if(item=="BEST_ROOT") a_gl2ps_opts |= TOOLS_GL2PS_BEST_ROOT; 825 else if(item=="OCCLUSION_CULL") a_gl 825 else if(item=="OCCLUSION_CULL") a_gl2ps_opts |= TOOLS_GL2PS_OCCLUSION_CULL; 826 else if(item=="NO_TEXT") a_gl 826 else if(item=="NO_TEXT") a_gl2ps_opts |= TOOLS_GL2PS_NO_TEXT; 827 else if(item=="LANDSCAPE") a_gl 827 else if(item=="LANDSCAPE") a_gl2ps_opts |= TOOLS_GL2PS_LANDSCAPE; 828 else if(item=="NO_PS3_SHADING") a_gl 828 else if(item=="NO_PS3_SHADING") a_gl2ps_opts |= TOOLS_GL2PS_NO_PS3_SHADING; 829 else if(item=="NO_PIXMAP") a_gl 829 else if(item=="NO_PIXMAP") a_gl2ps_opts |= TOOLS_GL2PS_NO_PIXMAP; 830 else if(item=="USE_CURRENT_VIEWPORT") a_gl 830 else if(item=="USE_CURRENT_VIEWPORT") a_gl2ps_opts |= TOOLS_GL2PS_USE_CURRENT_VIEWPORT; 831 else if(item=="COMPRESS") a_gl 831 else if(item=="COMPRESS") a_gl2ps_opts |= TOOLS_GL2PS_COMPRESS; 832 else if(item=="NO_BLENDING") a_gl 832 else if(item=="NO_BLENDING") a_gl2ps_opts |= TOOLS_GL2PS_NO_BLENDING; 833 else if(item=="TIGHT_BOUNDING_BOX") a_gl 833 else if(item=="TIGHT_BOUNDING_BOX") a_gl2ps_opts |= TOOLS_GL2PS_TIGHT_BOUNDING_BOX; 834 else if(item=="NO_OPENGL_CONTEXT") a_gl 834 else if(item=="NO_OPENGL_CONTEXT") a_gl2ps_opts |= TOOLS_GL2PS_NO_OPENGL_CONTEXT; 835 else if(item=="NO_TEX_FONTSIZE") a_gl 835 else if(item=="NO_TEX_FONTSIZE") a_gl2ps_opts |= TOOLS_GL2PS_NO_TEX_FONTSIZE; 836 else if(item=="PORTABLE_SORT") a_gl 836 else if(item=="PORTABLE_SORT") a_gl2ps_opts |= TOOLS_GL2PS_PORTABLE_SORT; 837 else {a_g 837 else {a_gl2ps_opts = 0;return false;} 838 } 838 } 839 return true; 839 return true; 840 } 840 } 841 841 842 }} 842 }} 843 843 844 844 845 #endif 845 #endif