]> icculus.org git repositories - btb/d2x.git/blob - main/lighting.c
automap resolution now selectable
[btb/d2x.git] / main / lighting.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: lighting.c,v 1.3 2001-01-31 15:17:54 bradleyb Exp $";
20 #endif
21
22 #include <stdio.h>
23 #include <string.h>     // for memset()
24
25 #include "fix.h"
26 #include "vecmat.h"
27 #include "gr.h"
28 #include "inferno.h"
29 #include "segment.h"
30 #include "error.h"
31 #include "mono.h"
32 #include "render.h"
33 #include "game.h"
34 #include "vclip.h"
35 #include "lighting.h"
36 #include "3d.h"
37 #include "laser.h"
38 #include "timer.h"
39 #include "player.h"
40 #include "weapon.h"
41 #include "powerup.h"
42 #include "fvi.h"
43 #include "robot.h"
44 #include "multi.h"
45
46 int     Do_dynamic_light=1;
47 //int   Use_fvi_lighting = 0;
48
49 fix     Dynamic_light[MAX_VERTICES];
50
51 #define LIGHTING_CACHE_SIZE     4096    //      Must be power of 2!
52 #define LIGHTING_FRAME_DELTA    256     //      Recompute cache value every 8 frames.
53 #define LIGHTING_CACHE_SHIFT    8
54
55 int     Lighting_frame_delta = 1;
56
57 int     Lighting_cache[LIGHTING_CACHE_SIZE];
58
59 int Cache_hits=0, Cache_lookups=1;
60
61 //      Return true if we think vertex vertnum is visible from segment segnum.
62 //      If some amount of time has gone by, then recompute, else use cached value.
63 int lighting_cache_visible(int vertnum, int segnum, int objnum, vms_vector *obj_pos, int obj_seg, vms_vector *vertpos)
64 {
65         int     cache_val, cache_frame, cache_vis;
66
67         cache_val = Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)];
68
69         cache_frame = cache_val >> 1;
70         cache_vis = cache_val & 1;
71
72 //mprintf((0, "%i %i %5i %i ", vertnum, segnum, cache_frame, cache_vis));
73
74 Cache_lookups++;
75         if ((cache_frame == 0) || (cache_frame + Lighting_frame_delta <= FrameCount)) {
76                 int                     apply_light=0;
77                 fvi_query       fq;
78                 fvi_info                hit_data;
79                 int                     segnum, hit_type;
80
81                 segnum = -1;
82
83                 #ifndef NDEBUG
84                 segnum = find_point_seg(obj_pos, obj_seg);
85                 if (segnum == -1) {
86                         Int3();         //      Obj_pos is not in obj_seg!
87                         return 0;               //      Done processing this object.
88                 }
89                 #endif
90
91                 fq.p0                                           = obj_pos;
92                 fq.startseg                             = obj_seg;
93                 fq.p1                                           = vertpos;
94                 fq.rad                                  = 0;
95                 fq.thisobjnum                   = objnum;
96                 fq.ignore_obj_list      = NULL;
97                 fq.flags                                        = FQ_TRANSWALL;
98
99                 hit_type = find_vector_intersection(&fq, &hit_data);
100
101                 // Hit_pos = Hit_data.hit_pnt;
102                 // Hit_seg = Hit_data.hit_seg;
103
104                 if (hit_type == HIT_OBJECT)
105                         Int3(); //      Hey, we're not supposed to be checking objects!
106
107                 if (hit_type == HIT_NONE)
108                         apply_light = 1;
109                 else if (hit_type == HIT_WALL) {
110                         fix     dist_dist;
111                         dist_dist = vm_vec_dist_quick(&hit_data.hit_pnt, obj_pos);
112                         if (dist_dist < F1_0/4) {
113                                 apply_light = 1;
114                                 // -- Int3();   //      Curious, did fvi detect intersection with wall containing vertex?
115                         }
116                 }
117                 Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)] = apply_light + (FrameCount << 1);
118 //mprintf((0, "%i\n", apply_light));
119                 return apply_light;
120         } else {
121 //mprintf((0, "\n"));
122 Cache_hits++;
123                 return cache_vis;
124         }       
125 }
126
127 #define HEADLIGHT_CONE_DOT      (F1_0*9/10)
128 #define HEADLIGHT_SCALE         (F1_0*10)
129
130 // ----------------------------------------------------------------------------------------------
131 void apply_light(fix obj_intensity, int obj_seg, vms_vector *obj_pos, int n_render_vertices, short *render_vertices, int objnum)
132 {
133         int     vv;
134
135         if (obj_intensity) {
136                 fix     obji_64 = obj_intensity*64;
137
138                 // for pretty dim sources, only process vertices in object's own segment.
139                 //      12/04/95, MK, markers only cast light in own segment.
140                 if ((abs(obji_64) <= F1_0*8) || (Objects[objnum].type == OBJ_MARKER)) {
141                         short *vp = Segments[obj_seg].verts;
142
143                         for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; vv++) {
144                                 int                     vertnum;
145                                 vms_vector      *vertpos;
146                                 fix                     dist;
147
148                                 vertnum = vp[vv];
149                                 if ((vertnum ^ FrameCount) & 1) {
150                                         vertpos = &Vertices[vertnum];
151                                         dist = vm_vec_dist_quick(obj_pos, vertpos);
152                                         dist = fixmul(dist/4, dist/4);
153                                         if (dist < abs(obji_64)) {
154                                                 if (dist < MIN_LIGHT_DIST)
155                                                         dist = MIN_LIGHT_DIST;
156         
157                                                 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
158                                         }
159                                 }
160                         }
161                 } else {
162                         int     headlight_shift = 0;
163                         fix     max_headlight_dist = F1_0*200;
164
165                         if (Objects[objnum].type == OBJ_PLAYER)
166                                 if (Players[Objects[objnum].id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
167                                         headlight_shift = 3;
168                                         if (Objects[objnum].id != Player_num) {
169                                                 vms_vector      tvec;
170                                                 fvi_query       fq;
171                                                 fvi_info                hit_data;
172                                                 int                     fate;
173
174                                                 vm_vec_scale_add(&tvec, &Objects[objnum].pos, &Objects[objnum].orient.fvec, F1_0*200);
175
176                                                 fq.startseg                             = Objects[objnum].segnum;
177                                                 fq.p0                                           = &Objects[objnum].pos;
178                                                 fq.p1                                           = &tvec;
179                                                 fq.rad                                  = 0;
180                                                 fq.thisobjnum                   = objnum;
181                                                 fq.ignore_obj_list      = NULL;
182                                                 fq.flags                                        = FQ_TRANSWALL;
183
184                                                 fate = find_vector_intersection(&fq, &hit_data);
185                                                 if (fate != HIT_NONE)
186                                                         max_headlight_dist = vm_vec_mag_quick(vm_vec_sub(&tvec, &hit_data.hit_pnt, &Objects[objnum].pos)) + F1_0*4;
187                                         }
188                                 }
189                         // -- for (vv=FrameCount&1; vv<n_render_vertices; vv+=2) {
190                         for (vv=0; vv<n_render_vertices; vv++) {
191                                 int                     vertnum;
192                                 vms_vector      *vertpos;
193                                 fix                     dist;
194                                 int                     apply_light;
195
196                                 vertnum = render_vertices[vv];
197                                 if ((vertnum ^ FrameCount) & 1) {
198                                         vertpos = &Vertices[vertnum];
199                                         dist = vm_vec_dist_quick(obj_pos, vertpos);
200                                         apply_light = 0;
201
202                                         if ((dist >> headlight_shift) < abs(obji_64)) {
203
204                                                 if (dist < MIN_LIGHT_DIST)
205                                                         dist = MIN_LIGHT_DIST;
206
207                                                 //if (Use_fvi_lighting) {
208                                                 //      if (lighting_cache_visible(vertnum, obj_seg, objnum, obj_pos, obj_seg, vertpos)) {
209                                                 //              apply_light = 1;
210                                                 //      }
211                                                 //} else
212                                                         apply_light = 1;
213
214                                                 if (apply_light) {
215                                                         if (headlight_shift) {
216                                                                 fix                     dot;
217                                                                 vms_vector      vec_to_point;
218
219                                                                 vm_vec_sub(&vec_to_point, vertpos, obj_pos);
220                                                                 vm_vec_normalize_quick(&vec_to_point);          //      MK, Optimization note: You compute distance about 15 lines up, this is partially redundant
221                                                                 dot = vm_vec_dot(&vec_to_point, &Objects[objnum].orient.fvec);
222                                                                 if (dot < F1_0/2)
223                                                                         Dynamic_light[vertnum] += fixdiv(obj_intensity, fixmul(HEADLIGHT_SCALE, dist)); //      Do the normal thing, but darken around headlight.
224                                                                 else {
225                                                                         if (Game_mode & GM_MULTI) {
226                                                                                 if (dist < max_headlight_dist)
227                                                                                         Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
228                                                                         } else
229                                                                                 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
230                                                                 }
231                                                         } else
232                                                                 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
233                                                 }
234                                         }
235                                 }
236                         }
237                 }
238         }
239 }
240
241 #define FLASH_LEN_FIXED_SECONDS (F1_0/3)
242 #define FLASH_SCALE                                     (3*F1_0/FLASH_LEN_FIXED_SECONDS)
243
244 // ----------------------------------------------------------------------------------------------
245 void cast_muzzle_flash_light(int n_render_vertices, short *render_vertices)
246 {
247         fix current_time;
248         int     i;
249         short   time_since_flash;
250
251         current_time = timer_get_fixed_seconds();
252
253         for (i=0; i<MUZZLE_QUEUE_MAX; i++) {
254                 if (Muzzle_data[i].create_time) {
255                         time_since_flash = current_time - Muzzle_data[i].create_time;
256                         if (time_since_flash < FLASH_LEN_FIXED_SECONDS)
257                                 apply_light((FLASH_LEN_FIXED_SECONDS - time_since_flash) * FLASH_SCALE, Muzzle_data[i].segnum, &Muzzle_data[i].pos, n_render_vertices, render_vertices, -1);
258                         else
259                                 Muzzle_data[i].create_time = 0;         // turn off this muzzle flash
260                 }
261         }
262 }
263
264 //      Translation table to make flares flicker at different rates
265 fix     Obj_light_xlate[16] =
266         {0x1234, 0x3321, 0x2468, 0x1735,
267          0x0123, 0x19af, 0x3f03, 0x232a,
268          0x2123, 0x39af, 0x0f03, 0x132a,
269          0x3123, 0x29af, 0x1f03, 0x032a};
270
271 //      Flag array of objects lit last frame.  Guaranteed to process this frame if lit last frame.
272 byte    Lighting_objects[MAX_OBJECTS];
273
274 #define MAX_HEADLIGHTS  8
275 object  *Headlights[MAX_HEADLIGHTS];
276 int             Num_headlights;
277
278 // ---------------------------------------------------------
279 fix compute_light_intensity(int objnum)
280 {
281         object          *obj = &Objects[objnum];
282         int                     objtype = obj->type;
283    fix hoardlight,s;
284          
285         switch (objtype) {
286                 case OBJ_PLAYER:
287                          if (Players[obj->id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
288                                 if (Num_headlights < MAX_HEADLIGHTS)
289                                         Headlights[Num_headlights++] = obj;
290                                 return HEADLIGHT_SCALE;
291                          } else if ((Game_mode & GM_HOARD) && Players[obj->id].secondary_ammo[PROXIMITY_INDEX]) {
292                         
293                    // If hoard game and player, add extra light based on how many orbs you have
294                         // Pulse as well.
295
296                            hoardlight=i2f(Players[obj->id].secondary_ammo[PROXIMITY_INDEX])/2; //i2f(12));
297                                 hoardlight++;
298                       fix_sincos ((GameTime/2) & 0xFFFF,&s,NULL); // probably a bad way to do it
299                            s+=F1_0;  
300                                 s>>=1;
301                            hoardlight=fixmul (s,hoardlight);
302                  //     mprintf ((0,"Hoardlight is %f!\n",f2fl(hoardlight)));
303                       return (hoardlight);
304                           }
305                         else
306                                 return max(vm_vec_mag_quick(&obj->mtype.phys_info.thrust)/4, F1_0*2) + F1_0/2;
307                         break;
308                 case OBJ_FIREBALL:
309                         if (obj->id != 0xff) {
310                                 if (obj->lifeleft < F1_0*4)
311                                         return fixmul(fixdiv(obj->lifeleft, Vclip[obj->id].play_time), Vclip[obj->id].light_value);
312                                 else
313                                         return Vclip[obj->id].light_value;
314                         } else
315                                  return 0;
316                         break;
317                 case OBJ_ROBOT:
318                         return F1_0*Robot_info[obj->id].lightcast;
319                         break;
320                 case OBJ_WEAPON: {
321                         fix tval = Weapon_info[obj->id].light;
322                         if (Game_mode & GM_MULTI)
323                                 if (obj->id == OMEGA_ID)
324                                         if (d_rand() > 8192)
325                                                 return 0;               //      3/4 of time, omega blobs will cast 0 light!
326
327                         if (obj->id == FLARE_ID )
328                                 return 2* (min(tval, obj->lifeleft) + ((GameTime ^ Obj_light_xlate[objnum&0x0f]) & 0x3fff));
329                         else
330                                 return tval;
331                 }
332
333                 case OBJ_MARKER: {
334                         fix     lightval = obj->lifeleft;
335
336                         lightval &= 0xffff;
337
338                         lightval = 8 * abs(F1_0/2 - lightval);
339
340                         if (obj->lifeleft < F1_0*1000)
341                                 obj->lifeleft += F1_0;  //      Make sure this object doesn't go out.
342
343                         return lightval;
344                 }
345
346                 case OBJ_POWERUP:
347                         return Powerup_info[obj->id].light;
348                         break;
349                 case OBJ_DEBRIS:
350                         return F1_0/4;
351                         break;
352                 case OBJ_LIGHT:
353                         return obj->ctype.light_info.intensity;
354                         break;
355                 default:
356                         return 0;
357                         break;
358         }
359 }
360
361 // ----------------------------------------------------------------------------------------------
362 void set_dynamic_light(void)
363 {
364         int     vv;
365         int     objnum;
366         int     n_render_vertices;
367         short   render_vertices[MAX_VERTICES];
368         byte    render_vertex_flags[MAX_VERTICES];
369         int     render_seg,segnum, v;
370         byte    new_lighting_objects[MAX_OBJECTS];
371
372         Num_headlights = 0;
373
374         if (!Do_dynamic_light)
375                 return;
376
377 //if (Use_fvi_lighting)
378 //      mprintf((0, "hits = %8i, misses = %8i, lookups = %8i, hit ratio = %7.4f\n", Cache_hits, Cache_lookups - Cache_hits, Cache_lookups, (float) Cache_hits / Cache_lookups));
379
380         memset(render_vertex_flags, 0, Highest_vertex_index+1);
381
382         //      Create list of vertices that need to be looked at for setting of ambient light.
383         n_render_vertices = 0;
384         for (render_seg=0; render_seg<N_render_segs; render_seg++) {
385                 segnum = Render_list[render_seg];
386                 if (segnum != -1) {
387                         short   *vp = Segments[segnum].verts;
388                         for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
389                                 int     vnum = vp[v];
390                                 if (vnum<0 || vnum>Highest_vertex_index) {
391                                         Int3();         //invalid vertex number
392                                         continue;       //ignore it, and go on to next one
393                                 }
394                                 if (!render_vertex_flags[vnum]) {
395                                         render_vertex_flags[vnum] = 1;
396                                         render_vertices[n_render_vertices++] = vnum;
397                                 }
398                                 //--old way-- for (s=0; s<n_render_vertices; s++)
399                                 //--old way--   if (render_vertices[s] == vnum)
400                                 //--old way--           break;
401                                 //--old way-- if (s == n_render_vertices)
402                                 //--old way--   render_vertices[n_render_vertices++] = vnum;
403                         }
404                 }
405         }
406
407         // -- for (vertnum=FrameCount&1; vertnum<n_render_vertices; vertnum+=2) {
408         for (vv=0; vv<n_render_vertices; vv++) {
409                 int     vertnum;
410
411                 vertnum = render_vertices[vv];
412                 Assert(vertnum >= 0 && vertnum <= Highest_vertex_index);
413                 if ((vertnum ^ FrameCount) & 1)
414                         Dynamic_light[vertnum] = 0;
415         }
416
417         cast_muzzle_flash_light(n_render_vertices, render_vertices);
418
419         for (objnum=0; objnum<=Highest_object_index; objnum++)
420                 new_lighting_objects[objnum] = 0;
421
422         //      July 5, 1995: New faster dynamic lighting code.  About 5% faster on the PC (un-optimized).
423         //      Only objects which are in rendered segments cast dynamic light.  We might wad6 to extend this
424         //      one or two segments if we notice light changing as objects go offscreen.  I couldn't see any
425         //      serious visual degradation.  In fact, I could see no humorous degradation, either. --MK
426         for (render_seg=0; render_seg<N_render_segs; render_seg++) {
427                 int     segnum = Render_list[render_seg];
428
429                 objnum = Segments[segnum].objects;
430
431                 while (objnum != -1) {
432                         object          *obj = &Objects[objnum];
433                         vms_vector      *objpos = &obj->pos;
434                         fix                     obj_intensity;
435
436                         obj_intensity = compute_light_intensity(objnum);
437
438                         if (obj_intensity) {
439                                 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, obj-Objects);
440                                 new_lighting_objects[objnum] = 1;
441                         }
442
443                         objnum = obj->next;
444                 }
445         }
446
447         //      Now, process all lights from last frame which haven't been processed this frame.
448         for (objnum=0; objnum<=Highest_object_index; objnum++) {
449                 //      In multiplayer games, process even unprocessed objects every 4th frame, else don't know about player sneaking up.
450                 if ((Lighting_objects[objnum]) || ((Game_mode & GM_MULTI) && (((objnum ^ FrameCount) & 3) == 0))) {
451                         if (!new_lighting_objects[objnum]) {
452                                 //      Lighted last frame, but not this frame.  Get intensity...
453                                 object          *obj = &Objects[objnum];
454                                 vms_vector      *objpos = &obj->pos;
455                                 fix                     obj_intensity;
456
457                                 obj_intensity = compute_light_intensity(objnum);
458
459                                 if (obj_intensity) {
460                                         apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, objnum);
461                                         Lighting_objects[objnum] = 1;
462                                 } else
463                                         Lighting_objects[objnum] = 0;
464                         }
465                 } else {
466                         //      Not lighted last frame, so we don't need to light it.  (Already lit if casting light this frame.)
467                         //      But copy value from new_lighting_objects to update Lighting_objects array.
468                         Lighting_objects[objnum] = new_lighting_objects[objnum];
469                 }
470         }
471 }
472
473 // ---------------------------------------------------------
474
475 void toggle_headlight_active()
476 {
477         if (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) {
478                 Players[Player_num].flags ^= PLAYER_FLAGS_HEADLIGHT_ON;                 
479 #ifdef NETWORK
480                 if (Game_mode & GM_MULTI)
481                         multi_send_flags(Player_num);           
482 #endif
483         }
484 }
485
486 #define HEADLIGHT_BOOST_SCALE 8         //how much to scale light when have headlight boost
487
488 fix     Beam_brightness = (F1_0/2);     //global saying how bright the light beam is
489
490 #define MAX_DIST_LOG    6                                                       //log(MAX_DIST-expressed-as-integer)
491 #define MAX_DIST                (f1_0<<MAX_DIST_LOG)    //no light beyond this dist
492
493 fix compute_headlight_light_on_object(object *objp)
494 {
495         int     i;
496         fix     light;
497
498         //      Let's just illuminate players and robots for speed reasons, ok?
499         if ((objp->type != OBJ_ROBOT) && (objp->type    != OBJ_PLAYER))
500                 return 0;
501
502         light = 0;
503
504         for (i=0; i<Num_headlights; i++) {
505                 fix                     dot, dist;
506                 vms_vector      vec_to_obj;
507                 object          *light_objp;
508
509                 light_objp = Headlights[i];
510
511                 vm_vec_sub(&vec_to_obj, &objp->pos, &light_objp->pos);
512                 dist = vm_vec_normalize_quick(&vec_to_obj);
513                 if (dist > 0) {
514                         dot = vm_vec_dot(&light_objp->orient.fvec, &vec_to_obj);
515
516                         if (dot < F1_0/2)
517                                 light += fixdiv(HEADLIGHT_SCALE, fixmul(HEADLIGHT_SCALE, dist));        //      Do the normal thing, but darken around headlight.
518                         else
519                                 light += fixmul(fixmul(dot, dot), HEADLIGHT_SCALE)/8;
520                 }
521         }
522
523         return light;
524 }
525
526
527 // -- Unused -- //Compute the lighting from the headlight for a given vertex on a face.
528 // -- Unused -- //Takes:
529 // -- Unused -- //  point - the 3d coords of the point
530 // -- Unused -- //  face_light - a scale factor derived from the surface normal of the face
531 // -- Unused -- //If no surface normal effect is wanted, pass F1_0 for face_light
532 // -- Unused -- fix compute_headlight_light(vms_vector *point,fix face_light)
533 // -- Unused -- {
534 // -- Unused --         fix light;
535 // -- Unused --         int use_beam = 0;               //flag for beam effect
536 // -- Unused -- 
537 // -- Unused --         light = Beam_brightness;
538 // -- Unused -- 
539 // -- Unused --         if ((Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) && (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON) && Viewer==&Objects[Players[Player_num].objnum] && Players[Player_num].energy > 0) {
540 // -- Unused --                 light *= HEADLIGHT_BOOST_SCALE;
541 // -- Unused --                 use_beam = 1;   //give us beam effect
542 // -- Unused --         }
543 // -- Unused -- 
544 // -- Unused --         if (light) {                            //if no beam, don't bother with the rest of this
545 // -- Unused --                 fix point_dist;
546 // -- Unused -- 
547 // -- Unused --                 point_dist = vm_vec_mag_quick(point);
548 // -- Unused -- 
549 // -- Unused --                 if (point_dist >= MAX_DIST)
550 // -- Unused -- 
551 // -- Unused --                         light = 0;
552 // -- Unused -- 
553 // -- Unused --                 else {
554 // -- Unused --                         fix dist_scale,face_scale;
555 // -- Unused -- 
556 // -- Unused --                         dist_scale = (MAX_DIST - point_dist) >> MAX_DIST_LOG;
557 // -- Unused --                         light = fixmul(light,dist_scale);
558 // -- Unused -- 
559 // -- Unused --                         if (face_light < 0)
560 // -- Unused --                                 face_light = 0;
561 // -- Unused -- 
562 // -- Unused --                         face_scale = f1_0/4 + face_light/2;
563 // -- Unused --                         light = fixmul(light,face_scale);
564 // -- Unused -- 
565 // -- Unused --                         if (use_beam) {
566 // -- Unused --                                 fix beam_scale;
567 // -- Unused -- 
568 // -- Unused --                                 if (face_light > f1_0*3/4 && point->z > i2f(12)) {
569 // -- Unused --                                         beam_scale = fixdiv(point->z,point_dist);
570 // -- Unused --                                         beam_scale = fixmul(beam_scale,beam_scale);     //square it
571 // -- Unused --                                         light = fixmul(light,beam_scale);
572 // -- Unused --                                 }
573 // -- Unused --                         }
574 // -- Unused --                 }
575 // -- Unused --         }
576 // -- Unused -- 
577 // -- Unused --         return light;
578 // -- Unused -- }
579
580 //compute the average dynamic light in a segment.  Takes the segment number
581 fix compute_seg_dynamic_light(int segnum)
582 {
583         fix sum;
584         segment *seg;
585         short *verts;
586
587         seg = &Segments[segnum];
588
589         verts = seg->verts;
590         sum = 0;
591
592         sum += Dynamic_light[*verts++];
593         sum += Dynamic_light[*verts++];
594         sum += Dynamic_light[*verts++];
595         sum += Dynamic_light[*verts++];
596         sum += Dynamic_light[*verts++];
597         sum += Dynamic_light[*verts++];
598         sum += Dynamic_light[*verts++];
599         sum += Dynamic_light[*verts];
600
601         return sum >> 3;
602
603 }
604
605 fix object_light[MAX_OBJECTS];
606 int object_sig[MAX_OBJECTS];
607 object *old_viewer;
608 int reset_lighting_hack;
609
610 #define LIGHT_RATE i2f(4)               //how fast the light ramps up
611
612 void start_lighting_frame(object *viewer)
613 {
614         reset_lighting_hack = (viewer != old_viewer);
615
616         old_viewer = viewer;
617 }
618
619 //compute the lighting for an object.  Takes a pointer to the object,
620 //and possibly a rotated 3d point.  If the point isn't specified, the
621 //object's center point is rotated.
622 fix compute_object_light(object *obj,vms_vector *rotated_pnt)
623 {
624         fix light;
625         g3s_point objpnt;
626         int objnum = obj-Objects;
627
628         if (!rotated_pnt) {
629                 g3_rotate_point(&objpnt,&obj->pos);
630                 rotated_pnt = &objpnt.p3_vec;
631         }
632
633         //First, get static light for this segment
634
635         light = Segment2s[obj->segnum].static_light;
636
637         //return light;
638
639
640         //Now, maybe return different value to smooth transitions
641
642         if (!reset_lighting_hack && object_sig[objnum] == obj->signature) {
643                 fix delta_light,frame_delta;
644
645                 delta_light = light - object_light[objnum];
646
647                 frame_delta = fixmul(LIGHT_RATE,FrameTime);
648
649                 if (abs(delta_light) <= frame_delta)
650
651                         object_light[objnum] = light;           //we've hit the goal
652
653                 else
654
655                         if (delta_light < 0)
656                                 light = object_light[objnum] -= frame_delta;
657                         else
658                                 light = object_light[objnum] += frame_delta;
659
660         }
661         else {          //new object, initialize
662
663                 object_sig[objnum] = obj->signature;
664                 object_light[objnum] = light;
665         }
666
667
668
669         //Next, add in headlight on this object
670
671         // -- Matt code: light += compute_headlight_light(rotated_pnt,f1_0);
672         light += compute_headlight_light_on_object(obj);
673   
674         //Finally, add in dynamic light for this segment
675
676         light += compute_seg_dynamic_light(obj->segnum);
677
678
679         return light;
680 }
681
682