Debian Codegraph @

editor/rad_cast.cpp

raw
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/editor/rad_cast.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Ray casting for radiosity, I think.
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 87 5/20/99 11:50a Jason
30 * made super detail not do volume lights super detail
31 *
32 * 86 4/18/99 3:37p 3dsmax
33 * tweaks
34 *
35 * 85 4/18/99 5:42a Chris
36 * Added the FQ_IGNORE_RENDER_THROUGH_PORTALS flag
37 *
38 * 84 4/16/99 5:50p Jason
39 * tweaked smooth specular stuff some more
40 *
41 * 83 4/15/99 2:56p Jason
42 * fixed stupid lighting bug
43 *
44 * 82 4/05/99 10:36a 3dsmax
45 * took out mprintfs
46 *
47 * 81 4/02/99 5:34p Jason
48 * added verbose mprintfs for super detail
49 *
50 * 80 3/23/99 4:09p Jason
51 * upped smooth specular sensitivity
52 *
53 * 79 3/11/99 1:07p Jason
54 * more fixes for smooth specular lighting
55 *
56 * 78 3/10/99 7:12p Jason
57 * added smooth specular shading for curved surfaces
58 *
59 * 77 3/10/99 2:09p Jason
60 * made better specular lighting
61 *
62 * 76 2/24/99 5:14p 3dsmax
63 *
64 *
65 * 75 2/09/99 9:59a Chris
66 * Massive BOA update :) Terrain happy now. Vis happy now. Sound happy
67 * now.
68 *
69 * 74 1/27/99 12:19p Dan
70 * another clipping fix
71 *
72 * 73 1/27/99 12:16p Dan
73 * finally fixed dumb clipping problem
74 *
75 * 72 1/27/99 11:06a Jason
76 * fixed satellite clipping problem
77 *
78 * 71 1/24/99 1:17p Jason
79 * fixed satellite casting problem
80 *
81 * 70 12/21/98 11:52a Jason
82 * changes for specular lighting
83 *
84 * 69 11/19/98 12:22p Jason
85 * optimizations
86 *
87 * 68 10/28/98 12:18p Jason
88 * sped up volume lighting a bit
89 *
90 * 67 10/05/98 6:53p Jason
91 * took out some unused variables
92 *
93 * 66 10/02/98 11:55a Jason
94 * more fixes for satellite clipping
95 *
96 * 65 9/24/98 11:27a Jason
97 * fixed satellite clipping
98 *
99 * 64 8/21/98 12:29p Jason
100 * upped specular lighting threshold
101 *
102 * 63 8/10/98 11:25a Jason
103 * added better specular lighting
104 *
105 * 62 6/30/98 3:12p Jason
106 * sped up super detail by not doing surper-detail from surfaces that
107 * aren't lightsources
108 *
109 * 61 6/03/98 12:22p Jason
110 * specular lighting optimizations
111 *
112 * 60 6/02/98 6:03p Jason
113 * added specular lightmaps
114 *
115 * 59 6/02/98 11:01a Jason
116 * Post E3 Checkin
117 *
118 * 58 5/26/98 10:11p Sean
119 * don't do specular lighting for single rooms
120 *
121 * 57 5/25/98 3:46p Jason
122 * added better light glows
123 *
124 * 56 5/22/98 3:28p Jason
125 * added specular lighting
126 *
127 * 55 5/21/98 11:51a Jason
128 * made volume lights work with super detail
129 *
130 * 54 5/20/98 1:37p Jason
131 * upped facelimit on volume raycasting
132 *
133 * 53 5/19/98 2:36p Jason
134 * improved volume light casting
135 *
136 * 52 5/15/98 5:41p Jason
137 * implemented volume lighting system
138 *
139 * 51 5/01/98 4:32p Jason
140 * temp fix for lighting
141 *
142 * 50 4/19/98 5:23p Jason
143 * made terrain satellites only cast light on rooms that touch terrain
144 *
145 * 49 4/08/98 3:26p Jason
146 * made terrain satellites automatically cast light into rooms
147 *
148 * 48 3/17/98 5:15p Jason
149 * changed normal casting length
150 *
151 * 47 3/16/98 5:47p Jason
152 * added FQ_NO_RELINK
153 *
154 * 46 3/11/98 6:14p Jason
155 * better lighting, again
156 *
157 * 45 3/11/98 5:13p Jason
158 * always draw an object if any point is behind you
159 *
160 * 44 3/06/98 3:54p Jason
161 * fixed bug with satellite lighting indoors
162 *
163 * 43 3/06/98 3:23p Jason
164 * added lighting from satellites to indoor rooms
165 *
166 * 42 2/27/98 5:58p Jason
167 * fixed terrain lighting problem
168 *
169 * 41 2/25/98 1:02p Jason
170 * offset lights and dests by 1/8 of a normal
171 *
172 * 40 2/23/98 6:57p Jason
173 * fixed bug caused by my last rev
174 *
175 * 39 2/23/98 6:50p Jason
176 * changes to help facilitate fast lighting with shadow volumes
177 *
178 *
179 * 38 2/22/98 3:05p Jason
180 * sped up raycasting by switching the directions rays get shot. This
181 * allows us to ignore backfaces
182 *
183 * 37 2/19/98 5:44p Jason
184 * made super detail work correctly
185 *
186 * 36 2/09/98 3:45p Jason
187 * changed normal distance offset to be friendly with BSP tree stuff
188 *
189 * 35 2/06/98 12:35p Jason
190 * fixed problem with external rooms
191 *
192 * 34 2/05/98 4:20p Matt
193 * Fixed terrain lighting broken in roomnum/cellnum change
194 *
195 * 33 2/04/98 6:23p Matt
196 * Changed object room number to indicate a terrain cell via a flag. Got
197 * rid of the object flag which used to indicate terrain.
198 *
199 */
200
201 #include <stdlib.h>
202 #include <memory.h>
203 #include "radiosity.h"
204 #include "rad_cast.h"
205 #include "terrain.h"
206 #include "room.h"
207 #include "editor_lighting.h"
208 #include "findintersection.h"
209 #include "special_face.h"
210 #include "bsp.h"
211 #include "boa.h"
212
213 // A ray must contribute at least this amount to be accepted (currently .5%)
214 #define DEFAULT_IGNORE_LIMIT .005f
215
216 float Ignore_limit = DEFAULT_IGNORE_LIMIT;
217 extern rad_element *rad_MaxElement;
218
219 float Current_max_specular_strength;
220 spectra Current_max_specular_color;
221
222 // Returns 1 if a src vector can hit dest vector for a volume point
223 int ShootRayToVolumePoint(vector *src, vector *dest, int start_room) {
224 fvi_info hit_info;
225 fvi_query fq;
226
227 if (UseBSP) {
228 int fate = BSPRayOccluded(src, dest, MineBSP.root);
229 if (!fate)
230 return 1;
231 return 0;
232 }
233
234 // shoot a ray from the light position to the current vertex
235 fq.p0 = src;
236 fq.p1 = dest;
237
238 fq.startroom = start_room;
239
240 fq.rad = 0.0f;
241 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_NON_LIGHTMAP_OBJECTS | FQ_NO_RELINK | FQ_IGNORE_RENDER_THROUGH_PORTALS;
242 fq.thisobjnum = -1;
243 fq.ignore_obj_list = NULL;
244
245 int fate = fvi_FindIntersection(&fq, &hit_info);
246
247 float dist = vm_VectorDistance(&hit_info.hit_pnt, dest);
248 if (dist > .1)
249 return 0;
250 return 1;
251 }
252
253 void ClipSatelliteToTerrain(vector *answer, vector *src_vec, vector *dest_vec) {
254 vector cur_vec = *src_vec;
255 float terrain_limit = 256 * TERRAIN_SIZE;
256 float mag, diff;
257 vector ray;
258
259 // Check ceiling
260 if (cur_vec.y > (MAX_TERRAIN_HEIGHT * 30)) {
261 ray = cur_vec - *dest_vec;
262
263 mag = vm_GetMagnitude(&ray);
264 ray /= mag;
265
266 diff = ((MAX_TERRAIN_HEIGHT * 30) - dest_vec->y) / ray.y;
267
268 cur_vec = (*dest_vec + (ray * diff)) - (ray / 4);
269 }
270
271 // Check right edge
272 if (cur_vec.x > terrain_limit) {
273 ray = cur_vec - *dest_vec;
274
275 mag = vm_GetMagnitude(&ray);
276 ray /= mag;
277
278 diff = (terrain_limit - dest_vec->x) / ray.x;
279
280 cur_vec = (*dest_vec + (ray * diff)) - (ray / 4);
281 }
282
283 // Check left edge
284 if (cur_vec.x < 0) {
285 ray = cur_vec - *dest_vec;
286
287 mag = vm_GetMagnitude(&ray);
288 ray /= mag;
289
290 diff = (-dest_vec->x) / ray.x;
291
292 cur_vec = (*dest_vec + (ray * diff)) - (ray / 4);
293 }
294
295 // Check top edge
296 if (cur_vec.z > terrain_limit) {
297 ray = cur_vec - *dest_vec;
298
299 mag = vm_GetMagnitude(&ray);
300 ray /= mag;
301
302 diff = (terrain_limit - dest_vec->z) / ray.z;
303
304 cur_vec = (*dest_vec + (ray * diff)) - (ray / 4);
305 }
306
307 // Check bottom edge
308 if (cur_vec.z < 0) {
309 ray = cur_vec - *dest_vec;
310
311 mag = vm_GetMagnitude(&ray);
312 ray /= mag;
313
314 diff = (-dest_vec->z) / ray.z;
315
316 cur_vec = (*dest_vec + (ray * diff)) - (ray / 4);
317 }
318
319 if (cur_vec.x > 4095)
320 cur_vec.x = 4095;
321 if (cur_vec.z > 4095)
322 cur_vec.z = 4095;
323
324 *answer = cur_vec;
325 }
326
327 // Returns 1 if a src vector can hit dest vector unobstructed, else 0
328 int ShootRayFromPoint(vector *src, vector *dest, rad_surface *src_surf, rad_surface *dest_surf) {
329 float dist;
330 bool do_backface = 0;
331 fvi_info hit_info;
332 fvi_query fq;
333 vector temp_src = *src;
334 vector temp_dest = *dest;
335 int from_satellite = 0;
336
337 // Trivially reject all rooms
338 if (dest_surf->surface_type == ST_ROOM || dest_surf->surface_type == ST_ROOM_OBJECT) {
339 if (src_surf->surface_type == ST_ROOM || src_surf->surface_type == ST_ROOM_OBJECT) {
340 if (!BOA_IsVisible(dest_surf->roomnum, src_surf->roomnum))
341 return 0;
342 }
343 }
344
345 if (UseBSP) {
346 if (dest_surf->surface_type == ST_ROOM || dest_surf->surface_type == ST_ROOM_OBJECT) {
347 if (src_surf->surface_type == ST_ROOM || src_surf->surface_type == ST_ROOM_OBJECT) {
348 int fate = BSPRayOccluded(src, dest, MineBSP.root);
349 if (!fate)
350 return 1;
351 return 0;
352 }
353 }
354 }
355
356 // If this ray is too high, clip it to the ceiling
357 if (src_surf->surface_type == ST_SATELLITE) {
358 if (1 || dest->y >= MAX_TERRAIN_HEIGHT) {
359 from_satellite = 1;
360 // swap the src/dest the variables because we now want to shoot from the ground to the satellite
361 rad_surface *temp_surf;
362 vector *temp_vec;
363
364 temp_surf = src_surf;
365 src_surf = dest_surf;
366 dest_surf = temp_surf;
367
368 temp_vec = src;
369 src = dest;
370 dest = temp_vec;
371
372 temp_dest = *dest;
373 temp_src = *src;
374
375 ClipSatelliteToTerrain(&temp_src, src, &temp_dest);
376 } else {
377 ClipSatelliteToTerrain(&temp_src, src, &temp_dest);
378 int src_cell = GetTerrainCellFromPos(&temp_src);
379
380 if (src_cell < 0) {
381 src_cell = 0;
382 Int3(); // Get Jason, satellite clipped off terrain?
383 }
384
385 src_surf->roomnum = MAKE_ROOMNUM(src_cell);
386 }
387 }
388
389 // shoot a ray from the light position to the current vertex
390 fq.p0 = &temp_src;
391 fq.p1 = &temp_dest;
392
393 if (src_surf->surface_type == ST_EXTERNAL_ROOM)
394 fq.startroom = GetTerrainRoomFromPos(src);
395 else
396 fq.startroom = src_surf->roomnum;
397
398 fq.rad = 0.0f;
399 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_NON_LIGHTMAP_OBJECTS | FQ_NO_RELINK | FQ_IGNORE_RENDER_THROUGH_PORTALS;
400 fq.thisobjnum = -1;
401 fq.ignore_obj_list = NULL;
402
403 int fate = fvi_FindIntersection(&fq, &hit_info);
404
405 if (from_satellite) {
406 if (fate == HIT_NONE || fate == HIT_OUT_OF_TERRAIN_BOUNDS)
407 return 1;
408 else
409 return 0;
410 }
411
412 dist = vm_VectorDistance(&hit_info.hit_pnt, &temp_dest);
413 if (dist > .1) {
414 // mprintf(0,"Didn't hit!\n");
415 return 0;
416 }
417
418 return 1;
419 }
420
421 int Rays_ignored = 0;
422
423 float GetMaxColor(spectra *sp);
424
425 float GetFormFactorForElementAndSatellite(rad_surface *dest_surf, rad_element *dest_element, vector *src_center) {
426 vector dest_center;
427 vector light_center = *src_center;
428 float form_factor = 0.0;
429
430 if (Ignore_satellites)
431 return 0;
432
433 // Bash src center to first element vertex (if sun or moon)
434 light_center = rad_MaxSurface->elements[0].verts[0];
435
436 for (int j = 0; j < dest_element->num_verts; j++) {
437 dest_center = dest_element->verts[j];
438 dest_center += (dest_surf->normal / 32.0);
439 float temp_factor = 0;
440
441 int hit = 0;
442 vector ray = light_center - dest_center;
443
444 if (dest_surf->surface_type != ST_TERRAIN) {
445 if ((vm_DotProduct(&ray, &dest_surf->normal)) < 0)
446 continue;
447 }
448
449 // If this surface is a terrain surface, use the terrain speedup table
450 if (dest_surf->surface_type == ST_TERRAIN) {
451 ASSERT(ROOMNUM_OUTSIDE(dest_surf->roomnum));
452 int cellnum = CELLNUM(dest_surf->roomnum);
453 if (dest_surf->facenum == 0) {
454 if (j == 1)
455 cellnum += TERRAIN_WIDTH;
456 else if (j == 2)
457 cellnum += (TERRAIN_WIDTH + 1);
458 } else {
459 if (j == 1)
460 cellnum += (TERRAIN_WIDTH + 1);
461 else if (j == 2)
462 cellnum++;
463 }
464
465 hit = TerrainLightSpeedup[rad_MaxSurface->roomnum][cellnum];
466 } else
467 hit = ShootRayFromPoint(&light_center, &dest_center, rad_MaxSurface, dest_surf);
468
469 if (hit) {
470 float ray_length = vm_GetMagnitude(&ray);
471 vector dest_norm_ray = ray / ray_length;
472 vector dest_normal = dest_surf->normal;
473 float ff;
474
475 ff = vm_DotProduct(&dest_normal, &dest_norm_ray);
476
477 if (ff > 0)
478 temp_factor += ff;
479 }
480
481 form_factor += temp_factor;
482 }
483
484 form_factor /= dest_element->num_verts;
485
486 if (form_factor > 1)
487 mprintf(0, "form factor >1!\n");
488
489 return form_factor;
490 }
491
492 // Calculates the percentage of specular light that is coming from the
493 // currently shooting patch
494 void CheckToUpdateSpecularFace(rad_surface *dest_surf, spectra *color, vector *src_center) {
495 vector light_center;
496
497 float total_strength = (color->r * .3) + (color->g * .33) + (color->b * .33);
498 float threshold = .7f;
499
500 if (dest_surf->surface_type != ST_ROOM)
501 return;
502
503 if (Rooms[dest_surf->roomnum].faces[dest_surf->facenum].special_handle == BAD_SPECIAL_FACE_INDEX)
504 return;
505
506 if (src_center == NULL)
507 GetCenterOfSurface(rad_MaxSurface, &light_center);
508 else
509 light_center = *src_center;
510
511 room *rp = &Rooms[dest_surf->roomnum];
512 face *fp = &rp->faces[dest_surf->facenum];
513
514 if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR)
515 threshold = .01f;
516 else {
517 if (!(rad_MaxSurface->flags & SF_LIGHTSOURCE))
518 return;
519 }
520
521 int i, t;
522
523 for (i = 0; i < 4; i++) {
524 if (total_strength > threshold &&
525 total_strength > Room_strongest_value[dest_surf->roomnum][i][dest_surf->facenum]) {
526 float scalar = (total_strength / 50.0) + .5;
527 if (scalar > 1)
528 scalar = 1.0;
529
530 int special_index = Rooms[dest_surf->roomnum].faces[dest_surf->facenum].special_handle;
531
532 // Move the others down
533 for (t = 2; t >= i; t--) {
534 Room_strongest_value[dest_surf->roomnum][t + 1][dest_surf->facenum] =
535 Room_strongest_value[dest_surf->roomnum][t][dest_surf->facenum];
536 SpecialFaces[special_index].spec_instance[t + 1].bright_color =
537 SpecialFaces[special_index].spec_instance[t].bright_color;
538 SpecialFaces[special_index].spec_instance[t + 1].bright_center =
539 SpecialFaces[special_index].spec_instance[t].bright_center;
540 }
541
542 // Update our arrays with strongest light and src patch center
543 Room_strongest_value[dest_surf->roomnum][i][dest_surf->facenum] = total_strength;
544
545 float rmax = GetMaxColor(color);
546
547 float r = color->r;
548 float g = color->g;
549 float b = color->b;
550
551 // Normalize colors
552 if (rmax > 1) {
553 r /= rmax;
554 g /= rmax;
555 b /= rmax;
556 }
557
558 r *= (255.0 * scalar);
559 g *= (255.0 * scalar);
560 b *= (255.0 * scalar);
561
562 SpecialFaces[special_index].spec_instance[i].bright_color = GR_RGB16((int)r, (int)g, (int)b);
563 SpecialFaces[special_index].spec_instance[i].bright_center = light_center;
564
565 return;
566 }
567 }
568 }
569
570 float GetFormFactorForElement(rad_surface *dest_surf, rad_element *dest_element, vector *src_center) {
571 int touched = 0;
572 vector dest_center;
573 int i, limit;
574 float ray_area;
575 int ignored = 0;
576 vector light_center = *src_center;
577 float form_factor = 0.0;
578 int multiple_shoots = 0;
579
580 ray_area = rad_MaxSurface->area;
581
582 float rmax = GetMaxColor(&rad_MaxSurface->exitance);
583
584 multiple_shoots = 0;
585 limit = 1;
586
587 for (i = 0; i < limit; i++) {
588 float temp_factor = 0.0;
589
590 if (multiple_shoots && (rad_MaxSurface->elements[i].flags & EF_IGNORE))
591 continue;
592
593 if (multiple_shoots) {
594 GetCenterOfElement(&rad_MaxSurface->elements[i], &light_center);
595 light_center += (rad_MaxSurface->normal / 16.0);
596 ray_area = rad_MaxSurface->elements[i].area;
597 } else
598 ray_area = rad_MaxSurface->area;
599
600 GetCenterOfElement(dest_element, &dest_center);
601 dest_center += (dest_surf->normal / 16.0);
602
603 int hit = 0;
604 vector ray = light_center - dest_center;
605 vector revray = dest_center - light_center;
606
607 if ((vm_DotProduct(&ray, &dest_surf->normal)) < 0)
608 continue;
609 if ((vm_DotProduct(&revray, &rad_MaxSurface->normal)) < 0)
610 continue;
611
612 // Check to see if this ray even matters
613 float ray_length = vm_GetMagnitudeFast(&ray);
614 vector dest_norm_ray = ray / ray_length;
615 vector src_norm_ray = -dest_norm_ray;
616 vector dest_normal = dest_surf->normal;
617 float ff;
618 vector src_normal = rad_MaxSurface->normal;
619 ff = (vm_DotProduct(&src_norm_ray, &src_normal) * vm_DotProduct(&dest_normal, &dest_norm_ray)) /
620 ((3.14 * ray_length * ray_length) + ray_area);
621 ff *= ray_area;
622 ff *= rmax;
623
624 // If shooting from elements, scale the test up
625 if (multiple_shoots)
626 ff *= rad_MaxSurface->area / rad_MaxSurface->elements[i].area;
627
628 if (ff < Ignore_limit) {
629 ignored++;
630 continue;
631 }
632
633 hit = ShootRayFromPoint(&light_center, &dest_center, rad_MaxSurface, dest_surf);
634
635 if (hit) {
636 // We got a hit...figure out how much light contribution this light has
637
638 ray_length = vm_GetMagnitude(&ray);
639 dest_norm_ray = ray / ray_length;
640 src_norm_ray = -dest_norm_ray;
641 dest_normal = dest_surf->normal;
642 src_normal = rad_MaxSurface->normal;
643
644 ff = (vm_DotProduct(&src_norm_ray, &src_normal) * vm_DotProduct(&dest_normal, &dest_norm_ray)) /
645 ((3.14 * ray_length * ray_length) + ray_area);
646
647 if (ff > 1)
648 mprintf(0, "ff >1!\n");
649
650 if (ff > 0)
651 temp_factor += ff;
652
653 // Update specularity if needed
654 if (rad_MaxSurface->surface_type == ST_ROOM && Calculate_specular_lighting) {
655 spectra color;
656 float scalar = ff;
657
658 scalar *= ray_area;
659 scalar *= 5;
660
661 color.r = rad_MaxSurface->exitance.r * scalar;
662 color.g = rad_MaxSurface->exitance.g * scalar;
663 color.b = rad_MaxSurface->exitance.b * scalar;
664
665 float total_strength = (color.r * .33) + (color.g * .33) + (color.b * .33);
666 if (total_strength > Current_max_specular_strength) {
667 Current_max_specular_strength = total_strength;
668 Current_max_specular_color = color;
669 }
670 }
671 }
672
673 temp_factor *= ray_area;
674
675 // temp_factor/=dest_element->num_verts;
676
677 // if (multiple_shoots)
678 // temp_factor*=(rad_MaxSurface->elements[i].area/rad_MaxSurface->area);
679
680 form_factor += temp_factor;
681
682 if (form_factor > 1)
683 mprintf(0, "form factor >1! val=%f\n", form_factor);
684 }
685
686 if (ignored > 0)
687 Rays_ignored++;
688
689 return form_factor;
690 }
691
692 float GetFormFactorForElementSuperDetail(rad_surface *dest_surf, rad_element *dest_element) {
693 int touched = 0;
694 vector dest_center;
695 float ray_area;
696 int ignored = 0;
697 vector light_center;
698 vector patch_center;
699 float form_factor = 0.0;
700 int multiple_shoots = 0;
701
702 ray_area = rad_MaxElement->area;
703
704 if (rad_MaxElement->flags & EF_IGNORE)
705 return 0;
706
707 float rmax = GetMaxColor(&rad_MaxElement->exitance);
708
709 float temp_factor = 0.0;
710
711 GetCenterOfSurface(rad_MaxSurface, &patch_center);
712 GetCenterOfElement(rad_MaxElement, &light_center);
713 light_center += (rad_MaxSurface->normal / 16.0);
714
715 GetCenterOfElement(dest_element, &dest_center);
716 dest_center += (dest_surf->normal / 16.0);
717
718 int hit = 0;
719 vector ray = light_center - dest_center;
720 vector revray = dest_center - light_center;
721
722 if ((vm_DotProduct(&ray, &dest_surf->normal)) < 0)
723 return 0;
724 if ((vm_DotProduct(&revray, &rad_MaxSurface->normal)) < 0)
725 return 0;
726
727 // Check to see if this ray even matters
728 float ray_length = vm_GetMagnitudeFast(&ray);
729 vector dest_norm_ray = ray / ray_length;
730 vector src_norm_ray = -dest_norm_ray;
731 vector dest_normal = dest_surf->normal;
732 float ff;
733 vector src_normal = rad_MaxSurface->normal;
734 ff = (vm_DotProduct(&src_norm_ray, &src_normal) * vm_DotProduct(&dest_normal, &dest_norm_ray)) /
735 ((3.14 * ray_length * ray_length) + ray_area);
736 ff *= ray_area;
737 ff *= rmax;
738
739 // If shooting from elements, scale the test up
740 ff *= rad_MaxSurface->area / rad_MaxElement->area;
741
742 if (ff < Ignore_limit) {
743 Rays_ignored++;
744 return 0;
745 }
746
747 hit = ShootRayFromPoint(&light_center, &dest_center, rad_MaxSurface, dest_surf);
748
749 if (hit) {
750 // We got a hit...figure out how much light contribution this light has
751
752 ray_length = vm_GetMagnitude(&ray);
753 dest_norm_ray = ray / ray_length;
754 src_norm_ray = -dest_norm_ray;
755 dest_normal = dest_surf->normal;
756 src_normal = rad_MaxSurface->normal;
757
758 ff = (vm_DotProduct(&src_norm_ray, &src_normal) * vm_DotProduct(&dest_normal, &dest_norm_ray)) /
759 ((3.14 * ray_length * ray_length) + ray_area);
760
761 if (ff > 1)
762 mprintf(0, "ff >1!\n");
763
764 if (ff > 0)
765 temp_factor += ff;
766
767 // Update specularity if needed
768 if (rad_MaxSurface->surface_type == ST_ROOM && Calculate_specular_lighting) {
769 spectra color;
770 float scalar = ff;
771
772 scalar *= ray_area;
773 scalar *= (rad_MaxSurface->area / ray_area);
774 scalar *= 5;
775
776 color.r = rad_MaxSurface->exitance.r * scalar;
777 color.g = rad_MaxSurface->exitance.g * scalar;
778 color.b = rad_MaxSurface->exitance.b * scalar;
779
780 float total_strength = (color.r * .33) + (color.g * .33) + (color.b * .33);
781 if (total_strength > Current_max_specular_strength) {
782 Current_max_specular_strength = total_strength;
783 Current_max_specular_color = color;
784 }
785 }
786 }
787
788 temp_factor *= ray_area;
789
790 form_factor += temp_factor;
791
792 if (form_factor > 1)
793 mprintf(0, "form factor >1! val=%f\n", form_factor);
794
795 return form_factor;
796 }
797
798 // Calculates the volume lighting for the currently shooting patch
799 void CalculateVolumeLightsForRay(float total_sphere_dist, vector *src_center) {
800 float sphere_dist = total_sphere_dist;
801 int i, t;
802
803 // Do volume lighting
804 if ((rad_MaxSurface->surface_type == ST_ROOM || rad_MaxSurface->surface_type == ST_ROOM_OBJECT)) {
805 if (sphere_dist > .1) {
806 fvi_face_room_list facelist[5000];
807 uint8_t check_room[MAX_VOLUME_ELEMENTS];
808
809 memset(check_room, 0, MAX_VOLUME_ELEMENTS);
810
811 // Build a list of rooms to check
812 int num_faces = fvi_QuickDistFaceList(rad_MaxSurface->roomnum, src_center, sphere_dist, facelist, 4000);
813 check_room[rad_MaxSurface->roomnum] = 1;
814
815 for (i = 0; i < num_faces; i++) {
816 if (Rooms[facelist[i].room_index].flags & RF_EXTERNAL)
817 continue;
818 check_room[facelist[i].room_index] = 1;
819 }
820
821 for (int roomnum = 0; roomnum < MAX_VOLUME_ELEMENTS; roomnum++) {
822 if (Volume_elements[roomnum] == NULL)
823 continue;
824 if (check_room[roomnum] == 0)
825 continue;
826
827 if (!BOA_IsVisible(roomnum, rad_MaxSurface->roomnum)) {
828 continue;
829 }
830
831 int w = Rooms[roomnum].volume_width;
832 int h = Rooms[roomnum].volume_height;
833 int d = Rooms[roomnum].volume_depth;
834
835 if (0 && !Shoot_from_patch && (rad_MaxSurface->flags & SF_LIGHTSOURCE)) // super detail
836 {
837 int src_num_elements = rad_MaxSurface->xresolution * rad_MaxSurface->yresolution;
838
839 for (int k = 0; k < src_num_elements; k++) {
840 vector light_center;
841 rad_MaxElement = &rad_MaxSurface->elements[k];
842
843 if (rad_MaxElement->flags & EF_IGNORE)
844 continue;
845
846 GetCenterOfElement(rad_MaxElement, &light_center);
847 light_center += (rad_MaxSurface->normal / 4);
848
849 sphere_dist = total_sphere_dist;
850 // sphere_dist*=(rad_MaxElement->area/rad_MaxSurface->area);
851
852 for (i = 0; i < d; i++) {
853 for (t = 0; t < h; t++) {
854 for (int j = 0; j < w; j++) {
855 volume_element *ve = &Volume_elements[roomnum][(i * w * h) + (t * w) + j];
856
857 if (ve->color.r < 0)
858 continue;
859
860 vector subvec = ve->pos - light_center;
861 float mag = vm_GetMagnitudeFast(&subvec);
862
863 if (mag > sphere_dist)
864 continue;
865
866 subvec /= mag;
867 int hit;
868
869 float scalar = subvec * rad_MaxSurface->normal;
870
871 if (scalar < 0)
872 continue;
873
874 scalar /= ((3.14 * mag * mag) + rad_MaxElement->area);
875 scalar *= rad_MaxElement->area;
876
877 // Trivially reject all rooms
878
879 if (!BOA_IsVisible(roomnum, rad_MaxSurface->roomnum)) {
880 hit = 0;
881 } else {
882 if (ve->flags & VEF_REVERSE_SHOOT)
883 hit = ShootRayToVolumePoint(&ve->pos, &light_center, roomnum);
884 else
885 hit = ShootRayToVolumePoint(&light_center, &ve->pos, rad_MaxSurface->roomnum);
886 }
887
888 if (hit) {
889
890 ve->color.r += rad_MaxElement->exitance.r * scalar;
891 ve->color.g += rad_MaxElement->exitance.g * scalar;
892 ve->color.b += rad_MaxElement->exitance.b * scalar;
893 }
894 }
895 }
896 }
897 }
898
899 } else {
900 for (i = 0; i < d; i++) {
901 for (t = 0; t < h; t++) {
902 for (int j = 0; j < w; j++) {
903 volume_element *ve = &Volume_elements[roomnum][(i * w * h) + (t * w) + j];
904
905 if (ve->color.r < 0)
906 continue;
907
908 vector subvec = ve->pos - *src_center;
909 float mag = vm_GetMagnitudeFast(&subvec);
910
911 if (mag > sphere_dist)
912 continue;
913
914 subvec /= mag;
915 int hit;
916
917 float scalar = subvec * rad_MaxSurface->normal;
918
919 if (scalar < 0)
920 continue;
921
922 scalar /= ((3.14 * mag * mag) + rad_MaxSurface->area);
923 scalar *= rad_MaxSurface->area;
924
925 if (!BOA_IsVisible(roomnum, rad_MaxSurface->roomnum)) {
926 hit = 0;
927 } else {
928 if (ve->flags & VEF_REVERSE_SHOOT)
929 hit = ShootRayToVolumePoint(&ve->pos, src_center, roomnum);
930 else
931 hit = ShootRayToVolumePoint(src_center, &ve->pos, rad_MaxSurface->roomnum);
932 }
933
934 if (hit) {
935 ve->color.r += rad_MaxSurface->exitance.r * scalar;
936 ve->color.g += rad_MaxSurface->exitance.g * scalar;
937 ve->color.b += rad_MaxSurface->exitance.b * scalar;
938 }
939 }
940 }
941 }
942 }
943 }
944 }
945 }
946 }
947
948 // Calculates the form factors using a raycasting method
949 void CalculateFormFactorsRaycast() {
950 int i, t, k;
951 int ignore;
952 vector src_center;
953 float form_factor;
954 int raycount = 0;
955 Rays_ignored = 0;
956
957 if (rad_MaxSurface->surface_type == ST_SATELLITE) {
958 src_center = rad_MaxSurface->verts[0];
959 } else {
960 GetCenterOfSurface(rad_MaxSurface, &src_center);
961 src_center += (rad_MaxSurface->normal / 16.0);
962 }
963
964 float sphere_dist = 0;
965 // Get the max area of influence for this light
966 if (rad_MaxSurface->surface_type == ST_ROOM || rad_MaxSurface->surface_type == ST_ROOM_OBJECT) {
967 float rmax = GetMaxColor(&rad_MaxSurface->exitance);
968 float power = rmax * rad_MaxSurface->area;
969 float temp_ignore = Ignore_limit / power;
970 float express = 1.0 / temp_ignore;
971 express /= 3.14f;
972 express -= rad_MaxSurface->area;
973 sphere_dist = sqrt(express);
974 if (sphere_dist > 0)
975 fvi_QuickDistFaceList(rad_MaxSurface->roomnum, &src_center, sphere_dist, NULL, rad_NumSurfaces);
976 }
977
978 // Do volume lighting
979 if (Do_volume_lighting)
980 CalculateVolumeLightsForRay(sphere_dist, &src_center);
981
982 // Shoot this patches light to each element within range
983 for (i = 0; i < rad_NumSurfaces; i++) {
984 rad_surface *dest_surf = &rad_Surfaces[i];
985
986 Current_max_specular_strength = 0;
987 Current_max_specular_color.r = 0;
988 Current_max_specular_color.g = 0;
989 Current_max_specular_color.b = 0;
990
991 // Check for self surface
992
993 if (rad_MaxSurface == dest_surf)
994 ignore = 1;
995 else
996 ignore = 0;
997
998 if (dest_surf->surface_type == ST_PORTAL)
999 ignore = 1;
1000
1001 if (dest_surf->surface_type == ST_SATELLITE)
1002 ignore = 1;
1003
1004 if (rad_MaxSurface->surface_type == ST_ROOM || rad_MaxSurface->surface_type == ST_ROOM_OBJECT) {
1005 if (dest_surf->surface_type == ST_ROOM) {
1006 if (!(Rooms[dest_surf->roomnum].faces[dest_surf->facenum].flags & FF_TOUCHED))
1007 ignore = 1;
1008
1009 Rooms[dest_surf->roomnum].faces[dest_surf->facenum].flags &= ~FF_TOUCHED;
1010 }
1011 }
1012
1013 if (dest_surf->surface_type == ST_ROOM)
1014 Rooms[dest_surf->roomnum].faces[dest_surf->facenum].flags &= ~FF_TOUCHED;
1015
1016 int dest_num_elements = dest_surf->xresolution * dest_surf->yresolution;
1017
1018 // Ignore this surface if we're shooting from a satellite and we cant possibly see it
1019 if (rad_MaxSurface->surface_type == ST_SATELLITE) {
1020 if (dest_surf->surface_type == ST_ROOM || dest_surf->surface_type == ST_ROOM_OBJECT) {
1021 if (!dest_surf->flags & SF_TOUCHES_TERRAIN)
1022 ignore = 1;
1023 }
1024 }
1025
1026 if (ignore) {
1027 raycount += dest_num_elements;
1028 continue;
1029 }
1030
1031 for (t = 0; t < dest_num_elements; t++) {
1032 rad_element *dest_element = &dest_surf->elements[t];
1033
1034 if ((raycount % 1000) == 0) {
1035 mprintf_at(2, 4, 0, "Ray=%d ", raycount);
1036 mprintf_at(2, 5, 0, "Ignore=%d ", Rays_ignored);
1037 }
1038
1039 raycount++;
1040
1041 if (ignore)
1042 continue;
1043
1044 if (dest_element->flags & EF_IGNORE)
1045 continue;
1046
1047 if (Shoot_from_patch || rad_MaxSurface->surface_type == ST_SATELLITE ||
1048 !(rad_MaxSurface->flags & SF_LIGHTSOURCE)) {
1049 if (rad_MaxSurface->surface_type == ST_SATELLITE)
1050 form_factor = GetFormFactorForElementAndSatellite(dest_surf, dest_element, &src_center);
1051 else
1052 form_factor = GetFormFactorForElement(dest_surf, dest_element, &src_center);
1053
1054 // Now extract the form factor info out to exitances
1055 if (form_factor > 0) {
1056 spectra shoot = rad_MaxSurface->exitance;
1057 spectra delta;
1058 float reflect_factor = dest_surf->reflectivity;
1059
1060 delta.r = shoot.r * reflect_factor * form_factor;
1061 delta.g = shoot.g * reflect_factor * form_factor;
1062 delta.b = shoot.b * reflect_factor * form_factor;
1063
1064 // update element exitance
1065 dest_element->exitance.r += delta.r;
1066 dest_element->exitance.g += delta.g;
1067 dest_element->exitance.b += delta.b;
1068
1069 dest_surf->exitance.r += ((dest_element->area / dest_surf->area) * delta.r);
1070 dest_surf->exitance.g += ((dest_element->area / dest_surf->area) * delta.g);
1071 dest_surf->exitance.b += ((dest_element->area / dest_surf->area) * delta.b);
1072 }
1073 } else // Do super detail
1074 {
1075 int src_num_elements = rad_MaxSurface->xresolution * rad_MaxSurface->yresolution;
1076
1077 for (k = 0; k < src_num_elements; k++) {
1078 rad_MaxElement = &rad_MaxSurface->elements[k];
1079
1080 if (rad_MaxElement->flags & EF_IGNORE)
1081 continue;
1082
1083 form_factor = GetFormFactorForElementSuperDetail(dest_surf, dest_element);
1084 // Now extract the form factor info out to exitances
1085 if (form_factor > 0) {
1086 spectra shoot = rad_MaxElement->exitance;
1087 spectra delta;
1088 float reflect_factor = dest_surf->reflectivity;
1089 delta.r = shoot.r * reflect_factor * form_factor;
1090 delta.g = shoot.g * reflect_factor * form_factor;
1091 delta.b = shoot.b * reflect_factor * form_factor;
1092 // update element exitance
1093 dest_element->exitance.r += delta.r;
1094 dest_element->exitance.g += delta.g;
1095 dest_element->exitance.b += delta.b;
1096
1097 dest_surf->exitance.r += ((dest_element->area / dest_surf->area) * delta.r);
1098 dest_surf->exitance.g += ((dest_element->area / dest_surf->area) * delta.g);
1099 dest_surf->exitance.b += ((dest_element->area / dest_surf->area) * delta.b);
1100 }
1101 }
1102 }
1103 }
1104
1105 if (rad_MaxSurface->surface_type == ST_ROOM && Calculate_specular_lighting)
1106 CheckToUpdateSpecularFace(dest_surf, &Current_max_specular_color, NULL);
1107
1108 // mprintf_at(2,4,0,"Ray=%d ",raycount);
1109 // mprintf_at(2,5,0,"Ignore=%d ",Rays_ignored);
1110 }
1111 }