Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/externals/g4tools/include/tools/sg/gl2ps_action

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/g4tools/include/tools/sg/gl2ps_action (Version 11.3.0) and /externals/g4tools/include/tools/sg/gl2ps_action (Version 9.3)


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