home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / main / elight.c < prev    next >
C/C++ Source or Header  |  1998-06-08  |  11KB  |  389 lines

  1. /*
  2.  * $Source: f:/miner/source/main/editor/rcs/elight.c $
  3.  * $Revision: 2.0 $
  4.  * $Author: john $
  5.  * $Date: 1995/02/27 11:35:16 $
  6.  * 
  7.  * Editor lighting functions.
  8.  * 
  9.  * $Log: elight.c $
  10.  * Revision 2.0  1995/02/27  11:35:16  john
  11.  * Version 2.0! No anonymous unions, Watcom 10.0, with no need
  12.  * for bitmaps.tbl.
  13.  * 
  14.  * Revision 1.21  1994/06/14  16:59:23  mike
  15.  * Fix references to tmap_num2, must strip off orientation bits.
  16.  * 
  17.  * Revision 1.20  1994/05/31  12:31:57  mike
  18.  * fix bug in lighting -- WALL_IS_DOORWAY return value getting ignored,
  19.  * almost never recursively propagated light.
  20.  * 
  21.  * Revision 1.19  1994/05/19  23:35:12  mike
  22.  * Support uv coordinates in range 0..1.0.
  23.  * 
  24.  * Revision 1.18  1994/05/16  12:05:29  john
  25.  * Made texturemap light be a fix from 0 to 1.
  26.  * 
  27.  * Revision 1.17  1994/05/14  18:00:38  matt
  28.  * Got rid of externs in source (non-header) files
  29.  * 
  30.  * Revision 1.16  1994/05/03  11:04:27  mike
  31.  * Add function to select edge.
  32.  * 
  33.  * Revision 1.15  1994/04/20  17:29:11  yuan
  34.  * Fixed bug where tmaps above 256 don't light properly.
  35.  * (duh!)
  36.  * 
  37.  * Revision 1.14  1994/03/22  14:20:46  yuan
  38.  * Made texture map 1 also cast light.  (Cumulative with tmap_num2)
  39.  * 
  40.  * Revision 1.13  1994/03/15  16:34:14  yuan
  41.  * Fixed bm loader (might have some changes in walls and switches)
  42.  * 
  43.  * Revision 1.12  1994/02/22  18:55:10  yuan
  44.  * Ambient lighting "shines" on doors too!
  45.  * 
  46.  * Revision 1.11  1994/02/17  12:05:55  matt
  47.  * Got rid of warnings
  48.  * 
  49.  * Revision 1.10  1994/02/16  22:28:03  mike
  50.  * fix ambient lighting and smoothing.
  51.  * 
  52.  * Revision 1.9  1994/02/14  12:05:42  mike
  53.  * change segment data structure.
  54.  * 
  55.  * Revision 1.8  1994/01/26  17:27:45  yuan
  56.  * Still not perfected ambient lighting
  57.  * 
  58.  * Revision 1.7  1994/01/25  17:58:08  yuan
  59.  * Added ambient lighting, and also added fixing bogus segments
  60.  * functions to the editor... (they don't work fully... need to
  61.  * check out seguvs.c
  62.  * 
  63.  * Revision 1.6  1994/01/24  11:46:10  yuan
  64.  * *** empty log message ***
  65.  * 
  66.  * Revision 1.5  1994/01/24  11:03:05  yuan
  67.  * Set lgiht maximum added... Changes are still in progress
  68.  * 
  69.  * Revision 1.4  1994/01/18  19:16:07  yuan
  70.  * Added assign default to lighting pad.
  71.  * 
  72.  * Revision 1.3  1993/12/17  12:26:00  mike
  73.  * Add functions for setting light values on whole segment at once.
  74.  * 
  75.  * Revision 1.2  1993/12/16  16:56:12  mike
  76.  * Add new texture map lighting control functions.
  77.  * 
  78.  * Revision 1.1  1993/12/16  13:21:50  mike
  79.  * Initial revision
  80.  * 
  81.  * 
  82.  */
  83.  
  84.  
  85. #pragma off (unreferenced)
  86. static char rcsid[] = "$Id: elight.c 2.0 1995/02/27 11:35:16 john Exp $";
  87. #pragma on (unreferenced)
  88.  
  89. #include <stdio.h>
  90. //#include <stdlib.h>
  91. //#include <stdarg.h>
  92. //#include <math.h>
  93. //#include <string.h>
  94.  
  95. #include "inferno.h"
  96. #include "segment.h"
  97. #include "editor.h"
  98. #include "seguvs.h"
  99.  
  100. #include "wall.h"
  101.  
  102. #include "textures.h"
  103.  
  104. #include "fix.h"
  105. #include "mono.h"
  106. #include "error.h"
  107. #include "kdefs.h"
  108. #include "gameseg.h"
  109.  
  110. #include "texmap.h"
  111.  
  112. // -----------------------------------------------------------------------------
  113. //    Return light intensity at an instance of a vertex on a side in a segment.
  114. fix get_light_intensity(segment *segp, int sidenum, int vert)
  115. {
  116.     Assert(sidenum <= MAX_SIDES_PER_SEGMENT);
  117.     Assert(vert <= 3);
  118.  
  119.     return segp->sides[sidenum].uvls[vert].l;
  120. }
  121.  
  122. // -----------------------------------------------------------------------------
  123. //    Set light intensity at a vertex, saturating in .5 to 15.5
  124. void set_light_intensity(segment *segp, int sidenum, int vert, fix intensity)
  125. {
  126.     Assert(sidenum <= MAX_SIDES_PER_SEGMENT);
  127.     Assert(vert <= 3);
  128.  
  129.     if (intensity < MIN_LIGHTING_VALUE)
  130.         intensity = MIN_LIGHTING_VALUE;
  131.  
  132.     if (intensity > MAX_LIGHTING_VALUE)
  133.         intensity = MAX_LIGHTING_VALUE;
  134.  
  135.     segp->sides[sidenum].uvls[vert].l = intensity;
  136.  
  137.     Update_flags |= UF_WORLD_CHANGED;
  138. }
  139.  
  140.  
  141. // -----------------------------------------------------------------------------
  142. //    Add light intensity to a vertex, saturating in .5 to 15.5
  143. void add_light_intensity(segment *segp, int sidenum, int vert, fix intensity)
  144. {
  145. //    fix    new_intensity;
  146.  
  147.     set_light_intensity(segp, sidenum, vert, segp->sides[sidenum].uvls[vert].l + intensity);
  148. }
  149.  
  150.  
  151. // -----------------------------------------------------------------------------
  152. //    Recursively apply light to segments.
  153. //    If current side is a wall, apply light there.
  154. //    If not a wall, apply light to child through that wall.
  155. //    Notes:
  156. //        It is possible to enter a segment twice by taking different paths.  It is easy
  157. //        to prevent this by maintaining a list of visited segments, but it is important
  158. //        to reach segments with the greatest light intensity.  This can be done by doing
  159. //        a breadth-first-search, or by storing the applied intensity with a visited segment,
  160. //        and if the current intensity is brighter, then apply the difference between it and
  161. //        the previous intensity.
  162. //        Note that it is also possible to visit the original light-casting segment, for example
  163. //        going from segment 0 to 2, then from 2 to 0.  This is peculiar and probably not
  164. //        desired, but not entirely invalid.  2 reflects some light back to 0.
  165. void apply_light_intensity(segment *segp, int sidenum, fix intensity, int depth)
  166. {
  167.     int    wid_result;
  168.  
  169.     if (intensity == 0)
  170.         return;
  171.  
  172.     wid_result = WALL_IS_DOORWAY(segp, sidenum);
  173.     if ((wid_result != WID_FLY_FLAG) && (wid_result != WID_NO_WALL)) {
  174.         int    v;
  175.         for (v=0; v<4; v++)                            // add light to this wall
  176.             add_light_intensity(segp, sidenum, v, intensity);
  177.         return;                                        // we return because there is a wall here, and light does not shine through walls
  178.     }
  179.  
  180.     //    No wall here, so apply light recursively
  181.     if (depth < 3) {
  182.         int    s;
  183.         for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  184.             apply_light_intensity(&Segments[segp->children[sidenum]], s, intensity/3, depth+1);
  185.     }
  186.  
  187. }
  188.  
  189. // -----------------------------------------------------------------------------
  190. //    Top level recursive function for applying light.
  191. //    Calls apply_light_intensity.
  192. //    Uses light value on segp:sidenum (tmap_num2 defines light value) and applies
  193. //    the associated intensity to segp.  It calls apply_light_intensity to apply intensity/3
  194. //    to all neighbors.  apply_light_intensity recursively calls itself to apply light to
  195. //    subsequent neighbors (and forming loops, see above).
  196. void propagate_light_intensity(segment *segp, int sidenum) 
  197. {
  198.     int        v,s;
  199.     fix        intensity;
  200.     short        texmap;
  201.  
  202.     intensity = 0;
  203.     texmap = segp->sides[sidenum].tmap_num;
  204.     intensity += TmapInfo[texmap].lighting;
  205.     texmap = (segp->sides[sidenum].tmap_num2) & 0x3fff;
  206.     intensity += TmapInfo[texmap].lighting;
  207.  
  208.     if (intensity > 0) {
  209.         for (v=0; v<4; v++)
  210.             add_light_intensity(segp, sidenum, v, intensity);
  211.     
  212.         //    Now, for all sides which are not the same as sidenum (the side casting the light),
  213.         //    add a light value to them (if they have no children, ie, they have a wall there).
  214.         for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  215.             if (s != sidenum)
  216.                 apply_light_intensity(segp, s, intensity/2, 1);
  217.     }
  218.  
  219. }
  220.  
  221.  
  222. // -----------------------------------------------------------------------------
  223. //    Highest level function, bound to a key.  Apply ambient light to all segments based
  224. //    on user-defined light sources.
  225. void LightAmbientLighting()
  226. {
  227.     int seg, side;
  228.  
  229.     for (seg=0; seg<=Highest_segment_index; seg++)
  230.         for (side=0;side<MAX_SIDES_PER_SEGMENT;side++)
  231.             propagate_light_intensity(&Segments[seg], side);
  232. }
  233.  
  234.  
  235. // -----------------------------------------------------------------------------
  236. int LightSelectNextVertex(void)
  237. {
  238.     Curvert++;
  239.     if (Curvert >= 4)
  240.         Curvert = 0;
  241.  
  242.     Update_flags |= UF_WORLD_CHANGED;
  243.  
  244.     return    1;
  245. }
  246.  
  247. // -----------------------------------------------------------------------------
  248. int LightSelectNextEdge(void)
  249. {
  250.     Curedge++;
  251.     if (Curedge >= 4)
  252.         Curedge = 0;
  253.  
  254.     Update_flags |= UF_WORLD_CHANGED;
  255.  
  256.     return    1;
  257. }
  258.  
  259. // -----------------------------------------------------------------------------
  260. //    Copy intensity from current vertex to all other vertices on side.
  261. int LightCopyIntensity(void)
  262. {
  263.     int    v,intensity;
  264.  
  265.     intensity = get_light_intensity(Cursegp, Curside, Curvert);
  266.  
  267.     for (v=0; v<4; v++)
  268.         if (v != Curvert)
  269.             set_light_intensity(Cursegp, Curside, v, intensity);
  270.  
  271.     return    1;
  272. }
  273.  
  274. // -----------------------------------------------------------------------------
  275. //    Copy intensity from current vertex to all other vertices on side.
  276. int LightCopyIntensitySegment(void)
  277. {
  278.     int    s,v,intensity;
  279.  
  280.     intensity = get_light_intensity(Cursegp, Curside, Curvert);
  281.  
  282.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  283.         for (v=0; v<4; v++)
  284.             if ((s != Curside) || (v != Curvert))
  285.                 set_light_intensity(Cursegp, s, v, intensity);
  286.  
  287.     return    1;
  288. }
  289.  
  290. // -----------------------------------------------------------------------------
  291. int LightDecreaseLightVertex(void)
  292. {
  293.     set_light_intensity(Cursegp, Curside, Curvert, get_light_intensity(Cursegp, Curside, Curvert)-F1_0/NUM_LIGHTING_LEVELS);
  294.  
  295.     return    1;
  296. }
  297.  
  298. // -----------------------------------------------------------------------------
  299. int LightIncreaseLightVertex(void)
  300. {
  301.     set_light_intensity(Cursegp, Curside, Curvert, get_light_intensity(Cursegp, Curside, Curvert)+F1_0/NUM_LIGHTING_LEVELS);
  302.  
  303.     return    1;
  304. }
  305.  
  306. // -----------------------------------------------------------------------------
  307. int LightDecreaseLightSide(void)
  308. {
  309.     int    v;
  310.  
  311.     for (v=0; v<4; v++)
  312.         set_light_intensity(Cursegp, Curside, v, get_light_intensity(Cursegp, Curside, v)-F1_0/NUM_LIGHTING_LEVELS);
  313.  
  314.     return    1;
  315. }
  316.  
  317. // -----------------------------------------------------------------------------
  318. int LightIncreaseLightSide(void)
  319. {
  320.     int    v;
  321.  
  322.     for (v=0; v<4; v++)
  323.         set_light_intensity(Cursegp, Curside, v, get_light_intensity(Cursegp, Curside, v)+F1_0/NUM_LIGHTING_LEVELS);
  324.  
  325.     return    1;
  326. }
  327.  
  328. // -----------------------------------------------------------------------------
  329. int LightDecreaseLightSegment(void)
  330. {
  331.     int    s,v;
  332.  
  333.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  334.         for (v=0; v<4; v++)
  335.             set_light_intensity(Cursegp, s, v, get_light_intensity(Cursegp, s, v)-F1_0/NUM_LIGHTING_LEVELS);
  336.  
  337.     return    1;
  338. }
  339.  
  340. // -----------------------------------------------------------------------------
  341. int LightIncreaseLightSegment(void)
  342. {
  343.     int    s,v;
  344.  
  345.     for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
  346.         for (v=0; v<4; v++)
  347.             set_light_intensity(Cursegp, s, v, get_light_intensity(Cursegp, s, v)+F1_0/NUM_LIGHTING_LEVELS);
  348.  
  349.     return    1;
  350. }
  351.  
  352. // -----------------------------------------------------------------------------
  353. int LightSetDefault(void)
  354. {
  355.     int    v;
  356.  
  357.     for (v=0; v<4; v++)
  358.         set_light_intensity(Cursegp, Curside, v, DEFAULT_LIGHTING);
  359.  
  360.     return    1;
  361. }
  362.  
  363.  
  364. // -----------------------------------------------------------------------------
  365. int LightSetMaximum(void)
  366. {
  367.     int    v;
  368.  
  369.     for (v=0; v<4; v++)
  370.         set_light_intensity(Cursegp, Curside, v, (NUM_LIGHTING_LEVELS-1)*F1_0/NUM_LIGHTING_LEVELS);
  371.  
  372.     return    1;
  373. }
  374.  
  375.  
  376. // -----------------------------------------------------------------------------
  377. int LightSetDefaultAll(void)
  378. {
  379.  
  380.     assign_default_lighting_all();
  381.  
  382.     Update_flags |= UF_WORLD_CHANGED;
  383.  
  384.     return    1;
  385. }
  386.  
  387.  
  388.  
  389.