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