home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / prev.tar.gz / prev.tar / polygon.c < prev    next >
C/C++ Source or Header  |  1991-03-20  |  2KB  |  112 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include "art.h"
  4. #include "objs.h"
  5. #include "macro.h"
  6. #include "gram.h"
  7.  
  8. extern mats    *mstackp;
  9. extern attr    *astackp;
  10.  
  11. extern hlist    *fhlist;
  12. extern int     linecount;
  13. extern float    tolerance;
  14. extern int    lookatdone;
  15.  
  16. /*
  17.  * polygoninit
  18.  *
  19.  *    initialise the function pointers and fields for a polygon
  20.  */
  21. void
  22. polygoninit(o, d)
  23.     object    *o;
  24.     details *d;
  25. {
  26.     polygon        *pg;
  27.     int        axis, i, count, ccount, ncount, dcount;
  28.     matrix        m, tmp;
  29.     vector        v, v1, v2, ovt[ART_MAXVERTS], vt[ART_MAXVERTS], norms[4];
  30.     vector        xv, yv, zv;
  31.     float        maxx, maxy, maxz, minx, miny, minz;
  32.     float        vx, vz, radius, area, tolerance;
  33.     vector        colours[4], on;
  34.     char        buf[MESLEN];
  35.     details        *ld, *d1, *ld1, *headd;
  36.  
  37.     count = 0;
  38.  
  39.     if (!lookatdone)
  40.         deflookat();
  41.  
  42.     while (d != (details *)NULL) {
  43.         switch (d->type) {
  44.         case VERTEX:
  45.                     /* transform vertex into ray space */
  46.             ovt[count] = d->u.v;
  47.             /*
  48.             vmmult(vt[count], d->u.v, mstackp->obj2ray);
  49.             */
  50.             count++;
  51.             break;
  52.         case COMPLEXVERTEX:
  53.             if (ccount == 4 || ncount == 4)
  54.                 fatal("art: can't interpolate across a polygon with more than 4 vertices.\n");
  55.  
  56.             dcount = 0;
  57.             for (d1 = d->u.det; d1 != (details *)NULL; d1 = d1->nxt)
  58.                 dcount++;
  59.                 
  60.             for (d1 = d->u.det; d1 != (details *)NULL; d1 = ld1) {
  61.                 if (d1->type == VERTEX) {
  62.                     if (dcount > 1) {    /* actual vertex will be last in list */
  63.                         norms[ncount] = d1->u.v;
  64.                         normalise(norms[ncount]);
  65.                         ncount++;
  66.                     } else {
  67.                         ovt[count] = d1->u.v;
  68.                         vmmult(vt[count], d1->u.v, mstackp->obj2ray);
  69.                     }
  70.                 } else {
  71.                     colours[ccount] = d1->u.v;
  72.                     ccount++;
  73.                 }
  74.                 dcount--;
  75.                 ld1 = d1->nxt;
  76.                 free(d1);
  77.             }
  78.  
  79.             count++;
  80.             break;
  81.         default:
  82.             warning("art: illegal field in polygon ignored.\n");
  83.         }
  84.         ld = d;
  85.         d = d->nxt;
  86.         free(ld);
  87.     }
  88.  
  89.     vsub(v1, ovt[1], ovt[0]);
  90.     vsub(v2, ovt[2], ovt[1]);
  91.  
  92.     xprod(on, v1, v2);
  93.  
  94.     if (on.x == 0.0 && on.y == 0.0 && on.z == 0.0) {
  95.         sprintf(buf, "invalid polygon in file before line %d\n", linecount);
  96.         warning(buf);
  97.         o->type = NULL_OBJ;
  98.  
  99.         return;
  100.     } else {
  101.         pushmatrix();
  102.             calctransforms(mstackp);
  103.             multmatrix(mstackp->obj2ray);
  104.  
  105.             move(ovt[count - 1].x, ovt[count - 1].y, ovt[count - 1].z);
  106.  
  107.             for (i = 0; i != count; i++)
  108.                 draw(ovt[i].x, ovt[i].y, ovt[i].z);
  109.         popmatrix();
  110.     }
  111. }
  112.