Debian Codegraph @

Descent3/lighting.cpp

raw symbols SCIP
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/lighting.cpp $
21 * $Revision: 109 $
22 * $Date: 10/02/01 12:34p $
23 * $Author: Matt $
24 * $NoKeywords: $
25 */
26
27 #include "3d.h"
28 #include "gametexture.h"
29 #include "lighting.h"
30 #include "lightmap.h"
31 #include "descent.h"
32 #include "game.h"
33 #include "room.h"
34 #include <string.h>
35 #include <stdlib.h>
36 #include "findintersection.h"
37 #include "lightmap_info.h"
38 #include "polymodel.h"
39 #include "special_face.h"
40 #include "mem.h"
41 #include "config.h"
42 #include "dedicated_server.h"
43 #include "objinfo.h"
44 #include "Macros.h"
45
46 #include <algorithm>
47
48 #define NUM_DYNAMIC_CLASSES 7
49 #define MAX_DYNAMIC_FACES 2000
50 #define MAX_DYNAMIC_CELLS 1500
51 #define MAX_VOLUME_OBJECTS 500
52
53 // How much memory set aside for dynamic lightmaps
54 // Currently 3 megs
55
56 int DYNAMIC_LIGHTMAP_MEMORY = 1000000;
57 #define MAX_DYNAMIC_LIGHTMAPS 2000
58
59 struct volume_object {
60 int objnum;
61 int handle;
62 };
63
64 float Specular_tables[3][MAX_SPECULAR_INCREMENTS];
65
66 static uint16_t *Dynamic_lightmap_memory = NULL;
67 static float Light_component_scalar[32];
68 float Ubyte_to_float[256];
69
70 static uint8_t Lmi_spoken_for[MAX_LIGHTMAP_INFOS / 8];
71
72 static dynamic_lightmap *Dynamic_lightmaps;
73 static dynamic_face Dynamic_face_list[MAX_DYNAMIC_FACES];
74 static uint16_t Specular_face_list[MAX_DYNAMIC_FACES];
75 static volume_object Dynamic_volume_object_list[MAX_VOLUME_OBJECTS];
76 static dynamic_cell Dynamic_cell_list[MAX_DYNAMIC_CELLS];
77 static int Specular_maps[NUM_DYNAMIC_CLASSES];
78
79 static uint16_t Edges_to_blend[MAX_DYNAMIC_LIGHTMAPS];
80 static int Num_edges_to_blend = 0;
81
82 static int Num_specular_faces = 0;
83 static int Num_dynamic_faces = 0;
84 static int Num_dynamic_lightmaps = 0;
85 static int Cur_dynamic_mem_ptr = 0;
86 static int Num_volume_objects = 0;
87 static int Num_dynamic_cells = 0;
88 static int Num_destroyed_lights_this_frame = 0;
89
90 #define MAX_DESTROYED_LIGHTS_PER_FRAME 20
91 static int Destroyed_light_rooms_this_frame[MAX_DESTROYED_LIGHTS_PER_FRAME];
92 static int Destroyed_light_faces_this_frame[MAX_DESTROYED_LIGHTS_PER_FRAME];
93
94 static void FreeLighting();
95 static int GetFreeDynamicLightmap(int w, int h);
96 static void BlendLightingEdges(lightmap_info *lmi_ptr);
97 static void ApplyLightingToExternalRoom(vector *pos, int roomnum, float light_dist, float red_scale, float green_scale,
98 float blue_scale, vector *light_direction, float dot_range);
99 static void StartLightingInstance(vector *pos, matrix *orient);
100 static void DoneLightingInstance();
101 static void ApplyLightingToSubmodel(object *obj, poly_model *pm, bsp_info *sm, float light_dist, float red_scale,
102 float green_scale, float blue_scale, float dot_range);
103 static void ApplyVolumeLightToObject(vector *pos, object *obj, float light_dist, float red_scale, float green_scale,
104 float blue_scale, vector *light_direction, float dot_range);
105 static void ApplyLightingToObjects(vector *pos, int roomnum, float light_dist, float red_scale, float green_scale,
106 float blue_scale, vector *light_direction, float dot_range);
107
108 // Frees memory used by dynamic light structures
109 void FreeLighting() {
110 if (Dynamic_lightmap_memory)
111 mem_free(Dynamic_lightmap_memory);
112 if (Dynamic_lightmaps)
113 mem_free(Dynamic_lightmaps);
114 }
115
116 // Sets up our dynamic lighting maps
117 // and tables
118 void InitDynamicLighting() {
119 int i, cl, size;
120
121 mprintf(0, "Initting dynamic lighting.\n");
122
123 memset(Lmi_spoken_for, 0, MAX_LIGHTMAP_INFOS / 8);
124
125 for (i = 0; i < 16; i++)
126 Light_component_scalar[i] = i / 15.0;
127 for (i = 16; i < 32; i++)
128 Light_component_scalar[i] = 1.0;
129
130 for (cl = 0, size = 128; size >= 2; size >>= 1, cl++) {
131 Specular_maps[cl] = lm_AllocLightmap(size, size);
132 ASSERT(Specular_maps[cl] != BAD_LM_INDEX);
133 }
134
135 // Init our dynamic memory
136 if (Mem_low_memory_mode) {
137 // Just use 1 meg if in low mem mode
138 DYNAMIC_LIGHTMAP_MEMORY = 500000;
139 }
140
141 Dynamic_lightmap_memory = (uint16_t *)mem_malloc(DYNAMIC_LIGHTMAP_MEMORY);
142
143 // Init our records list
144 Dynamic_lightmaps = (dynamic_lightmap *)mem_malloc(sizeof(dynamic_lightmap) * MAX_DYNAMIC_LIGHTMAPS);
145 ASSERT(Dynamic_lightmaps);
146
147 for (i = 0; i < MAX_DYNAMIC_LIGHTMAPS; i++) {
148 memset(&Dynamic_lightmaps[i], 0, sizeof(dynamic_lightmap));
149 }
150
151 // Setup uint8_t to float
152 for (i = 0; i < 256; i++)
153 Ubyte_to_float[i] = (float)i / 255.0;
154
155 // Setup specular tables
156 mprintf(0, "Building specular tables.\n");
157
158 for (i = 0; i < MAX_SPECULAR_INCREMENTS; i++) {
159 float val = (float)i / (float)(MAX_SPECULAR_INCREMENTS - 1);
160
161 // Metal
162 Specular_tables[0][i] = (val * val * val * val * val * val);
163
164 // Plastic
165 Specular_tables[1][i] = (val * val * val * val * val * val * val * val * val * val * val * val * val * val);
166
167 // Marble
168 Specular_tables[2][i] = (val * val * val * val);
169 }
170
171 atexit(FreeLighting);
172 }
173
174 // Given a float, returns the index into the Ubyte_float_table that this number
175 // corresponds to
176 uint8_t Float_to_ubyte(float fnum) {
177 int i;
178
179 if (fnum < 0 || fnum > 1.0) {
180 mprintf(0, "ERROR: Trying to get value in Float_to_ubyte that is %f!\n", fnum);
181 return 0;
182 }
183
184 for (i = 0; i < 256; i++) {
185 if (fnum < Ubyte_to_float[i])
186 return i;
187 }
188
189 return 255;
190 }
191
192 // Returns an index into the Dynamic_lightmaps array. Index returned is marked as unused
193 int GetFreeDynamicLightmap(int w, int h) {
194 int total = w * h * 2;
195
196 if (Num_dynamic_lightmaps == MAX_DYNAMIC_LIGHTMAPS)
197 return -1;
198
199 if (total + Cur_dynamic_mem_ptr > DYNAMIC_LIGHTMAP_MEMORY) {
200 mprintf(0, "Ran out of lightmap memory (%d)\n", DYNAMIC_LIGHTMAP_MEMORY);
201 return -1;
202 }
203
204 int n = Num_dynamic_lightmaps++;
205 ASSERT(Dynamic_lightmaps[n].used == 0);
206
207 Dynamic_lightmaps[n].used = 1;
208 Dynamic_lightmaps[n].mem_ptr = &Dynamic_lightmap_memory[Cur_dynamic_mem_ptr / 2];
209 ASSERT(Dynamic_lightmaps[n].mem_ptr);
210
211 Cur_dynamic_mem_ptr += total;
212
213 return n;
214 }
215
216 // Makes all the edges of dynamic lighting blend into the body of the lightmap
217 void BlendLightingEdges(lightmap_info *lmi_ptr) {
218 int lmi_handle = lmi_ptr - LightmapInfo;
219 int h = lmi_h(lmi_handle);
220 int w = lmi_w(lmi_handle);
221 int lm_handle = lmi_ptr->lm_handle;
222 uint16_t *dest_data = (uint16_t *)lm_data(lm_handle);
223 uint16_t *src_data = dest_data;
224 int dest_x = lmi_ptr->x1 - 1;
225 int dest_y = lmi_ptr->y1 - 1;
226 int i;
227
228 ASSERT(dest_data);
229 if (!dest_data)
230 return;
231
232 ASSERT(dest_x >= 0 && dest_y >= 0);
233 ASSERT(dest_x + w < 127);
234 ASSERT(dest_y + h < 127);
235
236 for (i = 0; i < h; i++) {
237 // Left edge
238 dest_data[((dest_y + 1 + i) * 128) + dest_x] = src_data[((dest_y + 1 + i) * 128) + dest_x + 1];
239 // Right edge
240 dest_data[((dest_y + 1 + i) * 128) + dest_x + w + 1] = src_data[((dest_y + 1 + i) * 128) + dest_x + w];
241 }
242
243 for (i = 0; i < w; i++) {
244 // Top edge
245 dest_data[(dest_y * 128) + (dest_x + i + 1)] = src_data[((dest_y + 1) * 128) + (dest_x + i + 1)];
246 // Bottom edge
247 dest_data[((dest_y + 1 + h) * 128) + (dest_x + i + 1)] = src_data[((dest_y + h) * 128) + (dest_x + i + 1)];
248 }
249
250 // Now copy the corners
251 // Upper left
252 dest_data[(dest_y * 128) + (dest_x)] = src_data[((dest_y + 1) * 128) + (dest_x + 1)];
253 // Upper right
254 dest_data[(dest_y * 128) + (dest_x + w + 1)] = src_data[((dest_y + 1) * 128) + (dest_x + w)];
255 // Lower left
256 dest_data[((dest_y + h + 1) * 128) + (dest_x)] = src_data[((dest_y + h) * 128) + (dest_x + 1)];
257 // Lower right
258 dest_data[((dest_y + h + 1) * 128) + (dest_x + w + 1)] = src_data[((dest_y + h) * 128) + (dest_x + w)];
259 }
260
261 // Does lighting for the passed in external room
262 void ApplyLightingToExternalRoom(vector *pos, int roomnum, float light_dist, float red_scale, float green_scale,
263 float blue_scale, vector *light_direction, float dot_range) {
264 int i, lm_handle, t;
265 uint16_t *dest_data;
266 vector rad;
267 room *rp = &Rooms[roomnum];
268 vector Light_min_xyz;
269 vector Light_max_xyz;
270 uint16_t lmilist[MAX_DYNAMIC_FACES];
271 int num_spoken_for = 0;
272
273 int red_limit = 31;
274 int green_limit = 31;
275 int blue_limit = 31;
276
277 rad.x = light_dist;
278 rad.y = light_dist;
279 rad.z = light_dist;
280
281 Light_min_xyz = *pos - rad;
282 Light_max_xyz = *pos + rad;
283
284 // Now the Light_position is in the submodels frame of reference
285 for (i = 0; i < rp->num_faces; i++) {
286 face *fp = &rp->faces[i];
287
288 // check to see if this face is within reach
289 if (Light_max_xyz.y < fp->min_xyz.y || fp->max_xyz.y < Light_min_xyz.y || Light_max_xyz.x < fp->min_xyz.x ||
290 fp->max_xyz.x < Light_min_xyz.x || Light_max_xyz.z < fp->min_xyz.z || fp->max_xyz.z < Light_min_xyz.z)
291 continue;
292
293 // Make sure face was rendered
294 if (fp->renderframe != ((FrameCount - 1) % 256))
295 continue;
296
297 if (Num_dynamic_faces >= MAX_DYNAMIC_FACES) {
298 mprintf(0, "Too many dynamic faces!\n");
299 return;
300 }
301
302 // Make sure there already is a lightmap for this face
303 if (!(fp->flags & FF_LIGHTMAP))
304 continue;
305
306 if (Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))
307 continue;
308
309 int xres = lmi_w(fp->lmi_handle);
310 int yres = lmi_h(fp->lmi_handle);
311
312 lightmap_info *lmi_ptr = &LightmapInfo[fp->lmi_handle];
313 // Lightmap width and height
314 int lmw = lm_w(lmi_ptr->lm_handle);
315
316 // Check for backfaces
317 vector subvec = *pos - lmi_ptr->upper_left;
318 float dist_from_plane = fabs(vm_DotProduct(&subvec, &lmi_ptr->normal));
319
320 if (dist_from_plane > light_dist)
321 continue;
322
323 if (light_direction) {
324 // If this is a directional light, check for backfaces
325 vector norm_vec = *pos - rp->verts[fp->face_verts[0]];
326
327 if ((norm_vec * lmi_ptr->normal) < 0) {
328 // Backface, skip this light
329 continue;
330 }
331 // Test to see if all vertices are behind the light object, if so then bail
332 int in_front = 0; //
333 for (t = 0; t < fp->num_verts && in_front == 0; t++) {
334 norm_vec = rp->verts[fp->face_verts[t]] - *pos;
335 if (norm_vec * *light_direction > 0)
336 in_front = 1;
337 }
338
339 if (!in_front) // This face is completely behind, so bail!
340 continue;
341 }
342
343 // Compute face matrix
344 matrix facematrix;
345 vector fvec = -lmi_ptr->normal;
346 vm_VectorToMatrix(&facematrix, &fvec, NULL, NULL);
347
348 // Get upper left vector of face
349 vector base_vector = lmi_ptr->upper_left;
350
351 // Get the area of effect for this light
352 float area_of_effect = (1.0 - (dist_from_plane / light_dist)) * light_dist;
353 vector touch_vector = *pos - (lmi_ptr->normal * dist_from_plane);
354
355 subvec = touch_vector - base_vector;
356 float rdist = vm_DotProduct(&subvec, &facematrix.rvec);
357 float udist = -vm_DotProduct(&subvec, &facematrix.uvec);
358 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacing / 2)) / lmi_ptr->xspacing);
359 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacing / 2)) / lmi_ptr->yspacing);
360
361 // Get the rectangle of the lightmap that we want to light
362 int start_x = (rdist / lmi_ptr->xspacing) - x_add;
363 int start_y = (udist / lmi_ptr->yspacing) - y_add;
364 int end_x = (rdist / lmi_ptr->xspacing) + x_add + .5;
365 int end_y = (udist / lmi_ptr->yspacing) + y_add + .5;
366
367 if (end_x < 0 || end_y < 0 || start_x >= (xres) || start_y >= (yres))
368 continue;
369
370 if (start_x < 0)
371 start_x = 0;
372 if (start_y < 0)
373 start_y = 0;
374 if (end_x > xres)
375 end_x = xres;
376 if (end_y > yres)
377 end_y = yres;
378
379 int width = end_x - start_x;
380 int height = end_y - start_y;
381
382 if (width == 0)
383 width++;
384 if (height == 0)
385 height++;
386
387 ASSERT(width > 0);
388 ASSERT(height > 0);
389
390 if (lmi_ptr->dynamic != BAD_LM_INDEX) // already lit, so just adjust, not start over
391 {
392 lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
393 dest_data = (uint16_t *)lm_data(lm_handle);
394
395 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
396 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
397 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
398 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
399
400 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
401 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
402 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
403 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
404
405 GameLightmaps[lm_handle].flags |= (LF_CHANGED | LF_LIMITS);
406
407 if (!(Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))) {
408 lmilist[num_spoken_for] = fp->lmi_handle;
409 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
410 num_spoken_for++;
411 }
412 } else // Start a new dynamic lightmap
413 {
414 // First find a suitable dynamic lightmap to work with
415 int dynamic_handle = GetFreeDynamicLightmap(xres, yres);
416
417 if (dynamic_handle < 0) {
418 mprintf(0, "No free dynamic maps!\n");
419 return; // None free!
420 }
421
422 // Now copy our source data to our dest data so we have a base to work with
423 uint16_t *src_data;
424
425 src_data = (uint16_t *)lm_data(LightmapInfo[fp->lmi_handle].lm_handle);
426 dest_data = Dynamic_lightmaps[dynamic_handle].mem_ptr;
427
428 for (int y = 0, index = 0; y < yres; y++) {
429 for (int x = 0; x < xres; x++, index++) {
430 dest_data[index] = src_data[((lmi_ptr->y1 + y) * lmw) + lmi_ptr->x1 + x];
431 }
432 }
433
434 dest_data = src_data;
435 lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
436
437 // Mark it as changed
438 lmi_ptr->dynamic = dynamic_handle;
439
440 if (!(GameLightmaps[lm_handle].flags & LF_LIMITS)) {
441 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
442 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
443 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
444 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
445 } else {
446 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
447 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
448 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
449 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
450
451 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
452 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
453 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
454 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
455 }
456
457 GameLightmaps[lm_handle].flags |= (LF_LIMITS | LF_CHANGED);
458
459 Dynamic_face_list[Num_dynamic_faces].lmi_handle = fp->lmi_handle;
460 Num_dynamic_faces++;
461
462 lmilist[num_spoken_for] = fp->lmi_handle;
463 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
464 num_spoken_for++;
465
466 // Setup edge blending
467 Edges_to_blend[Num_edges_to_blend++] = fp->lmi_handle;
468 }
469
470 vector element_vec;
471
472 base_vector -= (start_y * (facematrix.uvec * lmi_ptr->yspacing));
473 base_vector += (start_x * (facematrix.rvec * lmi_ptr->xspacing));
474
475 base_vector -= ((facematrix.uvec / 2) * lmi_ptr->yspacing);
476 base_vector += ((facematrix.rvec / 2) * lmi_ptr->xspacing);
477
478 // Go through and change each element of this lightmap
479 // SLOW!
480
481 int texel_num = ((start_y + lmi_ptr->y1) * lmw) + start_x + lmi_ptr->x1;
482 for (int y = 0; y < height; y++, base_vector -= (facematrix.uvec * lmi_ptr->yspacing), texel_num += lmw) {
483 element_vec = base_vector;
484
485 for (int x = 0; x < width; x++, element_vec += (facematrix.rvec * lmi_ptr->xspacing)) {
486 int lightmap_texel_num = texel_num + x;
487
488 uint16_t lightmap_texel = dest_data[lightmap_texel_num];
489
490 if (!(lightmap_texel & OPAQUE_FLAG))
491 continue;
492
493 float dist = vm_VectorDistanceQuick(&element_vec, pos);
494 float scalar = 1.0 - (dist / light_dist);
495
496 if (light_direction) {
497 vector lsubvec = element_vec - *pos;
498 vm_NormalizeVectorFast(&lsubvec);
499 float dp = vm_DotProduct(&lsubvec, light_direction);
500 if (dp < dot_range) {
501 continue;
502 } else {
503
504 float add_scale = (dp - dot_range) / (1.0 - dot_range);
505 scalar *= add_scale;
506 }
507 }
508
509 if (scalar <= 0)
510 continue;
511
512 int r = (lightmap_texel >> 10) & 0x1f;
513 int g = (lightmap_texel >> 5) & 0x1f;
514 int b = lightmap_texel & 0x1f;
515
516 if (red_scale < 0) {
517 // we are subtracting light
518 r = std::max<float>(0, r + (scalar * red_scale * 31));
519 } else {
520 if (r < red_limit)
521 r = std::min<float>(red_limit, r + (scalar * red_scale * 31));
522 }
523
524 if (green_scale < 0) {
525 // we are subtracting light
526 g = std::max<float>(0, g + (scalar * green_scale * 31));
527 } else {
528 if (g < green_limit)
529 g = std::min<float>(green_limit, g + (scalar * green_scale * 31));
530 }
531
532 if (blue_scale < 0) {
533 // we are subtracting light
534 b = std::max<float>(0, b + (scalar * blue_scale * 31));
535 } else {
536 if (b < blue_limit)
537 b = std::min<float>(blue_limit, b + (scalar * blue_scale * 31));
538 }
539
540 lightmap_texel = OPAQUE_FLAG | (r << 10) | (g << 5) | b;
541
542 dest_data[lightmap_texel_num] = lightmap_texel;
543 }
544 }
545 }
546
547 for (i = 0; i < num_spoken_for; i++) {
548 int index = lmilist[i];
549 Lmi_spoken_for[index / 8] = 0;
550 }
551 }
552
553 struct light_instance_context {
554 vector p;
555 vector dir;
556 } Light_instance_stack[MAX_SUBOBJECTS];
557
558 int Light_instance_depth = 0;
559 uint8_t Use_light_direction = 0;
560
561 vector Light_position;
562 vector Light_direction;
563
564 // instance at specified point with specified orientation
565 void StartLightingInstance(vector *pos, matrix *orient) {
566 vector tempv;
567
568 ASSERT(Light_instance_depth < MAX_SUBOBJECTS);
569
570 Light_instance_stack[Light_instance_depth].p = Light_position;
571 if (Use_light_direction)
572 Light_instance_stack[Light_instance_depth].dir = Light_direction;
573 Light_instance_depth++;
574
575 // Step 1: subtract position from light position
576 tempv = Light_position - *pos;
577 // step 2: rotate view vector through object matrix
578
579 Light_position = tempv * *orient;
580
581 // Rotate the light direction through this matrix
582 if (Use_light_direction) {
583 tempv = Light_direction;
584 Light_direction = tempv * *orient;
585 }
586 }
587
588 // pops the old context
589 void DoneLightingInstance() {
590 Light_instance_depth--;
591
592 ASSERT(Light_instance_depth >= 0);
593
594 Light_position = Light_instance_stack[Light_instance_depth].p;
595 if (Use_light_direction)
596 Light_direction = Light_instance_stack[Light_instance_depth].dir;
597 }
598
599 // Applies lighting to a submodel and all its children
600 void ApplyLightingToSubmodel(object *obj, poly_model *pm, bsp_info *sm, float light_dist, float red_scale,
601 float green_scale, float blue_scale, float dot_range) {
602 matrix mat;
603 vector Light_min_xyz;
604 vector Light_max_xyz;
605 vector rad;
606 int i, t;
607 int subnum = sm - pm->submodel;
608 int lm_handle;
609 uint16_t *dest_data;
610 uint16_t lmilist[MAX_DYNAMIC_FACES];
611 int num_spoken_for = 0;
612
613 int red_limit = 31;
614 int green_limit = 31;
615 int blue_limit = 31;
616
617 if (IsNonRenderableSubmodel(pm, subnum))
618 return; // Don't do shells, frontfaces, etc
619
620 // Start instance stuff
621 vector temp_vec = sm->mod_pos + sm->offset;
622 vm_AnglesToMatrix(&mat, sm->angs.p, sm->angs.h, sm->angs.b);
623 StartLightingInstance(&temp_vec, &mat);
624
625 rad.x = light_dist;
626 rad.y = light_dist;
627 rad.z = light_dist;
628
629 vector light_pos = Light_position;
630 vector light_dir = Light_direction;
631
632 Light_min_xyz = light_pos - rad;
633 Light_max_xyz = light_pos + rad;
634
635 // Now the Light_position is in the submodels frame of reference
636 for (i = 0; i < sm->num_faces; i++) {
637 // check to see if this face is within reach
638 if (Light_max_xyz.y < sm->face_min[i].y || sm->face_max[i].y < Light_min_xyz.y ||
639 Light_max_xyz.x < sm->face_min[i].x || sm->face_max[i].x < Light_min_xyz.x ||
640 Light_max_xyz.z < sm->face_min[i].z || sm->face_max[i].z < Light_min_xyz.z)
641 continue;
642
643 // Ok, now we know that this light touches this face
644 if (Num_dynamic_faces >= MAX_DYNAMIC_FACES) {
645 mprintf(0, "Too many dynamic faces!\n");
646 DoneLightingInstance();
647 return;
648 }
649
650 lightmap_object_face *fp = &obj->lm_object.lightmap_faces[subnum][i];
651
652 if (Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))
653 continue;
654
655 int xres = lmi_w(fp->lmi_handle);
656 int yres = lmi_h(fp->lmi_handle);
657
658 lightmap_info *lmi_ptr = &LightmapInfo[fp->lmi_handle];
659 // Lightmap width and height
660 int lmw = lm_w(lmi_ptr->lm_handle);
661
662 // Check for backfaces
663 // Get upper left vector of face
664 vector base_vector = lmi_ptr->upper_left;
665 vector subvec = light_pos - lmi_ptr->upper_left;
666 float dist_from_plane = fabs(vm_DotProduct(&subvec, &lmi_ptr->normal));
667
668 if (dist_from_plane > light_dist)
669 continue;
670
671 if (Use_light_direction) {
672 // If this is a directional light, check for backfaces
673 vector norm_vec = light_pos - base_vector;
674
675 if ((norm_vec * lmi_ptr->normal) < 0) {
676 // Backface, skip this face
677 continue;
678 }
679
680 // Test to see if all vertices are behind the light object, if so then bail
681 polyface *poly_fp = &sm->faces[i];
682 int in_front = 0; //
683 for (t = 0; t < fp->num_verts && in_front == 0; t++) {
684 norm_vec = sm->verts[poly_fp->vertnums[t]] - light_pos;
685 if (norm_vec * light_dir > 0)
686 in_front = 1;
687 }
688
689 if (!in_front) // This face is completely behind, so bail!
690 continue;
691 }
692
693 // Compute face matrix
694 matrix facematrix;
695 vector fvec = -lmi_ptr->normal;
696 vm_VectorToMatrix(&facematrix, &fvec, NULL, NULL);
697
698 // Get the area of effect for this light
699 float area_of_effect = (1.0 - (dist_from_plane / light_dist)) * light_dist;
700 vector touch_vector = light_pos - (lmi_ptr->normal * dist_from_plane);
701
702 subvec = touch_vector - base_vector;
703 float rdist = vm_DotProduct(&subvec, &facematrix.rvec);
704 float udist = -vm_DotProduct(&subvec, &facematrix.uvec);
705 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacing / 2)) / lmi_ptr->xspacing);
706 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacing / 2)) / lmi_ptr->yspacing);
707
708 // Get the rectangle of the lightmap that we want to light
709 int start_x = (rdist / lmi_ptr->xspacing) - x_add;
710 int start_y = (udist / lmi_ptr->yspacing) - y_add;
711 int end_x = (rdist / lmi_ptr->xspacing) + x_add + .5;
712 int end_y = (udist / lmi_ptr->yspacing) + y_add + .5;
713
714 if (end_x < 0 || end_y < 0 || start_x >= (xres) || start_y >= (yres))
715 continue;
716
717 if (start_x < 0)
718 start_x = 0;
719 if (start_y < 0)
720 start_y = 0;
721 if (end_x > xres)
722 end_x = xres;
723 if (end_y > yres)
724 end_y = yres;
725
726 int width = end_x - start_x;
727 int height = end_y - start_y;
728
729 if (width == 0)
730 width++;
731 if (height == 0)
732 height++;
733
734 ASSERT(width > 0);
735 ASSERT(height > 0);
736
737 if (lmi_ptr->dynamic != BAD_LM_INDEX) // already lit, so just adjust, not start over
738 {
739 lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
740 dest_data = (uint16_t *)lm_data(lm_handle);
741
742 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
743 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
744 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
745 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
746
747 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
748 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
749 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
750 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
751
752 GameLightmaps[lm_handle].flags |= (LF_CHANGED | LF_LIMITS);
753
754 if (!(Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))) {
755 lmilist[num_spoken_for] = fp->lmi_handle;
756 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
757 num_spoken_for++;
758 }
759 } else // Start a new dynamic lightmap
760 {
761 // First find a suitable dynamic lightmap to work with
762 int dynamic_handle = GetFreeDynamicLightmap(xres, yres);
763
764 if (dynamic_handle < 0) {
765 mprintf(0, "No free dynamic maps!\n");
766 DoneLightingInstance();
767
768 return; // None free!
769 }
770
771 // Now copy our source data to our dest data so we have a base to work with
772 uint16_t *src_data;
773
774 src_data = (uint16_t *)lm_data(LightmapInfo[fp->lmi_handle].lm_handle);
775 dest_data = Dynamic_lightmaps[dynamic_handle].mem_ptr;
776
777 for (int y = 0, index = 0; y < yres; y++) {
778 for (int x = 0; x < xres; x++, index++) {
779 dest_data[index] = src_data[((lmi_ptr->y1 + y) * lmw) + lmi_ptr->x1 + x];
780 }
781 }
782
783 dest_data = src_data;
784 lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
785
786 // Mark it as changed
787 lmi_ptr->dynamic = dynamic_handle;
788
789 if (!(GameLightmaps[lm_handle].flags & LF_LIMITS)) {
790 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
791 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
792 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
793 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
794 } else {
795 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
796 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
797 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
798 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
799
800 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
801 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
802 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
803 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
804 }
805
806 GameLightmaps[lm_handle].flags |= (LF_LIMITS | LF_CHANGED);
807
808 Dynamic_face_list[Num_dynamic_faces].lmi_handle = fp->lmi_handle;
809 Num_dynamic_faces++;
810
811 lmilist[num_spoken_for] = fp->lmi_handle;
812 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
813 num_spoken_for++;
814
815 // Setup edge blending
816 Edges_to_blend[Num_edges_to_blend++] = fp->lmi_handle;
817 }
818
819 vector element_vec;
820
821 base_vector -= (start_y * (facematrix.uvec * lmi_ptr->yspacing));
822 base_vector += (start_x * (facematrix.rvec * lmi_ptr->xspacing));
823
824 base_vector -= ((facematrix.uvec / 2) * lmi_ptr->yspacing);
825 base_vector += ((facematrix.rvec / 2) * lmi_ptr->xspacing);
826
827 // Go through and change each element of this lightmap
828 // SLOW!
829
830 int texel_num = ((start_y + lmi_ptr->y1) * lmw) + start_x + lmi_ptr->x1;
831 for (int y = 0; y < height; y++, base_vector -= (facematrix.uvec * lmi_ptr->yspacing), texel_num += lmw) {
832 element_vec = base_vector;
833
834 for (int x = 0; x < width; x++, element_vec += (facematrix.rvec * lmi_ptr->xspacing)) {
835 int lightmap_texel_num = texel_num + x;
836
837 uint16_t lightmap_texel = dest_data[lightmap_texel_num];
838
839 if (!(lightmap_texel & OPAQUE_FLAG))
840 continue;
841
842 float dist = vm_VectorDistanceQuick(&element_vec, &light_pos);
843 float scalar = 1.0 - (dist / light_dist);
844
845 if (Use_light_direction) {
846 vector lsubvec = element_vec - light_pos;
847 vm_NormalizeVectorFast(&lsubvec);
848 float dp = vm_DotProduct(&lsubvec, &light_dir);
849 if (dp < dot_range) {
850 continue;
851 } else {
852
853 float add_scale = (dp - dot_range) / (1.0 - dot_range);
854 scalar *= add_scale;
855 }
856 }
857
858 if (scalar <= 0)
859 continue;
860
861 int r = (lightmap_texel >> 10) & 0x1f;
862 int g = (lightmap_texel >> 5) & 0x1f;
863 int b = lightmap_texel & 0x1f;
864
865 if (red_scale < 0) {
866 r = std::max<float>(0, r + (scalar * red_scale * 31));
867 } else {
868 if (r < red_limit)
869 r = std::min<float>(red_limit, r + (scalar * red_scale * 31));
870 }
871
872 if (green_scale < 0) {
873 g = std::max<float>(0, g + (scalar * green_scale * 31));
874 } else {
875 if (g < green_limit)
876 g = std::min<float>(green_limit, g + (scalar * green_scale * 31));
877 }
878
879 if (blue_scale < 0) {
880 b = std::max<float>(0, b + (scalar * blue_scale * 31));
881 } else {
882 if (b < blue_limit)
883 b = std::min<float>(blue_limit, b + (scalar * blue_scale * 31));
884 }
885
886 lightmap_texel = OPAQUE_FLAG | (r << 10) | (g << 5) | b;
887
888 dest_data[lightmap_texel_num] = lightmap_texel;
889 }
890 }
891 }
892
893 for (i = 0; i < num_spoken_for; i++) {
894 int index = lmilist[i];
895 Lmi_spoken_for[index / 8] = 0;
896 }
897
898 for (i = 0; i < sm->num_children; i++)
899 ApplyLightingToSubmodel(obj, pm, &pm->submodel[sm->children[i]], light_dist, red_scale, green_scale, blue_scale,
900 dot_range);
901
902 DoneLightingInstance();
903 }
904 /*
905 // Test to see if this wall should show specular lighting from this light source
906 void ApplySpecularLightingToWall (room *rp,face *fp,vector *pos,float light_dist,float dist_from_plane,float
907 red_scale,float green_scale,float blue_scale)
908 {
909 special_face *sf=&SpecialFaces[fp->special_handle];
910
911 if (!(sf->flags))
912 {
913 // Reset this face
914 Specular_face_list[Num_specular_faces++]=fp->special_handle;
915 sf->flags=1;
916 sf->strength=0;
917 }
918
919 float norm=std::min(1.0,(light_dist/dist_from_plane)*2.0);
920 float strength=((red_scale*.33)+(green_scale*.33)+(blue_scale*.33))*light_dist;
921
922 if (strength>sf->strength)
923 {
924 sf->strength=strength;
925 sf->lightdist=light_dist;
926 sf->lightpos=*pos;
927 sf->color=GR_RGB16(norm*red_scale*255,norm*green_scale*255,norm*blue_scale*255);
928 }
929
930 }*/
931
932 // Applys dynamic volumetric lighting to an object
933 void ApplyVolumeLightToObject(vector *pos, object *obj, float light_dist, float red_scale, float green_scale,
934 float blue_scale, vector *light_direction, float dot_range) {
935 vector subvec = *pos - obj->pos;
936 float mag = vm_GetMagnitudeFast(&subvec);
937
938 float scalar = mag / light_dist;
939 if (scalar > 1)
940 scalar = 1;
941
942 scalar = 1 - scalar;
943
944 if (light_direction) {
945 // Do a directional light
946 vector dir_vec = obj->pos - *pos;
947 float mag = vm_NormalizeVectorFast(&dir_vec);
948 float dp = dir_vec * *light_direction;
949
950 if (mag > 1.0) {
951 if (dp < dot_range)
952 return;
953 else {
954 float add_scale = (dp - dot_range) / (1.0 - dot_range);
955 scalar *= add_scale;
956 }
957 }
958 }
959
960 ASSERT(obj->effect_info != NULL);
961
962 if (!obj->effect_info->dynamic_this_frame) {
963 if (Num_volume_objects < MAX_VOLUME_OBJECTS) {
964 Dynamic_volume_object_list[Num_volume_objects].handle = obj->handle;
965 Dynamic_volume_object_list[Num_volume_objects++].objnum = obj - Objects;
966 obj->effect_info->spec_mag = -100000;
967
968 obj->effect_info->dynamic_this_frame = 1;
969 }
970 }
971
972 // See if this specular light source is greater than our current one
973 if ((light_dist - mag) > obj->effect_info->spec_mag && Detail_settings.Specular_lighting &&
974 !(Object_info[obj->id].lighting_info.flags & OLF_NO_SPECULARITY)) {
975 // mprintf(0,"Setting specular!\n");
976 obj->effect_info->type_flags |= EF_SPECULAR;
977 obj->effect_info->spec_mag = light_dist - mag;
978 obj->effect_info->spec_pos = *pos;
979 obj->effect_info->spec_r = red_scale;
980 obj->effect_info->spec_g = green_scale;
981 obj->effect_info->spec_b = blue_scale;
982 }
983
984 if (obj->effect_info->type_flags & EF_VOLUME_LIT) {
985 obj->effect_info->dynamic_red = std::min<float>(1, obj->effect_info->dynamic_red + (scalar * red_scale));
986 obj->effect_info->dynamic_green = std::min<float>(1, obj->effect_info->dynamic_green + (scalar * green_scale));
987 obj->effect_info->dynamic_blue = std::min<float>(1, obj->effect_info->dynamic_blue + (scalar * blue_scale));
988 }
989 }
990
991 // Applies lighting to all objects in a certain distance
992 void ApplyLightingToObjects(vector *pos, int roomnum, float light_dist, float red_scale, float green_scale,
993 float blue_scale, vector *light_direction, float dot_range) {
994 int16_t objlist[MAX_DYNAMIC_FACES];
995 int num_objects, i;
996 float normalized_time[MAX_SUBOBJECTS];
997
998 num_objects = fvi_QuickDistObjectList(pos, roomnum, light_dist, objlist, MAX_DYNAMIC_FACES, false, false, true);
999
1000 for (i = 0; i < num_objects; i++) {
1001 object *obj = &Objects[objlist[i]];
1002
1003 if (obj->type == OBJ_ROOM) {
1004 ApplyLightingToExternalRoom(pos, obj->id, light_dist, red_scale, green_scale, blue_scale, light_direction,
1005 dot_range);
1006 continue;
1007 }
1008
1009 if (obj->renderframe != ((FrameCount - 1) % 65536)) {
1010 if (obj != Viewer_object)
1011 continue;
1012 }
1013
1014 if (obj->lm_object.used == 0) {
1015 if (obj->effect_info && ((obj->effect_info->type_flags & EF_VOLUME_LIT) ||
1016 obj->lighting_render_type == LRT_GOURAUD || obj->type == OBJ_POWERUP)) {
1017 ApplyVolumeLightToObject(pos, obj, light_dist, red_scale, green_scale, blue_scale, light_direction, dot_range);
1018 continue;
1019 } else
1020 continue;
1021 }
1022
1023 int model_num = obj->rtype.pobj_info.model_num;
1024 poly_model *pm = &Poly_models[model_num];
1025
1026 // Set our light position
1027 Light_position = *pos;
1028
1029 // Set up light direction if this is a directional light
1030 if (light_direction) {
1031 Light_direction = *light_direction;
1032 Use_light_direction = 1;
1033 } else
1034 Use_light_direction = 0;
1035
1036 StartLightingInstance(&obj->pos, &obj->orient);
1037
1038 // Now, get the lights position in the objects submodel space
1039 SetNormalizedTimeObj(obj, normalized_time);
1040 SetModelAnglesAndPos(pm, normalized_time);
1041
1042 for (int t = 0; t < pm->n_models; t++) {
1043 bsp_info *sm = &pm->submodel[t];
1044 if (sm->parent == -1)
1045 ApplyLightingToSubmodel(obj, pm, sm, light_dist, red_scale, green_scale, blue_scale, dot_range);
1046 }
1047
1048 DoneLightingInstance();
1049 ASSERT(Light_instance_depth == 0);
1050 }
1051 }
1052
1053 // Applys dynamic lightmap changes to rooms and room objects. If light direction is non-null, we are applying a
1054 // directional light
1055 void ApplyLightingToRooms(vector *pos, int roomnum, float light_dist, float red_scale, float green_scale,
1056 float blue_scale, vector *light_direction, float dot_range) {
1057 fvi_face_room_list facelist[MAX_DYNAMIC_FACES];
1058 uint16_t lmilist[MAX_DYNAMIC_FACES];
1059 int num_spoken_for = 0;
1060
1061 int num_faces, i, t, lm_handle;
1062 uint16_t *dest_data;
1063 int faces_misreported = 0;
1064
1065 if (Dedicated_server)
1066 return;
1067
1068 ApplyLightingToObjects(pos, roomnum, light_dist, red_scale, green_scale, blue_scale, light_direction, dot_range);
1069
1070 num_faces = fvi_QuickDistFaceList(roomnum, pos, light_dist, facelist, MAX_DYNAMIC_FACES);
1071
1072 #ifdef _DEBUG
1073 if (num_faces == MAX_DYNAMIC_FACES) {
1074 mprintf(0, "Dynamic light from 1 object is touching %d faces! dist=%f\n", MAX_DYNAMIC_FACES, light_dist);
1075 }
1076 #endif
1077
1078 if (num_faces < 1)
1079 return;
1080
1081 int red_limit = 31;
1082 int green_limit = 31;
1083 int blue_limit = 31;
1084
1085 for (i = 0; i < num_faces; i++) {
1086 room *rp = &Rooms[facelist[i].room_index];
1087 face *fp = &rp->faces[facelist[i].face_index];
1088
1089 ASSERT(Rooms[facelist[i].room_index].used);
1090 ASSERT(facelist[i].face_index < Rooms[facelist[i].room_index].num_faces);
1091
1092 // Make sure face was rendered
1093 if (fp->renderframe != ((FrameCount - 1) % 256))
1094 continue;
1095
1096 if (Num_dynamic_faces >= MAX_DYNAMIC_FACES) {
1097 mprintf(0, "Too many dynamic faces!\n");
1098 return;
1099 }
1100
1101 // Make sure there already is a lightmap for this face
1102 if (!(fp->flags & FF_LIGHTMAP))
1103 continue;
1104
1105 if (Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))
1106 continue;
1107
1108 int xres = lmi_w(fp->lmi_handle);
1109 int yres = lmi_h(fp->lmi_handle);
1110
1111 lightmap_info *lmi_ptr = &LightmapInfo[fp->lmi_handle];
1112 // Lightmap width and height
1113 int lmw = lm_w(lmi_ptr->lm_handle);
1114
1115 // Check for backfaces
1116 vector subvec = *pos - lmi_ptr->upper_left;
1117 float dist_from_plane = fabs(vm_DotProduct(&subvec, &lmi_ptr->normal));
1118
1119 if (dist_from_plane > light_dist)
1120 continue;
1121
1122 if (light_direction) {
1123 // If this is a directional light, check for backfaces
1124 vector norm_vec = *pos - rp->verts[fp->face_verts[0]];
1125
1126 if ((norm_vec * lmi_ptr->normal) < 0) {
1127 // Backface, skip this light
1128 continue;
1129 }
1130 // Test to see if all vertices are behind the light object, if so then bail
1131 int in_front = 0; //
1132 for (t = 0; t < fp->num_verts && in_front == 0; t++) {
1133 norm_vec = rp->verts[fp->face_verts[t]] - *pos;
1134 if (norm_vec * *light_direction > 0)
1135 in_front = 1;
1136 }
1137
1138 if (!in_front) // This face is completely behind, so bail!
1139 continue;
1140 }
1141
1142 /*// Check for specular lighting changes on this face
1143 if (Detail_settings.Specular_lighting && fp->special_handle!=BAD_SPECIAL_FACE_INDEX)
1144 {
1145 ApplySpecularLightingToWall(rp,fp,pos,light_dist,dist_from_plane,red_scale,green_scale,blue_scale);
1146 }*/
1147
1148 // Compute face matrix
1149 matrix facematrix;
1150 vector fvec = -lmi_ptr->normal;
1151 vm_VectorToMatrix(&facematrix, &fvec, NULL, NULL);
1152
1153 // Get upper left vector of face
1154 vector base_vector = lmi_ptr->upper_left;
1155
1156 // Get the area of effect for this light
1157 float area_of_effect = (1.0 - (dist_from_plane / light_dist)) * light_dist;
1158 vector touch_vector = *pos - (lmi_ptr->normal * dist_from_plane);
1159
1160 subvec = touch_vector - base_vector;
1161 float rdist = vm_DotProduct(&subvec, &facematrix.rvec);
1162 float udist = -vm_DotProduct(&subvec, &facematrix.uvec);
1163 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacing / 2)) / lmi_ptr->xspacing);
1164 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacing / 2)) / lmi_ptr->yspacing);
1165
1166 // Get the rectangle of the lightmap that we want to light
1167 int start_x = (rdist / lmi_ptr->xspacing) - x_add;
1168 int start_y = (udist / lmi_ptr->yspacing) - y_add;
1169 int end_x = (rdist / lmi_ptr->xspacing) + x_add + .5;
1170 int end_y = (udist / lmi_ptr->yspacing) + y_add + .5;
1171
1172 if (end_x < 0 || end_y < 0 || start_x >= (xres) || start_y >= (yres))
1173 continue;
1174
1175 if (start_x < 0)
1176 start_x = 0;
1177 if (start_y < 0)
1178 start_y = 0;
1179 if (end_x > xres)
1180 end_x = xres;
1181 if (end_y > yres)
1182 end_y = yres;
1183
1184 int width = end_x - start_x;
1185 int height = end_y - start_y;
1186
1187 if (width == 0)
1188 width++;
1189 if (height == 0)
1190 height++;
1191
1192 ASSERT(width > 0);
1193 ASSERT(height > 0);
1194
1195 if (lmi_ptr->dynamic != BAD_LM_INDEX) // already lit, so just adjust, not start over
1196 {
1197 lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
1198 dest_data = (uint16_t *)lm_data(lm_handle);
1199
1200 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
1201 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
1202 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
1203 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
1204
1205 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
1206 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
1207 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
1208 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
1209
1210 GameLightmaps[lm_handle].flags |= (LF_CHANGED | LF_LIMITS);
1211
1212 if (!(Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))) {
1213 lmilist[num_spoken_for] = fp->lmi_handle;
1214 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
1215 num_spoken_for++;
1216 }
1217 } else // Start a new dynamic lightmap
1218 {
1219 // First find a suitable dynamic lightmap to work with
1220 int dynamic_handle = GetFreeDynamicLightmap(xres, yres);
1221
1222 if (dynamic_handle < 0) {
1223 mprintf(0, "No free dynamic maps!\n");
1224 return; // None free!
1225 }
1226
1227 // Now copy our source data to our dest data so we have a base to work with
1228 uint16_t *src_data;
1229
1230 src_data = (uint16_t *)lm_data(LightmapInfo[fp->lmi_handle].lm_handle);
1231 dest_data = Dynamic_lightmaps[dynamic_handle].mem_ptr;
1232
1233 for (int y = 0, index = 0; y < yres; y++) {
1234 for (int x = 0; x < xres; x++, index++) {
1235 dest_data[index] = src_data[((lmi_ptr->y1 + y) * lmw) + lmi_ptr->x1 + x];
1236 }
1237 }
1238
1239 dest_data = src_data;
1240 lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
1241
1242 // Mark it as changed
1243 lmi_ptr->dynamic = dynamic_handle;
1244
1245 if (!(GameLightmaps[lm_handle].flags & LF_LIMITS)) {
1246 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
1247 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
1248 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
1249 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
1250 } else {
1251 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
1252 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
1253 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
1254 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
1255
1256 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
1257 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
1258 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
1259 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
1260 }
1261
1262 GameLightmaps[lm_handle].flags |= (LF_LIMITS | LF_CHANGED);
1263
1264 Dynamic_face_list[Num_dynamic_faces].lmi_handle = fp->lmi_handle;
1265 Num_dynamic_faces++;
1266
1267 lmilist[num_spoken_for] = fp->lmi_handle;
1268 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
1269 num_spoken_for++;
1270
1271 // Setup edge blending
1272 Edges_to_blend[Num_edges_to_blend++] = fp->lmi_handle;
1273 }
1274
1275 vector element_vec;
1276
1277 base_vector -= (start_y * (facematrix.uvec * lmi_ptr->yspacing));
1278 base_vector += (start_x * (facematrix.rvec * lmi_ptr->xspacing));
1279
1280 base_vector -= ((facematrix.uvec / 2) * lmi_ptr->yspacing);
1281 base_vector += ((facematrix.rvec / 2) * lmi_ptr->xspacing);
1282
1283 // Go through and change each element of this lightmap
1284 // SLOW!
1285
1286 int texel_num = ((start_y + lmi_ptr->y1) * lmw) + start_x + lmi_ptr->x1;
1287 for (int y = 0; y < height; y++, base_vector -= (facematrix.uvec * lmi_ptr->yspacing), texel_num += lmw) {
1288 element_vec = base_vector;
1289
1290 for (int x = 0; x < width; x++, element_vec += (facematrix.rvec * lmi_ptr->xspacing)) {
1291 int lightmap_texel_num = texel_num + x;
1292
1293 uint16_t lightmap_texel = dest_data[lightmap_texel_num];
1294
1295 if (!(lightmap_texel & OPAQUE_FLAG))
1296 continue;
1297
1298 float dist = vm_VectorDistanceQuick(&element_vec, pos);
1299 float scalar = 1.0 - (dist / light_dist);
1300
1301 if (light_direction) {
1302 vector lsubvec = element_vec - *pos;
1303 vm_NormalizeVectorFast(&lsubvec);
1304 float dp = vm_DotProduct(&lsubvec, light_direction);
1305 if (dp < dot_range) {
1306 continue;
1307 } else {
1308
1309 float add_scale = (dp - dot_range) / (1.0 - dot_range);
1310 scalar *= add_scale;
1311 }
1312 }
1313
1314 if (scalar <= 0)
1315 continue;
1316
1317 int r = (lightmap_texel >> 10) & 0x1f;
1318 int g = (lightmap_texel >> 5) & 0x1f;
1319 int b = lightmap_texel & 0x1f;
1320
1321 if (red_scale < 0) {
1322 // we are subtracting light
1323 r = std::max<float>(0, r + (scalar * red_scale * 31));
1324 } else {
1325 // we are adding light
1326 if (r < red_limit)
1327 r = std::min<float>(red_limit, r + (scalar * red_scale * 31));
1328 }
1329
1330 if (green_scale < 0) {
1331 // we are subtracting light
1332 g = std::max<float>(0, g + (scalar * green_scale * 31));
1333 } else {
1334 // we are adding light
1335 if (g < green_limit)
1336 g = std::min<float>(green_limit, g + (scalar * green_scale * 31));
1337 }
1338
1339 if (blue_scale < 0) {
1340 // we are subtracting light
1341 b = std::max<float>(0, b + (scalar * blue_scale * 31));
1342 } else {
1343 if (b < blue_limit)
1344 b = std::min<float>(blue_limit, b + (scalar * blue_scale * 31));
1345 }
1346
1347 lightmap_texel = OPAQUE_FLAG | (r << 10) | (g << 5) | b;
1348
1349 dest_data[lightmap_texel_num] = lightmap_texel;
1350 }
1351 }
1352 }
1353
1354 for (i = 0; i < num_spoken_for; i++) {
1355 int index = lmilist[i];
1356 Lmi_spoken_for[index / 8] = 0;
1357 }
1358 }
1359
1360 // Blends all the edges that need blending for this frame
1361 void BlendAllLightingEdges() {
1362 for (int i = 0; i < Num_edges_to_blend; i++) {
1363 if (LightmapInfo[Edges_to_blend[i]].used < 1)
1364 continue; // this face was killed last frame. This can happen with objects
1365 BlendLightingEdges(&LightmapInfo[Edges_to_blend[i]]);
1366 }
1367 }
1368
1369 // Clears the used flag for the dynamic lightmaps and restores colors on the terrain
1370 void ClearDynamicLightmaps() {
1371 int i;
1372
1373 // First clear dynamic lightmap list
1374 for (i = 0; i < Num_dynamic_lightmaps; i++)
1375 Dynamic_lightmaps[i].used = 0;
1376
1377 // Reset volume lights
1378 for (i = 0; i < Num_volume_objects; i++) {
1379 object *obj = &Objects[Dynamic_volume_object_list[i].objnum];
1380 if (obj->type == OBJ_NONE || obj->handle != Dynamic_volume_object_list[i].handle)
1381 continue; // object was destroyed this frame
1382
1383 obj->effect_info->type_flags &= ~EF_SPECULAR;
1384 obj->effect_info->dynamic_this_frame = 0;
1385 obj->effect_info->dynamic_red = 0;
1386 obj->effect_info->dynamic_green = 0;
1387 obj->effect_info->dynamic_blue = 0;
1388 }
1389
1390 Num_volume_objects = 0;
1391
1392 // Reset specular faces
1393 for (i = 0; i < Num_specular_faces; i++) {
1394 int n = Specular_face_list[i];
1395 SpecialFaces[n].flags = 0;
1396 // LightmapInfo[n].spec_map=-1;
1397 }
1398
1399 Num_specular_faces = 0;
1400
1401 // Clear diffuse faces
1402 for (i = 0; i < Num_dynamic_faces; i++) {
1403 if (LightmapInfo[Dynamic_face_list[i].lmi_handle].used == 0)
1404 continue; // this face was killed last frame. This can happen with objects
1405
1406 int dynamic_handle = LightmapInfo[Dynamic_face_list[i].lmi_handle].dynamic;
1407 int lmi_handle = Dynamic_face_list[i].lmi_handle;
1408 int lm_handle = LightmapInfo[lmi_handle].lm_handle;
1409 lightmap_info *lmi_ptr = &LightmapInfo[lmi_handle];
1410
1411 uint16_t *src_data = Dynamic_lightmaps[dynamic_handle].mem_ptr;
1412 uint16_t *dest_data = (uint16_t *)lm_data(lm_handle);
1413
1414 int lmw = lm_w(lm_handle);
1415
1416 dest_data += (lmi_ptr->y1 * lmw);
1417
1418 for (int y = 0; y < lmi_ptr->height; y++, dest_data += lmw) {
1419 for (int x = 0; x < lmi_ptr->width; x++) {
1420 dest_data[lmi_ptr->x1 + x] = src_data[y * lmi_ptr->width + x];
1421 }
1422 }
1423
1424 GameLightmaps[lm_handle].flags |= (LF_LIMITS | LF_CHANGED);
1425 LightmapInfo[lmi_handle].dynamic = BAD_LMI_INDEX;
1426 }
1427
1428 Num_dynamic_faces = 0;
1429
1430 // Now do terrain
1431
1432 for (i = 0; i < Num_dynamic_cells; i++) {
1433 int cellnum = Dynamic_cell_list[i].cellnum;
1434 Terrain_seg[cellnum].flags &= ~TF_DYNAMIC;
1435 Terrain_seg[cellnum].r = Dynamic_cell_list[i].r;
1436 Terrain_seg[cellnum].g = Dynamic_cell_list[i].g;
1437 Terrain_seg[cellnum].b = Dynamic_cell_list[i].b;
1438
1439 int seg_x = cellnum % TERRAIN_WIDTH;
1440 int seg_z = cellnum / TERRAIN_WIDTH;
1441
1442 int subx = seg_x % 128;
1443 int subz = 127 - (seg_z % 128);
1444
1445 int whichmap = ((seg_z / 128) * 2) + (seg_x / 128);
1446 whichmap = TerrainLightmaps[whichmap];
1447
1448 uint16_t color = OPAQUE_FLAG | GR_RGB16(Terrain_seg[cellnum].r, Terrain_seg[cellnum].g, Terrain_seg[cellnum].b);
1449 uint16_t *data = lm_data(whichmap);
1450
1451 data[subz * 128 + subx] = color;
1452
1453 GameLightmaps[whichmap].flags |= (LF_LIMITS | LF_CHANGED);
1454 }
1455
1456 Num_dynamic_cells = 0;
1457
1458 Num_dynamic_lightmaps = 0;
1459 Cur_dynamic_mem_ptr = 0;
1460
1461 BlendAllLightingEdges();
1462 Num_edges_to_blend = 0;
1463 }
1464
1465 // Changes the terrain shading to approximate lighting
1466 void ApplyLightingToTerrain(vector *pos, int cellnum, float light_dist, float red_scale, float green_scale,
1467 float blue_scale, vector *light_direction, float dot_range) {
1468 int celllist[MAX_DYNAMIC_CELLS];
1469 int num_cells, i;
1470
1471 if (Dedicated_server)
1472 return;
1473
1474 ApplyLightingToObjects(pos, MAKE_ROOMNUM(cellnum), light_dist, red_scale, green_scale, blue_scale, light_direction,
1475 dot_range);
1476
1477 num_cells = fvi_QuickDistCellList(cellnum, pos, light_dist, celllist, MAX_DYNAMIC_CELLS);
1478
1479 if (num_cells < 1)
1480 return;
1481
1482 if (num_cells == MAX_DYNAMIC_CELLS) {
1483 mprintf(0, "One object has hit %d terrain cells in lighting!\n", MAX_DYNAMIC_CELLS);
1484 }
1485
1486 int red_limit = 255;
1487 int green_limit = 255;
1488 int blue_limit = 255;
1489
1490 for (i = 0; i < num_cells; i++) {
1491 int cellnum = celllist[i];
1492 terrain_segment *tseg = &Terrain_seg[cellnum];
1493
1494 int seg_x = cellnum % TERRAIN_WIDTH;
1495 int seg_z = cellnum / TERRAIN_WIDTH;
1496
1497 int subx = seg_x % 128;
1498 int subz = 127 - (seg_z % 128);
1499
1500 int whichmap = ((seg_z / 128) * 2) + (seg_x / 128);
1501 whichmap = TerrainLightmaps[whichmap];
1502
1503 // Make sure face was rendered
1504 // if (tseg->renderframe!=((FrameCount-1)%256))
1505 // continue;
1506
1507 if (Num_dynamic_cells >= MAX_DYNAMIC_CELLS) {
1508 mprintf(0, "Too many dynamic cells!\n");
1509 return;
1510 }
1511
1512 // Check for backfaces
1513 vector tpos;
1514
1515 tpos.x = (seg_x * TERRAIN_SIZE) + (TERRAIN_SIZE / 2);
1516 tpos.z = (seg_z * TERRAIN_SIZE) + (TERRAIN_SIZE / 2);
1517 tpos.y = tseg->y;
1518
1519 vector subvec = *pos - tpos;
1520
1521 float dist = vm_GetMagnitudeFast(&subvec);
1522 float scalar = 1.0 - (dist / light_dist);
1523
1524 if (light_direction) {
1525 vector lsubvec = -subvec;
1526 lsubvec /= dist;
1527 float dp = vm_DotProduct(&lsubvec, light_direction);
1528 if (dp < dot_range) {
1529 continue;
1530 } else {
1531 float add_scale = (dp - dot_range) / (1.0 - dot_range);
1532 scalar *= add_scale;
1533 }
1534 }
1535
1536 if (scalar <= 0)
1537 continue;
1538
1539 // Add a new face to our list
1540 if (!(tseg->flags & TF_DYNAMIC)) {
1541 Dynamic_cell_list[Num_dynamic_cells].cellnum = cellnum;
1542 Dynamic_cell_list[Num_dynamic_cells].r = tseg->r;
1543 Dynamic_cell_list[Num_dynamic_cells].g = tseg->g;
1544 Dynamic_cell_list[Num_dynamic_cells].b = tseg->b;
1545 tseg->flags |= TF_DYNAMIC;
1546 Num_dynamic_cells++;
1547
1548 if (GameLightmaps[whichmap].flags & LF_LIMITS) {
1549 if (subx < GameLightmaps[whichmap].cx1)
1550 GameLightmaps[whichmap].cx1 = subx;
1551 if (subx > GameLightmaps[whichmap].cx2)
1552 GameLightmaps[whichmap].cx2 = subx;
1553
1554 if (subz < GameLightmaps[whichmap].cy1)
1555 GameLightmaps[whichmap].cy1 = subz;
1556 if (subz > GameLightmaps[whichmap].cy2)
1557 GameLightmaps[whichmap].cy2 = subz;
1558
1559 } else {
1560 GameLightmaps[whichmap].cx1 = subx;
1561 GameLightmaps[whichmap].cx2 = subx + 1;
1562 GameLightmaps[whichmap].cy1 = subz;
1563 GameLightmaps[whichmap].cy2 = subz + 1;
1564 GameLightmaps[whichmap].flags |= (LF_LIMITS | LF_CHANGED);
1565 }
1566 }
1567
1568 int r = tseg->r;
1569 int g = tseg->g;
1570 int b = tseg->b;
1571
1572 if (red_scale < 0) {
1573 // we are subtracting light
1574 tseg->r = std::max<float>(0, r + (scalar * red_scale * 255));
1575 } else {
1576 if (r < red_limit)
1577 tseg->r = std::min<float>(red_limit, r + (scalar * red_scale * 255));
1578 }
1579
1580 if (green_scale < 0) {
1581 // we are subtracting light
1582 tseg->g = std::max<float>(0, g + (scalar * green_scale * 255));
1583 } else {
1584 if (g < green_limit)
1585 tseg->g = std::min<float>(green_limit, g + (scalar * green_scale * 255));
1586 }
1587
1588 if (blue_scale < 0) {
1589 // we are subtracting light
1590 tseg->b = std::max<float>(0, b + (scalar * blue_scale * 255));
1591 } else {
1592 if (b < blue_limit)
1593 tseg->b = std::min<float>(blue_limit, b + (scalar * blue_scale * 255));
1594 }
1595
1596 uint16_t color = OPAQUE_FLAG | GR_RGB16(tseg->r, tseg->g, tseg->b);
1597 uint16_t *data = lm_data(whichmap);
1598
1599 ASSERT(data);
1600
1601 data[subz * 128 + subx] = color;
1602 }
1603 }
1604
1605 // TODO: MTS: Unused?
1606 // Sets pulse parameters for an entire room
1607 void SetRoomPulse(room *rp, uint8_t pulse_time, uint8_t pulse_offset) {
1608 ASSERT(rp->used);
1609
1610 rp->pulse_time = pulse_time;
1611 rp->pulse_offset = pulse_offset;
1612 }
1613
1614 // TODO: MTS: Unused?
1615 // Returns the total number of bytes needed for volume lighting in this room
1616 int GetVolumeSizeOfRoom(room *rp, int *w, int *h, int *d) {
1617 int width = ((rp->max_xyz.x - rp->min_xyz.x) / VOLUME_SPACING) + 1;
1618 int height = ((rp->max_xyz.y - rp->min_xyz.y) / VOLUME_SPACING) + 1;
1619 int depth = ((rp->max_xyz.z - rp->min_xyz.z) / VOLUME_SPACING) + 1;
1620
1621 if (w)
1622 *w = width;
1623 if (h)
1624 *h = height;
1625 if (d)
1626 *d = depth;
1627
1628 return (width * height * depth);
1629 }
1630
1631 /*
1632 // Returns a lightmap that can be applied for specular lighting
1633 int GetSpecularLightmapForFace (vector *pos,room *rp,face *fp)
1634 {
1635 int xres=lmi_w(fp->lmi_handle);
1636 int yres=lmi_h(fp->lmi_handle);
1637 lightmap_info *lmi_ptr=&LightmapInfo[fp->lmi_handle];
1638
1639 matrix facematrix;
1640 vector fvec=-lmi_ptr->normal;
1641 vm_VectorToMatrix(&facematrix,&fvec,NULL,NULL);
1642
1643 // Get upper left vector of face
1644 vector base_vector=lmi_ptr->upper_left;
1645
1646 int square_res=GameLightmaps[lmi_ptr->lm_handle].square_res;
1647
1648 //if (square_res>=128)
1649 //return -1;
1650
1651 //square_res*=2;
1652 //xres*=2;
1653 //yres*=2;
1654 float xspacing=(float)lmi_ptr->xspacing;
1655 float yspacing=(float)lmi_ptr->yspacing;
1656
1657 int cl=GetLightmapClass (square_res);
1658 int lm_handle;
1659
1660 uint16_t *dest_data;
1661 uint16_t *src_data=(uint16_t *)lm_data(lmi_ptr->lm_handle);
1662
1663 if (lmi_ptr->spec_map==-1)
1664 {
1665 // Get a new specular map and clear it
1666 int dynamic_handle=FindFreeDynamicLightmap (cl);
1667
1668 if (dynamic_handle<0)
1669 {
1670 mprintf(0,"No free specular maps for class %d!\n",cl);
1671 return-1; // None free!
1672 }
1673
1674 Dynamic_lightmaps[cl][dynamic_handle].used=1;
1675 int dynamic_lmi_handle=Dynamic_lightmaps[cl][dynamic_handle].lmi_handle;
1676 lm_handle=LightmapInfo[dynamic_lmi_handle].lm_handle;
1677
1678 lmi_ptr->spec_map=lm_handle;
1679 dest_data=(uint16_t *)lm_data(lm_handle);
1680
1681 memset (dest_data,0,xres*yres*2);
1682
1683 Specular_face_list[Num_specular_faces]=fp->lmi_handle;
1684 Num_specular_faces++;
1685 }
1686 else
1687 {
1688 return 1;// already done for this face
1689 }
1690
1691 GameLightmaps[lm_handle].flags|=LF_CHANGED;
1692
1693 vector element_vec;
1694
1695 // Choose material
1696 int material_type=0;
1697
1698 if (GameTextures[fp->tmap].flags & TF_PLASTIC)
1699 material_type=1;
1700 else if (GameTextures[fp->tmap].flags & TF_MARBLE)
1701 material_type=2;
1702
1703 // Go through and change each element of this lightmap
1704 // SLOW!
1705
1706 int texel_num=0;
1707
1708 int num=SpecialFaces[fp->special_handle].num;
1709
1710 for (int y=0;y<yres;y++,base_vector-=(facematrix.uvec*yspacing),texel_num+=xres)
1711 {
1712 element_vec=base_vector;
1713
1714 for (int x=0;x<xres;x++,element_vec+=(facematrix.rvec*xspacing))
1715 {
1716 int lightmap_texel_num=texel_num+x;
1717
1718 uint16_t lightmap_texel=src_data[lightmap_texel_num];
1719
1720 if (! (lightmap_texel & OPAQUE_FLAG))
1721 continue;
1722
1723 float rscale,gscale,bscale;
1724
1725 rscale=Light_component_scalar [(lightmap_texel>>10 & 0x1f)];
1726 gscale=Light_component_scalar [(lightmap_texel>>5 & 0x1f)];
1727 bscale=Light_component_scalar [(lightmap_texel & 0x1f)];
1728
1729 for (int i=0;i<num;i++)
1730 {
1731 if (SpecialFaces[fp->special_handle].spec_instance[i].bright_color==0)
1732 continue;
1733
1734 lightmap_texel=dest_data[lightmap_texel_num];
1735
1736 int old_r=lightmap_texel>>10 & 0x1f;
1737 int old_g=lightmap_texel>>5 & 0x1f;
1738 int old_b=lightmap_texel & 0x1f;
1739
1740 vector subvec=*pos-element_vec;
1741 vm_NormalizeVectorFast (&subvec);
1742
1743 vector
1744 incident_norm=element_vec-SpecialFaces[fp->special_handle].spec_instance[i].bright_center; vm_NormalizeVectorFast
1745 (&incident_norm);
1746
1747 float d=incident_norm * fp->normal;
1748 vector upvec=d * fp->normal;
1749 incident_norm-=(2*upvec);
1750
1751 float dotp=subvec * incident_norm;
1752
1753 if (dotp<0)
1754 continue;
1755 if (dotp>1)
1756 dotp=1;
1757
1758 if (dotp>0)
1759 {
1760 int index=((float)(MAX_SPECULAR_INCREMENTS-1)*dotp);
1761 float val=Specular_tables[material_type][index];
1762
1763 uint16_t color=SpecialFaces[fp->special_handle].spec_instance[i].bright_color;
1764 int r=color >> 10 & 0x1f;
1765 int g=color >> 5 & 0x1f;
1766 int b=color & 0x1f;
1767
1768 r*=val*rscale;
1769 g*=val*gscale;
1770 b*=val*bscale;
1771
1772 r=std::min(31,old_r+r);
1773 g=std::min(31,old_g+g);
1774 b=std::min(31,old_b+b);
1775
1776
1777 dest_data[lightmap_texel_num]=OPAQUE_FLAG|(r<<10)|(g<<5)|(b);
1778 }
1779 }
1780 }
1781 }
1782
1783 return lm_handle;
1784 }*/
1785
1786 /*
1787 // Returns a lightmap that can be applied for specular lighting
1788 int GetSpecularLightmapForFace (vector *pos,room *rp,face *fp)
1789 {
1790
1791 int xres=lmi_w(fp->lmi_handle);
1792 int yres=lmi_h(fp->lmi_handle);
1793 lightmap_info *lmi_ptr=&LightmapInfo[fp->lmi_handle];
1794 vector center=SpecialFaces[fp->special_handle].center;
1795
1796 matrix facematrix;
1797 vector fvec=-lmi_ptr->normal;
1798 vm_VectorToMatrix(&facematrix,&fvec,NULL,NULL);
1799
1800 // Get upper left vector of face
1801 vector base_vector=lmi_ptr->upper_left;
1802
1803 int start_x=fp->x1;
1804 int start_y=fp->y1;
1805 int end_x=fp->x1+fp->lw;
1806 int end_y=fp->y1+fp->lh;
1807
1808 int width=end_x-start_x;
1809 int height=end_y-start_y;
1810
1811 if (width==0)
1812 width++;
1813 if (height==0)
1814 height++;
1815
1816 ASSERT (width>0);
1817 ASSERT (height>0);
1818
1819 int square_res=GameLightmaps[lmi_ptr->lm_handle].square_res;
1820 int cl=GetLightmapClass (square_res);
1821 int lm_handle;
1822
1823 uint16_t *dest_data;
1824 uint16_t *src_data=(uint16_t *)lm_data(lmi_ptr->lm_handle);
1825
1826 if (lmi_ptr->spec_map==-1)
1827 {
1828 // Get a new specular map and clear it
1829 int dynamic_handle=FindFreeDynamicLightmap (cl);
1830
1831 if (dynamic_handle<0)
1832 {
1833 mprintf(0,"No free specular maps for class %d!\n",cl);
1834 return-1; // None free!
1835 }
1836
1837 Dynamic_lightmaps[cl][dynamic_handle].used=1;
1838 int dynamic_lmi_handle=Dynamic_lightmaps[cl][dynamic_handle].lmi_handle;
1839 lm_handle=LightmapInfo[dynamic_lmi_handle].lm_handle;
1840
1841 lmi_ptr->spec_map=lm_handle;
1842 dest_data=(uint16_t *)lm_data(lm_handle);
1843
1844 memset (dest_data,0,xres*yres*2);
1845
1846 Specular_face_list[Num_specular_faces]=fp->lmi_handle;
1847 Num_specular_faces++;
1848 }
1849 else
1850 {
1851 lm_handle=lmi_ptr->spec_map;
1852 dest_data=(uint16_t *)lm_data(lm_handle);
1853 }
1854
1855
1856 GameLightmaps[lm_handle].flags|=LF_CHANGED;
1857
1858 // Rotate the base_vector, the eyepoint, face normal and the lightsource into the Z plane
1859 vector tempvec,eye_pos,light_pos[4];
1860 vector norm;
1861
1862 base_vector-=center;
1863
1864 vm_MatrixMulVector (&tempvec,&base_vector,&facematrix);
1865 base_vector=tempvec;
1866
1867 tempvec=*pos-center;
1868 vm_MatrixMulVector (&eye_pos,&tempvec,&facematrix);
1869
1870 for (int i=0;i<4;i++)
1871 {
1872 if (SpecialFaces[fp->special_handle].spec_instance[i].bright_color!=0)
1873 {
1874 tempvec=SpecialFaces[fp->special_handle].spec_instance[i].bright_center-center;
1875 vm_MatrixMulVector (&light_pos[i],&tempvec,&facematrix);
1876 }
1877 }
1878
1879 vm_MatrixMulVector (&norm,&fp->normal,&facematrix);
1880
1881 // Move to offset
1882 vector element_vec;
1883
1884 base_vector.y-=(start_y*lmi_ptr->yspacing);
1885 base_vector.x+=(start_x*lmi_ptr->xspacing);
1886
1887 // Move to center of texel
1888 base_vector.y-=(lmi_ptr->yspacing/2);
1889 base_vector.x+=(lmi_ptr->xspacing/2);
1890
1891 // Choose material
1892 int material_type=0;
1893
1894 if (GameTextures[fp->tmap].flags & TF_PLASTIC)
1895 material_type=1;
1896 else if (GameTextures[fp->tmap].flags & TF_MARBLE)
1897 material_type=2;
1898
1899 // Go through and change each element of this lightmap
1900 // SLOW!
1901
1902 int texel_num=start_y*xres+start_x;
1903 int num=SpecialFaces[fp->special_handle].num;
1904
1905 for (int y=0;y<height;y++,base_vector.y-=lmi_ptr->yspacing,texel_num+=xres)
1906 {
1907 element_vec=base_vector;
1908
1909 for (int x=0;x<width;x++,element_vec.x+=lmi_ptr->xspacing)
1910 {
1911 int lightmap_texel_num=texel_num+x;
1912
1913 if (SpecialFaces[fp->special_handle].normal_map[lightmap_texel_num*3+2]==128)
1914 continue;
1915 else
1916 {
1917 norm.x=Normal_table[SpecialFaces[fp->special_handle].normal_map[lightmap_texel_num*3+0]];
1918 norm.y=Normal_table[SpecialFaces[fp->special_handle].normal_map[lightmap_texel_num*3+1]];
1919 norm.z=Normal_table[SpecialFaces[fp->special_handle].normal_map[lightmap_texel_num*3+2]];
1920 }
1921
1922 uint16_t lightmap_texel=src_data[lightmap_texel_num];
1923
1924 if (! (lightmap_texel & OPAQUE_FLAG))
1925 continue;
1926
1927 float base_r=(lightmap_texel>>10 & 0x1f)>15?1:0;
1928 float base_g=(lightmap_texel>>5 & 0x1f)>15?1:0;
1929 float base_b=(lightmap_texel & 0x1f)>15?1:0;
1930
1931
1932 for (int i=0;i<num;i++)
1933 {
1934 if (SpecialFaces[fp->special_handle].spec_instance[i].bright_color==0)
1935 continue;
1936
1937 lightmap_texel=dest_data[lightmap_texel_num];
1938
1939 int old_r=lightmap_texel>>10 & 0x1f;
1940 int old_g=lightmap_texel>>5 & 0x1f;
1941 int old_b=lightmap_texel & 0x1f;
1942
1943 vector subvec=eye_pos-element_vec;
1944 vm_NormalizeVectorFast (&subvec);
1945
1946 vector incident_norm=element_vec-light_pos[i];
1947 vm_NormalizeVectorFast (&incident_norm);
1948
1949 float d=incident_norm * norm;
1950 vector upvec=d * norm;
1951 incident_norm-=(2*upvec);
1952
1953 float dotp=subvec * incident_norm;
1954
1955 // if (rp-Rooms==0 && fp-rp->faces==5 && (x==width/2) && (y==height/2))
1956 // mprintf(0,"dotp=%f\n",dotp);
1957
1958
1959 if (dotp<0)
1960 continue;
1961 if (dotp>1)
1962 dotp=1;
1963
1964 if (dotp>0)
1965 {
1966 int index=((float)(MAX_SPECULAR_INCREMENTS-1)*dotp);
1967 float val=Specular_tables[material_type][index];
1968
1969 uint16_t color=SpecialFaces[fp->special_handle].spec_instance[i].bright_color;
1970 int r=color >> 10 & 0x1f;
1971 int g=color >> 5 & 0x1f;
1972 int b=color & 0x1f;
1973
1974 r*=val*base_r;
1975 g*=val*base_g;
1976 b*=val*base_b;
1977
1978 r=std::min(31,old_r+r);
1979 g=std::min(31,old_g+g);
1980 b=std::min(31,old_b+b);
1981
1982
1983 dest_data[lightmap_texel_num]=OPAQUE_FLAG|(r<<10)|(g<<5)|(b);
1984 }
1985 }
1986 }
1987 }
1988
1989 return lm_handle;
1990 }*/
1991
1992 #define DESTROYABLE_IGNORE_LIMIT .2f
1993
1994 // Kills the lighting that a face casts and dampens all the faces that light influences
1995 void DestroyLight(int roomnum, int facenum) {
1996 vector vecs[MAX_VERTS_PER_FACE], center;
1997 room *destroy_rp = &Rooms[roomnum];
1998 face *destroy_fp = &destroy_rp->faces[facenum];
1999 float r, g, b;
2000 fvi_face_room_list facelist[MAX_DYNAMIC_FACES];
2001 int num_faces, i;
2002 uint16_t lmilist[MAX_DYNAMIC_FACES];
2003 int num_spoken_for = 0;
2004
2005 if (Dedicated_server)
2006 return;
2007
2008 float mul = ((float)destroy_fp->light_multiple) / 4.0;
2009
2010 r = GameTextures[destroy_fp->tmap].r * mul;
2011 g = GameTextures[destroy_fp->tmap].g * mul;
2012 b = GameTextures[destroy_fp->tmap].b * mul;
2013
2014 // Get highest component
2015 float rmax = std::max({r, g, b});
2016
2017 // Get the normalized color that this face emits
2018 float red_scale = r / rmax;
2019 float green_scale = g / rmax;
2020 float blue_scale = b / rmax;
2021
2022 // red_scale/=30;
2023 // green_scale/=30;
2024 // blue_scale/=30;
2025
2026 mprintf(0, "r=%f g=%f b=%f\n", red_scale, green_scale, blue_scale);
2027
2028 // Get center and area of light face
2029 for (i = 0; i < destroy_fp->num_verts; i++)
2030 vecs[i] = destroy_rp->verts[destroy_fp->face_verts[i]];
2031
2032 float area = vm_GetCentroid(&center, vecs, destroy_fp->num_verts);
2033
2034 // Get the sphere of influence of this light
2035 float power = rmax * area;
2036 float temp_ignore = DESTROYABLE_IGNORE_LIMIT / power;
2037 float express = 1.0 / temp_ignore;
2038 express /= 3.14f;
2039 express -= area;
2040 float sphere_dist = sqrt(express);
2041
2042 // ApplyLightingToObjects (pos,roomnum,light_dist,red_scale,green_scale,blue_scale,light_direction,dot_range);
2043
2044 num_faces = fvi_QuickDistFaceList(roomnum, &center, sphere_dist, facelist, MAX_DYNAMIC_FACES);
2045
2046 for (i = 0; i < num_faces; i++) {
2047 room *rp = &Rooms[facelist[i].room_index];
2048 face *fp = &rp->faces[facelist[i].face_index];
2049
2050 ASSERT(Rooms[facelist[i].room_index].used);
2051 ASSERT(facelist[i].face_index < Rooms[facelist[i].room_index].num_faces);
2052
2053 // Make sure there already is a lightmap for this face
2054 if (!(fp->flags & FF_LIGHTMAP))
2055 continue;
2056
2057 if (GameTextures[fp->tmap].flags & TF_LIGHT)
2058 continue;
2059
2060 if (Lmi_spoken_for[fp->lmi_handle / 8] & (1 << (fp->lmi_handle % 8)))
2061 continue;
2062
2063 int xres = lmi_w(fp->lmi_handle);
2064 int yres = lmi_h(fp->lmi_handle);
2065
2066 lightmap_info *lmi_ptr = &LightmapInfo[fp->lmi_handle];
2067
2068 int lmw = lm_w(lmi_ptr->lm_handle);
2069
2070 vector subvec = center - lmi_ptr->upper_left;
2071 float dist_from_plane = vm_DotProduct(&subvec, &lmi_ptr->normal);
2072
2073 // Check for backfaces
2074 if (dist_from_plane < 0)
2075 continue;
2076
2077 dist_from_plane = fabs(dist_from_plane);
2078
2079 if (dist_from_plane > sphere_dist)
2080 continue;
2081
2082 // Compute face matrix
2083 matrix facematrix;
2084 vector fvec = -lmi_ptr->normal;
2085 vm_VectorToMatrix(&facematrix, &fvec, NULL, NULL);
2086
2087 // Get upper left vector of face
2088 vector base_vector = lmi_ptr->upper_left;
2089
2090 // Get the area of effect for this light
2091 float area_of_effect = (1.0 - (dist_from_plane / sphere_dist)) * sphere_dist;
2092 vector touch_vector = center - (lmi_ptr->normal * dist_from_plane);
2093
2094 subvec = touch_vector - base_vector;
2095 float rdist = vm_DotProduct(&subvec, &facematrix.rvec);
2096 float udist = -vm_DotProduct(&subvec, &facematrix.uvec);
2097 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacing / 2)) / lmi_ptr->xspacing);
2098 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacing / 2)) / lmi_ptr->yspacing);
2099
2100 // Get the rectangle of the lightmap that we want to light
2101 int start_x = (rdist / lmi_ptr->xspacing) - x_add;
2102 int start_y = (udist / lmi_ptr->yspacing) - y_add;
2103 int end_x = (rdist / lmi_ptr->xspacing) + x_add + .5;
2104 int end_y = (udist / lmi_ptr->yspacing) + y_add + .5;
2105
2106 if (end_x < 0 || end_y < 0 || start_x >= (xres) || start_y >= (yres))
2107 continue;
2108
2109 if (start_x < 0)
2110 start_x = 0;
2111 if (start_y < 0)
2112 start_y = 0;
2113 if (end_x > xres)
2114 end_x = xres;
2115 if (end_y > yres)
2116 end_y = yres;
2117
2118 end_x++;
2119 end_y++;
2120
2121 if (end_x > xres)
2122 end_x = xres;
2123 if (end_y > yres)
2124 end_y = yres;
2125
2126 int width = end_x - start_x;
2127 int height = end_y - start_y;
2128
2129 if (width == 0)
2130 width++;
2131 if (height == 0)
2132 height++;
2133
2134 ASSERT(width > 0);
2135 ASSERT(height > 0);
2136
2137 // Now copy our source data to our dest data so we have a base to work with
2138 uint16_t *dest_data;
2139
2140 dest_data = (uint16_t *)lm_data(LightmapInfo[fp->lmi_handle].lm_handle);
2141 int lm_handle = LightmapInfo[fp->lmi_handle].lm_handle;
2142
2143 if (!(GameLightmaps[lm_handle].flags & LF_LIMITS)) {
2144 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
2145 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
2146 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
2147 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
2148 } else {
2149 if (start_x + lmi_ptr->x1 < GameLightmaps[lm_handle].cx1)
2150 GameLightmaps[lm_handle].cx1 = start_x + lmi_ptr->x1;
2151 if (start_x + lmi_ptr->x1 + width > GameLightmaps[lm_handle].cx2)
2152 GameLightmaps[lm_handle].cx2 = start_x + width + lmi_ptr->x1;
2153
2154 if (start_y + lmi_ptr->y1 < GameLightmaps[lm_handle].cy1)
2155 GameLightmaps[lm_handle].cy1 = start_y + lmi_ptr->y1;
2156 if (lmi_ptr->y1 + start_y + height > GameLightmaps[lm_handle].cy2)
2157 GameLightmaps[lm_handle].cy2 = start_y + height + lmi_ptr->y1;
2158 }
2159
2160 GameLightmaps[lm_handle].flags |= (LF_LIMITS | LF_CHANGED);
2161
2162 lmilist[num_spoken_for] = fp->lmi_handle;
2163 Lmi_spoken_for[fp->lmi_handle / 8] |= (1 << (fp->lmi_handle % 8));
2164 num_spoken_for++;
2165
2166 vector element_vec;
2167
2168 base_vector -= (start_y * (facematrix.uvec * lmi_ptr->yspacing));
2169 base_vector += (start_x * (facematrix.rvec * lmi_ptr->xspacing));
2170
2171 base_vector -= ((facematrix.uvec / 2) * lmi_ptr->yspacing);
2172 base_vector += ((facematrix.rvec / 2) * lmi_ptr->xspacing);
2173
2174 // Go through and change each element of this lightmap
2175 // SLOW!
2176
2177 int texel_num = ((start_y + lmi_ptr->y1) * lmw) + start_x + lmi_ptr->x1;
2178 for (int y = 0; y < height; y++, base_vector -= (facematrix.uvec * lmi_ptr->yspacing), texel_num += lmw) {
2179 element_vec = base_vector;
2180
2181 for (int x = 0; x < width; x++, element_vec += (facematrix.rvec * lmi_ptr->xspacing)) {
2182 int lightmap_texel_num = texel_num + x;
2183
2184 uint16_t lightmap_texel = dest_data[lightmap_texel_num];
2185
2186 if (!(lightmap_texel & OPAQUE_FLAG))
2187 continue;
2188
2189 float dist = vm_VectorDistanceQuick(&element_vec, &center);
2190 float scalar = 1.0 - (dist / sphere_dist);
2191
2192 if (scalar <= 0)
2193 continue;
2194
2195 int r = (lightmap_texel >> 10) & 0x1f;
2196 int g = (lightmap_texel >> 5) & 0x1f;
2197 int b = lightmap_texel & 0x1f;
2198
2199 if (r > 0)
2200 r = std::max<float>(0, r - (scalar * red_scale * 31));
2201
2202 if (g > 0)
2203 g = std::max<float>(0, g - (scalar * green_scale * 31));
2204
2205 if (b > 0)
2206 b = std::max<float>(0, b - (scalar * blue_scale * 31));
2207
2208 lightmap_texel = OPAQUE_FLAG | (r << 10) | (g << 5) | b;
2209
2210 dest_data[lightmap_texel_num] = lightmap_texel;
2211 }
2212 }
2213
2214 BlendLightingEdges(lmi_ptr);
2215 }
2216
2217 for (i = 0; i < num_spoken_for; i++) {
2218 int index = lmilist[i];
2219 Lmi_spoken_for[index / 8] = 0;
2220 }
2221 }
2222
2223 // TODO: MTS: unused?
2224 // Adds to our list of destroyable lights that got destroyed this frame
2225 void AddToDestroyableLightList(int roomnum, int facenum) {
2226 if (Num_destroyed_lights_this_frame >= MAX_DESTROYED_LIGHTS_PER_FRAME) {
2227 mprintf(0, "Ran out of destroyable light slots!\n");
2228 return;
2229 }
2230
2231 mprintf(0, "Destroying light. Room=%d face=%d\n", roomnum, facenum);
2232
2233 Destroyed_light_rooms_this_frame[Num_destroyed_lights_this_frame] = roomnum;
2234 Destroyed_light_faces_this_frame[Num_destroyed_lights_this_frame++] = facenum;
2235 }
2236
2237 // Goes through our destroyable light list and actually kills all the lights
2238 void DoDestroyedLightsForFrame() {
2239 int i;
2240 for (i = 0; i < Num_destroyed_lights_this_frame; i++) {
2241 int roomnum = Destroyed_light_rooms_this_frame[i];
2242 int facenum = Destroyed_light_faces_this_frame[i];
2243 DestroyLight(roomnum, facenum);
2244 }
2245
2246 Num_destroyed_lights_this_frame = 0;
2247 }