home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / liblight / extended.c next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.0 KB  |  134 lines

  1. /*
  2.  * extended.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * extended.c,v 4.1 1994/08/09 07:56:50 explorer Exp
  17.  *
  18.  * extended.c,v
  19.  * Revision 4.1  1994/08/09  07:56:50  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:03  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:34:03  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "light.h"
  30. #include "libcommon/sampling.h"
  31. #include "extended.h"
  32.  
  33. static LightMethods *iExtendedMethods = NULL;
  34.  
  35. Extended *
  36. ExtendedCreate(r, pos)
  37. Float r;
  38. Vector *pos;
  39. {
  40.     Extended *e;
  41.  
  42.     e = (Extended *)share_malloc(sizeof(Extended));
  43.     e->pos = *pos;
  44.     e->radius = r;
  45.     
  46.     return e;
  47. }
  48.  
  49. LightMethods *
  50. ExtendedMethods()
  51. {
  52.     
  53.         int ExtendedIntens();
  54.     
  55.     if (iExtendedMethods == (LightMethods *)NULL) {
  56.         iExtendedMethods = LightMethodsCreate();
  57.         iExtendedMethods->intens = ExtendedIntens;
  58.         iExtendedMethods->dir = ExtendedDirection;
  59.     }
  60.     return iExtendedMethods;
  61. }
  62.  
  63. /*
  64.  * Compute intensity ('color') of extended light source 'lp' from 'pos'.
  65.  */
  66. static int
  67. ExtendedIntens(lp, lcolor, cache, ray, dist, noshadow, color)
  68. Extended *lp;
  69. Color *lcolor, *color;
  70. ShadowCache *cache;
  71. Ray *ray;
  72. Float dist;
  73. int noshadow;
  74. {
  75.     int uSample, vSample, islit;
  76.     Float jit, vbase, ubase, vpos, upos, lightdist;
  77.     Color newcol;
  78.     Ray newray;
  79.     Vector Uaxis, Vaxis, ldir;
  80.  
  81.     if (noshadow) {
  82.         *color = *lcolor;
  83.         return TRUE;
  84.     }
  85.  
  86.     newray = *ray;
  87.     /*
  88.      * Determinte two orthoganal vectors that lay in the plane
  89.      * whose normal is defined by the vector from the center
  90.      * of the light source to the point of intersection and
  91.      * passes through the center of the light source.
  92.       */
  93.     VecSub(lp->pos, ray->pos, &ldir);
  94.     VecCoordSys(&ldir, &Uaxis, &Vaxis);
  95.  
  96.     jit = 2. * lp->radius * Sampling.spacing;
  97.  
  98.     /*
  99.      * Sample a single point, determined by SampleNumber,
  100.      * on the extended source.
  101.      */
  102.     vpos = -lp->radius + (ray->sample % Sampling.sidesamples)*jit;
  103.     upos = -lp->radius + (ray->sample / Sampling.sidesamples)*jit;
  104.     vpos += nrand() * jit;
  105.     upos += nrand() * jit;
  106.     VecComb(upos, Uaxis, vpos, Vaxis, &newray.dir);
  107.     VecAdd(ldir, newray.dir, &newray.dir);
  108.     lightdist = VecNormalize(&newray.dir);
  109.  
  110.     return !Shadowed(color, lcolor, cache, &newray,
  111.         lightdist, noshadow);
  112. }
  113.  
  114. void
  115. ExtendedDirection(lp, pos, dir, dist)
  116. Extended *lp;
  117. Vector *pos, *dir;
  118. Float *dist;
  119. {
  120.     /*
  121.      * Calculate dir from position to center of
  122.      * light source.
  123.      */
  124.     VecSub(lp->pos, *pos, dir);
  125.     *dist = VecNormalize(dir);
  126. }
  127.  
  128. ExtendedMethodRegister(meth)
  129. UserMethodType meth;
  130. {
  131.     if (iExtendedMethods)
  132.         iExtendedMethods->user = meth;
  133. }
  134.