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

ArtDisplayDevice.C

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: ArtDisplayDevice.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.34 $       $Date: 2011/02/24 17:24:44 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Writes to the ART raytracer.  This is available from gondwana.ecr.mu.oz.au
00019  *   as part of the vort package.  To see the output I suggest:
00020  *     art plot.scn 1000 1000
00021  *     vort2ppm plot.pix > plot.ppm
00022  *     fromppm plot.ppm plot.rgb
00023  *     ipaste plot.rgb
00024  *
00025  * NOTE: As of 2011, the original VORT package home page has vanished, 
00026  *       but it can currently be found here:
00027  *         http://bund.com.au/~dgh/eric/
00028  *         http://bund.com.au/~dgh/eric/vort.tar.gz
00029  *         http://bund.com.au/~dgh/eric/artimages/index.html
00030  *
00031  ***************************************************************************/
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <math.h>
00035 #include "ArtDisplayDevice.h"
00036 #include "Matrix4.h"
00037 #include "DispCmds.h"
00038 #include "Inform.h"
00039 #include "utilities.h"
00040 
00041 #define DEFAULT_RADIUS 0.002f
00042 #define DASH_LENGTH 0.02f
00043 
00044 // Be careful when you modify the coordinates.  To make things view the
00045 // right way, I have to rotate everything around the (x,y,z) = (1,1,1)
00046 // vector so that x->z, y->x, and z->y
00047 
00048 #define ORDER(x,y,z) -z, -x, y
00049 //#define ORDER(x,y,z) x,y,z
00050 
00052 
00053 // constructor ... initialize some variables
00054 ArtDisplayDevice::ArtDisplayDevice() 
00055 : FileRenderer("ART", "ART (VORT ray tracer)", "vmdscene.scn", "art %s 500 650"){ }
00056 
00057 //destructor
00058 ArtDisplayDevice::~ArtDisplayDevice(void) { }
00059 
00061 
00062 // draw a point
00063 void ArtDisplayDevice::point(float * spdata) {
00064   float vec[3];
00065 
00066   // transform the world coordinates
00067   (transMat.top()).multpoint3d(spdata, vec);
00068    
00069   // draw the sphere
00070   fprintf(outfile, "sphere {\ncolour %f,%f,%f\n",
00071           matData[colorIndex][0],
00072           matData[colorIndex][1],
00073           matData[colorIndex][2]);
00074 
00075   fprintf(outfile, "radius %f\n", float(lineWidth) * DEFAULT_RADIUS);
00076   fprintf(outfile, "center (%f,%f,%f)\n}\n", ORDER(vec[0], vec[1], vec[2]));
00077 }
00078 
00079 // draw a sphere
00080 void ArtDisplayDevice::sphere(float * spdata) {
00081   float vec[3];
00082   float radius;
00083     
00084   // transform the world coordinates
00085   (transMat.top()).multpoint3d(spdata, vec);
00086   radius = scale_radius(spdata[3]);
00087    
00088   // draw the sphere
00089   fprintf(outfile, "sphere {\ncolour %f,%f,%f\n",
00090           matData[colorIndex][0],
00091           matData[colorIndex][1],
00092           matData[colorIndex][2]);
00093 
00094   fprintf(outfile, "radius %f\n", radius);
00095   fprintf(outfile, "center (%f,%f,%f)\n}\n", ORDER(vec[0], vec[1], vec[2]));
00096 }
00097 
00098 
00099 // draw a line (cylinder) from a to b
00100 void ArtDisplayDevice::line(float *a, float *b) {
00101   int i, j, test;
00102   float dirvec[3], unitdirvec[3];
00103   float from[3], to[3], tmp1[3], tmp2[3];
00104  
00105   if (lineStyle == ::SOLIDLINE) {
00106     // transform the world coordinates
00107     (transMat.top()).multpoint3d(a, from);
00108     (transMat.top()).multpoint3d(b, to);
00109     
00110     // draw the cylinder
00111     fprintf(outfile, "cylinder {\n");
00112     fprintf(outfile, "colour %f,%f,%f\n",
00113            matData[colorIndex][0],
00114            matData[colorIndex][1],
00115            matData[colorIndex][2]);
00116     fprintf(outfile, "center(%f,%f,%f)\n", 
00117             ORDER(from[0], from[1], from[2])); // first point
00118     fprintf(outfile, "center(%f,%f,%f)\n", 
00119             ORDER(to[0], to[1], to[2])); // second point
00120     fprintf(outfile, "radius %f\n}\n", 
00121             float(lineWidth)*DEFAULT_RADIUS); // radius
00122         
00123   } else if (lineStyle == ::DASHEDLINE) {
00124      // transform the world coordinates
00125     (transMat.top()).multpoint3d(a, tmp1);
00126     (transMat.top()).multpoint3d(b, tmp2);
00127 
00128     // how to create a dashed line
00129     vec_sub(dirvec, tmp2, tmp1);  // vector from a to b
00130     vec_copy(unitdirvec, dirvec);
00131     vec_normalize(unitdirvec);    // unit vector from a to b
00132     test = 1;
00133     i = 0;
00134     while (test == 1) {
00135       for (j=0; j<3; j++) {
00136         from[j] = (float) (tmp1[j] + (2*i    )*DASH_LENGTH*unitdirvec[j]);
00137           to[j] = (float) (tmp1[j] + (2*i + 1)*DASH_LENGTH*unitdirvec[j]);
00138       }
00139       if (fabsf(tmp1[0] - to[0]) >= fabsf(dirvec[0])) {
00140         vec_copy(to, tmp2);
00141         test = 0;
00142       }
00143     
00144       // draw the cylinder
00145       fprintf(outfile, "cylinder {\n");
00146       fprintf(outfile, "colour %f,%f,%f\n",
00147               matData[colorIndex][0],
00148               matData[colorIndex][1],
00149               matData[colorIndex][2]);
00150 
00151       // first point
00152       fprintf(outfile, "center(%f,%f,%f)\n", ORDER(from[0], from[1], from[2]));
00153       // second point
00154       fprintf(outfile, "center(%f,%f,%f)\n", ORDER(to[0], to[1], to[2])); 
00155       // radius
00156       fprintf(outfile, "radius %f\n}\n", float(lineWidth)*DEFAULT_RADIUS); 
00157       i++;
00158     }
00159   } else {
00160     msgErr << "ArtDisplayDevice: Unknown line style " << lineStyle << sendmsg;
00161   }
00162 }
00163 
00164 // draw a cylinder
00165 void ArtDisplayDevice::cylinder(float *a, float *b, float r,int /*filled*/) {
00166 
00167   float vec1[3], vec2[3];
00168   
00169   // transform the world coordinates
00170   (transMat.top()).multpoint3d(a, vec1);
00171   (transMat.top()).multpoint3d(b, vec2);
00172     
00173   // draw the cylinder
00174   fprintf(outfile, "cylinder {\n");
00175   fprintf(outfile, "colour %f,%f,%f\n",
00176           matData[colorIndex][0],
00177           matData[colorIndex][1],
00178           matData[colorIndex][2]);
00179   // first point
00180   fprintf(outfile, "center(%f,%f,%f)\n", ORDER(vec1[0], vec1[1], vec1[2]));
00181   // second point
00182   fprintf(outfile, "center(%f,%f,%f)\n", ORDER(vec2[0], vec2[1], vec2[2])); 
00183   // radius
00184   fprintf(outfile, "radius %f\n}\n", scale_radius(r));
00185 }
00186 
00187 // draw a cone
00188 void ArtDisplayDevice::cone(float *a, float *b, float r) {
00189 
00190   float vec1[3], vec2[3];
00191   
00192   // transform the world coordinates
00193   (transMat.top()).multpoint3d(a, vec1);
00194   (transMat.top()).multpoint3d(b, vec2);
00195     
00196   fprintf(outfile, "cone {\n");
00197   fprintf(outfile, "colour %f,%f,%f\n",
00198           matData[colorIndex][0],
00199           matData[colorIndex][1],
00200           matData[colorIndex][2]);
00201   // second point
00202   fprintf(outfile, "vertex(%f,%f,%f)\n", ORDER(vec2[0], vec2[1], vec2[2])); 
00203   // first point
00204   fprintf(outfile, "center(%f,%f,%f)\n", ORDER(vec1[0], vec1[1], vec1[2]));
00205   // radius
00206   fprintf(outfile, "radius %f\n}\n", scale_radius(r));
00207 }
00208 
00209 // draw a triangle
00210 void ArtDisplayDevice::triangle(const float *a, const float *b, const float *c, const float *n1, 
00211 const float *n2, const float *n3) {
00212 
00213   float vec1[3], vec2[3], vec3[3];
00214   float nor1[3], nor2[3], nor3[3];
00215   
00216   // transform the world coordinates
00217   (transMat.top()).multpoint3d(a, vec1);
00218   (transMat.top()).multpoint3d(b, vec2);
00219   (transMat.top()).multpoint3d(c, vec3);
00220 
00221   // and the normals
00222   (transMat.top()).multnorm3d(n1, nor1);
00223   (transMat.top()).multnorm3d(n2, nor2);
00224   (transMat.top()).multnorm3d(n3, nor3);
00225 
00226   // draw the triangle
00227   fprintf(outfile, "polygon {\n");
00228   fprintf(outfile, "colour %f,%f,%f\n",
00229           matData[colorIndex][0],
00230           matData[colorIndex][1],
00231           matData[colorIndex][2]);
00232   
00233   fprintf(outfile, "vertex (%f,%f,%f),(%f,%f,%f)\n", 
00234           ORDER(vec1[0], vec1[1], vec1[2]), // point one
00235           ORDER(nor1[0], nor1[1], nor1[2]));
00236   
00237   fprintf(outfile, "vertex (%f,%f,%f),(%f,%f,%f)\n", 
00238           ORDER(vec2[0], vec2[1], vec2[2]), // point two
00239           ORDER(nor2[0], nor2[1], nor2[2]));
00240   fprintf(outfile, "vertex (%f,%f,%f),(%f,%f,%f)\n", 
00241           ORDER(vec3[0], vec3[1], vec3[2]), // point three
00242           ORDER(nor3[0], nor3[1], nor3[2]));
00243   fprintf(outfile, "}\n");
00244 }
00245 
00246 // draw a square
00247 void ArtDisplayDevice::square(float *, float *a, float *b, float *c, float *d) {
00248   
00249   float vec1[3], vec2[3], vec3[3], vec4[3];
00250   
00251   // transform the world coordinates
00252   (transMat.top()).multpoint3d(a, vec1);
00253   (transMat.top()).multpoint3d(b, vec2);
00254   (transMat.top()).multpoint3d(c, vec3);
00255   (transMat.top()).multpoint3d(d, vec4);
00256 
00257   // draw the square
00258   fprintf(outfile, "polygon {\n");
00259   fprintf(outfile, "colour %f,%f,%f\n", 
00260           matData[colorIndex][0],
00261           matData[colorIndex][1],
00262           matData[colorIndex][2]);
00263 
00264   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00265           ORDER(vec1[0], vec1[1], vec1[2])); // point one
00266   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00267           ORDER(vec2[0], vec2[1], vec2[2])); // point two
00268   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00269           ORDER(vec3[0], vec3[1], vec3[2])); // point three
00270   fprintf(outfile, "vertex (%f,%f,%f)\n", 
00271           ORDER(vec4[0], vec4[1], vec4[2])); // point four
00272   fprintf(outfile, "}\n");
00273   
00274 }
00275 
00276 void ArtDisplayDevice::comment(const char *s)
00277 {
00278   fprintf(outfile, "// %s\n", s);
00279 }
00280 
00282 
00283 // initialize the file for output
00284 void ArtDisplayDevice::write_header() {
00285 
00286     Initialized = TRUE;
00287 
00288     fprintf(outfile, "up(0, 0, 1) \n");
00289     fprintf(outfile, "lookat(-1, 0, 0, 0, 0, 0, 0)\n");
00290     fprintf(outfile, "fieldofview 45 \n");
00291     
00292 
00293     // write the light sources
00294     // The light code doesn't work because at this point I don't have the
00295     //  correct transformation matrix, so I'll leave only the one light source
00296     fprintf(outfile, "light {\n\tcolour 1, 1, 1\n"
00297             "\tlocation (-10, 0, 0)\n}\n");
00298 
00299     // set the background
00300     fprintf(outfile, "background %f, %f, %f\n", 
00301             backColor[0], backColor[1], backColor[2]);
00302 
00303     // everything is plastic-like
00304     fprintf(outfile, "material 0.0, 0.75, 0.25, 20.0\n");
00305 }
00306 
00307     
00308 // clean up after yourself
00309 void ArtDisplayDevice::write_trailer() {
00310     fprintf(outfile, "//End of tokens \n");
00311     msgInfo << "Art file generation finished" << sendmsg;
00312 }
00313 

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