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.
16 static char rcsid[] = "$Id: lighting.c,v 1.1.1.1 2001-01-19 03:30:00 bradleyb Exp $";
21 #include <string.h> // for memset()
44 int Do_dynamic_light=1;
45 //int Use_fvi_lighting = 0;
47 fix Dynamic_light[MAX_VERTICES];
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
53 int Lighting_frame_delta = 1;
55 int Lighting_cache[LIGHTING_CACHE_SIZE];
57 int Cache_hits=0, Cache_lookups=1;
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)
63 int cache_val, cache_frame, cache_vis;
65 cache_val = Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)];
67 cache_frame = cache_val >> 1;
68 cache_vis = cache_val & 1;
70 //mprintf((0, "%i %i %5i %i ", vertnum, segnum, cache_frame, cache_vis));
73 if ((cache_frame == 0) || (cache_frame + Lighting_frame_delta <= FrameCount)) {
80 segnum = find_point_seg(obj_pos, obj_seg);
82 Int3(); // Obj_pos is not in obj_seg!
83 return 0; // Done processing this object.
88 fq.startseg = obj_seg;
91 fq.thisobjnum = objnum;
92 fq.ignore_obj_list = NULL;
93 fq.flags = FQ_TRANSWALL;
95 hit_type = find_vector_intersection(&fq, &hit_data);
97 // Hit_pos = Hit_data.hit_pnt;
98 // Hit_seg = Hit_data.hit_seg;
100 if (hit_type == HIT_OBJECT)
101 Int3(); // Hey, we're not supposed to be checking objects!
103 if (hit_type == HIT_NONE)
105 else if (hit_type == HIT_WALL) {
107 dist_dist = vm_vec_dist_quick(&hit_data.hit_pnt, obj_pos);
108 if (dist_dist < F1_0/4) {
110 // -- Int3(); // Curious, did fvi detect intersection with wall containing vertex?
113 Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)] = apply_light + (FrameCount << 1);
114 //mprintf((0, "%i\n", apply_light));
117 //mprintf((0, "\n"));
123 #define HEADLIGHT_CONE_DOT (F1_0*9/10)
124 #define HEADLIGHT_SCALE (F1_0*10)
126 // ----------------------------------------------------------------------------------------------
127 void apply_light(fix obj_intensity, int obj_seg, vms_vector *obj_pos, int n_render_vertices, short *render_vertices, int objnum)
132 fix obji_64 = obj_intensity*64;
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;
139 for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; 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;
153 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
158 int headlight_shift = 0;
159 fix max_headlight_dist = F1_0*200;
161 if (Objects[objnum].type == OBJ_PLAYER)
162 if (Players[Objects[objnum].id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
164 if (Objects[objnum].id != Player_num) {
170 vm_vec_scale_add(&tvec, &Objects[objnum].pos, &Objects[objnum].orient.fvec, F1_0*200);
172 fq.startseg = Objects[objnum].segnum;
173 fq.p0 = &Objects[objnum].pos;
176 fq.thisobjnum = objnum;
177 fq.ignore_obj_list = NULL;
178 fq.flags = FQ_TRANSWALL;
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;
185 // -- for (vv=FrameCount&1; vv<n_render_vertices; vv+=2) {
186 for (vv=0; vv<n_render_vertices; vv++) {
192 vertnum = render_vertices[vv];
193 if ((vertnum ^ FrameCount) & 1) {
194 vertpos = &Vertices[vertnum];
195 dist = vm_vec_dist_quick(obj_pos, vertpos);
198 if ((dist >> headlight_shift) < abs(obji_64)) {
200 if (dist < MIN_LIGHT_DIST)
201 dist = MIN_LIGHT_DIST;
203 //if (Use_fvi_lighting) {
204 // if (lighting_cache_visible(vertnum, obj_seg, objnum, obj_pos, obj_seg, vertpos)) {
211 if (headlight_shift) {
213 vms_vector vec_to_point;
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);
219 Dynamic_light[vertnum] += fixdiv(obj_intensity, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
221 if (Game_mode & GM_MULTI) {
222 if (dist < max_headlight_dist)
223 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
225 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
228 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
237 #define FLASH_LEN_FIXED_SECONDS (F1_0/3)
238 #define FLASH_SCALE (3*F1_0/FLASH_LEN_FIXED_SECONDS)
240 // ----------------------------------------------------------------------------------------------
241 void cast_muzzle_flash_light(int n_render_vertices, short *render_vertices)
245 short time_since_flash;
247 current_time = timer_get_fixed_seconds();
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);
255 Muzzle_data[i].create_time = 0; // turn off this muzzle flash
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};
267 // Flag array of objects lit last frame. Guaranteed to process this frame if lit last frame.
268 byte Lighting_objects[MAX_OBJECTS];
270 #define MAX_HEADLIGHTS 8
271 object *Headlights[MAX_HEADLIGHTS];
274 // ---------------------------------------------------------
275 fix compute_light_intensity(int objnum)
277 object *obj = &Objects[objnum];
278 int objtype = obj->type;
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]) {
289 // If hoard game and player, add extra light based on how many orbs you have
292 hoardlight=i2f(Players[obj->id].secondary_ammo[PROXIMITY_INDEX])/2; //i2f(12));
294 fix_sincos ((GameTime/2) & 0xFFFF,&s,NULL); // probably a bad way to do it
297 hoardlight=fixmul (s,hoardlight);
298 // mprintf ((0,"Hoardlight is %f!\n",f2fl(hoardlight)));
302 return max(vm_vec_mag_quick(&obj->mtype.phys_info.thrust)/4, F1_0*2) + F1_0/2;
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);
309 return Vclip[obj->id].light_value;
314 return F1_0*Robot_info[obj->id].lightcast;
317 fix tval = Weapon_info[obj->id].light;
318 if (Game_mode & GM_MULTI)
319 if (obj->id == OMEGA_ID)
321 return 0; // 3/4 of time, omega blobs will cast 0 light!
323 if (obj->id == FLARE_ID )
324 return 2* (min(tval, obj->lifeleft) + ((GameTime ^ Obj_light_xlate[objnum&0x0f]) & 0x3fff));
330 fix lightval = obj->lifeleft;
334 lightval = 8 * abs(F1_0/2 - lightval);
336 if (obj->lifeleft < F1_0*1000)
337 obj->lifeleft += F1_0; // Make sure this object doesn't go out.
343 return Powerup_info[obj->id].light;
349 return obj->ctype.light_info.intensity;
357 // ----------------------------------------------------------------------------------------------
358 void set_dynamic_light(void)
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];
370 if (!Do_dynamic_light)
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));
376 memset(render_vertex_flags, 0, Highest_vertex_index+1);
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];
383 short *vp = Segments[segnum].verts;
384 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
386 if (vnum<0 || vnum>Highest_vertex_index) {
387 Int3(); //invalid vertex number
388 continue; //ignore it, and go on to next one
390 if (!render_vertex_flags[vnum]) {
391 render_vertex_flags[vnum] = 1;
392 render_vertices[n_render_vertices++] = vnum;
394 //--old way-- for (s=0; s<n_render_vertices; s++)
395 //--old way-- if (render_vertices[s] == vnum)
397 //--old way-- if (s == n_render_vertices)
398 //--old way-- render_vertices[n_render_vertices++] = vnum;
403 // -- for (vertnum=FrameCount&1; vertnum<n_render_vertices; vertnum+=2) {
404 for (vv=0; vv<n_render_vertices; vv++) {
407 vertnum = render_vertices[vv];
408 Assert(vertnum >= 0 && vertnum <= Highest_vertex_index);
409 if ((vertnum ^ FrameCount) & 1)
410 Dynamic_light[vertnum] = 0;
413 cast_muzzle_flash_light(n_render_vertices, render_vertices);
415 for (objnum=0; objnum<=Highest_object_index; objnum++)
416 new_lighting_objects[objnum] = 0;
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];
425 objnum = Segments[segnum].objects;
427 while (objnum != -1) {
428 object *obj = &Objects[objnum];
429 vms_vector *objpos = &obj->pos;
432 obj_intensity = compute_light_intensity(objnum);
435 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, obj-Objects);
436 new_lighting_objects[objnum] = 1;
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;
453 obj_intensity = compute_light_intensity(objnum);
456 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, objnum);
457 Lighting_objects[objnum] = 1;
459 Lighting_objects[objnum] = 0;
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];
469 // ---------------------------------------------------------
471 void toggle_headlight_active()
473 if (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) {
474 Players[Player_num].flags ^= PLAYER_FLAGS_HEADLIGHT_ON;
476 if (Game_mode & GM_MULTI)
477 multi_send_flags(Player_num);
482 #define HEADLIGHT_BOOST_SCALE 8 //how much to scale light when have headlight boost
484 fix Beam_brightness = (F1_0/2); //global saying how bright the light beam is
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
489 fix compute_headlight_light_on_object(object *objp)
494 // Let's just illuminate players and robots for speed reasons, ok?
495 if ((objp->type != OBJ_ROBOT) && (objp->type != OBJ_PLAYER))
500 for (i=0; i<Num_headlights; i++) {
502 vms_vector vec_to_obj;
505 light_objp = Headlights[i];
507 vm_vec_sub(&vec_to_obj, &objp->pos, &light_objp->pos);
508 dist = vm_vec_normalize_quick(&vec_to_obj);
510 dot = vm_vec_dot(&light_objp->orient.fvec, &vec_to_obj);
513 light += fixdiv(HEADLIGHT_SCALE, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
515 light += fixmul(fixmul(dot, dot), HEADLIGHT_SCALE)/8;
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)
530 // -- Unused -- fix light;
531 // -- Unused -- int use_beam = 0; //flag for beam effect
533 // -- Unused -- light = Beam_brightness;
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
540 // -- Unused -- if (light) { //if no beam, don't bother with the rest of this
541 // -- Unused -- fix point_dist;
543 // -- Unused -- point_dist = vm_vec_mag_quick(point);
545 // -- Unused -- if (point_dist >= MAX_DIST)
547 // -- Unused -- light = 0;
549 // -- Unused -- else {
550 // -- Unused -- fix dist_scale,face_scale;
552 // -- Unused -- dist_scale = (MAX_DIST - point_dist) >> MAX_DIST_LOG;
553 // -- Unused -- light = fixmul(light,dist_scale);
555 // -- Unused -- if (face_light < 0)
556 // -- Unused -- face_light = 0;
558 // -- Unused -- face_scale = f1_0/4 + face_light/2;
559 // -- Unused -- light = fixmul(light,face_scale);
561 // -- Unused -- if (use_beam) {
562 // -- Unused -- fix beam_scale;
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);
573 // -- Unused -- return light;
576 //compute the average dynamic light in a segment. Takes the segment number
577 fix compute_seg_dynamic_light(int segnum)
583 seg = &Segments[segnum];
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];
601 fix object_light[MAX_OBJECTS];
602 int object_sig[MAX_OBJECTS];
604 int reset_lighting_hack;
606 #define LIGHT_RATE i2f(4) //how fast the light ramps up
608 void start_lighting_frame(object *viewer)
610 reset_lighting_hack = (viewer != old_viewer);
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)
622 int objnum = obj-Objects;
625 g3_rotate_point(&objpnt,&obj->pos);
626 rotated_pnt = &objpnt.p3_vec;
629 //First, get static light for this segment
631 light = Segment2s[obj->segnum].static_light;
636 //Now, maybe return different value to smooth transitions
638 if (!reset_lighting_hack && object_sig[objnum] == obj->signature) {
639 fix delta_light,frame_delta;
641 delta_light = light - object_light[objnum];
643 frame_delta = fixmul(LIGHT_RATE,FrameTime);
645 if (abs(delta_light) <= frame_delta)
647 object_light[objnum] = light; //we've hit the goal
652 light = object_light[objnum] -= frame_delta;
654 light = object_light[objnum] += frame_delta;
657 else { //new object, initialize
659 object_sig[objnum] = obj->signature;
660 object_light[objnum] = light;
665 //Next, add in headlight on this object
667 // -- Matt code: light += compute_headlight_light(rotated_pnt,f1_0);
668 light += compute_headlight_light_on_object(obj);
670 //Finally, add in dynamic light for this segment
672 light += compute_seg_dynamic_light(obj->segnum);