home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / lightdef.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-24  |  3.5 KB  |  149 lines

  1. /*
  2.  * lightdef.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: lightdef.c,v 4.0 91/07/17 14:46:25 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    lightdef.c,v $
  19.  * Revision 4.0  91/07/17  14:46:25  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "rayshade.h"
  24. #include "options.h"
  25. #include "light.h"
  26. #include "infinite.h"    /* to create default infinite light */
  27. #include "jittered.h"    /* to create jittered light sources */
  28.  
  29. Light *Lights = NULL;        /* Linked list of defined lights */
  30.  
  31. void
  32. LightAddToDefined(light)
  33. Light *light;
  34. {
  35.     if (light) {
  36.         light->next = Lights;
  37.         Lights = light;
  38.     }
  39. }
  40.  
  41. void
  42. LightSetup()
  43. {
  44.     long shadowopts;
  45.     Light *ltmp;
  46.  
  47.     /*
  48.      * Set shadowing options.
  49.      */
  50.     shadowopts = 0;
  51.     if (Options.no_shadows)
  52.         shadowopts |= SHADOW_NONE;
  53.     if (Options.shadowtransp)
  54.         shadowopts |= SHADOW_TRANSP;
  55.     if (Options.csg)
  56.         shadowopts |= SHADOW_CSG;
  57.     if (Options.cache)
  58.         shadowopts |= SHADOW_CACHE;
  59.     if (Options.shutterspeed > 0.)
  60.         shadowopts |= SHADOW_BLUR;
  61.     ShadowSetOptions(shadowopts);
  62.  
  63.     /*
  64.      * If no light sources were defined, add a default.
  65.      */
  66.     if (Lights == (Light *)NULL) {
  67.         Color ctmp;
  68.         Vector vtmp;
  69.         vtmp.x = vtmp.z = 1.;
  70.         vtmp.y = -1;
  71.         ctmp.r = ctmp.g = ctmp.b = 1.;
  72.  
  73.         LightAddToDefined(LightInfiniteCreate(&ctmp, &vtmp));
  74.     }
  75.     
  76.         
  77.     /*
  78.      * Now that we've parsed the input file, we know what
  79.      * maxlevel is, and we can allocate the correct amount of
  80.      * space for each light source's cache.
  81.      */
  82.     for (ltmp = Lights; ltmp; ltmp = ltmp->next) {
  83.         ltmp->cache = (ShadowCache *)Calloc(
  84.             (unsigned)Options.maxdepth + 1, sizeof(ShadowCache));
  85.     }
  86. }
  87.  
  88. void
  89. AreaLightCreate(color, corner, u, usamp, v, vsamp, shadow)
  90. Color *color;
  91. Vector *corner, *u, *v;
  92. int usamp, vsamp, shadow;
  93. {
  94.     Vector vpos, curpos;
  95.     int i, j, numlight;
  96.     Float ulen, vlen;
  97.     Color intens;
  98.     Light *ltmp;
  99.  
  100.     if (usamp < 1 || vsamp < 1)
  101.         RLerror(RL_ABORT, "Invalid area light specification.\n",0,0,0);
  102.  
  103.     numlight = usamp * vsamp;    /* Total number of jittered sources */
  104.  
  105.     /*
  106.      * Sum of all intensities is equal to specified intensity.
  107.      */
  108.     intens.r = color->r / (Float)numlight;
  109.     intens.g = color->g / (Float)numlight;
  110.     intens.b = color->b / (Float)numlight;
  111.  
  112.     VecSub(*u, *corner, u);
  113.     VecSub(*v, *corner, v);
  114.     /*
  115.      * Make sure that u and v are not degenerate.
  116.      */
  117.     ulen = VecNormalize(u);
  118.     vlen = VecNormalize(v);
  119.     if (ulen < EPSILON || vlen < EPSILON)
  120.         RLerror(RL_ABORT, "Degenerate area light source.\n",0,0,0);
  121.     /*
  122.      * Scale u and v such that they define the area covered by a
  123.      * single sample.
  124.      */
  125.     VecScale(ulen / (Float)usamp, *u, u); 
  126.     VecScale(vlen / (Float)vsamp, *v, v);
  127.     /*
  128.      * For each sample...
  129.      */
  130.     vpos = *corner;
  131.     for (i = 0; i < vsamp; i++) {
  132.         curpos = vpos;
  133.         for (j = 0; j < usamp; j++) {
  134.             /*
  135.              * current pos is the "corner" of a new light
  136.              * source.  A jittered position based on
  137.              * the "corner" and the two edge vectors
  138.              * is used as the position for the
  139.              * light source in lighting calculations.
  140.              */
  141.             ltmp = LightJitteredCreate(&intens, &curpos, u, v);
  142.             ltmp->shadow = shadow;
  143.             LightAddToDefined(ltmp);
  144.             VecAdd(curpos, *u, &curpos);
  145.         }
  146.         VecAdd(vpos, *v, &vpos);
  147.     }
  148. }
  149.