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.2 2001-01-22 13:22:39 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)) {
82 segnum = find_point_seg(obj_pos, obj_seg);
84 Int3(); // Obj_pos is not in obj_seg!
85 return 0; // Done processing this object.
90 fq.startseg = obj_seg;
93 fq.thisobjnum = objnum;
94 fq.ignore_obj_list = NULL;
95 fq.flags = FQ_TRANSWALL;
97 hit_type = find_vector_intersection(&fq, &hit_data);
99 // Hit_pos = Hit_data.hit_pnt;
100 // Hit_seg = Hit_data.hit_seg;
102 if (hit_type == HIT_OBJECT)
103 Int3(); // Hey, we're not supposed to be checking objects!
105 if (hit_type == HIT_NONE)
107 else if (hit_type == HIT_WALL) {
109 dist_dist = vm_vec_dist_quick(&hit_data.hit_pnt, obj_pos);
110 if (dist_dist < F1_0/4) {
112 // -- Int3(); // Curious, did fvi detect intersection with wall containing vertex?
115 Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)] = apply_light + (FrameCount << 1);
116 //mprintf((0, "%i\n", apply_light));
119 //mprintf((0, "\n"));
125 #define HEADLIGHT_CONE_DOT (F1_0*9/10)
126 #define HEADLIGHT_SCALE (F1_0*10)
128 // ----------------------------------------------------------------------------------------------
129 void apply_light(fix obj_intensity, int obj_seg, vms_vector *obj_pos, int n_render_vertices, short *render_vertices, int objnum)
134 fix obji_64 = obj_intensity*64;
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;
141 for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; 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;
155 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
160 int headlight_shift = 0;
161 fix max_headlight_dist = F1_0*200;
163 if (Objects[objnum].type == OBJ_PLAYER)
164 if (Players[Objects[objnum].id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
166 if (Objects[objnum].id != Player_num) {
172 vm_vec_scale_add(&tvec, &Objects[objnum].pos, &Objects[objnum].orient.fvec, F1_0*200);
174 fq.startseg = Objects[objnum].segnum;
175 fq.p0 = &Objects[objnum].pos;
178 fq.thisobjnum = objnum;
179 fq.ignore_obj_list = NULL;
180 fq.flags = FQ_TRANSWALL;
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;
187 // -- for (vv=FrameCount&1; vv<n_render_vertices; vv+=2) {
188 for (vv=0; vv<n_render_vertices; vv++) {
194 vertnum = render_vertices[vv];
195 if ((vertnum ^ FrameCount) & 1) {
196 vertpos = &Vertices[vertnum];
197 dist = vm_vec_dist_quick(obj_pos, vertpos);
200 if ((dist >> headlight_shift) < abs(obji_64)) {
202 if (dist < MIN_LIGHT_DIST)
203 dist = MIN_LIGHT_DIST;
205 //if (Use_fvi_lighting) {
206 // if (lighting_cache_visible(vertnum, obj_seg, objnum, obj_pos, obj_seg, vertpos)) {
213 if (headlight_shift) {
215 vms_vector vec_to_point;
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);
221 Dynamic_light[vertnum] += fixdiv(obj_intensity, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
223 if (Game_mode & GM_MULTI) {
224 if (dist < max_headlight_dist)
225 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
227 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
230 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
239 #define FLASH_LEN_FIXED_SECONDS (F1_0/3)
240 #define FLASH_SCALE (3*F1_0/FLASH_LEN_FIXED_SECONDS)
242 // ----------------------------------------------------------------------------------------------
243 void cast_muzzle_flash_light(int n_render_vertices, short *render_vertices)
247 short time_since_flash;
249 current_time = timer_get_fixed_seconds();
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);
257 Muzzle_data[i].create_time = 0; // turn off this muzzle flash
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};
269 // Flag array of objects lit last frame. Guaranteed to process this frame if lit last frame.
270 byte Lighting_objects[MAX_OBJECTS];
272 #define MAX_HEADLIGHTS 8
273 object *Headlights[MAX_HEADLIGHTS];
276 // ---------------------------------------------------------
277 fix compute_light_intensity(int objnum)
279 object *obj = &Objects[objnum];
280 int objtype = obj->type;
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]) {
291 // If hoard game and player, add extra light based on how many orbs you have
294 hoardlight=i2f(Players[obj->id].secondary_ammo[PROXIMITY_INDEX])/2; //i2f(12));
296 fix_sincos ((GameTime/2) & 0xFFFF,&s,NULL); // probably a bad way to do it
299 hoardlight=fixmul (s,hoardlight);
300 // mprintf ((0,"Hoardlight is %f!\n",f2fl(hoardlight)));
304 return max(vm_vec_mag_quick(&obj->mtype.phys_info.thrust)/4, F1_0*2) + F1_0/2;
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);
311 return Vclip[obj->id].light_value;
316 return F1_0*Robot_info[obj->id].lightcast;
319 fix tval = Weapon_info[obj->id].light;
320 if (Game_mode & GM_MULTI)
321 if (obj->id == OMEGA_ID)
323 return 0; // 3/4 of time, omega blobs will cast 0 light!
325 if (obj->id == FLARE_ID )
326 return 2* (min(tval, obj->lifeleft) + ((GameTime ^ Obj_light_xlate[objnum&0x0f]) & 0x3fff));
332 fix lightval = obj->lifeleft;
336 lightval = 8 * abs(F1_0/2 - lightval);
338 if (obj->lifeleft < F1_0*1000)
339 obj->lifeleft += F1_0; // Make sure this object doesn't go out.
345 return Powerup_info[obj->id].light;
351 return obj->ctype.light_info.intensity;
359 // ----------------------------------------------------------------------------------------------
360 void set_dynamic_light(void)
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];
372 if (!Do_dynamic_light)
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));
378 memset(render_vertex_flags, 0, Highest_vertex_index+1);
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];
385 short *vp = Segments[segnum].verts;
386 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
388 if (vnum<0 || vnum>Highest_vertex_index) {
389 Int3(); //invalid vertex number
390 continue; //ignore it, and go on to next one
392 if (!render_vertex_flags[vnum]) {
393 render_vertex_flags[vnum] = 1;
394 render_vertices[n_render_vertices++] = vnum;
396 //--old way-- for (s=0; s<n_render_vertices; s++)
397 //--old way-- if (render_vertices[s] == vnum)
399 //--old way-- if (s == n_render_vertices)
400 //--old way-- render_vertices[n_render_vertices++] = vnum;
405 // -- for (vertnum=FrameCount&1; vertnum<n_render_vertices; vertnum+=2) {
406 for (vv=0; vv<n_render_vertices; vv++) {
409 vertnum = render_vertices[vv];
410 Assert(vertnum >= 0 && vertnum <= Highest_vertex_index);
411 if ((vertnum ^ FrameCount) & 1)
412 Dynamic_light[vertnum] = 0;
415 cast_muzzle_flash_light(n_render_vertices, render_vertices);
417 for (objnum=0; objnum<=Highest_object_index; objnum++)
418 new_lighting_objects[objnum] = 0;
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];
427 objnum = Segments[segnum].objects;
429 while (objnum != -1) {
430 object *obj = &Objects[objnum];
431 vms_vector *objpos = &obj->pos;
434 obj_intensity = compute_light_intensity(objnum);
437 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, obj-Objects);
438 new_lighting_objects[objnum] = 1;
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;
455 obj_intensity = compute_light_intensity(objnum);
458 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, objnum);
459 Lighting_objects[objnum] = 1;
461 Lighting_objects[objnum] = 0;
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];
471 // ---------------------------------------------------------
473 void toggle_headlight_active()
475 if (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) {
476 Players[Player_num].flags ^= PLAYER_FLAGS_HEADLIGHT_ON;
478 if (Game_mode & GM_MULTI)
479 multi_send_flags(Player_num);
484 #define HEADLIGHT_BOOST_SCALE 8 //how much to scale light when have headlight boost
486 fix Beam_brightness = (F1_0/2); //global saying how bright the light beam is
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
491 fix compute_headlight_light_on_object(object *objp)
496 // Let's just illuminate players and robots for speed reasons, ok?
497 if ((objp->type != OBJ_ROBOT) && (objp->type != OBJ_PLAYER))
502 for (i=0; i<Num_headlights; i++) {
504 vms_vector vec_to_obj;
507 light_objp = Headlights[i];
509 vm_vec_sub(&vec_to_obj, &objp->pos, &light_objp->pos);
510 dist = vm_vec_normalize_quick(&vec_to_obj);
512 dot = vm_vec_dot(&light_objp->orient.fvec, &vec_to_obj);
515 light += fixdiv(HEADLIGHT_SCALE, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
517 light += fixmul(fixmul(dot, dot), HEADLIGHT_SCALE)/8;
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)
532 // -- Unused -- fix light;
533 // -- Unused -- int use_beam = 0; //flag for beam effect
535 // -- Unused -- light = Beam_brightness;
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
542 // -- Unused -- if (light) { //if no beam, don't bother with the rest of this
543 // -- Unused -- fix point_dist;
545 // -- Unused -- point_dist = vm_vec_mag_quick(point);
547 // -- Unused -- if (point_dist >= MAX_DIST)
549 // -- Unused -- light = 0;
551 // -- Unused -- else {
552 // -- Unused -- fix dist_scale,face_scale;
554 // -- Unused -- dist_scale = (MAX_DIST - point_dist) >> MAX_DIST_LOG;
555 // -- Unused -- light = fixmul(light,dist_scale);
557 // -- Unused -- if (face_light < 0)
558 // -- Unused -- face_light = 0;
560 // -- Unused -- face_scale = f1_0/4 + face_light/2;
561 // -- Unused -- light = fixmul(light,face_scale);
563 // -- Unused -- if (use_beam) {
564 // -- Unused -- fix beam_scale;
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);
575 // -- Unused -- return light;
578 //compute the average dynamic light in a segment. Takes the segment number
579 fix compute_seg_dynamic_light(int segnum)
585 seg = &Segments[segnum];
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];
603 fix object_light[MAX_OBJECTS];
604 int object_sig[MAX_OBJECTS];
606 int reset_lighting_hack;
608 #define LIGHT_RATE i2f(4) //how fast the light ramps up
610 void start_lighting_frame(object *viewer)
612 reset_lighting_hack = (viewer != old_viewer);
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)
624 int objnum = obj-Objects;
627 g3_rotate_point(&objpnt,&obj->pos);
628 rotated_pnt = &objpnt.p3_vec;
631 //First, get static light for this segment
633 light = Segment2s[obj->segnum].static_light;
638 //Now, maybe return different value to smooth transitions
640 if (!reset_lighting_hack && object_sig[objnum] == obj->signature) {
641 fix delta_light,frame_delta;
643 delta_light = light - object_light[objnum];
645 frame_delta = fixmul(LIGHT_RATE,FrameTime);
647 if (abs(delta_light) <= frame_delta)
649 object_light[objnum] = light; //we've hit the goal
654 light = object_light[objnum] -= frame_delta;
656 light = object_light[objnum] += frame_delta;
659 else { //new object, initialize
661 object_sig[objnum] = obj->signature;
662 object_light[objnum] = light;
667 //Next, add in headlight on this object
669 // -- Matt code: light += compute_headlight_light(rotated_pnt,f1_0);
670 light += compute_headlight_light_on_object(obj);
672 //Finally, add in dynamic light for this segment
674 light += compute_seg_dynamic_light(obj->segnum);