Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_zb_action 5 #define tools_sg_zb_action 6 7 #include "zb_manager" 8 9 #include "render_action" 10 #include "primitive_visitor" 11 #include "../glprims" 12 13 #include "../zb/buffer" 14 15 #include "../colorfs" 16 #include "../lina/plane" 17 #include "../mathf" 18 #include "../lina/vec3d" //ZZ=double 19 20 namespace tools { 21 namespace sg { 22 23 class zb_action : public render_action { 24 TOOLS_ACTION(zb_action,tools::sg::zb_action, 25 private: 26 zb_action& get_me() {return *this;} 27 public: 28 virtual void draw_vertex_array(gl::mode_t a_ 29 m_pv.add_primitive(a_mode,a_floatn,a_xyzs) 30 } 31 32 virtual void draw_vertex_array_xy(gl::mode_t 33 m_pv.add_primitive_xy(a_mode,a_floatn,a_xy 34 } 35 36 virtual void draw_vertex_color_array(gl::mod 37 const f 38 m_pv.add_primitive_rgba(a_mode,a_floatn,a_ 39 } 40 41 virtual void draw_vertex_normal_array(gl::mo 42 const 43 m_pv.add_primitive_normal(a_mode,a_floatn, 44 } 45 46 virtual void draw_vertex_color_normal_array( 47 48 // We expect a_nms of size : 3*(a_floatn/3 49 // (then one normal per 3D point). 50 m_pv.add_primitive_normal_rgba(a_mode,a_fl 51 } 52 53 virtual void clear_color(float a_r,float a_g 54 zb::buffer::ZPixel pix; 55 zb::buffer::rgba2pix(a_r,a_g,a_b,a_a,pix); 56 m_zb.clear_color_buffer(pix); 57 } 58 virtual void color4f(float a_r,float a_g,flo 59 m_rgba.set_value(a_r,a_g,a_b,a_a); 60 } 61 virtual void line_width(float a_v){m_line_wi 62 virtual void point_size(float a_v) {m_point_ 63 virtual void set_polygon_offset(bool a_v) {m 64 virtual void normal(float a_x,float a_y,floa 65 m_normal.set_value(a_x,a_y,a_z); 66 } 67 68 virtual void set_winding(winding_type a_v) { 69 m_ccw = (a_v==winding_ccw?true:false); 70 } 71 virtual void set_shade_model(shade_type) {} 72 73 virtual void set_depth_test(bool a_on) {m_DE 74 75 virtual void set_cull_face(bool a_on) {m_CUL 76 virtual void set_point_smooth(bool a_on) {m_ 77 virtual void set_line_smooth(bool a_on) {m_L 78 79 virtual void load_proj_matrix(const mat4f& a 80 m_proj = a_mtx; 81 } 82 83 virtual void load_model_matrix(const mat4f& 84 m_model = a_mtx; 85 set_normal_matrix(); 86 } 87 88 virtual unsigned int max_lights() {return 10 89 90 virtual void enable_light(unsigned int, 91 float a_dx,float a 92 float a_r,float a_ 93 float a_ar,float a 94 m_light_color.set_value(a_r,a_g,a_b,a_a); 95 m_light_ambient.set_value(a_ar,a_ag,a_ab,a 96 m_light_direction.set_value(a_dx,a_dy,a_dz 97 m_light_direction.normalize(); 98 m_light_on = true; 99 } 100 101 virtual void set_lighting(bool a_value) {m_l 102 virtual void set_blend(bool a_value) {m_blen 103 104 virtual void restore_state(unsigned int /*a_ 105 const sg::state& _state = state(); 106 m_proj = _state.m_proj; 107 m_model = _state.m_model; 108 set_normal_matrix(); 109 110 m_rgba = _state.m_color; 111 m_normal = _state.m_normal; 112 113 m_ccw = (_state.m_winding==winding_ccw?tru 114 m_POLYGON_OFFSET_FILL = _state.m_GL_POLYGO 115 m_CULL_FACE = _state.m_GL_CULL_FACE; 116 m_POINT_SMOOTH = _state.m_GL_POINT_SMOOTH; 117 m_LINE_SMOOTH = _state.m_GL_LINE_SMOOTH; 118 m_line_width = _state.m_line_width; 119 m_point_size = _state.m_point_size; 120 m_light_on = _state.m_GL_LIGHTING; 121 m_DEPTH_TEST = _state.m_GL_DEPTH_TEST; 122 m_blend = _state.m_GL_BLEND; 123 124 /* 125 if(_state.m_GL_TEXTURE_2D) ::glEnable(GL_T 126 else ::glDisable(GL_ 127 128 // The "return of separator" state had ret 129 // The restored state has m_light. 130 // We have to glDisable lights with index 131 for(unsigned int index=_state.m_light;inde 132 ::glDisable(GL_LIGHT0+index); 133 } 134 */ 135 } 136 137 //////////////////////////////////////////// 138 /// texture //////////////////////////////// 139 //////////////////////////////////////////// 140 virtual void draw_vertex_array_texture(gl::m 141 gstoi 142 //::printf("debug : zb_action : 000 : %d\n 143 img_byte img; 144 if(!m_mgr.find(a_id,img)) return; 145 m_pv.add_texture(m_out,a_xyzn,a_xyzs,img,a 146 } 147 148 virtual void draw_vertex_normal_array_textur 149 150 151 draw_vertex_array_texture(a_mode,a_xyzn,a_ 152 } 153 154 //////////////////////////////////////////// 155 /// VBO //////////////////////////////////// 156 //////////////////////////////////////////// 157 virtual void begin_gsto(gstoid) {} 158 virtual void draw_gsto_v(gl::mode_t,size_t,b 159 virtual void draw_gsto_vc(gl::mode_t,size_t, 160 virtual void draw_gsto_vn(gl::mode_t,size_t, 161 virtual void draw_gsto_vcn(gl::mode_t,size_t 162 virtual void end_gsto() {} 163 //////////////////////////////////////////// 164 //////////////////////////////////////////// 165 //////////////////////////////////////////// 166 virtual sg::render_manager& render_manager() 167 public: 168 zb_action(zb_manager& a_mgr,std::ostream& a_ 169 :parent(a_out,a_ww,a_wh) 170 ,m_mgr(a_mgr) 171 ,m_pv(get_me()) 172 ,m_light_color(colorf_white()) 173 ,m_light_ambient(colorf_black()) 174 ,m_light_direction(vec3f(0,0,-1)) 175 ,m_normal(0,0,1) 176 177 ,m_ccw(true) 178 ,m_POLYGON_OFFSET_FILL(false) 179 ,m_CULL_FACE(true) 180 ,m_POINT_SMOOTH(false) 181 ,m_LINE_SMOOTH(false) 182 ,m_line_width(1) 183 ,m_point_size(1) 184 ,m_light_on(false) 185 ,m_DEPTH_TEST(true) 186 ,m_blend(false) 187 { 188 m_vp_mtx.set_identity(); 189 m_vp_mtx.mul_translate(float(m_ww)/2,float 190 m_vp_mtx.mul_scale(float(m_ww)/2,float(m_w 191 192 m_zb.change_size(a_ww,a_wh); 193 //m_zb.clear_color_buffer(0); 194 //m_zb.clear_depth_buffer(); 195 196 m_proj.set_identity(); 197 m_model.set_identity(); 198 m_normal_matrix.set_identity(); 199 } 200 virtual ~zb_action(){} 201 protected: 202 zb_action(const zb_action& a_from) 203 :parent(a_from) 204 ,m_mgr(a_from.m_mgr) 205 ,m_vp_mtx(a_from.m_vp_mtx) 206 ,m_pv(a_from.m_pv) 207 ,m_light_color(a_from.m_light_color) 208 ,m_light_ambient(a_from.m_light_ambient) 209 ,m_light_direction(a_from.m_light_direction) 210 ,m_normal(a_from.m_normal) 211 212 ,m_proj(a_from.m_proj) 213 ,m_model(a_from.m_model) 214 ,m_normal_matrix(a_from.m_normal_matrix) 215 ,m_rgba(a_from.m_rgba) 216 ,m_ccw(a_from.m_ccw) 217 ,m_POLYGON_OFFSET_FILL(a_from.m_POLYGON_OFFS 218 ,m_CULL_FACE(a_from.m_CULL_FACE) 219 ,m_POINT_SMOOTH(a_from.m_POINT_SMOOTH) 220 ,m_LINE_SMOOTH(a_from.m_LINE_SMOOTH) 221 ,m_line_width(a_from.m_line_width) 222 ,m_point_size(a_from.m_point_size) 223 ,m_light_on(a_from.m_light_on) 224 ,m_DEPTH_TEST(a_from.m_DEPTH_TEST) 225 ,m_blend(a_from.m_blend) 226 {} 227 zb_action& operator=(const zb_action& a_from 228 parent::operator=(a_from); 229 m_vp_mtx = a_from.m_vp_mtx; 230 m_pv = a_from.m_pv; 231 m_light_color = a_from.m_light_color; 232 m_light_ambient = a_from.m_light_ambient; 233 m_light_direction = a_from.m_light_directi 234 m_normal = a_from.m_normal; 235 236 m_proj = a_from.m_proj; 237 m_model = a_from.m_model; 238 m_normal_matrix = a_from.m_normal_matrix; 239 m_rgba = a_from.m_rgba; 240 m_ccw = a_from.m_ccw; 241 m_POLYGON_OFFSET_FILL = a_from.m_POLYGON_O 242 m_CULL_FACE = a_from.m_CULL_FACE; 243 m_POINT_SMOOTH = a_from.m_POINT_SMOOTH; 244 m_LINE_SMOOTH = a_from.m_LINE_SMOOTH; 245 m_line_width = a_from.m_line_width; 246 m_point_size = a_from.m_point_size; 247 m_light_on = a_from.m_light_on; 248 m_DEPTH_TEST = a_from.m_DEPTH_TEST; 249 m_blend = a_from.m_blend; 250 return *this; 251 } 252 public: 253 void clear_color_buffer(float a_r,float a_g, 254 clear_color(a_r,a_g,a_b,a_a); 255 } 256 void clear_color_buffer(const colorf& a_colo 257 clear_color(a_color.r(),a_color.g(),a_colo 258 } 259 260 void clear_depth_buffer() {m_zb.clear_depth_ 261 protected: 262 typedef unsigned char uchar; 263 protected: 264 static void color2pix(const colorf& a_rgba,z 265 zb::buffer::rgba2pix(a_rgba.r(),a_rgba.g() 266 } 267 public: 268 zb::buffer::ZPixel* get_color_buffer(unsigne 269 270 bool get_rgbs(bool a_top_to_bottom,std::vect 271 a_buffer.clear(); 272 if(!m_ww || !m_wh) return false; 273 size_t sz = 3 * m_ww * m_wh; 274 a_buffer.resize(sz); 275 uchar* pos = vec_data(a_buffer); 276 zb::buffer::ZPixel pix; 277 uchar* _pix = 0; 278 for(unsigned int row=0;row<m_wh;row++) { 279 for(unsigned int col=0;col<m_ww;col++) { 280 if(!m_zb.get_clipped_pixel(col,a_top_t 281 m_out << "tools::sg::zb_action::get_ 282 *pos = 0xFF;pos++; 283 *pos = 0x00;pos++; 284 *pos = 0x00;pos++; 285 } else { 286 _pix = (uchar*)&pix; 287 *pos = *_pix;_pix++;pos++; 288 *pos = *_pix;_pix++;pos++; 289 *pos = *_pix;_pix++;pos++; 290 } 291 } 292 } 293 return true; 294 } 295 296 bool get_rgbas(bool a_top_to_bottom,std::vec 297 a_buffer.clear(); 298 if(!m_ww || !m_wh) return false; 299 size_t sz = 4 * m_ww * m_wh; 300 a_buffer.resize(sz); 301 if(a_top_to_bottom) { 302 ::memcpy(vec_data(a_buffer),m_zb.zimage( 303 return true; 304 } 305 size_t stride = m_ww*4; 306 uchar* zpos = ((uchar*)m_zb.zimage())+sz-s 307 uchar* pos = vec_data(a_buffer); 308 for(unsigned int row=0;row<m_wh;row++,pos+ 309 return true; 310 } 311 bool get_rgbas_cocoa(unsigned int a_factor,s 312 a_buffer.clear(); 313 if(!m_ww || !m_wh) return false; 314 if(!a_factor) return false; 315 size_t bpp = 4; 316 a_buffer.resize(a_factor* m_ww * a_factor 317 uchar* zbuffer = (uchar*)m_zb.zimage(); 318 uchar* abuffer = vec_data(a_buffer); 319 size_t zstride = m_ww*bpp; 320 size_t astride = a_factor*m_ww*bpp; 321 size_t i,j,ar,ac,ipix; 322 uchar* zpos;uchar* apos; 323 for(j=0;j<m_wh;j++) { 324 for(i=0;i<m_ww;i++) { 325 //position in the original image. 326 zpos = zbuffer + (m_wh-1-j) * zstride 327 328 for(ar=0;ar<a_factor;ar++) { 329 for(ac=0;ac<a_factor;ac++) { 330 //position in the new image. 331 apos = abuffer + (j*a_factor+ar) * 332 for(ipix=0;ipix<bpp;ipix++) { 333 *(apos+ipix) = *(zpos+ipix); 334 } 335 } 336 } 337 338 } 339 } 340 341 return true; 342 } 343 344 bool get_bgras(bool a_top_to_bottom,std::vec 345 a_buffer.clear(); 346 if(!m_ww || !m_wh) return false; 347 size_t sz = 4 * m_ww * m_wh; 348 a_buffer.resize(sz); 349 if(a_top_to_bottom) { 350 uchar* pos = vec_data(a_buffer); 351 uchar* zpos = ((uchar*)m_zb.zimage()); 352 for(size_t count=0;count<sz;count+=4,pos 353 *(pos+0) = *(zpos+2); 354 *(pos+1) = *(zpos+1); 355 *(pos+2) = *(zpos+0); 356 *(pos+3) = *(zpos+3); 357 } 358 } else { 359 size_t stride = m_ww*4; 360 uchar* pos_line = vec_data(a_buffer); 361 uchar* zpos_line = ((uchar*)m_zb.zimage( 362 uchar* pos; 363 uchar* zpos; 364 size_t count; 365 for(unsigned int row=0;row<m_wh;row++,po 366 pos = pos_line; 367 zpos = zpos_line; 368 for(count=0;count<m_ww;count++,pos+=4, 369 *(pos+0) = *(zpos+2); 370 *(pos+1) = *(zpos+1); 371 *(pos+2) = *(zpos+0); 372 *(pos+3) = *(zpos+3); 373 } 374 } 375 } 376 return true; 377 } 378 public: 379 static bool get_rgb(void* a_tag,unsigned int 380 //used with wps. 381 zb_action* rzb = (zb_action*)a_tag; 382 zb::buffer::ZPixel pix; 383 if(!rzb->m_zb.get_clipped_pixel(a_col,rzb- 384 rzb->out() << "tools::sg;:zb_action::get 385 a_r = 1; 386 a_g = 0; 387 a_b = 0; 388 return false; 389 } 390 float a; 391 zb::buffer::pix2rgba(pix,a_r,a_g,a_b,a); 392 return true; 393 } 394 protected: 395 void set_normal_matrix() { 396 mat4f tmp(m_model); 397 tmp.no_translate(); 398 if(!tmp.invert(m_normal_matrix)) { 399 m_out << "tools::sg::zb_action::set_norm 400 } 401 m_normal_matrix.transpose(); 402 } 403 404 bool project_point(float& a_x,float& a_y,flo 405 a_w = 1; 406 m_model.mul_4f_opt(a_x,a_y,a_z,a_w,m_tmp); 407 m_proj.mul_4f_opt(a_x,a_y,a_z,a_w,m_tmp); 408 if(a_w==0) return false; 409 a_x /= a_w; 410 a_y /= a_w; 411 a_z /= a_w; 412 return true; 413 } 414 // bool project_normal(float& a_x,float& a_y, 415 // m_model.mul_dir_3f(a_x,a_y,a_z); 416 // m_proj.mul_dir_3f(a_x,a_y,a_z); 417 // return true; 418 // } 419 420 class primvis : public primitive_visitor { 421 protected: 422 virtual bool project(float& a_x,float& a_y 423 return m_this.project_point(a_x,a_y,a_z, 424 } 425 virtual bool add_point(float a_x,float a_y 426 return _add_point(a_x,a_y,a_z,m_this.m_r 427 } 428 429 virtual bool add_point(float a_x,float a_y 430 float a_r,float a_g 431 return _add_point(a_x,a_y,a_z,a_r,a_g,a_ 432 } 433 434 virtual bool add_line(float a_bx,float a_b 435 float a_ex,float a_e 436 m_this.m_vp_mtx.mul_3f_opt(a_bx,a_by,a_b 437 m_this.m_vp_mtx.mul_3f_opt(a_ex,a_ey,a_e 438 a_bz *= -1; 439 a_ez *= -1; 440 441 zb::point beg; 442 zinit(beg,a_bx,a_by,a_bz); 443 444 zb::point end; 445 zinit(end,a_ex,a_ey,a_ez); 446 447 m_this.m_zb.set_depth_test(m_this.m_DEPT 448 m_this.m_zb.set_blend(m_this.m_blend); 449 450 zb::buffer::ZPixel pix; 451 color2pix(m_this.m_rgba,pix); 452 m_this.m_zb.draw_line(beg,end,pix,npix(m 453 454 return true; 455 } 456 457 virtual bool add_line(float a_bx,float a_b 458 float a_br,float a_b 459 float a_ex,float a_e 460 float,float,float,fl 461 m_this.m_vp_mtx.mul_3f_opt(a_bx,a_by,a_b 462 m_this.m_vp_mtx.mul_3f_opt(a_ex,a_ey,a_e 463 a_bz *= -1; 464 a_ez *= -1; 465 466 zb::point beg; 467 zinit(beg,a_bx,a_by,a_bz); 468 469 zb::point end; 470 zinit(end,a_ex,a_ey,a_ez); 471 472 m_this.m_zb.set_depth_test(m_this.m_DEPT 473 m_this.m_zb.set_blend(m_this.m_blend); 474 475 // interpolate color with beg,end ? 476 zb::buffer::ZPixel pix; 477 zb::buffer::rgba2pix(a_br,a_bg,a_bb,a_ba 478 m_this.m_zb.draw_line(beg,end,pix,npix(m 479 480 return true; 481 } 482 483 virtual bool add_triangle(float a_p1x,floa 484 float a_p2x,floa 485 float a_p3x,floa 486 return _add_triangle(a_p1x,a_p1y,a_p1z,a 487 m_this.m_normal.x() 488 m_this.m_normal.y() 489 m_this.m_normal.z() 490 a_p2x,a_p2y,a_p2z,a 491 m_this.m_normal.x() 492 m_this.m_normal.y() 493 m_this.m_normal.z() 494 a_p3x,a_p3y,a_p3z,a 495 m_this.m_normal.x() 496 m_this.m_normal.y() 497 m_this.m_normal.z() 498 m_this.m_rgba); 499 } 500 501 virtual bool add_triangle( 502 float a_p1x,float a_p1y,float a_p1z,floa 503 float a_r1,float a_g1,float a_b1,float a 504 float a_p2x,float a_p2y,float a_p2z,floa 505 float a_r2,float a_g2,float a_b2,float a 506 float a_p3x,float a_p3y,float a_p3z,floa 507 float a_r3,float a_g3,float a_b3,float a 508 509 float r = (a_r1+a_r2+a_r3)/3.0f; 510 float g = (a_g1+a_g2+a_g3)/3.0f; 511 float b = (a_b1+a_b2+a_b3)/3.0f; 512 float a = (a_a1+a_a2+a_a3)/3.0f; 513 colorf col(r,g,b,a); 514 515 return _add_triangle(a_p1x,a_p1y,a_p1z,a 516 m_this.m_normal.x() 517 m_this.m_normal.y() 518 m_this.m_normal.z() 519 a_p2x,a_p2y,a_p2z,a 520 m_this.m_normal.x() 521 m_this.m_normal.y() 522 m_this.m_normal.z() 523 a_p3x,a_p3y,a_p3z,a 524 m_this.m_normal.x() 525 m_this.m_normal.y() 526 m_this.m_normal.z() 527 col); 528 } 529 530 virtual bool project_normal(float&,float&, 531 //return m_this.project_normal(a_x,a_y,a 532 return true; 533 } 534 virtual bool add_point_normal(float a_x,fl 535 float /*a_nx 536 add_point(a_x,a_y,a_z,a_w); 537 return true; 538 } 539 virtual bool add_point_normal(float a_x,fl 540 float /*a_nx 541 float a_r,fl 542 add_point(a_x,a_y,a_z,a_w,a_r,a_g,a_b,a_ 543 return true; 544 } 545 virtual bool add_line_normal(float a_bx,fl 546 float /*a_bnx 547 float a_ex,fl 548 float /*a_enx 549 add_line(a_bx,a_by,a_bz,a_bw, a_ex,a_ey, 550 return true; 551 } 552 virtual bool add_line_normal(float a_bx,fl 553 float /*a_bnx 554 float a_br,fl 555 float a_ex,fl 556 float /*a_enx 557 float a_er,fl 558 add_line(a_bx,a_by,a_bz,a_bw, a_br,a_bg, 559 return true; 560 } 561 virtual bool add_triangle_normal( 562 float a_p1x,float a_p1y,float a_p1z,floa 563 float a_n1x,float a_n1y,float a_n1z, 564 float a_p2x,float a_p2y,float a_p2z,floa 565 float a_n2x,float a_n2y,float a_n2z, 566 float a_p3x,float a_p3y,float a_p3z,floa 567 float a_n3x,float a_n3y,float a_n3z) { 568 569 return _add_triangle(a_p1x,a_p1y,a_p1z,a 570 a_n1x,a_n1y,a_n1z, 571 a_p2x,a_p2y,a_p2z,a 572 a_n2x,a_n2y,a_n2z, 573 a_p3x,a_p3y,a_p3z,a 574 a_n3x,a_n3y,a_n3z, 575 m_this.m_rgba); 576 return true; 577 } 578 virtual bool add_triangle_normal( 579 float a_p1x,float a_p1y,float a_p1z,floa 580 float a_n1x,float a_n1y,float a_n1z, 581 float a_r1,float a_g1,float a_b1,float a 582 float a_p2x,float a_p2y,float a_p2z,floa 583 float a_n2x,float a_n2y,float a_n2z, 584 float a_r2,float a_g2,float a_b2,float a 585 float a_p3x,float a_p3y,float a_p3z,floa 586 float a_n3x,float a_n3y,float a_n3z, 587 float a_r3,float a_g3,float a_b3,float a 588 589 float r = (a_r1+a_r2+a_r3)/3.0f; 590 float g = (a_g1+a_g2+a_g3)/3.0f; 591 float b = (a_b1+a_b2+a_b3)/3.0f; 592 float a = (a_a1+a_a2+a_a3)/3.0f; 593 colorf col(r,g,b,a); 594 595 return _add_triangle(a_p1x,a_p1y,a_p1z,a 596 a_n1x,a_n1y,a_n1z, 597 a_p2x,a_p2y,a_p2z,a 598 a_n2x,a_n2y,a_n2z, 599 a_p3x,a_p3y,a_p3z,a 600 a_n3x,a_n3y,a_n3z, 601 col); 602 return true; 603 } 604 public: 605 primvis(zb_action& a_zb):m_this(a_zb){} 606 virtual ~primvis(){} 607 public: 608 primvis(const primvis& a_from) 609 :primitive_visitor(a_from) 610 ,m_this(a_from.m_this) 611 {} 612 primvis& operator=(const primvis& a_from){ 613 primitive_visitor::operator=(a_from); 614 return *this; 615 } 616 protected: 617 static void zinit(zb::point& a_p,float a_x 618 a_p.x = fround(a_x); //float -> int 619 a_p.y = fround(a_y); //float -> int 620 a_p.z = (zb::ZZ)a_z; //float -> double 621 } 622 623 unsigned int npix(float a_size) { 624 // 0 -> 0 625 // 1 -> 0 626 // 2 -> 1 3x3 627 // 3 -> 1 3x3 628 // 4 -> 2 5x5 629 // 5 -> 2 5x5 630 // 6 -> 3 7x7 631 unsigned int num = (unsigned int)a_size; 632 unsigned int num_2 = num/2; 633 if(2*num_2==num) {num++;num_2 = num/2;} 634 return num_2; 635 } 636 637 bool _add_point(float a_x,float a_y,float 638 m_this.m_zb.set_depth_test(m_this.m_DEPT 639 m_this.m_zb.set_blend(m_this.m_blend); 640 641 m_this.m_vp_mtx.mul_3f_opt(a_x,a_y,a_z,m 642 a_z *= -1; 643 644 zb::point p; 645 zinit(p,a_x,a_y,a_z); 646 647 zb::buffer::ZPixel pix; 648 zb::buffer::rgba2pix(a_r,a_g,a_b,a_a,pix 649 m_this.m_zb.draw_point(p,pix,npix(m_this 650 651 return true; 652 } 653 654 bool _add_triangle(float a_p1x,float a_p1y 655 float a_n1x,float a_n1y 656 float a_p2x,float a_p2y 657 float a_n2x,float a_n2y 658 float a_p3x,float a_p3y 659 float a_n3x,float a_n3y 660 const colorf& a_color){ 661 662 float p1x = a_p1x;float p1y = a_p1y;floa 663 float p2x = a_p2x;float p2y = a_p2y;floa 664 float p3x = a_p3x;float p3y = a_p3y;floa 665 666 m_this.m_vp_mtx.mul_3f_opt(p1x,p1y,p1z,m 667 m_this.m_vp_mtx.mul_3f_opt(p2x,p2y,p2z,m 668 m_this.m_vp_mtx.mul_3f_opt(p3x,p3y,p3z,m 669 p1z *= -1; 670 p2z *= -1; 671 p3z *= -1; 672 673 if(m_this.m_POLYGON_OFFSET_FILL){ 674 //note : gopaw pawex9,14,15,21 with "l 675 // zs are in [-1,1] 676 float epsil = 1e-4f; 677 p1z -= epsil; 678 p2z -= epsil; 679 p3z -= epsil; 680 } 681 682 typedef zb::ZZ ZZ; //double 683 684 plane<vec3d> pn( 685 vec3<ZZ>(p1x,p1y,p1z), 686 vec3<ZZ>(p2x,p2y,p2z), 687 vec3<ZZ>(p3x,p3y,p3z) 688 ); 689 if(!pn.is_valid()) return true; 690 691 // norm[0]*x+norm[1]*y+norm[2]*z = dist 692 // A*x+B*y+C*z+D = 0 693 694 ZZ C = pn.normal()[2]; 695 696 if(m_this.m_CULL_FACE){ 697 if(m_this.m_ccw) { 698 if(C<=0) return true; 699 } else { 700 if(C>=0) return true; 701 } 702 } 703 704 ZZ A = pn.normal()[0]; 705 ZZ B = pn.normal()[1]; 706 ZZ D = -pn.distance_from_origin(); 707 708 //ZZ zmn = mn<ZZ>(mn<ZZ>(p1z,p2z),p3z); 709 //ZZ zmx = mx<ZZ>(mx<ZZ>(p1z,p2z),p3z); 710 711 zb::point list[3]; 712 zinit(list[0],p1x,p1y,p1z); 713 zinit(list[1],p2x,p2y,p2z); 714 zinit(list[2],p3x,p3y,p3z); 715 716 m_this.m_zb.set_depth_test(m_this.m_DEPT 717 m_this.m_zb.set_blend(m_this.m_blend); 718 719 colorf frag_color = a_color; 720 721 if(m_this.m_light_on) { // same logic a 722 723 float nx = (a_n1x+a_n2x+a_n3x)/3.0f; 724 float ny = (a_n1y+a_n2y+a_n3y)/3.0f; 725 float nz = (a_n1z+a_n2z+a_n3z)/3.0f; 726 727 m_this.m_normal_matrix.mul_dir_3f_opt( 728 729 vec3f _normal(nx,ny,nz);_normal.normal 730 731 float _dot = _normal.dot(m_this.m_ligh 732 733 if(_dot<0.0f) { 734 _dot *= -1.0f; 735 736 colorf _tmp = m_this.m_light_color; 737 _tmp *= _dot; 738 // _tmp *= 1.4f; //to have same intens 739 _tmp += m_this.m_light_ambient; 740 741 frag_color *= _tmp; 742 743 } else { 744 frag_color *= m_this.m_light_ambient 745 } 746 747 frag_color.clamp(); 748 frag_color.set_a(a_color.a()); 749 750 } 751 752 zb::buffer::ZPixel pix; 753 color2pix(frag_color,pix); 754 m_this.m_zb.draw_polygon(3,list,A,B,C,D, 755 756 return true; 757 } 758 protected: 759 zb_action& m_this; 760 private: //optimize: 761 float m_tmp[3]; 762 }; 763 764 protected: 765 zb_manager& m_mgr; 766 mat4f m_vp_mtx; 767 zb::buffer m_zb; 768 primvis m_pv; 769 colorf m_light_color; 770 colorf m_light_ambient; 771 vec3f m_light_direction; 772 vec3f m_normal; 773 774 // to be restored in restore_state() : 775 mat4f m_proj; 776 mat4f m_model; 777 mat4f m_normal_matrix; 778 colorf m_rgba; 779 bool m_ccw; 780 bool m_POLYGON_OFFSET_FILL; 781 bool m_CULL_FACE; 782 bool m_POINT_SMOOTH; 783 bool m_LINE_SMOOTH; 784 float m_line_width; 785 float m_point_size; 786 bool m_light_on; 787 bool m_DEPTH_TEST; 788 bool m_blend; 789 private: //optimize: 790 float m_tmp[4]; 791 }; 792 793 }} 794 795 #endif