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