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 reset() {} >> 254 253 void clear_color_buffer(float a_r,float a_g, 255 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); 256 clear_color(a_r,a_g,a_b,a_a); 255 } 257 } 256 void clear_color_buffer(const colorf& a_colo 258 void clear_color_buffer(const colorf& a_color){ 257 clear_color(a_color.r(),a_color.g(),a_colo 259 clear_color(a_color.r(),a_color.g(),a_color.b(),a_color.a()); 258 } 260 } 259 261 260 void clear_depth_buffer() {m_zb.clear_depth_ 262 void clear_depth_buffer() {m_zb.clear_depth_buffer();} 261 protected: 263 protected: 262 typedef unsigned char uchar; 264 typedef unsigned char uchar; 263 protected: 265 protected: 264 static void color2pix(const colorf& a_rgba,z 266 static void color2pix(const colorf& a_rgba,zb::buffer::ZPixel& a_pix) { 265 zb::buffer::rgba2pix(a_rgba.r(),a_rgba.g() 267 zb::buffer::rgba2pix(a_rgba.r(),a_rgba.g(),a_rgba.b(),a_rgba.a(),a_pix); 266 } 268 } 267 public: 269 public: 268 zb::buffer::ZPixel* get_color_buffer(unsigne 270 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 271 270 bool get_rgbs(bool a_top_to_bottom,std::vect << 272 unsigned char* get_rgbas(size_t& a_sz,bool a_top_to_bottom = true) { 271 a_buffer.clear(); << 273 if(!m_ww || !m_wh) {a_sz = 0;return 0;} 272 if(!m_ww || !m_wh) return false; << 274 a_sz = 4 * m_ww * m_wh; 273 size_t sz = 3 * m_ww * m_wh; << 275 uchar* rgbas = new uchar[a_sz]; 274 a_buffer.resize(sz); << 276 if(!rgbas) {a_sz = 0;return 0;} 275 uchar* pos = vec_data(a_buffer); << 277 uchar* pos = rgbas; 276 zb::buffer::ZPixel pix; 278 zb::buffer::ZPixel pix; 277 uchar* _pix = 0; 279 uchar* _pix = 0; 278 for(unsigned int row=0;row<m_wh;row++) { 280 for(unsigned int row=0;row<m_wh;row++) { 279 for(unsigned int col=0;col<m_ww;col++) { 281 for(unsigned int col=0;col<m_ww;col++) { 280 if(!m_zb.get_clipped_pixel(col,a_top_t 282 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_ << 283 m_out << "tools::sg::zb_action::get_rgbas : can't get zbuffer pixel" << std::endl; 282 *pos = 0xFF;pos++; 284 *pos = 0xFF;pos++; 283 *pos = 0x00;pos++; 285 *pos = 0x00;pos++; 284 *pos = 0x00;pos++; 286 *pos = 0x00;pos++; >> 287 *pos = 0xFF;pos++; 285 } else { 288 } else { 286 _pix = (uchar*)&pix; 289 _pix = (uchar*)&pix; 287 *pos = *_pix;_pix++;pos++; 290 *pos = *_pix;_pix++;pos++; 288 *pos = *_pix;_pix++;pos++; 291 *pos = *_pix;_pix++;pos++; 289 *pos = *_pix;_pix++;pos++; 292 *pos = *_pix;_pix++;pos++; >> 293 *pos = *_pix;_pix++;pos++; 290 } 294 } 291 } 295 } 292 } 296 } 293 return true; << 297 return rgbas; 294 } 298 } 295 299 296 bool get_rgbas(bool a_top_to_bottom,std::vec << 300 unsigned char* get_rgbs(size_t& a_sz,bool a_top_to_bottom = true) { 297 a_buffer.clear(); << 301 if(!m_ww || !m_wh) {a_sz = 0;return 0;} 298 if(!m_ww || !m_wh) return false; << 302 a_sz = 3 * m_ww * m_wh; 299 size_t sz = 4 * m_ww * m_wh; << 303 uchar* rgbs = new uchar[a_sz]; 300 a_buffer.resize(sz); << 304 if(!rgbs) {a_sz = 0;return 0;} 301 if(a_top_to_bottom) { << 305 uchar* pos = rgbs; 302 ::memcpy(vec_data(a_buffer),m_zb.zimage( << 306 zb::buffer::ZPixel pix; 303 return true; << 307 uchar* _pix = 0; 304 } << 308 for(unsigned int row=0;row<m_wh;row++) { 305 size_t stride = m_ww*4; << 309 for(unsigned int col=0;col<m_ww;col++) { 306 uchar* zpos = ((uchar*)m_zb.zimage())+sz-s << 310 if(!m_zb.get_clipped_pixel(col,a_top_to_bottom?row:m_wh-1-row,pix)){ 307 uchar* pos = vec_data(a_buffer); << 311 m_out << "tools::sg::zb_action::get_rgbs : can't get zbuffer pixel" << std::endl; 308 for(unsigned int row=0;row<m_wh;row++,pos+ << 312 *pos = 0xFF;pos++; 309 return true; << 313 *pos = 0x00;pos++; 310 } << 314 *pos = 0x00;pos++; 311 bool get_rgbas_cocoa(unsigned int a_factor,s << 315 } else { 312 a_buffer.clear(); << 316 _pix = (uchar*)&pix; 313 if(!m_ww || !m_wh) return false; << 317 *pos = *_pix;_pix++;pos++; 314 if(!a_factor) return false; << 318 *pos = *_pix;_pix++;pos++; 315 size_t bpp = 4; << 319 *pos = *_pix;_pix++;pos++; 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 } 320 } 337 << 338 } 321 } 339 } 322 } 340 << 323 return rgbs; 341 return true; << 342 } 324 } 343 325 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: 326 public: 379 static bool get_rgb(void* a_tag,unsigned int 327 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. 328 //used with wps. 381 zb_action* rzb = (zb_action*)a_tag; 329 zb_action* rzb = (zb_action*)a_tag; 382 zb::buffer::ZPixel pix; 330 zb::buffer::ZPixel pix; 383 if(!rzb->m_zb.get_clipped_pixel(a_col,rzb- 331 if(!rzb->m_zb.get_clipped_pixel(a_col,rzb->wh()-1-a_row,pix)){ 384 rzb->out() << "tools::sg;:zb_action::get 332 rzb->out() << "tools::sg;:zb_action::get_rgb: can't get zbuffer pixel" << std::endl; 385 a_r = 1; 333 a_r = 1; 386 a_g = 0; 334 a_g = 0; 387 a_b = 0; 335 a_b = 0; 388 return false; 336 return false; 389 } 337 } 390 float a; 338 float a; 391 zb::buffer::pix2rgba(pix,a_r,a_g,a_b,a); 339 zb::buffer::pix2rgba(pix,a_r,a_g,a_b,a); 392 return true; 340 return true; 393 } 341 } >> 342 static bool get_rgba(void* a_tag,unsigned int a_col,unsigned int a_row,float& a_r,float& a_g,float& a_b,float& a_a){ >> 343 zb_action* rzb = (zb_action*)a_tag; >> 344 zb::buffer::ZPixel pix; >> 345 if(!rzb->m_zb.get_clipped_pixel(a_col,rzb->wh()-1-a_row,pix)){ >> 346 rzb->out() << "tools::sg;:zb_action::get_rgba : can't get zbuffer pixel" << std::endl; >> 347 a_r = 1; >> 348 a_g = 0; >> 349 a_b = 0; >> 350 a_a = 1; >> 351 return false; >> 352 } >> 353 zb::buffer::pix2rgba(pix,a_r,a_g,a_b,a_a); >> 354 return true; >> 355 } >> 356 394 protected: 357 protected: 395 void set_normal_matrix() { 358 void set_normal_matrix() { 396 mat4f tmp(m_model); 359 mat4f tmp(m_model); 397 tmp.no_translate(); 360 tmp.no_translate(); 398 if(!tmp.invert(m_normal_matrix)) { 361 if(!tmp.invert(m_normal_matrix)) { 399 m_out << "tools::sg::zb_action::set_norm 362 m_out << "tools::sg::zb_action::set_normal_matrix : can't invert model matrix." << std::endl; 400 } 363 } 401 m_normal_matrix.transpose(); 364 m_normal_matrix.transpose(); 402 } 365 } 403 366 404 bool project_point(float& a_x,float& a_y,flo 367 bool project_point(float& a_x,float& a_y,float& a_z,float& a_w) { 405 a_w = 1; 368 a_w = 1; 406 m_model.mul_4f_opt(a_x,a_y,a_z,a_w,m_tmp); << 369 m_model.mul_4f(a_x,a_y,a_z,a_w); 407 m_proj.mul_4f_opt(a_x,a_y,a_z,a_w,m_tmp); << 370 m_proj.mul_4f(a_x,a_y,a_z,a_w); 408 if(a_w==0) return false; 371 if(a_w==0) return false; 409 a_x /= a_w; 372 a_x /= a_w; 410 a_y /= a_w; 373 a_y /= a_w; 411 a_z /= a_w; 374 a_z /= a_w; 412 return true; 375 return true; 413 } 376 } 414 // bool project_normal(float& a_x,float& a_y, 377 // bool project_normal(float& a_x,float& a_y,float& a_z) { 415 // m_model.mul_dir_3f(a_x,a_y,a_z); 378 // m_model.mul_dir_3f(a_x,a_y,a_z); 416 // m_proj.mul_dir_3f(a_x,a_y,a_z); 379 // m_proj.mul_dir_3f(a_x,a_y,a_z); 417 // return true; 380 // return true; 418 // } 381 // } 419 382 420 class primvis : public primitive_visitor { 383 class primvis : public primitive_visitor { 421 protected: 384 protected: 422 virtual bool project(float& a_x,float& a_y 385 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, 386 return m_this.project_point(a_x,a_y,a_z,a_w); 424 } 387 } 425 virtual bool add_point(float a_x,float a_y 388 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 << 389 return _add_point(a_x,a_y,a_z,m_this.m_rgba); 427 } 390 } 428 391 429 virtual bool add_point(float a_x,float a_y 392 virtual bool add_point(float a_x,float a_y,float a_z,float, 430 float a_r,float a_g 393 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_ << 394 colorf c(a_r,a_g,a_b,a_a); >> 395 return _add_point(a_x,a_y,a_z,c); 432 } 396 } 433 397 434 virtual bool add_line(float a_bx,float a_b 398 virtual bool add_line(float a_bx,float a_by,float a_bz,float, 435 float a_ex,float a_e 399 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 << 400 m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz); 437 m_this.m_vp_mtx.mul_3f_opt(a_ex,a_ey,a_e << 401 m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez); 438 a_bz *= -1; 402 a_bz *= -1; 439 a_ez *= -1; 403 a_ez *= -1; 440 404 441 zb::point beg; 405 zb::point beg; 442 zinit(beg,a_bx,a_by,a_bz); 406 zinit(beg,a_bx,a_by,a_bz); 443 407 444 zb::point end; 408 zb::point end; 445 zinit(end,a_ex,a_ey,a_ez); 409 zinit(end,a_ex,a_ey,a_ez); 446 410 447 m_this.m_zb.set_depth_test(m_this.m_DEPT 411 m_this.m_zb.set_depth_test(m_this.m_DEPTH_TEST); 448 m_this.m_zb.set_blend(m_this.m_blend); 412 m_this.m_zb.set_blend(m_this.m_blend); 449 413 450 zb::buffer::ZPixel pix; 414 zb::buffer::ZPixel pix; 451 color2pix(m_this.m_rgba,pix); 415 color2pix(m_this.m_rgba,pix); 452 m_this.m_zb.draw_line(beg,end,pix,npix(m 416 m_this.m_zb.draw_line(beg,end,pix,npix(m_this.m_line_width)); 453 417 454 return true; 418 return true; 455 } 419 } 456 420 457 virtual bool add_line(float a_bx,float a_b 421 virtual bool add_line(float a_bx,float a_by,float a_bz,float, 458 float a_br,float a_b 422 float a_br,float a_bg,float a_bb,float a_ba, 459 float a_ex,float a_e 423 float a_ex,float a_ey,float a_ez,float, 460 float,float,float,fl 424 float,float,float,float) { 461 m_this.m_vp_mtx.mul_3f_opt(a_bx,a_by,a_b << 425 m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz); 462 m_this.m_vp_mtx.mul_3f_opt(a_ex,a_ey,a_e << 426 m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez); 463 a_bz *= -1; 427 a_bz *= -1; 464 a_ez *= -1; 428 a_ez *= -1; 465 429 466 zb::point beg; 430 zb::point beg; 467 zinit(beg,a_bx,a_by,a_bz); 431 zinit(beg,a_bx,a_by,a_bz); 468 432 469 zb::point end; 433 zb::point end; 470 zinit(end,a_ex,a_ey,a_ez); 434 zinit(end,a_ex,a_ey,a_ez); 471 435 472 m_this.m_zb.set_depth_test(m_this.m_DEPT 436 m_this.m_zb.set_depth_test(m_this.m_DEPTH_TEST); 473 m_this.m_zb.set_blend(m_this.m_blend); 437 m_this.m_zb.set_blend(m_this.m_blend); 474 438 475 // interpolate color with beg,end ? 439 // interpolate color with beg,end ? 476 zb::buffer::ZPixel pix; 440 zb::buffer::ZPixel pix; 477 zb::buffer::rgba2pix(a_br,a_bg,a_bb,a_ba 441 zb::buffer::rgba2pix(a_br,a_bg,a_bb,a_ba,pix); 478 m_this.m_zb.draw_line(beg,end,pix,npix(m 442 m_this.m_zb.draw_line(beg,end,pix,npix(m_this.m_line_width)); 479 443 480 return true; 444 return true; 481 } 445 } 482 446 483 virtual bool add_triangle(float a_p1x,floa 447 virtual bool add_triangle(float a_p1x,float a_p1y,float a_p1z,float a_p1w, 484 float a_p2x,floa 448 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 485 float a_p3x,floa 449 float a_p3x,float a_p3y,float a_p3z,float a_p3w){ 486 return _add_triangle(a_p1x,a_p1y,a_p1z,a 450 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 487 m_this.m_normal.x() 451 m_this.m_normal.x(), 488 m_this.m_normal.y() 452 m_this.m_normal.y(), 489 m_this.m_normal.z() 453 m_this.m_normal.z(), 490 a_p2x,a_p2y,a_p2z,a 454 a_p2x,a_p2y,a_p2z,a_p2w, 491 m_this.m_normal.x() 455 m_this.m_normal.x(), 492 m_this.m_normal.y() 456 m_this.m_normal.y(), 493 m_this.m_normal.z() 457 m_this.m_normal.z(), 494 a_p3x,a_p3y,a_p3z,a 458 a_p3x,a_p3y,a_p3z,a_p3w, 495 m_this.m_normal.x() 459 m_this.m_normal.x(), 496 m_this.m_normal.y() 460 m_this.m_normal.y(), 497 m_this.m_normal.z() 461 m_this.m_normal.z(), 498 m_this.m_rgba); 462 m_this.m_rgba); 499 } 463 } 500 464 501 virtual bool add_triangle( 465 virtual bool add_triangle( 502 float a_p1x,float a_p1y,float a_p1z,floa 466 float a_p1x,float a_p1y,float a_p1z,float a_p1w, 503 float a_r1,float a_g1,float a_b1,float a 467 float a_r1,float a_g1,float a_b1,float a_a1, 504 float a_p2x,float a_p2y,float a_p2z,floa 468 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 505 float a_r2,float a_g2,float a_b2,float a 469 float a_r2,float a_g2,float a_b2,float a_a2, 506 float a_p3x,float a_p3y,float a_p3z,floa 470 float a_p3x,float a_p3y,float a_p3z,float a_p3w, 507 float a_r3,float a_g3,float a_b3,float a 471 float a_r3,float a_g3,float a_b3,float a_a3){ 508 472 509 float r = (a_r1+a_r2+a_r3)/3.0f; 473 float r = (a_r1+a_r2+a_r3)/3.0f; 510 float g = (a_g1+a_g2+a_g3)/3.0f; 474 float g = (a_g1+a_g2+a_g3)/3.0f; 511 float b = (a_b1+a_b2+a_b3)/3.0f; 475 float b = (a_b1+a_b2+a_b3)/3.0f; 512 float a = (a_a1+a_a2+a_a3)/3.0f; 476 float a = (a_a1+a_a2+a_a3)/3.0f; 513 colorf col(r,g,b,a); 477 colorf col(r,g,b,a); 514 478 515 return _add_triangle(a_p1x,a_p1y,a_p1z,a 479 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 516 m_this.m_normal.x() 480 m_this.m_normal.x(), 517 m_this.m_normal.y() 481 m_this.m_normal.y(), 518 m_this.m_normal.z() 482 m_this.m_normal.z(), 519 a_p2x,a_p2y,a_p2z,a 483 a_p2x,a_p2y,a_p2z,a_p2w, 520 m_this.m_normal.x() 484 m_this.m_normal.x(), 521 m_this.m_normal.y() 485 m_this.m_normal.y(), 522 m_this.m_normal.z() 486 m_this.m_normal.z(), 523 a_p3x,a_p3y,a_p3z,a 487 a_p3x,a_p3y,a_p3z,a_p3w, 524 m_this.m_normal.x() 488 m_this.m_normal.x(), 525 m_this.m_normal.y() 489 m_this.m_normal.y(), 526 m_this.m_normal.z() 490 m_this.m_normal.z(), 527 col); 491 col); 528 } 492 } 529 493 530 virtual bool project_normal(float&,float&, 494 virtual bool project_normal(float&,float&,float&) { 531 //return m_this.project_normal(a_x,a_y,a 495 //return m_this.project_normal(a_x,a_y,a_z); 532 return true; 496 return true; 533 } 497 } 534 virtual bool add_point_normal(float a_x,fl 498 virtual bool add_point_normal(float a_x,float a_y,float a_z,float a_w, 535 float /*a_nx 499 float /*a_nx*/,float /*a_ny*/,float /*a_nz*/) { 536 add_point(a_x,a_y,a_z,a_w); 500 add_point(a_x,a_y,a_z,a_w); 537 return true; 501 return true; 538 } 502 } 539 virtual bool add_point_normal(float a_x,fl 503 virtual bool add_point_normal(float a_x,float a_y,float a_z,float a_w, 540 float /*a_nx 504 float /*a_nx*/,float /*a_ny*/,float /*a_nz*/, 541 float a_r,fl 505 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_ 506 add_point(a_x,a_y,a_z,a_w,a_r,a_g,a_b,a_a); 543 return true; 507 return true; 544 } 508 } 545 virtual bool add_line_normal(float a_bx,fl 509 virtual bool add_line_normal(float a_bx,float a_by,float a_bz,float a_bw, 546 float /*a_bnx 510 float /*a_bnx*/,float /*a_bny*/,float /*a_bnz*/, 547 float a_ex,fl 511 float a_ex,float a_ey,float a_ez,float a_ew, 548 float /*a_enx 512 float /*a_enx*/,float /*a_eny*/,float /*a_enz*/) { 549 add_line(a_bx,a_by,a_bz,a_bw, a_ex,a_ey, 513 add_line(a_bx,a_by,a_bz,a_bw, a_ex,a_ey,a_ez,a_ew); 550 return true; 514 return true; 551 } 515 } 552 virtual bool add_line_normal(float a_bx,fl 516 virtual bool add_line_normal(float a_bx,float a_by,float a_bz,float a_bw, 553 float /*a_bnx 517 float /*a_bnx*/,float /*a_bny*/,float /*a_bnz*/, 554 float a_br,fl 518 float a_br,float a_bg,float a_bb,float a_ba, 555 float a_ex,fl 519 float a_ex,float a_ey,float a_ez,float a_ew, 556 float /*a_enx 520 float /*a_enx*/,float /*a_eny*/,float /*a_enz*/, 557 float a_er,fl 521 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, 522 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; 523 return true; 560 } 524 } 561 virtual bool add_triangle_normal( 525 virtual bool add_triangle_normal( 562 float a_p1x,float a_p1y,float a_p1z,floa 526 float a_p1x,float a_p1y,float a_p1z,float a_p1w, 563 float a_n1x,float a_n1y,float a_n1z, 527 float a_n1x,float a_n1y,float a_n1z, 564 float a_p2x,float a_p2y,float a_p2z,floa 528 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 565 float a_n2x,float a_n2y,float a_n2z, 529 float a_n2x,float a_n2y,float a_n2z, 566 float a_p3x,float a_p3y,float a_p3z,floa 530 float a_p3x,float a_p3y,float a_p3z,float a_p3w, 567 float a_n3x,float a_n3y,float a_n3z) { 531 float a_n3x,float a_n3y,float a_n3z) { 568 532 569 return _add_triangle(a_p1x,a_p1y,a_p1z,a 533 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 570 a_n1x,a_n1y,a_n1z, 534 a_n1x,a_n1y,a_n1z, 571 a_p2x,a_p2y,a_p2z,a 535 a_p2x,a_p2y,a_p2z,a_p2w, 572 a_n2x,a_n2y,a_n2z, 536 a_n2x,a_n2y,a_n2z, 573 a_p3x,a_p3y,a_p3z,a 537 a_p3x,a_p3y,a_p3z,a_p3w, 574 a_n3x,a_n3y,a_n3z, 538 a_n3x,a_n3y,a_n3z, 575 m_this.m_rgba); 539 m_this.m_rgba); 576 return true; 540 return true; 577 } 541 } 578 virtual bool add_triangle_normal( 542 virtual bool add_triangle_normal( 579 float a_p1x,float a_p1y,float a_p1z,floa 543 float a_p1x,float a_p1y,float a_p1z,float a_p1w, 580 float a_n1x,float a_n1y,float a_n1z, 544 float a_n1x,float a_n1y,float a_n1z, 581 float a_r1,float a_g1,float a_b1,float a 545 float a_r1,float a_g1,float a_b1,float a_a1, 582 float a_p2x,float a_p2y,float a_p2z,floa 546 float a_p2x,float a_p2y,float a_p2z,float a_p2w, 583 float a_n2x,float a_n2y,float a_n2z, 547 float a_n2x,float a_n2y,float a_n2z, 584 float a_r2,float a_g2,float a_b2,float a 548 float a_r2,float a_g2,float a_b2,float a_a2, 585 float a_p3x,float a_p3y,float a_p3z,floa 549 float a_p3x,float a_p3y,float a_p3z,float a_p3w, 586 float a_n3x,float a_n3y,float a_n3z, 550 float a_n3x,float a_n3y,float a_n3z, 587 float a_r3,float a_g3,float a_b3,float a 551 float a_r3,float a_g3,float a_b3,float a_a3){ 588 552 589 float r = (a_r1+a_r2+a_r3)/3.0f; 553 float r = (a_r1+a_r2+a_r3)/3.0f; 590 float g = (a_g1+a_g2+a_g3)/3.0f; 554 float g = (a_g1+a_g2+a_g3)/3.0f; 591 float b = (a_b1+a_b2+a_b3)/3.0f; 555 float b = (a_b1+a_b2+a_b3)/3.0f; 592 float a = (a_a1+a_a2+a_a3)/3.0f; 556 float a = (a_a1+a_a2+a_a3)/3.0f; 593 colorf col(r,g,b,a); 557 colorf col(r,g,b,a); 594 558 595 return _add_triangle(a_p1x,a_p1y,a_p1z,a 559 return _add_triangle(a_p1x,a_p1y,a_p1z,a_p1w, 596 a_n1x,a_n1y,a_n1z, 560 a_n1x,a_n1y,a_n1z, 597 a_p2x,a_p2y,a_p2z,a 561 a_p2x,a_p2y,a_p2z,a_p2w, 598 a_n2x,a_n2y,a_n2z, 562 a_n2x,a_n2y,a_n2z, 599 a_p3x,a_p3y,a_p3z,a 563 a_p3x,a_p3y,a_p3z,a_p3w, 600 a_n3x,a_n3y,a_n3z, 564 a_n3x,a_n3y,a_n3z, 601 col); 565 col); 602 return true; 566 return true; 603 } 567 } 604 public: 568 public: 605 primvis(zb_action& a_zb):m_this(a_zb){} 569 primvis(zb_action& a_zb):m_this(a_zb){} 606 virtual ~primvis(){} 570 virtual ~primvis(){} 607 public: 571 public: 608 primvis(const primvis& a_from) 572 primvis(const primvis& a_from) 609 :primitive_visitor(a_from) 573 :primitive_visitor(a_from) 610 ,m_this(a_from.m_this) 574 ,m_this(a_from.m_this) 611 {} 575 {} 612 primvis& operator=(const primvis& a_from){ 576 primvis& operator=(const primvis& a_from){ 613 primitive_visitor::operator=(a_from); 577 primitive_visitor::operator=(a_from); 614 return *this; 578 return *this; 615 } 579 } 616 protected: 580 protected: 617 static void zinit(zb::point& a_p,float a_x 581 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 582 a_p.x = fround(a_x); //float -> int 619 a_p.y = fround(a_y); //float -> int 583 a_p.y = fround(a_y); //float -> int 620 a_p.z = (zb::ZZ)a_z; //float -> double 584 a_p.z = (zb::ZZ)a_z; //float -> double 621 } 585 } 622 586 623 unsigned int npix(float a_size) { 587 unsigned int npix(float a_size) { 624 // 0 -> 0 588 // 0 -> 0 625 // 1 -> 0 589 // 1 -> 0 626 // 2 -> 1 3x3 590 // 2 -> 1 3x3 627 // 3 -> 1 3x3 591 // 3 -> 1 3x3 628 // 4 -> 2 5x5 592 // 4 -> 2 5x5 629 // 5 -> 2 5x5 593 // 5 -> 2 5x5 630 // 6 -> 3 7x7 594 // 6 -> 3 7x7 631 unsigned int num = (unsigned int)a_size; 595 unsigned int num = (unsigned int)a_size; 632 unsigned int num_2 = num/2; 596 unsigned int num_2 = num/2; 633 if(2*num_2==num) {num++;num_2 = num/2;} 597 if(2*num_2==num) {num++;num_2 = num/2;} 634 return num_2; 598 return num_2; 635 } 599 } 636 600 637 bool _add_point(float a_x,float a_y,float << 601 bool _add_point(float a_x,float a_y,float a_z,const colorf& a_color){ 638 m_this.m_zb.set_depth_test(m_this.m_DEPT 602 m_this.m_zb.set_depth_test(m_this.m_DEPTH_TEST); 639 m_this.m_zb.set_blend(m_this.m_blend); 603 m_this.m_zb.set_blend(m_this.m_blend); 640 604 641 m_this.m_vp_mtx.mul_3f_opt(a_x,a_y,a_z,m << 605 m_this.m_vp_mtx.mul_3f(a_x,a_y,a_z); 642 a_z *= -1; 606 a_z *= -1; 643 607 644 zb::point p; 608 zb::point p; 645 zinit(p,a_x,a_y,a_z); 609 zinit(p,a_x,a_y,a_z); 646 610 647 zb::buffer::ZPixel pix; 611 zb::buffer::ZPixel pix; 648 zb::buffer::rgba2pix(a_r,a_g,a_b,a_a,pix << 612 color2pix(a_color,pix); 649 m_this.m_zb.draw_point(p,pix,npix(m_this 613 m_this.m_zb.draw_point(p,pix,npix(m_this.m_point_size)); 650 614 651 return true; 615 return true; 652 } 616 } 653 617 654 bool _add_triangle(float a_p1x,float a_p1y 618 bool _add_triangle(float a_p1x,float a_p1y,float a_p1z,float /*a_p1w*/, 655 float a_n1x,float a_n1y 619 float a_n1x,float a_n1y,float a_n1z, 656 float a_p2x,float a_p2y 620 float a_p2x,float a_p2y,float a_p2z,float /*a_p2w*/, 657 float a_n2x,float a_n2y 621 float a_n2x,float a_n2y,float a_n2z, 658 float a_p3x,float a_p3y 622 float a_p3x,float a_p3y,float a_p3z,float /*a_p3w*/, 659 float a_n3x,float a_n3y 623 float a_n3x,float a_n3y,float a_n3z, 660 const colorf& a_color){ 624 const colorf& a_color){ 661 625 662 float p1x = a_p1x;float p1y = a_p1y;floa 626 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 627 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 628 float p3x = a_p3x;float p3y = a_p3y;float p3z = a_p3z;//float p3w = a_p3w; 665 629 666 m_this.m_vp_mtx.mul_3f_opt(p1x,p1y,p1z,m << 630 m_this.m_vp_mtx.mul_3f(p1x,p1y,p1z); 667 m_this.m_vp_mtx.mul_3f_opt(p2x,p2y,p2z,m << 631 m_this.m_vp_mtx.mul_3f(p2x,p2y,p2z); 668 m_this.m_vp_mtx.mul_3f_opt(p3x,p3y,p3z,m << 632 m_this.m_vp_mtx.mul_3f(p3x,p3y,p3z); 669 p1z *= -1; 633 p1z *= -1; 670 p2z *= -1; 634 p2z *= -1; 671 p3z *= -1; 635 p3z *= -1; 672 636 673 if(m_this.m_POLYGON_OFFSET_FILL){ 637 if(m_this.m_POLYGON_OFFSET_FILL){ 674 //note : gopaw pawex9,14,15,21 with "l 638 //note : gopaw pawex9,14,15,21 with "lego" (drawing cubes) are sensitive to the below epsil. 675 // zs are in [-1,1] 639 // zs are in [-1,1] 676 float epsil = 1e-4f; 640 float epsil = 1e-4f; 677 p1z -= epsil; 641 p1z -= epsil; 678 p2z -= epsil; 642 p2z -= epsil; 679 p3z -= epsil; 643 p3z -= epsil; 680 } 644 } 681 645 682 typedef zb::ZZ ZZ; //double 646 typedef zb::ZZ ZZ; //double 683 647 684 plane<vec3d> pn( 648 plane<vec3d> pn( 685 vec3<ZZ>(p1x,p1y,p1z), 649 vec3<ZZ>(p1x,p1y,p1z), 686 vec3<ZZ>(p2x,p2y,p2z), 650 vec3<ZZ>(p2x,p2y,p2z), 687 vec3<ZZ>(p3x,p3y,p3z) 651 vec3<ZZ>(p3x,p3y,p3z) 688 ); 652 ); 689 if(!pn.is_valid()) return true; 653 if(!pn.is_valid()) return true; 690 654 691 // norm[0]*x+norm[1]*y+norm[2]*z = dist 655 // norm[0]*x+norm[1]*y+norm[2]*z = dist 692 // A*x+B*y+C*z+D = 0 656 // A*x+B*y+C*z+D = 0 693 657 694 ZZ C = pn.normal()[2]; 658 ZZ C = pn.normal()[2]; 695 659 696 if(m_this.m_CULL_FACE){ 660 if(m_this.m_CULL_FACE){ 697 if(m_this.m_ccw) { 661 if(m_this.m_ccw) { 698 if(C<=0) return true; 662 if(C<=0) return true; 699 } else { 663 } else { 700 if(C>=0) return true; 664 if(C>=0) return true; 701 } 665 } 702 } 666 } 703 667 704 ZZ A = pn.normal()[0]; 668 ZZ A = pn.normal()[0]; 705 ZZ B = pn.normal()[1]; 669 ZZ B = pn.normal()[1]; 706 ZZ D = -pn.distance_from_origin(); 670 ZZ D = -pn.distance_from_origin(); 707 671 708 //ZZ zmn = mn<ZZ>(mn<ZZ>(p1z,p2z),p3z); 672 //ZZ zmn = mn<ZZ>(mn<ZZ>(p1z,p2z),p3z); 709 //ZZ zmx = mx<ZZ>(mx<ZZ>(p1z,p2z),p3z); 673 //ZZ zmx = mx<ZZ>(mx<ZZ>(p1z,p2z),p3z); 710 674 711 zb::point list[3]; 675 zb::point list[3]; 712 zinit(list[0],p1x,p1y,p1z); 676 zinit(list[0],p1x,p1y,p1z); 713 zinit(list[1],p2x,p2y,p2z); 677 zinit(list[1],p2x,p2y,p2z); 714 zinit(list[2],p3x,p3y,p3z); 678 zinit(list[2],p3x,p3y,p3z); 715 679 716 m_this.m_zb.set_depth_test(m_this.m_DEPT 680 m_this.m_zb.set_depth_test(m_this.m_DEPTH_TEST); 717 m_this.m_zb.set_blend(m_this.m_blend); 681 m_this.m_zb.set_blend(m_this.m_blend); 718 682 719 colorf frag_color = a_color; 683 colorf frag_color = a_color; 720 684 721 if(m_this.m_light_on) { // same logic a 685 if(m_this.m_light_on) { // same logic as toolx/wasm/webgl.js: 722 686 723 float nx = (a_n1x+a_n2x+a_n3x)/3.0f; 687 float nx = (a_n1x+a_n2x+a_n3x)/3.0f; 724 float ny = (a_n1y+a_n2y+a_n3y)/3.0f; 688 float ny = (a_n1y+a_n2y+a_n3y)/3.0f; 725 float nz = (a_n1z+a_n2z+a_n3z)/3.0f; 689 float nz = (a_n1z+a_n2z+a_n3z)/3.0f; 726 690 727 m_this.m_normal_matrix.mul_dir_3f_opt( << 691 m_this.m_normal_matrix.mul_dir_3f(nx,ny,nz); 728 692 729 vec3f _normal(nx,ny,nz);_normal.normal 693 vec3f _normal(nx,ny,nz);_normal.normalize(); 730 694 731 float _dot = _normal.dot(m_this.m_ligh 695 float _dot = _normal.dot(m_this.m_light_direction); 732 696 733 if(_dot<0.0f) { 697 if(_dot<0.0f) { 734 _dot *= -1.0f; 698 _dot *= -1.0f; 735 699 736 colorf _tmp = m_this.m_light_color; 700 colorf _tmp = m_this.m_light_color; 737 _tmp *= _dot; 701 _tmp *= _dot; 738 // _tmp *= 1.4f; //to have same intens 702 // _tmp *= 1.4f; //to have same intensity as GL on desktops. 739 _tmp += m_this.m_light_ambient; 703 _tmp += m_this.m_light_ambient; 740 704 741 frag_color *= _tmp; 705 frag_color *= _tmp; 742 706 743 } else { 707 } else { 744 frag_color *= m_this.m_light_ambient 708 frag_color *= m_this.m_light_ambient; 745 } 709 } 746 710 747 frag_color.clamp(); 711 frag_color.clamp(); 748 frag_color.set_a(a_color.a()); 712 frag_color.set_a(a_color.a()); 749 713 750 } 714 } 751 715 752 zb::buffer::ZPixel pix; 716 zb::buffer::ZPixel pix; 753 color2pix(frag_color,pix); 717 color2pix(frag_color,pix); 754 m_this.m_zb.draw_polygon(3,list,A,B,C,D, 718 m_this.m_zb.draw_polygon(3,list,A,B,C,D,pix); 755 719 756 return true; 720 return true; 757 } 721 } 758 protected: 722 protected: 759 zb_action& m_this; 723 zb_action& m_this; 760 private: //optimize: << 761 float m_tmp[3]; << 762 }; 724 }; 763 725 764 protected: 726 protected: 765 zb_manager& m_mgr; 727 zb_manager& m_mgr; 766 mat4f m_vp_mtx; 728 mat4f m_vp_mtx; 767 zb::buffer m_zb; 729 zb::buffer m_zb; 768 primvis m_pv; 730 primvis m_pv; 769 colorf m_light_color; 731 colorf m_light_color; 770 colorf m_light_ambient; 732 colorf m_light_ambient; 771 vec3f m_light_direction; 733 vec3f m_light_direction; 772 vec3f m_normal; 734 vec3f m_normal; 773 735 774 // to be restored in restore_state() : 736 // to be restored in restore_state() : 775 mat4f m_proj; 737 mat4f m_proj; 776 mat4f m_model; 738 mat4f m_model; 777 mat4f m_normal_matrix; 739 mat4f m_normal_matrix; 778 colorf m_rgba; 740 colorf m_rgba; 779 bool m_ccw; 741 bool m_ccw; 780 bool m_POLYGON_OFFSET_FILL; 742 bool m_POLYGON_OFFSET_FILL; 781 bool m_CULL_FACE; 743 bool m_CULL_FACE; 782 bool m_POINT_SMOOTH; 744 bool m_POINT_SMOOTH; 783 bool m_LINE_SMOOTH; 745 bool m_LINE_SMOOTH; 784 float m_line_width; 746 float m_line_width; 785 float m_point_size; 747 float m_point_size; 786 bool m_light_on; 748 bool m_light_on; 787 bool m_DEPTH_TEST; 749 bool m_DEPTH_TEST; 788 bool m_blend; 750 bool m_blend; 789 private: //optimize: << 790 float m_tmp[4]; << 791 }; 751 }; 792 752 793 }} 753 }} 794 754 795 #endif 755 #endif