home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / extended.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  3KB  |  125 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.  * $Id: extended.c,v 4.0 91/07/17 14:34:03 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    extended.c,v $
  19.  * Revision 4.0  91/07/17  14:34:03  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "light.h"
  24. #include "sampling.h"
  25. #include "extended.h"
  26.  
  27. static LightMethods *iExtendedMethods = NULL;
  28.  
  29. Extended *
  30. ExtendedCreate(r, pos)
  31. Float r;
  32. Vector *pos;
  33. {
  34.     Extended *e;
  35.  
  36.     e = (Extended *)share_malloc(sizeof(Extended));
  37.     e->pos = *pos;
  38.     e->radius = r;
  39.     
  40.     return e;
  41. }
  42.  
  43. LightMethods *
  44. ExtendedMethods()
  45. {
  46.     if (iExtendedMethods == (LightMethods *)NULL) {
  47.         iExtendedMethods = LightMethodsCreate();
  48.         iExtendedMethods->intens = ExtendedIntens;
  49.         iExtendedMethods->dir = ExtendedDirection;
  50.     }
  51.     return iExtendedMethods;
  52. }
  53.  
  54. /*
  55.  * Compute intensity ('color') of extended light source 'lp' from 'pos'.
  56.  */
  57. static int
  58. ExtendedIntens(lp, lcolor, cache, ray, dist, noshadow, color)
  59. Extended *lp;
  60. Color *lcolor, *color;
  61. ShadowCache *cache;
  62. Ray *ray;
  63. Float dist;
  64. int noshadow;
  65. {
  66.     int uSample, vSample, islit;
  67.     Float jit, vbase, ubase, vpos, upos, lightdist;
  68.     Color newcol;
  69.     Ray newray;
  70.     Vector Uaxis, Vaxis, ldir;
  71.  
  72.     if (noshadow) {
  73.         *color = *lcolor;
  74.         return TRUE;
  75.     }
  76.  
  77.     newray = *ray;
  78.     /*
  79.      * Determinte two orthoganal vectors that lay in the plane
  80.      * whose normal is defined by the vector from the center
  81.      * of the light source to the point of intersection and
  82.      * passes through the center of the light source.
  83.       */
  84.     VecSub(lp->pos, ray->pos, &ldir);
  85.     VecCoordSys(&ldir, &Uaxis, &Vaxis);
  86.  
  87.     jit = 2. * lp->radius * Sampling.spacing;
  88.  
  89.     /*
  90.      * Sample a single point, determined by SampleNumber,
  91.      * on the extended source.
  92.      */
  93.     vpos = -lp->radius + (ray->sample % Sampling.sidesamples)*jit;
  94.     upos = -lp->radius + (ray->sample / Sampling.sidesamples)*jit;
  95.     vpos += nrand() * jit;
  96.     upos += nrand() * jit;
  97.     VecComb(upos, Uaxis, vpos, Vaxis, &newray.dir);
  98.     VecAdd(ldir, newray.dir, &newray.dir);
  99.     lightdist = VecNormalize(&newray.dir);
  100.  
  101.     return !Shadowed(color, lcolor, cache, &newray,
  102.         lightdist, noshadow);
  103. }
  104.  
  105. void
  106. ExtendedDirection(lp, pos, dir, dist)
  107. Extended *lp;
  108. Vector *pos, *dir;
  109. Float *dist;
  110. {
  111.     /*
  112.      * Calculate dir from position to center of
  113.      * light source.
  114.      */
  115.     VecSub(lp->pos, *pos, dir);
  116.     *dist = VecNormalize(dir);
  117. }
  118.  
  119. ExtendedMethodRegister(meth)
  120. UserMethodType meth;
  121. {
  122.     if (iExtendedMethods)
  123.         iExtendedMethods->user = meth;
  124. }
  125.