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