home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / atomart.tar.gz / atomart.tar / light.c < prev    next >
C/C++ Source or Header  |  1990-06-27  |  1KB  |  81 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include "atomart.h"
  4. #include "macro.h"
  5. #include "gram.h"
  6.  
  7. extern attr    *astackp;
  8. extern matrix    trans;
  9.  
  10. extern int    maxhitlevel;
  11.  
  12. /*
  13.  * lightinit
  14.  *
  15.  *    initialise the function pointers and fields for a light object,
  16.  * returning a pointer to it.
  17.  */
  18. light *
  19. lightinit(d)
  20.     details *d;
  21. {
  22.     light    *l;
  23.     details    *ld;
  24.     matrix    tmp;
  25.     int    i;
  26.  
  27.     l = (light *)smalloc(sizeof(light));
  28.  
  29.     l->rad = l->cosang = 0.0;
  30.  
  31.         /* combine viewing and current transformation matrix */
  32.     mmult4(tmp, trans, astackp->m);
  33.  
  34.     l->rays = 1;
  35.  
  36.     l->type = POINT;
  37.  
  38.     while (d != (details *)NULL) {
  39.         switch (d->type) {
  40.         case CENTER:
  41.                 /* transform light into ray space */
  42.             d->u.v.x -= tmp[3][0];
  43.             d->u.v.y -= tmp[3][1];
  44.             d->u.v.z -= tmp[3][2];
  45.             v3x3tmult(l->org, d->u.v, tmp);
  46.             break;
  47.         case DIRECTION:
  48.             smult(d->u.v, -1.0);
  49.             normalise(d->u.v);
  50.             v3x3tmult(l->dir, d->u.v, tmp)
  51.             l->type = DIRECTIONAL;
  52.             break;
  53.         case RADIUS:
  54.             l->rad = d->u.f;
  55.             break;
  56.         case ANGLE:
  57.             l->cosang = cos((double)d->u.f / 180.0 * M_PI);
  58.             break;
  59.         case NUMRAYS:
  60.             l->rays = d->u.f;
  61.             break;
  62.         default:
  63.             warning("art: illegal field in light ignored.\n");
  64.         }
  65.         ld = d;
  66.         d = d->nxt;
  67.         free(ld);
  68.     }
  69.  
  70.     l->c.r = astackp->s.c.r / l->rays; 
  71.     l->c.g = astackp->s.c.g / l->rays;
  72.     l->c.b = astackp->s.c.b / l->rays;
  73.  
  74.     l->lasthits = (object **)smalloc(sizeof(object *) * (maxhitlevel + 1));
  75.  
  76.     for (i = 0; i <= maxhitlevel; i++)
  77.         l->lasthits[i] = (object *)NULL;
  78.  
  79.     return(l);
  80. }
  81.