Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

FileRenderer.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: FileRenderer.h,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.115 $      $Date: 2012/03/13 18:41:55 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *
00019  * The FileRenderer class implements the data and functions needed to 
00020  * render a scene to a file in some format (postscript, raster3d, etc.)
00021  *
00022  ***************************************************************************/
00023 #ifndef FILERENDERER_H
00024 #define FILERENDERER_H
00025 
00026 #include <stdio.h>
00027 
00028 #include "DisplayDevice.h"
00029 #include "Scene.h"
00030 #include "NameList.h"
00031 #include "Inform.h"
00032 
00033 #define FILERENDERER_NOWARNINGS    0
00034 #define FILERENDERER_NOMISCFEATURE 1
00035 #define FILERENDERER_NOCLIP        2
00036 #define FILERENDERER_NOCUEING      4
00037 #define FILERENDERER_NOTEXTURE     8
00038 #define FILERENDERER_NOGEOM       16
00039 #define FILERENDERER_NOTEXT       32
00040 
00044 class FileRenderer : public DisplayDevice {
00045 protected:
00046   char *publicName;         
00047   char *publicPrettyName;   
00048   char *defaultFilename;    
00049   char *defaultCommandLine; 
00050 
00051   char *execCmd;     
00052   FILE *outfile;     
00053   int isOpened;      
00054   char *my_filename; 
00055   int has_aa;        
00056   int aasamples;     
00057   int aosamples;     
00058   int has_imgsize;   
00059 
00060   int warningflags;  
00061 
00062 
00063   int imgwidth, imgheight;  
00064   float aspectratio;        
00065   NameList<int> formats;    
00066   int curformat;     
00067 
00068   float textoffset_x; 
00069   float textoffset_y; 
00070 
00073   virtual void update_exec_cmd() {}
00074 
00076   struct LightState {
00077     float color[3];             
00078     float pos[3];               
00079     int on;                     
00080   };
00081   LightState lightState[DISP_LIGHTS]; 
00082 
00084   struct AdvancedLightState {
00085     float color[3];             
00086     float pos[3];               
00087     float constfactor;          
00088     float linearfactor;         
00089     float quadfactor;           
00090     float spotdir[3];           
00091     float fallstart;            
00092     float fallend;              
00093     int spoton;                 
00094     int on;                     
00095   };
00096   AdvancedLightState advLightState[DISP_LIGHTS]; 
00097 
00099   float matData[MAXCOLORS][3];
00100   virtual void do_use_colors();
00101 
00103   float backColor[3];
00104 
00105   float backgradientenabled;      
00106   float backgradienttopcolor[3];  
00107   float backgradientbotcolor[3];  
00108  
00109 public:
00111   FileRenderer(const char *public_name, 
00112                const char *public_pretty_name,
00113                const char *default_file_name,
00114                const char *default_command_line);
00115   virtual ~FileRenderer(void);
00116 
00117   const char *visible_name(void) const { return publicName;}
00118   const char *pretty_name(void) const { return publicPrettyName;}
00119   const char *default_filename(void) const {return defaultFilename;}
00120   const char *default_exec_string(void) const {return defaultCommandLine;}
00121   const char *saved_exec_string(void) const { return execCmd; }
00122 
00123   void set_exec_string(const char *);
00124 
00126   int has_antialiasing() const { return has_aa; }
00127 
00129   int set_aasamples(int newval) {
00130     if (has_aa && (newval >= 0)) {
00131       aasamples = newval;
00132       update_exec_cmd();
00133     }
00134     return aasamples;
00135   }
00136 
00138   int set_aosamples(int newval) {
00139     if (newval >= 0) {
00140       aosamples = newval;
00141       update_exec_cmd();
00142     }
00143     return aosamples;
00144   }
00145 
00147   int has_imagesize() const { return has_imgsize; }
00148 
00153   int set_imagesize(int *w, int *h);
00154 
00157   float set_aspectratio(float aspect);
00158   
00160   int numformats() const { return formats.num(); }
00161   
00163   const char *format(int i) const { return formats.name(i); }
00164   const char *format() const { return formats.name(curformat); }
00165   int set_format(const char *format) {
00166     int ind = formats.typecode(format);
00167     if (ind < 0) return FALSE;
00168     if (curformat != ind) {
00169       curformat = ind;
00170       update_exec_cmd();
00171     }
00172     return TRUE;
00173   }
00174 
00176   virtual void set_background(const float *);
00177 
00179   virtual void set_backgradient(const float *top, const float *bot);
00180 
00185   virtual int open_file(const char *filename);
00186 
00187   virtual int do_define_light(int n, float *color, float *position);
00188   virtual int do_activate_light(int n, int turnon);
00189 
00190   virtual int do_define_adv_light(int n, float *color, float *position,
00191                                   float constant, float linear, float quad,
00192                                   float *spotdir, float fallstart, 
00193                                   float fallend, int spoton); 
00194   virtual int do_activate_adv_light(int n, int turnon);
00195 
00196 private:
00197   int sph_nverts;   
00198   float *sph_verts; 
00199 
00200 protected:
00202   virtual void write_header(void) {};
00203   void reset_state(void);
00204 
00205 public:
00206   virtual int prepare3D(int); 
00207   virtual void render(const VMDDisplayList *); // render the display list
00208 
00209 protected:
00211   virtual void write_trailer(void) {};
00212 
00216   virtual void close_file(void);
00217 
00218 public:
00220   virtual void update(int) {
00221     if (isOpened) {
00222       write_trailer();
00223       close_file();
00224       isOpened = FALSE;
00225   
00226       // Emit any pending warning messages for missing or unsupported
00227       // geometric primitives.
00228       if (warningflags & FILERENDERER_NOCLIP)
00229         msgWarn << "User-defined clipping planes not exported for this renderer" << sendmsg;
00230 
00231       if (warningflags & FILERENDERER_NOTEXT)
00232         msgWarn << "Text not exported for this renderer" << sendmsg;
00233 
00234       if (warningflags & FILERENDERER_NOTEXTURE)
00235         msgWarn << "Texture mapping not exported for this renderer" << sendmsg;
00236 
00237       if (warningflags & FILERENDERER_NOCUEING)
00238         msgWarn << "Depth cueing not exported for this renderer" << sendmsg;
00239 
00240       if (warningflags & FILERENDERER_NOGEOM)
00241         msgWarn << "One or more geometry types not exported for this renderer" << sendmsg;
00242 
00243       if (warningflags != FILERENDERER_NOWARNINGS)
00244         msgWarn << "Unimplemented features may negatively affect the appearance of the scene" << sendmsg;
00245     }
00246   }
00247 
00248 protected:
00250   // (for those that do not want to take care of it themselves)
00251   // the 'super_' version is called by render to set the matrix.  It
00252   // then calls the non-super version
00253   Stack<Matrix4> transMat;
00254   void super_load(float *cmdptr);
00255   virtual void load(const Matrix4& /*mat*/) {}
00256   void super_multmatrix(const float *cmdptr);
00257   virtual void multmatrix(const Matrix4& /*mat*/) {}
00258   void super_translate(float *cmdptr);
00259   virtual void translate(float /*x*/, float /*y*/, float /*z*/) {}
00260   void super_rot(float *cmdptr);
00261   virtual void rot(float /*ang*/, char /*axis*/) {}
00262   void super_scale(float *cmdptr);
00263   void super_scale(float);
00264   virtual void scale(float /*scalex*/, float /*scaley*/, 
00265                      float /*scalez*/) {}
00266 
00267   float scale_factor(void);         
00268 
00269   float scale_radius(float);        
00270 
00271   // change the color definitions
00272   int colorIndex;                   
00273   void super_set_color(int index);  
00274   virtual void set_color(int) {}    
00275   
00280   int nearest_index(float r, float g, float b) const;
00281 
00282   // change the material definition
00283   int materialIndex;                    
00284   float mat_ambient;                    
00285   float mat_diffuse;                    
00286   float mat_specular;                   
00287   float mat_shininess;                  
00288   float mat_opacity;                    
00289   float mat_outline;                    
00290   float mat_outlinewidth;               
00291   float mat_transmode;                  
00292   void super_set_material(int index);   
00293   virtual void set_material(int) {}     
00294 
00295   float clip_center[VMD_MAX_CLIP_PLANE][3]; 
00296   float clip_normal[VMD_MAX_CLIP_PLANE][3]; 
00297   float clip_color[VMD_MAX_CLIP_PLANE][3];  
00298   int clip_mode[VMD_MAX_CLIP_PLANE];        
00299 
00300   virtual void start_clipgroup();       
00301   virtual void end_clipgroup() {}       
00302 
00303   // change the line definitions
00304   int lineWidth, lineStyle, pointSize;
00305   virtual void set_line_width(int new_width) {
00306     lineWidth = new_width;
00307   }
00308   virtual void set_line_style(int /*new_style*/) {}  
00309 
00310   // change the sphere definitions
00311   int sphereResolution, sphereStyle;
00312   virtual void set_sphere_res(int /*res*/) {}        
00313   virtual void set_sphere_style(int /*style*/) {}    
00314 
00315   int materials_on;
00316   void super_materials(int on_or_off);
00317   virtual void activate_materials(void) {}           
00318   virtual void deactivate_materials(void) {}         
00319   
00320 
00322 
00324   virtual void cone(float * xyz1, float * xyz2, float radius, int resolution) { 
00325     cone(xyz1, xyz2, radius, 0.0, resolution);
00326   }
00327 
00329   virtual void cone(float * /*xyz1*/, float * /*xyz2*/, 
00330                     float /* radius*/, float /* radius2 */, int /*resolution*/);
00331 
00332 
00334   virtual void cylinder(float * base, float * apex, float radius, int filled);
00335 
00336 
00338   virtual void line(float * a, float * b);
00339 
00341   virtual void line_array(int num, float thickness, float *points);
00342 
00344   virtual void polyline_array(int num, float thickness, float *points);
00345 
00346 
00348   virtual void point(float * xyz) {
00349     float xyzr[4];
00350     vec_copy(xyzr, xyz);
00351     xyzr[3] = lineWidth * 0.002f; // hack for renderers that don't have points
00352   }
00353 
00355   virtual void point_array(int num, float size, float *xyz, float *colors);
00356 
00358   virtual void point_array_lit(int num, float size, 
00359                                float *xyz, float *norm, float *colors);
00360 
00361 
00363   virtual void sphere(float * xyzr);
00364 
00366   virtual void sphere_array(int num, int res, float *centers, float *radii, float *colors);
00367 
00368 
00370   virtual void square(float * norm, float * a, float * b, 
00371                       float * c, float * d) {
00372     // draw as two triangles, with correct winding order etc
00373     triangle(a, b, c, norm, norm, norm);
00374     triangle(a, c, d, norm, norm, norm);
00375   }
00376 
00377 
00379   virtual void triangle(const float * /*xyz1*/, const float * /*xyz2*/, const float * /*xyz3*/, 
00380                         const float * /*n1*/, const float * /*n2*/, const float * /*n3*/) {
00381     warningflags |= FILERENDERER_NOGEOM; // no triangles written
00382   }
00383 
00384 
00386   virtual void tricolor(const float * xyz1, const float * xyz2, const float * xyz3, 
00387                         const float * n1, const float * n2, const float * n3,
00388                         const float *c1, const float *c2, const float *c3) {
00389     int index = 1;
00390     float r, g, b;
00391     r = (c1[0] + c2[0] + c3[0]) / 3.0f; // average three vertex colors 
00392     g = (c1[1] + c2[1] + c3[1]) / 3.0f;
00393     b = (c1[2] + c2[2] + c3[2]) / 3.0f;
00394 
00395     index = nearest_index(r,g,b); // lookup nearest color here.
00396     super_set_color(index); // use the closest color
00397 
00398     triangle(xyz1, xyz2, xyz3, n1, n2, n3); // draw a regular triangle   
00399   }
00400 
00402   virtual void trimesh_n3v3(float *n, float *v, int numfacets) { 
00403     int i;
00404     for (i=0; i<numfacets*9; i+=9) {
00405       triangle(v + i    , 
00406                v + i + 3, 
00407                v + i + 6,
00408                n + i    , 
00409                n + i + 3, 
00410                n + i + 6); 
00411     }           
00412   }
00413 
00415   virtual void trimesh_c3n3v3(float *c, float *n, float *v, int numfacets) { 
00416     int i;
00417     for (i=0; i<numfacets*9; i+=9) {
00418       tricolor(v + i    ,
00419                v + i + 3, 
00420                v + i + 6, 
00421                n + i    , 
00422                n + i + 3, 
00423                n + i + 6, 
00424                c + i    , 
00425                c + i + 3, 
00426                c + i + 6);
00427     }           
00428   }
00429 
00431   virtual void trimesh_c4n3v3(int /* numverts */, float * cnv, 
00432                               int numfacets, int * facets) { 
00433     int i;
00434     for (i=0; i<numfacets*3; i+=3) {
00435       int v0 = facets[i    ] * 10;
00436       int v1 = facets[i + 1] * 10;
00437       int v2 = facets[i + 2] * 10;
00438       tricolor(cnv + v0 + 7, // vertices 0, 1, 2
00439                cnv + v1 + 7, 
00440                cnv + v2 + 7,
00441                cnv + v0 + 4, // normals 0, 1, 2
00442                cnv + v1 + 4, 
00443                cnv + v2 + 4,
00444                cnv + v0,     // colors 0, 1, 2
00445                cnv + v1, 
00446                cnv + v2);
00447     }           
00448   }
00449 
00450 
00452   virtual void trimesh_singlecolor(int cindex, int /* numverts */, float * nv, 
00453                                    int numfacets, int * facets) { 
00454     super_set_color(cindex); // set current color
00455 
00456     int i;
00457     for (i=0; i<numfacets*3; i+=3) {
00458       int v0 = facets[i    ] * 6;
00459       int v1 = facets[i + 1] * 6; 
00460       int v2 = facets[i + 2] * 6;
00461       triangle(nv + v0 + 3, // vertices 0, 1, 2
00462                nv + v1 + 3, 
00463                nv + v2 + 3,
00464                nv + v0,     // normals 0, 1, 2
00465                nv + v1, 
00466                nv + v2);
00467     }           
00468   }
00469 
00470 
00472   virtual void tristrip(int /* numverts */, const float * cnv, 
00473                         int numstrips, const int *vertsperstrip, 
00474                         const int *facets) { 
00475     // render triangle strips one triangle at a time
00476     // triangle winding order is:
00477     //   v0, v1, v2, then v2, v1, v3, then v2, v3, v4, etc.
00478     int strip, t, v = 0;
00479     int stripaddr[2][3] = { {0, 1, 2}, {1, 0, 2} };
00480  
00481     // loop over all of the triangle strips
00482     for (strip=0; strip < numstrips; strip++) {       
00483       // loop over all triangles in this triangle strip
00484       for (t = 0; t < (vertsperstrip[strip] - 2); t++) {
00485         // render one triangle, using lookup table to fix winding order
00486         int v0 = facets[v + (stripaddr[t & 0x01][0])] * 10;
00487         int v1 = facets[v + (stripaddr[t & 0x01][1])] * 10;
00488         int v2 = facets[v + (stripaddr[t & 0x01][2])] * 10;
00489  
00490         tricolor(cnv + v0 + 7, // vertices 0, 1, 2
00491                  cnv + v1 + 7, 
00492                  cnv + v2 + 7,
00493                  cnv + v0 + 4, // normals 0, 1, 2
00494                  cnv + v1 + 4, 
00495                  cnv + v2 + 4,
00496                  cnv + v0,     // colors 0, 1, 2
00497                  cnv + v1, 
00498                  cnv + v2);
00499         v++; // move on to next vertex
00500       }
00501       v+=2; // last two vertices are already used by last triangle
00502     }
00503   }
00504 
00505 
00508   virtual void tristrip_singlecolor(int /* numverts */, const float * nv, 
00509                                     int numstrips, const int *stripcolindex,
00510                                     const int *vertsperstrip, const int *facets) { 
00511     // render triangle strips one triangle at a time
00512     // triangle winding order is:
00513     //   v0, v1, v2, then v2, v1, v3, then v2, v3, v4, etc.
00514     int strip, t, v = 0;
00515     int stripaddr[2][3] = { {0, 1, 2}, {1, 0, 2} };
00516  
00517     // loop over all of the triangle strips
00518     for (strip=0; strip < numstrips; strip++) {       
00519       super_set_color(stripcolindex[strip]); // set current color
00520       
00521       // loop over all triangles in this triangle strip
00522       for (t = 0; t < (vertsperstrip[strip] - 2); t++) {
00523         // render one triangle, using lookup table to fix winding order
00524         int v0 = facets[v + (stripaddr[t & 0x01][0])] * 6;
00525         int v1 = facets[v + (stripaddr[t & 0x01][1])] * 6;
00526         int v2 = facets[v + (stripaddr[t & 0x01][2])] * 6;
00527  
00528         triangle(nv + v0 + 3, // vertices 0, 1, 2
00529                  nv + v1 + 3, 
00530                  nv + v2 + 3,
00531                  nv + v0, // normals 0, 1, 2
00532                  nv + v1, 
00533                  nv + v2);
00534         v++; // move on to next vertex
00535       }
00536       v+=2; // last two vertices are already used by last triangle
00537     }
00538   }
00539 
00540 
00543   virtual void trifan_singlecolor(int /* numverts */, const float * nv,
00544                                   int numfans, const int *fancolindex,
00545                                   const int *vertsperfan, const int *facets) {
00546     // render triangle fans one triangle at a time
00547     // triangle winding order is:
00548     //   v0, v1, v2, then v0, v2, v3, then v0, v3, v4, etc.
00549     int fan, t, v = 0;
00550 
00551     // loop over all of the triangle fans
00552     for (fan=0; fan < numfans; fan++) {
00553       super_set_color(fancolindex[fan]); // set current color
00554 
00555       // loop over all triangles in this triangle fan
00556       int v0 = facets[v] * 6;
00557       v++;
00558       for (t = 1; t < (vertsperfan[fan] - 1); t++) {
00559         // render one triangle with correct winding order
00560         int v1 = facets[v    ] * 6;
00561         int v2 = facets[v + 1] * 6;
00562 
00563         triangle(nv + v0 + 3, // vertices 0, 1, 2
00564                  nv + v1 + 3,
00565                  nv + v2 + 3,
00566                  nv + v0, // normals 0, 1, 2
00567                  nv + v1,
00568                  nv + v2);
00569         v++; // move on to next vertex
00570       }
00571       v++; // last vertex is already used by last triangle
00572     }
00573   }
00574 
00575 
00577   virtual void define_volume_texture(int ID, int xs, int ys, int zs,
00578                                      const float *xplaneeq, 
00579                                      const float *yplaneeq,
00580                                      const float *zplaneeq,
00581                                      unsigned char *texmap) {
00582     warningflags |= FILERENDERER_NOTEXTURE;
00583   }
00584 
00585 
00587   virtual void volume_texture_on(int texmode) {
00588     warningflags |= FILERENDERER_NOTEXTURE;
00589   }
00590 
00591 
00593   virtual void volume_texture_off(void) {
00594     warningflags |= FILERENDERER_NOTEXTURE;
00595   }
00596 
00597 
00599   virtual void wiremesh(int /* numverts */, float * cnv, 
00600                        int numlines, int * lines) { 
00601     int i;
00602     int index = 1;
00603 
00604     for (i=0; i<numlines; i++) {
00605       float r, g, b;
00606       int ind = i * 2;
00607       int v0 = lines[ind    ] * 10;
00608       int v1 = lines[ind + 1] * 10;
00609 
00610       r = cnv[v0 + 0] + cnv[v1 + 0] / 2.0f;
00611       g = cnv[v0 + 1] + cnv[v1 + 1] / 2.0f;
00612       b = cnv[v0 + 2] + cnv[v1 + 2] / 2.0f;
00613 
00614       index = nearest_index(r,g,b); // lookup nearest color here.
00615       super_set_color(index); // use the closest color
00616 
00617       line(cnv + v0 + 7, cnv + v1 + 7); 
00618     }           
00619   }
00620 
00624   virtual void beginrepgeomgroup(const char *) {}
00625 
00627   virtual void comment(const char *) {}
00628 
00630   virtual void text(float *pos, float size, float thickness, const char *str);
00631 
00633   virtual void pick_point(float * /*xyz*/, int /*id*/) {}
00634 
00635 };
00636 
00637 #endif
00638 

Generated on Sat May 26 01:47:58 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002