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.
25 #include <string.h> // for memset()
37 int Do_dynamic_light=1;
38 //int Use_fvi_lighting = 0;
40 fix Dynamic_light[MAX_VERTICES];
42 #define LIGHTING_CACHE_SIZE 4096 // Must be power of 2!
43 #define LIGHTING_FRAME_DELTA 256 // Recompute cache value every 8 frames.
44 #define LIGHTING_CACHE_SHIFT 8
46 int Lighting_frame_delta = 1;
48 int Lighting_cache[LIGHTING_CACHE_SIZE];
50 int Cache_hits=0, Cache_lookups=1;
52 // Return true if we think vertex vertnum is visible from segment segnum.
53 // If some amount of time has gone by, then recompute, else use cached value.
54 int lighting_cache_visible(int vertnum, int segnum, int objnum, vms_vector *obj_pos, int obj_seg, vms_vector *vertpos)
56 int cache_val, cache_frame, cache_vis;
58 cache_val = Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)];
60 cache_frame = cache_val >> 1;
61 cache_vis = cache_val & 1;
63 //mprintf((0, "%i %i %5i %i ", vertnum, segnum, cache_frame, cache_vis));
66 if ((cache_frame == 0) || (cache_frame + Lighting_frame_delta <= FrameCount)) {
75 segnum = find_point_seg(obj_pos, obj_seg);
77 Int3(); // Obj_pos is not in obj_seg!
78 return 0; // Done processing this object.
83 fq.startseg = obj_seg;
86 fq.thisobjnum = objnum;
87 fq.ignore_obj_list = NULL;
88 fq.flags = FQ_TRANSWALL;
90 hit_type = find_vector_intersection(&fq, &hit_data);
92 // Hit_pos = Hit_data.hit_pnt;
93 // Hit_seg = Hit_data.hit_seg;
95 if (hit_type == HIT_OBJECT)
96 Int3(); // Hey, we're not supposed to be checking objects!
98 if (hit_type == HIT_NONE)
100 else if (hit_type == HIT_WALL) {
102 dist_dist = vm_vec_dist_quick(&hit_data.hit_pnt, obj_pos);
103 if (dist_dist < F1_0/4) {
105 // -- Int3(); // Curious, did fvi detect intersection with wall containing vertex?
108 Lighting_cache[((segnum << LIGHTING_CACHE_SHIFT) ^ vertnum) & (LIGHTING_CACHE_SIZE-1)] = apply_light + (FrameCount << 1);
109 //mprintf((0, "%i\n", apply_light));
112 //mprintf((0, "\n"));
118 #define HEADLIGHT_CONE_DOT (F1_0*9/10)
119 #define HEADLIGHT_SCALE (F1_0*10)
121 // ----------------------------------------------------------------------------------------------
122 void apply_light(fix obj_intensity, int obj_seg, vms_vector *obj_pos, int n_render_vertices, short *render_vertices, int objnum)
127 fix obji_64 = obj_intensity*64;
129 // for pretty dim sources, only process vertices in object's own segment.
130 // 12/04/95, MK, markers only cast light in own segment.
131 if ((abs(obji_64) <= F1_0*8) || (Objects[objnum].type == OBJ_MARKER)) {
132 short *vp = Segments[obj_seg].verts;
134 for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; vv++) {
140 if ((vertnum ^ FrameCount) & 1) {
141 vertpos = &Vertices[vertnum];
142 dist = vm_vec_dist_quick(obj_pos, vertpos);
143 dist = fixmul(dist/4, dist/4);
144 if (dist < abs(obji_64)) {
145 if (dist < MIN_LIGHT_DIST)
146 dist = MIN_LIGHT_DIST;
148 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
153 int headlight_shift = 0;
154 fix max_headlight_dist = F1_0*200;
156 if (Objects[objnum].type == OBJ_PLAYER)
157 if (Players[Objects[objnum].id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
159 if (Objects[objnum].id != Player_num) {
165 vm_vec_scale_add(&tvec, &Objects[objnum].pos, &Objects[objnum].orient.fvec, F1_0*200);
167 fq.startseg = Objects[objnum].segnum;
168 fq.p0 = &Objects[objnum].pos;
171 fq.thisobjnum = objnum;
172 fq.ignore_obj_list = NULL;
173 fq.flags = FQ_TRANSWALL;
175 fate = find_vector_intersection(&fq, &hit_data);
176 if (fate != HIT_NONE)
177 max_headlight_dist = vm_vec_mag_quick(vm_vec_sub(&tvec, &hit_data.hit_pnt, &Objects[objnum].pos)) + F1_0*4;
180 // -- for (vv=FrameCount&1; vv<n_render_vertices; vv+=2) {
181 for (vv=0; vv<n_render_vertices; vv++) {
187 vertnum = render_vertices[vv];
188 if ((vertnum ^ FrameCount) & 1) {
189 vertpos = &Vertices[vertnum];
190 dist = vm_vec_dist_quick(obj_pos, vertpos);
193 if ((dist >> headlight_shift) < abs(obji_64)) {
195 if (dist < MIN_LIGHT_DIST)
196 dist = MIN_LIGHT_DIST;
198 //if (Use_fvi_lighting) {
199 // if (lighting_cache_visible(vertnum, obj_seg, objnum, obj_pos, obj_seg, vertpos)) {
206 if (headlight_shift) {
208 vms_vector vec_to_point;
210 vm_vec_sub(&vec_to_point, vertpos, obj_pos);
211 vm_vec_normalize_quick(&vec_to_point); // MK, Optimization note: You compute distance about 15 lines up, this is partially redundant
212 dot = vm_vec_dot(&vec_to_point, &Objects[objnum].orient.fvec);
214 Dynamic_light[vertnum] += fixdiv(obj_intensity, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
216 if (Game_mode & GM_MULTI) {
217 if (dist < max_headlight_dist)
218 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
220 Dynamic_light[vertnum] += fixmul(fixmul(dot, dot), obj_intensity)/8;
223 Dynamic_light[vertnum] += fixdiv(obj_intensity, dist);
232 #define FLASH_LEN_FIXED_SECONDS (F1_0/3)
233 #define FLASH_SCALE (3*F1_0/FLASH_LEN_FIXED_SECONDS)
235 // ----------------------------------------------------------------------------------------------
236 void cast_muzzle_flash_light(int n_render_vertices, short *render_vertices)
240 short time_since_flash;
242 current_time = timer_get_fixed_seconds();
244 for (i=0; i<MUZZLE_QUEUE_MAX; i++) {
245 if (Muzzle_data[i].create_time) {
246 time_since_flash = current_time - Muzzle_data[i].create_time;
247 if (time_since_flash < FLASH_LEN_FIXED_SECONDS)
248 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);
250 Muzzle_data[i].create_time = 0; // turn off this muzzle flash
255 // Translation table to make flares flicker at different rates
256 fix Obj_light_xlate[16] =
257 {0x1234, 0x3321, 0x2468, 0x1735,
258 0x0123, 0x19af, 0x3f03, 0x232a,
259 0x2123, 0x39af, 0x0f03, 0x132a,
260 0x3123, 0x29af, 0x1f03, 0x032a};
262 // Flag array of objects lit last frame. Guaranteed to process this frame if lit last frame.
263 sbyte Lighting_objects[MAX_OBJECTS];
265 #define MAX_HEADLIGHTS 8
266 object *Headlights[MAX_HEADLIGHTS];
269 // ---------------------------------------------------------
270 fix compute_light_intensity(int objnum)
272 object *obj = &Objects[objnum];
273 int objtype = obj->type;
278 if (Players[obj->id].flags & PLAYER_FLAGS_HEADLIGHT_ON) {
279 if (Num_headlights < MAX_HEADLIGHTS)
280 Headlights[Num_headlights++] = obj;
281 return HEADLIGHT_SCALE;
282 } else if ((Game_mode & GM_HOARD) && Players[obj->id].secondary_ammo[PROXIMITY_INDEX]) {
284 // If hoard game and player, add extra light based on how many orbs you have
287 hoardlight=i2f(Players[obj->id].secondary_ammo[PROXIMITY_INDEX])/2; //i2f(12));
289 fix_sincos ((GameTime/2) & 0xFFFF,&s,NULL); // probably a bad way to do it
292 hoardlight=fixmul (s,hoardlight);
293 // mprintf ((0,"Hoardlight is %f!\n",f2fl(hoardlight)));
297 return max(vm_vec_mag_quick(&obj->mtype.phys_info.thrust)/4, F1_0*2) + F1_0/2;
300 if (obj->id != 0xff) {
301 if (obj->lifeleft < F1_0*4)
302 return fixmul(fixdiv(obj->lifeleft, Vclip[obj->id].play_time), Vclip[obj->id].light_value);
304 return Vclip[obj->id].light_value;
309 return F1_0*Robot_info[obj->id].lightcast;
312 fix tval = Weapon_info[obj->id].light;
313 if (Game_mode & GM_MULTI)
314 if (obj->id == OMEGA_ID)
316 return 0; // 3/4 of time, omega blobs will cast 0 light!
318 if (obj->id == FLARE_ID )
319 return 2* (min(tval, obj->lifeleft) + ((GameTime ^ Obj_light_xlate[objnum&0x0f]) & 0x3fff));
325 fix lightval = obj->lifeleft;
329 lightval = 8 * abs(F1_0/2 - lightval);
331 if (obj->lifeleft < F1_0*1000)
332 obj->lifeleft += F1_0; // Make sure this object doesn't go out.
338 return Powerup_info[obj->id].light;
344 return obj->ctype.light_info.intensity;
352 // ----------------------------------------------------------------------------------------------
353 void set_dynamic_light(void)
357 int n_render_vertices;
358 short render_vertices[MAX_VERTICES];
359 sbyte render_vertex_flags[MAX_VERTICES];
360 int render_seg,segnum, v;
361 sbyte new_lighting_objects[MAX_OBJECTS];
365 if (!Do_dynamic_light)
368 //if (Use_fvi_lighting)
369 // 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));
371 memset(render_vertex_flags, 0, Highest_vertex_index+1);
373 // Create list of vertices that need to be looked at for setting of ambient light.
374 n_render_vertices = 0;
375 for (render_seg=0; render_seg<N_render_segs; render_seg++) {
376 segnum = Render_list[render_seg];
378 short *vp = Segments[segnum].verts;
379 for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) {
381 if (vnum<0 || vnum>Highest_vertex_index) {
382 Int3(); //invalid vertex number
383 continue; //ignore it, and go on to next one
385 if (!render_vertex_flags[vnum]) {
386 render_vertex_flags[vnum] = 1;
387 render_vertices[n_render_vertices++] = vnum;
389 //--old way-- for (s=0; s<n_render_vertices; s++)
390 //--old way-- if (render_vertices[s] == vnum)
392 //--old way-- if (s == n_render_vertices)
393 //--old way-- render_vertices[n_render_vertices++] = vnum;
398 // -- for (vertnum=FrameCount&1; vertnum<n_render_vertices; vertnum+=2) {
399 for (vv=0; vv<n_render_vertices; vv++) {
402 vertnum = render_vertices[vv];
403 Assert(vertnum >= 0 && vertnum <= Highest_vertex_index);
404 if ((vertnum ^ FrameCount) & 1)
405 Dynamic_light[vertnum] = 0;
408 cast_muzzle_flash_light(n_render_vertices, render_vertices);
410 for (objnum=0; objnum<=Highest_object_index; objnum++)
411 new_lighting_objects[objnum] = 0;
413 // July 5, 1995: New faster dynamic lighting code. About 5% faster on the PC (un-optimized).
414 // Only objects which are in rendered segments cast dynamic light. We might wad6 to extend this
415 // one or two segments if we notice light changing as objects go offscreen. I couldn't see any
416 // serious visual degradation. In fact, I could see no humorous degradation, either. --MK
417 for (render_seg=0; render_seg<N_render_segs; render_seg++) {
418 int segnum = Render_list[render_seg];
420 objnum = Segments[segnum].objects;
422 while (objnum != -1) {
423 object *obj = &Objects[objnum];
424 vms_vector *objpos = &obj->pos;
427 obj_intensity = compute_light_intensity(objnum);
430 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, OBJECT_NUMBER(obj));
431 new_lighting_objects[objnum] = 1;
438 // Now, process all lights from last frame which haven't been processed this frame.
439 for (objnum=0; objnum<=Highest_object_index; objnum++) {
440 // In multiplayer games, process even unprocessed objects every 4th frame, else don't know about player sneaking up.
441 if ((Lighting_objects[objnum]) || ((Game_mode & GM_MULTI) && (((objnum ^ FrameCount) & 3) == 0))) {
442 if (!new_lighting_objects[objnum]) {
443 // Lighted last frame, but not this frame. Get intensity...
444 object *obj = &Objects[objnum];
445 vms_vector *objpos = &obj->pos;
448 obj_intensity = compute_light_intensity(objnum);
451 apply_light(obj_intensity, obj->segnum, objpos, n_render_vertices, render_vertices, objnum);
452 Lighting_objects[objnum] = 1;
454 Lighting_objects[objnum] = 0;
457 // Not lighted last frame, so we don't need to light it. (Already lit if casting light this frame.)
458 // But copy value from new_lighting_objects to update Lighting_objects array.
459 Lighting_objects[objnum] = new_lighting_objects[objnum];
464 // ---------------------------------------------------------
466 void toggle_headlight_active()
468 if (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) {
469 Players[Player_num].flags ^= PLAYER_FLAGS_HEADLIGHT_ON;
471 if (Game_mode & GM_MULTI)
472 multi_send_flags(Player_num);
477 #define HEADLIGHT_BOOST_SCALE 8 //how much to scale light when have headlight boost
479 fix Beam_brightness = (F1_0/2); //global saying how bright the light beam is
481 #define MAX_DIST_LOG 6 //log(MAX_DIST-expressed-as-integer)
482 #define MAX_DIST (f1_0<<MAX_DIST_LOG) //no light beyond this dist
484 fix compute_headlight_light_on_object(object *objp)
489 // Let's just illuminate players and robots for speed reasons, ok?
490 if ((objp->type != OBJ_ROBOT) && (objp->type != OBJ_PLAYER))
495 for (i=0; i<Num_headlights; i++) {
497 vms_vector vec_to_obj;
500 light_objp = Headlights[i];
502 vm_vec_sub(&vec_to_obj, &objp->pos, &light_objp->pos);
503 dist = vm_vec_normalize_quick(&vec_to_obj);
505 dot = vm_vec_dot(&light_objp->orient.fvec, &vec_to_obj);
508 light += fixdiv(HEADLIGHT_SCALE, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight.
510 light += fixmul(fixmul(dot, dot), HEADLIGHT_SCALE)/8;
518 // -- Unused -- //Compute the lighting from the headlight for a given vertex on a face.
519 // -- Unused -- //Takes:
520 // -- Unused -- // point - the 3d coords of the point
521 // -- Unused -- // face_light - a scale factor derived from the surface normal of the face
522 // -- Unused -- //If no surface normal effect is wanted, pass F1_0 for face_light
523 // -- Unused -- fix compute_headlight_light(vms_vector *point,fix face_light)
525 // -- Unused -- fix light;
526 // -- Unused -- int use_beam = 0; //flag for beam effect
528 // -- Unused -- light = Beam_brightness;
530 // -- 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) {
531 // -- Unused -- light *= HEADLIGHT_BOOST_SCALE;
532 // -- Unused -- use_beam = 1; //give us beam effect
535 // -- Unused -- if (light) { //if no beam, don't bother with the rest of this
536 // -- Unused -- fix point_dist;
538 // -- Unused -- point_dist = vm_vec_mag_quick(point);
540 // -- Unused -- if (point_dist >= MAX_DIST)
542 // -- Unused -- light = 0;
544 // -- Unused -- else {
545 // -- Unused -- fix dist_scale,face_scale;
547 // -- Unused -- dist_scale = (MAX_DIST - point_dist) >> MAX_DIST_LOG;
548 // -- Unused -- light = fixmul(light,dist_scale);
550 // -- Unused -- if (face_light < 0)
551 // -- Unused -- face_light = 0;
553 // -- Unused -- face_scale = f1_0/4 + face_light/2;
554 // -- Unused -- light = fixmul(light,face_scale);
556 // -- Unused -- if (use_beam) {
557 // -- Unused -- fix beam_scale;
559 // -- Unused -- if (face_light > f1_0*3/4 && point->z > i2f(12)) {
560 // -- Unused -- beam_scale = fixdiv(point->z,point_dist);
561 // -- Unused -- beam_scale = fixmul(beam_scale,beam_scale); //square it
562 // -- Unused -- light = fixmul(light,beam_scale);
568 // -- Unused -- return light;
571 //compute the average dynamic light in a segment. Takes the segment number
572 fix compute_seg_dynamic_light(int segnum)
578 seg = &Segments[segnum];
583 sum += Dynamic_light[*verts++];
584 sum += Dynamic_light[*verts++];
585 sum += Dynamic_light[*verts++];
586 sum += Dynamic_light[*verts++];
587 sum += Dynamic_light[*verts++];
588 sum += Dynamic_light[*verts++];
589 sum += Dynamic_light[*verts++];
590 sum += Dynamic_light[*verts];
596 fix object_light[MAX_OBJECTS];
597 int object_sig[MAX_OBJECTS];
599 int reset_lighting_hack;
601 #define LIGHT_RATE i2f(4) //how fast the light ramps up
603 void start_lighting_frame(object *viewer)
605 reset_lighting_hack = (viewer != old_viewer);
610 //compute the lighting for an object. Takes a pointer to the object,
611 //and possibly a rotated 3d point. If the point isn't specified, the
612 //object's center point is rotated.
613 fix compute_object_light(object *obj,vms_vector *rotated_pnt)
617 int objnum = OBJECT_NUMBER(obj);
620 g3_rotate_point(&objpnt,&obj->pos);
621 rotated_pnt = &objpnt.p3_vec;
624 //First, get static light for this segment
626 light = Segment2s[obj->segnum].static_light;
631 //Now, maybe return different value to smooth transitions
633 if (!reset_lighting_hack && object_sig[objnum] == obj->signature) {
634 fix delta_light,frame_delta;
636 delta_light = light - object_light[objnum];
638 frame_delta = fixmul(LIGHT_RATE,FrameTime);
640 if (abs(delta_light) <= frame_delta)
642 object_light[objnum] = light; //we've hit the goal
647 light = object_light[objnum] -= frame_delta;
649 light = object_light[objnum] += frame_delta;
652 else { //new object, initialize
654 object_sig[objnum] = obj->signature;
655 object_light[objnum] = light;
660 //Next, add in headlight on this object
662 // -- Matt code: light += compute_headlight_light(rotated_pnt,f1_0);
663 light += compute_headlight_light_on_object(obj);
665 //Finally, add in dynamic light for this segment
667 light += compute_seg_dynamic_light(obj->segnum);