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.
19 static char rcsid[] = "$Id: lighting.c,v 1.3 2001-01-31 15:17:54 bradleyb Exp $";
23 #include <string.h> // for memset()
46 int Do_dynamic_light=1;
47 //int Use_fvi_lighting = 0;
49 fix Dynamic_light[MAX_VERTICES];
51 #define LIGHTING_CACHE_SIZE 4096 // Must be power of 2!
52 #define LIGHTING_FRAME_DELTA 256 // Recompute cache value every 8 frames.
53 #define LIGHTING_CACHE_SHIFT 8
55 int Lighting_frame_delta = 1;
57 int Lighting_cache[LIGHTING_CACHE_SIZE];
59 int Cache_hits=0, Cache_lookups=1;
61 // Return true if we think vertex vertnum is visible from segment segnum.
62 // If some amount of time has gone by, then recompute, else use cached value.
63 int lighting_cache_visible(int vertnum, int segnum, int objnum, vms_vector *obj_pos, int obj_seg, vms_vector *vertpos)
65 int cache_val, cache_frame, cache_vis;
67 cache_val = Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)];
69 cache_frame = cache_val >> 1;
70 cache_vis = cache_val & 1;
72 //mprintf((0, "%i %i %5i %i ", vertnum, segnum, cache_frame, cache_vis));
75 if ((cache_frame == 0) || (cache_frame + Lighting_frame_delta <= FrameCount)) {
84 segnum = find_point_seg(obj_pos, obj_seg);
86 Int3(); // Obj_pos is not in obj_seg!
87 return 0; // Done processing this object.
92 fq.startseg = obj_seg;
95 fq.thisobjnum = objnum;
96 fq.ignore_obj_list = NULL;
97 fq.flags = FQ_TRANSWALL;
99 hit_type = find_vector_intersection(&fq, &hit_data);
101 // Hit_pos = Hit_data.hit_pnt;
102 // Hit_seg = Hit_data.hit_seg;
104 if (hit_type == HIT_OBJECT)
105 Int3(); // Hey, we're not supposed to be checking objects!
107 if (hit_type == HIT_NONE)
109 else if (hit_type == HIT_WALL) {
111 dist_dist = vm_vec_dist_quick(&hit_data.hit_pnt, obj_pos);
112 if (dist_dist < F1_0/4) {
114 // -- Int3(); // Curious, did fvi detect intersection with wall containing vertex?
117 Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)] = apply_light + (FrameCount << 1);
118 //mprintf((0, "%i\n", apply_light));
121 //mprintf((0, "\n"));
127 #define HEADLIGHT_CONE_DOT (F1_0*9/10)
128 #define HEADLIGHT_SCALE (F1_0*10)
130 // ----------------------------------------------------------------------------------------------
131 void apply_light(fix obj_intensity, int obj_seg, vms_vector *obj_pos, int n_render_vertices, short *render_vertices, int objnum)
136 fix obji_64 = obj_intensity*64;
138 // for pretty dim sources, only process vertices in object's own segment.
139 // 12/04/95, MK, markers only cast light in own segment.
140 if ((abs(obji_64) <= F1_0*8) || (Objects[objnum].type == OBJ_MARKER)) {
141 short *vp = Segments[obj_seg].verts;
143 for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; vv++) {
149 if ((vertnum ^ FrameCount) & 1) {
150 vertpos = &Vertices[vertnum];
151 dist = vm_vec_dist_quick(obj_pos, vertpos);
152 dist = fixmul(dist/4, dist/4);
153 if (dist < abs(obji_64)) {
154 if (dist < MIN_LIGHT_DIST)
155 dist = MIN_LIGHT_DIST;
157 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
162 int headlight_shift = 0;
163 fix max_headlight_dist = F1_0*200;
165 if (Objects[objnum].type == OBJ_PLAYER)
166 if (Players[Objects[objnum].id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
168 if (Objects[objnum].id != Player_num) {
174 vm_vec_scale_add(&tvec, &Objects[objnum].pos, &Objects[objnum].orient.fvec, F1_0*200);
176 fq.startseg = Objects[objnum].segnum;
177 fq.p0 = &Objects[objnum].pos;
180 fq.thisobjnum = objnum;
181 fq.ignore_obj_list = NULL;
182 fq.flags = FQ_TRANSWALL;
184 fate = find_vector_intersection(&fq, &hit_data);
185 if (fate != HIT_NONE)
186 max_headlight_dist = vm_vec_mag_quick(vm_vec_sub(&tvec, &hit_data.hit_pnt, &Objects[objnum].pos)) + F1_0*4;
189 // -- for (vv=FrameCount&1; vv<n_render_vertices; vv+=2) {
190 for (vv=0; vv<n_render_vertices; vv++) {
196 vertnum = render_vertices[vv];
197 if ((vertnum ^ FrameCount) & 1) {
198 vertpos = &Vertices[vertnum];
199 dist = vm_vec_dist_quick(obj_pos, vertpos);
202 if ((dist >> headlight_shift) < abs(obji_64)) {
204 if (dist < MIN_LIGHT_DIST)
205 dist = MIN_LIGHT_DIST;
207 //if (Use_fvi_lighting) {
208 // if (lighting_cache_visible(vertnum, obj_seg, objnum, obj_pos, obj_seg, vertpos)) {
215 if (headlight_shift) {
217 vms_vector vec_to_point;
219 vm_vec_sub(&vec_to_point, vertpos, obj_pos);
220 vm_vec_normalize_quick(&vec_to_point); // MK, Optimization note: You compute distance about 15 lines up, this is partially redundant
221 dot = vm_vec_dot(&vec_to_point, &Objects[objnum].orient.fvec);
223 Dynamic_light[vertnum] += fixdiv(obj_intensity, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
225 if (Game_mode & GM_MULTI) {
226 if (dist < max_headlight_dist)
227 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
229 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
232 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
241 #define FLASH_LEN_FIXED_SECONDS (F1_0/3)
242 #define FLASH_SCALE (3*F1_0/FLASH_LEN_FIXED_SECONDS)
244 // ----------------------------------------------------------------------------------------------
245 void cast_muzzle_flash_light(int n_render_vertices, short *render_vertices)
249 short time_since_flash;
251 current_time = timer_get_fixed_seconds();
253 for (i=0; i<MUZZLE_QUEUE_MAX; i++) {
254 if (Muzzle_data[i].create_time) {
255 time_since_flash = current_time - Muzzle_data[i].create_time;
256 if (time_since_flash < FLASH_LEN_FIXED_SECONDS)
257 apply_light((FLASH_LEN_FIXED_SECONDS - time_since_flash) * FLASH_SCALE, Muzzle_data[i].segnum, &Muzzle_data[i].pos, n_render_vertices, render_vertices, -1);
259 Muzzle_data[i].create_time = 0; // turn off this muzzle flash
264 // Translation table to make flares flicker at different rates
265 fix Obj_light_xlate[16] =
266 {0x1234, 0x3321, 0x2468, 0x1735,
267 0x0123, 0x19af, 0x3f03, 0x232a,
268 0x2123, 0x39af, 0x0f03, 0x132a,
269 0x3123, 0x29af, 0x1f03, 0x032a};
271 // Flag array of objects lit last frame. Guaranteed to process this frame if lit last frame.
272 byte Lighting_objects[MAX_OBJECTS];
274 #define MAX_HEADLIGHTS 8
275 object *Headlights[MAX_HEADLIGHTS];
278 // ---------------------------------------------------------
279 fix compute_light_intensity(int objnum)
281 object *obj = &Objects[objnum];
282 int objtype = obj->type;
287 if (Players[obj->id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
288 if (Num_headlights < MAX_HEADLIGHTS)
289 Headlights[Num_headlights++] = obj;
290 return HEADLIGHT_SCALE;
291 } else if ((Game_mode & GM_HOARD) && Players[obj->id].secondary_ammo[PROXIMITY_INDEX]) {
293 // If hoard game and player, add extra light based on how many orbs you have
296 hoardlight=i2f(Players[obj->id].secondary_ammo[PROXIMITY_INDEX])/2; //i2f(12));
298 fix_sincos ((GameTime/2) & 0xFFFF,&s,NULL); // probably a bad way to do it
301 hoardlight=fixmul (s,hoardlight);
302 // mprintf ((0,"Hoardlight is %f!\n",f2fl(hoardlight)));
306 return max(vm_vec_mag_quick(&obj->mtype.phys_info.thrust)/4, F1_0*2) + F1_0/2;
309 if (obj->id != 0xff) {
310 if (obj->lifeleft < F1_0*4)
311 return fixmul(fixdiv(obj->lifeleft, Vclip[obj->id].play_time), Vclip[obj->id].light_value);
313 return Vclip[obj->id].light_value;
318 return F1_0*Robot_info[obj->id].lightcast;
321 fix tval = Weapon_info[obj->id].light;
322 if (Game_mode & GM_MULTI)
323 if (obj->id == OMEGA_ID)
325 return 0; // 3/4 of time, omega blobs will cast 0 light!
327 if (obj->id == FLARE_ID )
328 return 2* (min(tval, obj->lifeleft) + ((GameTime ^ Obj_light_xlate[objnum&0x0f]) & 0x3fff));
334 fix lightval = obj->lifeleft;
338 lightval = 8 * abs(F1_0/2 - lightval);
340 if (obj->lifeleft < F1_0*1000)
341 obj->lifeleft += F1_0; // Make sure this object doesn't go out.
347 return Powerup_info[obj->id].light;
353 return obj->ctype.light_info.intensity;
361 // ----------------------------------------------------------------------------------------------
362 void set_dynamic_light(void)
366 int n_render_vertices;
367 short render_vertices[MAX_VERTICES];
368 byte render_vertex_flags[MAX_VERTICES];
369 int render_seg,segnum, v;
370 byte new_lighting_objects[MAX_OBJECTS];
374 if (!Do_dynamic_light)
377 //if (Use_fvi_lighting)
378 // mprintf((0, "hits = %8i, misses = %8i, lookups = %8i, hit ratio = %7.4f\n", Cache_hits, Cache_lookups - Cache_hits, Cache_lookups, (float) Cache_hits / Cache_lookups));
380 memset(render_vertex_flags, 0, Highest_vertex_index+1);
382 // Create list of vertices that need to be looked at for setting of ambient light.
383 n_render_vertices = 0;
384 for (render_seg=0; render_seg<N_render_segs; render_seg++) {
385 segnum = Render_list[render_seg];
387 short *vp = Segments[segnum].verts;
388 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
390 if (vnum<0 || vnum>Highest_vertex_index) {
391 Int3(); //invalid vertex number
392 continue; //ignore it, and go on to next one
394 if (!render_vertex_flags[vnum]) {
395 render_vertex_flags[vnum] = 1;
396 render_vertices[n_render_vertices++] = vnum;
398 //--old way-- for (s=0; s<n_render_vertices; s++)
399 //--old way-- if (render_vertices[s] == vnum)
401 //--old way-- if (s == n_render_vertices)
402 //--old way-- render_vertices[n_render_vertices++] = vnum;
407 // -- for (vertnum=FrameCount&1; vertnum<n_render_vertices; vertnum+=2) {
408 for (vv=0; vv<n_render_vertices; vv++) {
411 vertnum = render_vertices[vv];
412 Assert(vertnum >= 0 && vertnum <= Highest_vertex_index);
413 if ((vertnum ^ FrameCount) & 1)
414 Dynamic_light[vertnum] = 0;
417 cast_muzzle_flash_light(n_render_vertices, render_vertices);
419 for (objnum=0; objnum<=Highest_object_index; objnum++)
420 new_lighting_objects[objnum] = 0;
422 // July 5, 1995: New faster dynamic lighting code. About 5% faster on the PC (un-optimized).
423 // Only objects which are in rendered segments cast dynamic light. We might wad6 to extend this
424 // one or two segments if we notice light changing as objects go offscreen. I couldn't see any
425 // serious visual degradation. In fact, I could see no humorous degradation, either. --MK
426 for (render_seg=0; render_seg<N_render_segs; render_seg++) {
427 int segnum = Render_list[render_seg];
429 objnum = Segments[segnum].objects;
431 while (objnum != -1) {
432 object *obj = &Objects[objnum];
433 vms_vector *objpos = &obj->pos;
436 obj_intensity = compute_light_intensity(objnum);
439 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, obj-Objects);
440 new_lighting_objects[objnum] = 1;
447 // Now, process all lights from last frame which haven't been processed this frame.
448 for (objnum=0; objnum<=Highest_object_index; objnum++) {
449 // In multiplayer games, process even unprocessed objects every 4th frame, else don't know about player sneaking up.
450 if ((Lighting_objects[objnum]) || ((Game_mode & GM_MULTI) && (((objnum ^ FrameCount) & 3) == 0))) {
451 if (!new_lighting_objects[objnum]) {
452 // Lighted last frame, but not this frame. Get intensity...
453 object *obj = &Objects[objnum];
454 vms_vector *objpos = &obj->pos;
457 obj_intensity = compute_light_intensity(objnum);
460 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, objnum);
461 Lighting_objects[objnum] = 1;
463 Lighting_objects[objnum] = 0;
466 // Not lighted last frame, so we don't need to light it. (Already lit if casting light this frame.)
467 // But copy value from new_lighting_objects to update Lighting_objects array.
468 Lighting_objects[objnum] = new_lighting_objects[objnum];
473 // ---------------------------------------------------------
475 void toggle_headlight_active()
477 if (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) {
478 Players[Player_num].flags ^= PLAYER_FLAGS_HEADLIGHT_ON;
480 if (Game_mode & GM_MULTI)
481 multi_send_flags(Player_num);
486 #define HEADLIGHT_BOOST_SCALE 8 //how much to scale light when have headlight boost
488 fix Beam_brightness = (F1_0/2); //global saying how bright the light beam is
490 #define MAX_DIST_LOG 6 //log(MAX_DIST-expressed-as-integer)
491 #define MAX_DIST (f1_0<<MAX_DIST_LOG) //no light beyond this dist
493 fix compute_headlight_light_on_object(object *objp)
498 // Let's just illuminate players and robots for speed reasons, ok?
499 if ((objp->type != OBJ_ROBOT) && (objp->type != OBJ_PLAYER))
504 for (i=0; i<Num_headlights; i++) {
506 vms_vector vec_to_obj;
509 light_objp = Headlights[i];
511 vm_vec_sub(&vec_to_obj, &objp->pos, &light_objp->pos);
512 dist = vm_vec_normalize_quick(&vec_to_obj);
514 dot = vm_vec_dot(&light_objp->orient.fvec, &vec_to_obj);
517 light += fixdiv(HEADLIGHT_SCALE, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
519 light += fixmul(fixmul(dot, dot), HEADLIGHT_SCALE)/8;
527 // -- Unused -- //Compute the lighting from the headlight for a given vertex on a face.
528 // -- Unused -- //Takes:
529 // -- Unused -- // point - the 3d coords of the point
530 // -- Unused -- // face_light - a scale factor derived from the surface normal of the face
531 // -- Unused -- //If no surface normal effect is wanted, pass F1_0 for face_light
532 // -- Unused -- fix compute_headlight_light(vms_vector *point,fix face_light)
534 // -- Unused -- fix light;
535 // -- Unused -- int use_beam = 0; //flag for beam effect
537 // -- Unused -- light = Beam_brightness;
539 // -- Unused -- if ((Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) && (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON) && Viewer==&Objects[Players[Player_num].objnum] && Players[Player_num].energy > 0) {
540 // -- Unused -- light *= HEADLIGHT_BOOST_SCALE;
541 // -- Unused -- use_beam = 1; //give us beam effect
544 // -- Unused -- if (light) { //if no beam, don't bother with the rest of this
545 // -- Unused -- fix point_dist;
547 // -- Unused -- point_dist = vm_vec_mag_quick(point);
549 // -- Unused -- if (point_dist >= MAX_DIST)
551 // -- Unused -- light = 0;
553 // -- Unused -- else {
554 // -- Unused -- fix dist_scale,face_scale;
556 // -- Unused -- dist_scale = (MAX_DIST - point_dist) >> MAX_DIST_LOG;
557 // -- Unused -- light = fixmul(light,dist_scale);
559 // -- Unused -- if (face_light < 0)
560 // -- Unused -- face_light = 0;
562 // -- Unused -- face_scale = f1_0/4 + face_light/2;
563 // -- Unused -- light = fixmul(light,face_scale);
565 // -- Unused -- if (use_beam) {
566 // -- Unused -- fix beam_scale;
568 // -- Unused -- if (face_light > f1_0*3/4 && point->z > i2f(12)) {
569 // -- Unused -- beam_scale = fixdiv(point->z,point_dist);
570 // -- Unused -- beam_scale = fixmul(beam_scale,beam_scale); //square it
571 // -- Unused -- light = fixmul(light,beam_scale);
577 // -- Unused -- return light;
580 //compute the average dynamic light in a segment. Takes the segment number
581 fix compute_seg_dynamic_light(int segnum)
587 seg = &Segments[segnum];
592 sum += Dynamic_light[*verts++];
593 sum += Dynamic_light[*verts++];
594 sum += Dynamic_light[*verts++];
595 sum += Dynamic_light[*verts++];
596 sum += Dynamic_light[*verts++];
597 sum += Dynamic_light[*verts++];
598 sum += Dynamic_light[*verts++];
599 sum += Dynamic_light[*verts];
605 fix object_light[MAX_OBJECTS];
606 int object_sig[MAX_OBJECTS];
608 int reset_lighting_hack;
610 #define LIGHT_RATE i2f(4) //how fast the light ramps up
612 void start_lighting_frame(object *viewer)
614 reset_lighting_hack = (viewer != old_viewer);
619 //compute the lighting for an object. Takes a pointer to the object,
620 //and possibly a rotated 3d point. If the point isn't specified, the
621 //object's center point is rotated.
622 fix compute_object_light(object *obj,vms_vector *rotated_pnt)
626 int objnum = obj-Objects;
629 g3_rotate_point(&objpnt,&obj->pos);
630 rotated_pnt = &objpnt.p3_vec;
633 //First, get static light for this segment
635 light = Segment2s[obj->segnum].static_light;
640 //Now, maybe return different value to smooth transitions
642 if (!reset_lighting_hack && object_sig[objnum] == obj->signature) {
643 fix delta_light,frame_delta;
645 delta_light = light - object_light[objnum];
647 frame_delta = fixmul(LIGHT_RATE,FrameTime);
649 if (abs(delta_light) <= frame_delta)
651 object_light[objnum] = light; //we've hit the goal
656 light = object_light[objnum] -= frame_delta;
658 light = object_light[objnum] += frame_delta;
661 else { //new object, initialize
663 object_sig[objnum] = obj->signature;
664 object_light[objnum] = light;
669 //Next, add in headlight on this object
671 // -- Matt code: light += compute_headlight_light(rotated_pnt,f1_0);
672 light += compute_headlight_light_on_object(obj);
674 //Finally, add in dynamic light for this segment
676 light += compute_seg_dynamic_light(obj->segnum);