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