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