Main Page   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-2008 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.86 $       $Date: 2008/03/27 19:36:39 $
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   // default parameters for this instance; these don't ever change.
00047   char *publicName, *defaultFilename, *defaultCommandLine;
00048   char *execCmd;     
00049   FILE *outfile;     
00050   int isOpened;      
00051   char *my_filename; 
00052   int has_aa;        
00053   int aalevel;       
00054   int has_imgsize;   
00055 
00056   int warningflags;  
00057 
00058 
00059   int imgwidth, imgheight;  
00060   float aspectratio;        
00061   NameList<int> formats;    
00062   int curformat;     
00063 
00066   virtual void update_exec_cmd() {}
00067 
00069   struct LightState {
00070     float color[3];             
00071     float pos[3];               
00072     int on;                     
00073   };
00074 
00075   LightState lightState[DISP_LIGHTS]; 
00076 
00078   float matData[MAXCOLORS][3];
00079   virtual void do_use_colors();
00080 
00082   float backColor[3];
00083  
00084 public:
00086   FileRenderer(const char *public_name, const char *default_file_name,
00087                const char *default_command_line);
00088   virtual ~FileRenderer(void);
00089 
00090   const char *visible_name(void) const { return publicName;}
00091   const char *default_filename(void) const {return defaultFilename;}
00092   const char *default_exec_string(void) const {return defaultCommandLine;}
00093   const char *saved_exec_string(void) const { return execCmd; }
00094 
00095   void set_exec_string(const char *);
00096 
00098   int has_antialiasing() const { return has_aa; }
00099 
00101   int set_aalevel(int newval) {
00102     if (has_aa && newval >= 0) {
00103       aalevel = newval;
00104       update_exec_cmd();
00105     }
00106     return aalevel;
00107   }
00108 
00110   int has_imagesize() const { return has_imgsize; }
00111 
00116   int set_imagesize(int *w, int *h);
00117 
00120   float set_aspectratio(float aspect);
00121   
00123   int numformats() const { return formats.num(); }
00124   
00126   const char *format(int i) const { return formats.name(i); }
00127   const char *format() const { return formats.name(curformat); }
00128   int set_format(const char *format) {
00129     int ind = formats.typecode(format);
00130     if (ind < 0) return FALSE;
00131     if (curformat != ind) {
00132       curformat = ind;
00133       update_exec_cmd();
00134     }
00135     return TRUE;
00136   }
00137 
00139   virtual void set_background(const float *);
00140 
00145   virtual int open_file(const char *filename);
00146 
00147   virtual int do_define_light(int n, float *color, float *position);
00148   virtual int do_activate_light(int n, int turnon);
00149 
00150 private:
00151   void reset_state(void);
00152   int sph_nverts;   
00153   float *sph_verts; 
00154 
00155 protected:
00157   virtual void write_header(void) {};
00158 
00159 public:
00160   virtual int prepare3D(int); 
00161   virtual void render(const VMDDisplayList *); // render the display list
00162 
00163 protected:
00165   virtual void write_trailer(void) {};
00166 
00170   virtual void close_file(void);
00171 
00172 public:
00174   virtual void update(int) {
00175     if (isOpened) {
00176       write_trailer();
00177       close_file();
00178       isOpened = FALSE;
00179   
00180       // Emit any pending warning messages for missing or unsupported
00181       // geometric primitives.
00182       if (warningflags & FILERENDERER_NOCLIP)
00183         msgWarn << "User-defined clipping planes not exported for this renderer" << sendmsg;
00184 
00185       if (warningflags & FILERENDERER_NOTEXT)
00186         msgWarn << "Text not exported for this renderer" << sendmsg;
00187 
00188       if (warningflags & FILERENDERER_NOTEXTURE)
00189         msgWarn << "Texture mapping not exported for this renderer" << sendmsg;
00190 
00191       if (warningflags & FILERENDERER_NOCUEING)
00192         msgWarn << "Depth cueing not exported for this renderer" << sendmsg;
00193 
00194       if (warningflags & FILERENDERER_NOGEOM)
00195         msgWarn << "One or more geometry types not exported for this renderer" << sendmsg;
00196 
00197       if (warningflags != FILERENDERER_NOWARNINGS)
00198         msgWarn << "Unimplemented features may negatively affect the appearance of the scene" << sendmsg;
00199     }
00200   }
00201 
00202 protected:
00204   float *dataBlock;
00205   
00207   // (for those that do not want to take care of it themselves)
00208   // the 'super_' version is called by render to set the matrix.  It
00209   // then calls the non-super version
00210   Stack<Matrix4> transMat;
00211   void super_load(float *cmdptr);
00212   virtual void load(const Matrix4& /*mat*/) {}
00213   void super_multmatrix(const float *cmdptr);
00214   virtual void multmatrix(const Matrix4& /*mat*/) {}
00215   void super_translate(float *cmdptr);
00216   virtual void translate(float /*x*/, float /*y*/, float /*z*/) {}
00217   void super_rot(float *cmdptr);
00218   virtual void rot(float /*ang*/, char /*axis*/) {}
00219   void super_scale(float *cmdptr);
00220   void super_scale(float);
00221   virtual void scale(float /*scalex*/, float /*scaley*/, 
00222                      float /*scalez*/) {}
00223   float scale_radius(float);        
00224 
00225   // change the color definitions
00226   int colorIndex;                   
00227   void super_set_color(int index);  
00228   virtual void set_color(int) {}    
00229   
00234   int nearest_index(float r, float g, float b) const;
00235 
00236   // change the material definition
00237   int materialIndex;                    
00238   float mat_ambient;                    
00239   float mat_diffuse;                    
00240   float mat_specular;                   
00241   float mat_shininess;                  
00242   float mat_opacity;                    
00243   void super_set_material(int index);   
00244   virtual void set_material(int) {}     
00245 
00246   float clip_center[VMD_MAX_CLIP_PLANE][3]; 
00247   float clip_normal[VMD_MAX_CLIP_PLANE][3]; 
00248   float clip_color[VMD_MAX_CLIP_PLANE][3];  
00249   int clip_mode[VMD_MAX_CLIP_PLANE];        
00250 
00251   virtual void start_clipgroup();       
00252   virtual void end_clipgroup() {}       
00253 
00254   // change the line definitions
00255   int lineWidth, lineStyle, pointSize;
00256   virtual void set_line_width(int new_width) {
00257     lineWidth = new_width;
00258   }
00259   virtual void set_line_style(int /*new_style*/) {}  
00260 
00261   // change the sphere definitions
00262   int sphereResolution, sphereStyle;
00263   virtual void set_sphere_res(int /*res*/) {}        
00264   virtual void set_sphere_style(int /*style*/) {}    
00265 
00266   int materials_on;
00267   void super_materials(int on_or_off);
00268   virtual void activate_materials(void) {}           
00269   virtual void deactivate_materials(void) {}         
00270   
00271 
00273 
00274   // single-radius cones (pointy top)
00275   virtual void cone(float * xyz1, float * xyz2, float radius, int resolution) { 
00276     cone(xyz1, xyz2, radius, 0.0, resolution);
00277   }
00278 
00279 
00280   // two radius cones
00281   virtual void cone(float * /*xyz1*/, float * /*xyz2*/, 
00282                     float /* radius*/, float /* radius2 */, int /*resolution*/);
00283 
00284 
00285   // cylinders, with optional caps
00286   virtual void cylinder(float * base, float * apex, float radius, int filled);
00287 
00288 
00289   // simple lines
00290   virtual void line(float * a, float * b);
00291 
00292 
00293   // simple points
00294   virtual void point(float * xyz) {
00295     float xyzr[4];
00296     vec_copy(xyzr, xyz);
00297     xyzr[3] = lineWidth * 0.002f; // hack for renderers that don't have points
00298   }
00299 
00300 
00301   // simple sphere
00302   virtual void sphere(float * xyzr);
00303 
00304 
00305   // quadrilateral
00306   virtual void square(float * norm, float * a, float * b, 
00307                       float * c, float * d) {
00308     // draw as two triangles, with correct winding order etc
00309     triangle(a, b, c, norm, norm, norm);
00310     triangle(a, c, d, norm, norm, norm);
00311   }
00312 
00313 
00314   // single color triangle with interpolated surface normals
00315   virtual void triangle(const float * /*xyz1*/, const float * /*xyz2*/, const float * /*xyz3*/, 
00316                         const float * /*n1*/, const float * /*n2*/, const float * /*n3*/) {
00317     warningflags |= FILERENDERER_NOGEOM; // no triangles written
00318   }
00319 
00320 
00321   // triangle with interpolated surface normals and vertex colors
00322   virtual void tricolor(const float * xyz1, const float * xyz2, const float * xyz3, 
00323                         const float * n1, const float * n2, const float * n3,
00324                         const float *c1, const float *c2, const float *c3) {
00325     int index = 1;
00326     float r, g, b;
00327     r = (c1[0] + c2[0] + c3[0]) / 3.0f; // average three vertex colors 
00328     g = (c1[1] + c2[1] + c3[1]) / 3.0f;
00329     b = (c1[2] + c2[2] + c3[2]) / 3.0f;
00330 
00331     index = nearest_index(r,g,b); // lookup nearest color here.
00332     super_set_color(index); // use the closest color
00333 
00334     triangle(xyz1, xyz2, xyz3, n1, n2, n3); // draw a regular triangle   
00335   }
00336 
00337 
00338   // triangle mesh built from a vertex array and facet vertex index arrays
00339   virtual void trimesh(int /* numverts */, float * cnv, 
00340                        int numfacets, int * facets) { 
00341     int i;
00342     for (i=0; i<numfacets*3; i+=3) {
00343       int v0 = facets[i    ] * 10;
00344       int v1 = facets[i + 1] * 10;
00345       int v2 = facets[i + 2] * 10;
00346       tricolor(cnv + v0 + 7, // vertices 0, 1, 2
00347                cnv + v1 + 7, 
00348                cnv + v2 + 7,
00349                cnv + v0 + 4, // normals 0, 1, 2
00350                cnv + v1 + 4, 
00351                cnv + v2 + 4,
00352                cnv + v0,     // colors 0, 1, 2
00353                cnv + v1, 
00354                cnv + v2);
00355     }           
00356   }
00357 
00358 
00359   // triangle strips built from a vertex array and vertex index arrays
00360   virtual void tristrip(int /* numverts */, const float * cnv, 
00361                         int numstrips, const int *vertsperstrip, 
00362                         const int *facets) { 
00363     // render triangle strips one triangle at a time
00364     // triangle winding order is:
00365     //   v0, v1, v2, then v2, v1, v3, then v2, v3, v4, etc.
00366     int strip, t, v = 0;
00367     int stripaddr[2][3] = { {0, 1, 2}, {1, 0, 2} };
00368  
00369     // loop over all of the triangle strips
00370     for (strip=0; strip < numstrips; strip++) {       
00371       // loop over all triangles in this triangle strip
00372       for (t = 0; t < (vertsperstrip[strip] - 2); t++) {
00373         // render one triangle, using lookup table to fix winding order
00374         int v0 = facets[v + (stripaddr[t & 0x01][0])] * 10;
00375         int v1 = facets[v + (stripaddr[t & 0x01][1])] * 10;
00376         int v2 = facets[v + (stripaddr[t & 0x01][2])] * 10;
00377  
00378         tricolor(cnv + v0 + 7, // vertices 0, 1, 2
00379                  cnv + v1 + 7, 
00380                  cnv + v2 + 7,
00381                  cnv + v0 + 4, // normals 0, 1, 2
00382                  cnv + v1 + 4, 
00383                  cnv + v2 + 4,
00384                  cnv + v0,     // colors 0, 1, 2
00385                  cnv + v1, 
00386                  cnv + v2);
00387         v++; // move on to next vertex
00388       }
00389       v+=2; // last two vertices are already used by last triangle
00390     }
00391   }
00392 
00393 
00394   // define a volumetric texture map
00395   virtual void define_volume_texture(int ID, int xs, int ys, int zs,
00396                                      const float *xplaneeq, 
00397                                      const float *yplaneeq,
00398                                      const float *zplaneeq,
00399                                      unsigned char *texmap) {
00400     warningflags |= FILERENDERER_NOTEXTURE;
00401   }
00402 
00403 
00404   // enable volumetric texturing, either in "replace" or "modulate" mode
00405   virtual void volume_texture_on(int texmode) {
00406     warningflags |= FILERENDERER_NOTEXTURE;
00407   }
00408 
00409 
00410   // disable volumetric texturing
00411   virtual void volume_texture_off(void) {
00412     warningflags |= FILERENDERER_NOTEXTURE;
00413   }
00414 
00415 
00416   // wire mesh built from a vertex array and an vertex index array
00417   virtual void wiremesh(int /* numverts */, float * cnv, 
00418                        int numlines, int * lines) { 
00419     int i;
00420     int index = 1;
00421 
00422     for (i=0; i<numlines; i++) {
00423       float r, g, b;
00424       int ind = i * 2;
00425       int v0 = lines[ind    ] * 10;
00426       int v1 = lines[ind + 1] * 10;
00427 
00428       r = cnv[v0 + 0] + cnv[v1 + 0] / 2.0f;
00429       g = cnv[v0 + 1] + cnv[v1 + 1] / 2.0f;
00430       b = cnv[v0 + 2] + cnv[v1 + 2] / 2.0f;
00431 
00432       index = nearest_index(r,g,b); // lookup nearest color here.
00433       super_set_color(index); // use the closest color
00434 
00435       line(cnv + v0 + 7, cnv + v1 + 7); 
00436     }           
00437   }
00438 
00439 
00440   virtual void text(const char *);
00441   virtual void comment(const char *) {}
00442 
00443   float textpos[3];
00444   void super_text_position(float x, float y, float z);
00445   virtual void text_position(float /* x*/, float /* y*/, float /* z*/) {
00446     warningflags |= FILERENDERER_NOTEXT;
00447   }
00448 
00450   virtual void pick_point(float * /*xyz*/, int /*id*/) {}
00451 
00452 };
00453 
00454 #endif
00455 

Generated on Mon Oct 13 01:27:32 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002