home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / structf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-17  |  13.4 KB  |  466 lines

  1. /**********************************************************************/
  2. /* struct.c                                                           */
  3. /*                                                                    */
  4. /* Routines to print out structures, and outputting final scene.      */
  5. /*                                                                    */
  6. /**********************************************************************/
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "geo.h"
  10. #include "misc.h"
  11. #include "io.h"
  12. #include "struct.h"
  13. #include "rad.h"
  14. #include "adj.h"
  15.  
  16. extern Vertex_tree *vtree;
  17. extern int vtree_size;
  18. extern RadParams ReadLog;
  19. extern void BoundsPrint();
  20. extern void Poly_VertexB();
  21. extern OptionType Option;
  22.  
  23. #define ON 1
  24. #define OFF 0
  25. FILE *logf;                        /* File for logging */
  26. char *logfilename = "out.scene";
  27. FILE *oscenef;                     /* File for scene output */
  28. char *oscenefilename = "out.scene";
  29.  
  30. /**********************************************************************/
  31. /* Print polygon and it's children */
  32. /**********************************************************************/
  33. void print_Polygon(fp,label,p)
  34.      Polygon *p;
  35.      FILE *fp;
  36.      char *label;
  37. {
  38.   int i;
  39.  
  40.   fprintf(fp,"\tPolygon %s%d {\n", p->name, p->id);
  41.   fprintf(fp,"\ttype: %s; area: %g", 
  42.       (p->class == TRIANGLE ? "tri" : "quad"),
  43.       p->area);
  44.   fprintf(fp," level %d; chgnrm %d; number vertices %d, plane d %g\n",
  45.       p->level, p->changingNormal, p->numVert, p->d);
  46.   if (p->uvw != 0) {
  47.     fprintf(fp,"\tU(N=%g,%g,%g d=%g); V(N=%g,%g,%g d=%g);\n",
  48.         p->uvw->Nu.x, p->uvw->Nu.y,  p->uvw->Nu.z, p->uvw->du, 
  49.         p->uvw->Nv.x, p->uvw->Nv.y,  p->uvw->Nv.z, p->uvw->dv);
  50.     if (p->class == TRIANGLE) 
  51.       fprintf(fp,"\tW(N=%g,%g,%g d=%g)\n",
  52.           p->uvw->Nw.x, p->uvw->Nw.y,  p->uvw->Nw.z, p->uvw->dw);
  53.   }
  54.   fprintf(fp,"\tunshot B: ");
  55.   for(i=0;i<MAX_SPECTRA_SAMPLES;i++) 
  56.     fprintf(fp,"%g ", p->unshot_B.samples[i]);
  57.   fprintf(fp," total B: ");
  58.   for(i=0;i<MAX_SPECTRA_SAMPLES;i++) 
  59.     fprintf(fp,"%g ", p->B.samples[i]);
  60.   fprintf(fp,"\n");
  61.  
  62.   if (p->Mfather != 0)
  63.     fprintf(fp,"\tParent mesh: %s%d\n", p->Mfather->name, p->Mfather->id);
  64.   if (p->Pfather != 0)
  65.     fprintf(fp,"\tParent patch: %s\n", p->Pfather->name);
  66.   if (p->Links != 0) {
  67.     print_PolyList(fp, "of FFlinks", p->Links, 
  68.            p->polyhead.num_polys, 0);
  69.   }
  70.  
  71.   for (i=0;i<(p->numVert);i++) {
  72.     print_vector(fp, p->vert[i]->name, &(p->vert[i]->pos));
  73.     print_vector(fp, "Normal", &(p->normal[i]));
  74.   }
  75.   
  76.   for (i=0;i<MAX_PATCH_CHILDREN;i++)
  77.     if(p->child[i] != 0) {
  78.       fprintf(fp, "\tChild #%d\n", i);
  79.       print_Polygon(fp, "", p->child[i]);
  80.     }
  81.   fprintf(fp,"\t}\n");
  82. }
  83.  
  84. /*********************************************************************/
  85. /* Print list of polygons */
  86. /*********************************************************************/
  87. void print_PolyList(fp, label, pl,plsize,full)
  88.      FILE *fp;
  89.      char *label;
  90.      PolyList *pl;
  91.      int plsize;
  92.      int full;
  93. {
  94.   PolyList *plptr;
  95.   int i;
  96.   
  97.   fprintf(fp, "\tPolylist %s {\n", label);
  98.   for(i=0, plptr= pl;i<plsize;i++, plptr=plptr->next) {
  99.     if(full) {
  100.       fprintf(fp,"\n");
  101.       print_Polygon(fp, "", plptr->patch);
  102.     } else 
  103.       fprintf(fp,"\tPolygon %s%d\n", plptr->patch->name, plptr->patch->id);
  104.   }
  105.   fprintf(fp,"\t}\n");
  106. }
  107.  
  108. /**********************************************************************/
  109. /* Print mesh */
  110. /**********************************************************************/
  111. void print_Mesh(fp, label,m)
  112.      Mesh *m;
  113.      FILE *fp;
  114.      char *label;
  115. {
  116.   Polygon *pptr;
  117.   int i;
  118.  
  119.   fprintf(fp,"\n\tMesh %s%d; %d polygon(s); Object: %s%d\n", 
  120.       m->name, m->id, m->num_polys, m->Ofather->name,
  121.       m->Ofather->id);
  122.   if (m->box != 0)
  123.     BoundsPrint(*(m->box), fp, m->name);
  124.   for (i=0, pptr = m->polys; i<m->num_polys; i++, pptr = pptr->next) {
  125.     print_Polygon(fp,"",pptr);
  126.   }
  127. }
  128.  
  129. /**********************************************************************/
  130. /* Print vertex */
  131. /**********************************************************************/
  132. void print_Vertex(fp,id,v)
  133.      FILE *fp;
  134.      int id;
  135.      Vertex *v;
  136. {
  137.   fprintf(fp,"\tVertex%d {\n", id);
  138.   print_vector(fp, "Position", &(v->pos));
  139.   
  140.   fprintf(fp,"\t%d adjacent polygons\n", v->polyhead.num_polys);
  141.   print_PolyList(fp, "of adjacent polyons", 
  142.          v->polylist, v->polyhead.num_polys,0);
  143.   fprintf(fp,"\t}\n");
  144. }
  145.  
  146. /**********************************************************************/
  147. /* Print list of vertices */
  148. /**********************************************************************/
  149. /* void print_Vertex_List(fp, label, vl)
  150.      FILE *fp;
  151.      char *label;
  152.      Vertex_list *vl;
  153. {
  154.   int i;
  155.   Vertex_list *vlptr;  
  156.  
  157.   fprintf(fp,"\n\tVertexList size: %d {\n", vlist_size);
  158.   for(i=0, vlptr = vl;i<vlist_size;i++, vlptr=vlptr->next)
  159.     print_Vertex(fp, i, vlptr->vtx);
  160. }
  161. */
  162.  
  163. int vtx_count = 0;                    /* For labelling vertices */
  164. /**********************************************************************/
  165. /* Print tree of vertices */
  166. /**********************************************************************/
  167. void print_Vertex_Tree(fp, label, vtptr)
  168.      FILE *fp;
  169.      char *label;
  170.      Vertex_tree *vtptr;
  171. {
  172.   int octant;
  173.  
  174.   if (vtptr != 0) { 
  175.     for (octant=0;octant<num_octants;octant++) 
  176.       if (vtptr->child[octant] != 0) 
  177.     print_Vertex_Tree(fp, label, vtptr->child[octant]);
  178.     print_Vertex(fp,vtx_count,vtptr->vtx);
  179.     vtx_count++;
  180.   }
  181.  
  182. }
  183.     
  184. /**********************************************************************/
  185. /* Print a colour */
  186. /**********************************************************************/
  187. void print_Colour(fp, label, c)
  188.      FILE *fp;
  189.      char *label;
  190.      Colour *c;
  191. {
  192.   fprintf(fp,"\tColour %s: %g %g %g\n", label, c->r, c->g, c->b);
  193. }
  194.  
  195. /**********************************************************************/
  196. /* Print spectrum */
  197. /**********************************************************************/
  198. void print_Spectra(fp, label, s)
  199.      FILE *fp;
  200.      char *label;
  201.      Spectra s;
  202. {
  203.   int i;
  204.   fprintf(fp,"\t%s { ", label);
  205.   for (i=0;i<MAX_SPECTRA_SAMPLES;i++) fprintf(fp, "%g ",s.samples[i]);
  206.   fprintf(fp,"}\n");
  207. }
  208.  
  209. /**********************************************************************/
  210. /* Print shade properties */
  211. /**********************************************************************/
  212. void print_ShadeType(fp, label, st)
  213.      FILE *fp;
  214.      char *label;
  215.      ShadeType st;
  216. {
  217.   fprintf(fp,"\tShadetype %s\n", label);
  218.   print_Spectra(fp,"Ka",st.Ka);
  219.   print_Spectra(fp,"Kd",st.Kd);
  220.   print_Spectra(fp,"Ks",st.Ks);
  221.   print_Spectra(fp,"p",st.p);
  222.   print_Spectra(fp,"t",st.t);
  223.   print_Spectra(fp,"n",st.n);
  224. }
  225.  
  226. /**********************************************************************/
  227. /* Print radiance */
  228. /**********************************************************************/
  229. void print_Radiance(fp, label, r)
  230.      FILE *fp;
  231.      char *label;
  232.      Radiance r;
  233. {
  234.   fprintf(fp,"\tRadiance %s\n", label);
  235.   print_Spectra(fp,"E",r.E);
  236.   print_Spectra(fp,"B",r.B);
  237. }
  238.  
  239. /**********************************************************************/
  240. /* Print surface properties */
  241. /**********************************************************************/
  242. void print_SurfaceProp(fp, label, s)
  243.      FILE *fp;
  244.      char *label;
  245.      SurfaceProp *s;
  246. {
  247.   fprintf(fp,"\tSurfaceProp%d %s\n", s->id, s->name);
  248.   print_ShadeType(fp,label,s->shade);
  249.   /* print_Radiance(fp,label,s->rad); */
  250.   /* print_Specularity(fp,label,s.highlight);
  251.      print_Transmit(fp,label,s.transmissivity);
  252.      */
  253. }
  254.  
  255. /**********************************************************************/
  256. /* Print light properties */
  257. /**********************************************************************/
  258. void print_LightProp(fp, label, l)
  259.      FILE *fp;
  260.      char *label;
  261.      LightProp *l;
  262. {
  263.   fprintf(fp,"\tLightProp%d %s\n", l->id, l->name);
  264.   fprintf(fp,"\tstandardDistance: %g\n", l->standardDistance);
  265.   print_vector(fp,"shineFrom", l->shineFrom);
  266.   print_vector(fp,"shineAt", l->shineAt);
  267.   print_vector(fp,"shineDir", l->shineDir);
  268.   fprintf(fp,"\tdropoff\n", l->dropoff);
  269.   fprintf(fp,"\tcutoff\n", l->cutoff);
  270.   fprintf(fp,"\tcosCutoff\n", l->cosCutoff);
  271.   fprintf(fp,"\tmaxSamples\n", l->maxSamples);
  272.   fprintf(fp,"\tusedSamples\n", l->usedSamples);
  273. }
  274.  
  275. /**********************************************************************/
  276. /* Print a texture map */
  277. /**********************************************************************/
  278. void print_TextureMap(fp, label, t)
  279.      FILE *fp;
  280.      char *label;
  281.      TextureProp *t;
  282. {
  283.   int i,j;
  284.  
  285.   fprintf(fp,"\tTextureMap%d %s\n", t->id, t->name);
  286.   fprintf(fp,"\tclass %d; resolution %d\n", t->class, t->resolution);
  287.   for(i=0;i<t->resolution;i++) {
  288.     fprintf(fp,"\t");
  289.     for(j=0;j<t->resolution;j++) {
  290.       fprintf(fp,"%u ", *t->map);
  291.       t->map++;
  292.     }
  293.     fprintf(fp,"\n");
  294.   }
  295. }
  296.  
  297. /**********************************************************************/
  298. /* Print an object */
  299. /**********************************************************************/
  300. void print_Object(fp, label, n)
  301.      FILE *fp;
  302.      char *label;
  303.      Objectt *n;
  304. {
  305.   fprintf(fp,"\tObject%d %s\n", n->id, n->name);
  306.   fprintf(fp,"\trayID %d\n", n->rayID);
  307.   fprintf(fp,"\tprimtype[%d] =  %s\n", n->primid, n->primtype);
  308.   BoundsPrint(*(n->box), fp, n->name);
  309.   /* print_SurfaceProp(fp, "", n->surface); */
  310.   /* more to come */
  311. }
  312.  
  313. /**********************************************************************/
  314. /* Switch log on / off */
  315. /**********************************************************************/
  316. void Log(logflag,what)
  317.      int logflag;
  318.      char *what;
  319. {
  320.   if (logflag) {
  321.     if (!(logf = fopen(logfilename, "w"))) {
  322.       fprintf(stderr,"%s: cannot open log file %s\n", ProgName, logfilename);
  323.       exit(1);
  324.     } 
  325.     printf("\n\t*** Print log of %s to %s ***\n", what, logfilename);
  326.   } else 
  327.     fclose(logf);
  328. }
  329.  
  330.  
  331. /**********************************************************************/
  332. /* Print viewing position */
  333. /**********************************************************************/
  334. void print_View(fp,c)
  335.      FILE *fp;
  336.      Camera *c;
  337. {
  338.   fprintf(fp,"\tCamera {\n");
  339.  
  340.   fprintf(fp,"\t\tlookfrom %g %g %g\n",
  341.       c->lookfrom.x, c->lookfrom.y, c->lookfrom.z);
  342.   fprintf(fp,"\t\tlookat %g %g %g\n", 
  343.       c->lookat.x,c->lookat.y,c->lookat.z);
  344.   fprintf(fp,"\t\tlookup %g %g %g\n",
  345.       c->lookup.x,c->lookup.y, c->lookup.z);
  346.   fprintf(fp,"\t\tfovx %d fovy %d near %g far %g\n",
  347.       c->fovx, c->fovy,c->near, c->far);
  348.   fprintf(fp,"\t\txRes %d yRes %d\n", c->xRes, c->yRes);
  349.   fprintf(fp,"\t}");
  350. }
  351.  
  352. /**********************************************************************/
  353. /* Print the scene out to log file */
  354. /**********************************************************************/
  355. void print_Scene(s, fname)
  356.      Scene *s;
  357.      char *fname;
  358. {
  359.   int i,j;
  360.   Objectt *optr;
  361.   Mesh *mptr;
  362.  
  363.   /* Print all polygons, and vertices */
  364.   logfilename = fname;
  365.  
  366.   Log(ON,"scene");
  367.   optr = s->objects;
  368.   for(i=0;i<s->num_objects;i++,optr++) {
  369.     mptr = optr->meshes;
  370.     print_Object(logf,"",optr);
  371.     for (j=0;j<optr->num_meshes;j++, mptr++) 
  372.       print_Mesh(logf,"blah",mptr);
  373.   }
  374.  
  375.   vtx_count = 0;
  376.   fprintf(logf,"\n\tVertexTree size: %d {\n", vtree_size);
  377.   print_Vertex_Tree(logf, "", vtree);
  378.   Log(OFF,"");
  379.  
  380.   /* Print display view */
  381.   /* logfilename = "log.view";
  382.      Log(ON,"view");
  383.      print_View(logf,&(ReadLog.displayView));
  384.      Log(OFF,""); */
  385. }
  386.  
  387. /**********************************************************************/
  388. /* Output results after running radiosity on scene */
  389. /**********************************************************************/
  390. void Write_Rad(rparams, filename, num)
  391.      RadParams rparams;
  392.      char *filename;
  393.      int num;
  394. {
  395.   int i,j,k;
  396.   Elist *elptr;
  397.   Polygon *eptr;
  398.   FILE *fp;
  399.   char ffilename[80];
  400.   Colour c[MAX_PATCH_VTX];
  401.  
  402.   if (num > 0) 
  403.     sprintf(ffilename,"%s%d", filename, num);
  404.   else
  405.     sprintf(ffilename,"%s", filename);
  406.   if (!(fp = fopen(ffilename, "w"))) {
  407.     fprintf(stderr,"%s: cannot open results file %s\n", ProgName, ffilename);
  408.     exit(1);
  409.   } 
  410.   printf("\n\t*** Printing %d polygons to %s ***\n", 
  411.      rparams.num_elements, ffilename);
  412.   
  413.   /* Print elements to file */
  414.   fprintf(fp,"Number polygons %d\n", rparams.num_elements);
  415.   elptr = rparams.elements;
  416.   for(i=0; i<rparams.num_elements;i++, elptr = elptr->next) {
  417.     eptr = elptr->element;
  418.  
  419.     /* Print patch label */
  420.     fprintf(fp,"Poly %s%d %d {\n", "poly", i, 
  421.         eptr->numVert);
  422.  
  423.     /* Print colour of patch (flat shade it for now) */
  424.     fprintf(fp,"  B { ");
  425.     if (Option.rad_interp_type == INTERP_VTX_FROM_PATCH) {
  426.       for (k=0;k<MAX_SPECTRA_SAMPLES;k++) 
  427.     fprintf(fp,"%g ", 
  428.         (eptr->B.samples[k] > 1.0 ? 1.0 : eptr->B.samples[k]));
  429.     } else {
  430.       for (k=0;k<MAX_SPECTRA_SAMPLES;k++) fprintf(fp,"-1.0 ");
  431.     }      
  432.     fprintf(fp,"}\n");
  433.  
  434.     /* Find and print colour of vertices */
  435.     Poly_VertexB(eptr, c);
  436.     fprintf(fp,"  vtx_B { ");
  437.     for (j=0;j<eptr->numVert;j++)
  438.       fprintf(fp,"{ %g %g %g } ", c[j].r, c[j].g, c[j].b);
  439.     fprintf(fp,"}\n");
  440.  
  441.     /* Print form factors */
  442.     fprintf(fp,"  vtx_F { ");
  443.     for (j=0;j<eptr->numVert;j++)
  444.       fprintf(fp,"%g ", elptr->vtx_ff[j]);
  445.     fprintf(fp,"}\n");
  446.     
  447.     /* Print normals */
  448.     fprintf(fp,"  norm {",i);
  449.     for (j=0;j<eptr->numVert;j++) 
  450.       fprintf(fp," { %g %g %g } ",
  451.          eptr->normal[j].x, eptr->normal[j].y, eptr->normal[j].z);
  452.     fprintf(fp,"}\n");
  453.     
  454.     /* Print vertices */
  455.     fprintf(fp,"  vert {",i);
  456.     for (j=0;j<eptr->numVert;j++) 
  457.       fprintf(fp," { %g %g %g } ", eptr->vert[j]->pos.x, 
  458.           eptr->vert[j]->pos.y, eptr->vert[j]->pos.z);
  459.     fprintf(fp,"}\n");
  460.  
  461.     fprintf(fp,"}\n");
  462.   }
  463.  
  464.   fclose(fp);
  465. }
  466.