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