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"cxx . . $ `<file>/lib/3d.h`/
28 #include "gametexture.h"cxx . . $ `<file>/Descent3/gametexture.h`/
29 #include "lighting.h"cxx . . $ `<file>/Descent3/lighting.h`/
30 #include "lightmap.h"cxx . . $ `<file>/lib/lightmap.h`/
31 #include "descent.h"cxx . . $ `<file>/Descent3/descent.h`/
32 #include "game.h"cxx . . $ `<file>/Descent3/game.h`/
33 #include "room.h"cxx . . $ `<file>/Descent3/room.h`/
34 #include <string.h>
35 #include <stdlib.h>
36 #include "findintersection.h"cxx . . $ `<file>/physics/findintersection.h`/
37 #include "lightmap_info.h"cxx . . $ `<file>/Descent3/lightmap_info.h`/
38 #include "polymodel.h"cxx . . $ `<file>/model/polymodel.h`/
39 #include "special_face.h"cxx . . $ `<file>/Descent3/special_face.h`/
40 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
41 #include "config.h"cxx . . $ `<file>/Descent3/config.h`/
42 #include "dedicated_server.h"cxx . . $ `<file>/lib/dedicated_server.h`/
43 #include "objinfo.h"cxx . . $ `<file>/Descent3/objinfo.h`/
44 #include "Macros.h"cxx . . $ `<file>/lib/Macros.h`/
45
46 #include <algorithm>
47
48 #define NUM_DYNAMIC_CLASSEScxx . . $ `Descent3/lighting.cpp:48:9`! 7
49 #define MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`! 2000
50 #define MAX_DYNAMIC_CELLScxx . . $ `Descent3/lighting.cpp:50:9`! 1500
51 #define MAX_VOLUME_OBJECTScxx . . $ `Descent3/lighting.cpp:51:9`! 500
52
53 // How much memory set aside for dynamic lightmaps
54 // Currently 3 megs
55
56 int DYNAMIC_LIGHTMAP_MEMORYcxx . . $ DYNAMIC_LIGHTMAP_MEMORY. = 1000000;
57 #define MAX_DYNAMIC_LIGHTMAPScxx . . $ `Descent3/lighting.cpp:57:9`! 2000
58
59 struct volume_objectcxx . . $ volume_object# {
60 int objnumcxx . . $ volume_object#objnum.;
61 int handlecxx . . $ volume_object#handle.;
62 };
63
64 float Specular_tablescxx . . $ Specular_tables.[3][MAX_SPECULAR_INCREMENTScxx . . $ `Descent3/lighting.h:38:9`!];
65
66 static uint16_tcxx . . $ uint16_t# *Dynamic_lightmap_memorycxx . . $ Dynamic_lightmap_memory. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
67 static float Light_component_scalarcxx . . $ Light_component_scalar.[32];
68 float Ubyte_to_floatcxx . . $ Ubyte_to_float.[256];
69
70 static uint8_tcxx . . $ uint8_t# Lmi_spoken_forcxx . . $ Lmi_spoken_for.[MAX_LIGHTMAP_INFOScxx . . $ `Descent3/lightmap_info.h:74:9`! / 8];
71
72 static dynamic_lightmapcxx . . $ dynamic_lightmap# *Dynamic_lightmapscxx . . $ Dynamic_lightmaps.;
73 static dynamic_facecxx . . $ dynamic_face# Dynamic_face_listcxx . . $ Dynamic_face_list.[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
74 static uint16_tcxx . . $ uint16_t# Specular_face_listcxx . . $ Specular_face_list.[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
75 static volume_objectcxx . . $ volume_object# Dynamic_volume_object_listcxx . . $ Dynamic_volume_object_list.[MAX_VOLUME_OBJECTScxx . . $ `Descent3/lighting.cpp:51:9`!];
76 static dynamic_cellcxx . . $ dynamic_cell# Dynamic_cell_listcxx . . $ Dynamic_cell_list.[MAX_DYNAMIC_CELLScxx . . $ `Descent3/lighting.cpp:50:9`!];
77 static int Specular_mapscxx . . $ Specular_maps.[NUM_DYNAMIC_CLASSEScxx . . $ `Descent3/lighting.cpp:48:9`!];
78
79 static uint16_tcxx . . $ uint16_t# Edges_to_blendcxx . . $ Edges_to_blend.[MAX_DYNAMIC_LIGHTMAPScxx . . $ `Descent3/lighting.cpp:57:9`!];
80 static int Num_edges_to_blendcxx . . $ Num_edges_to_blend. = 0;
81
82 static int Num_specular_facescxx . . $ Num_specular_faces. = 0;
83 static int Num_dynamic_facescxx . . $ Num_dynamic_faces. = 0;
84 static int Num_dynamic_lightmapscxx . . $ Num_dynamic_lightmaps. = 0;
85 static int Cur_dynamic_mem_ptrcxx . . $ Cur_dynamic_mem_ptr. = 0;
86 static int Num_volume_objectscxx . . $ Num_volume_objects. = 0;
87 static int Num_dynamic_cellscxx . . $ Num_dynamic_cells. = 0;
88 static int Num_destroyed_lights_this_framecxx . . $ Num_destroyed_lights_this_frame. = 0;
89
90 #define MAX_DESTROYED_LIGHTS_PER_FRAMEcxx . . $ `Descent3/lighting.cpp:90:9`! 20
91 static int Destroyed_light_rooms_this_framecxx . . $ Destroyed_light_rooms_this_frame.[MAX_DESTROYED_LIGHTS_PER_FRAMEcxx . . $ `Descent3/lighting.cpp:90:9`!];
92 static int Destroyed_light_faces_this_framecxx . . $ Destroyed_light_faces_this_frame.[MAX_DESTROYED_LIGHTS_PER_FRAMEcxx . . $ `Descent3/lighting.cpp:90:9`!];
93
94 static void FreeLightingcxx . . $ FreeLighting(49f6e7a06ebc5aa8).();
95 static int GetFreeDynamicLightmapcxx . . $ GetFreeDynamicLightmap(9b79fb6aee4c0440).(int w, int h);
96 static void BlendLightingEdgescxx . . $ BlendLightingEdges(d2f3fbe01c9a6be1).(lightmap_infocxx . . $ lightmap_info# *lmi_ptr);
97 static void ApplyLightingToExternalRoomcxx . . $ ApplyLightingToExternalRoom(31a30e6d6b9dd744).(vectorcxx . . $ vector# *pos, int roomnum, float light_dist, float red_scale, float green_scale,
98 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range);
99 static void StartLightingInstancecxx . . $ StartLightingInstance(bc315f98e1febb02).(vectorcxx . . $ vector# *pos, matrixcxx . . $ matrix# *orient);
100 static void DoneLightingInstancecxx . . $ DoneLightingInstance(49f6e7a06ebc5aa8).();
101 static void ApplyLightingToSubmodelcxx . . $ ApplyLightingToSubmodel(164d4140fb0d0402).(objectcxx . . $ object# *obj, poly_modelcxx . . $ poly_model# *pm, bsp_infocxx . . $ bsp_info# *sm, float light_dist, float red_scale,
102 float green_scale, float blue_scale, float dot_range);
103 static void ApplyVolumeLightToObjectcxx . . $ ApplyVolumeLightToObject(1ebef0143163e7d6).(vectorcxx . . $ vector# *pos, objectcxx . . $ object# *obj, float light_dist, float red_scale, float green_scale,
104 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range);
105 static void ApplyLightingToObjectscxx . . $ ApplyLightingToObjects(31a30e6d6b9dd744).(vectorcxx . . $ vector# *pos, int roomnum, float light_dist, float red_scale, float green_scale,
106 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range);
107
108 // Frees memory used by dynamic light structures
109 void FreeLightingcxx . . $ FreeLighting(49f6e7a06ebc5aa8).() {
110 if (Dynamic_lightmap_memorycxx . . $ Dynamic_lightmap_memory.)
111 mem_freecxx . . $ `mem/mem.h:80:9`!(Dynamic_lightmap_memory);
112 if (Dynamic_lightmapscxx . . $ Dynamic_lightmaps.)
113 mem_freecxx . . $ `mem/mem.h:80:9`!(Dynamic_lightmaps);
114 }
115
116 // Sets up our dynamic lighting maps
117 // and tables
118 void InitDynamicLightingcxx . . $ InitDynamicLighting(49f6e7a06ebc5aa8).() {
119 int i, cl, size;
120
121 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Initting dynamic lighting.\n");
122
123 memset(Lmi_spoken_forcxx . . $ Lmi_spoken_for., 0, MAX_LIGHTMAP_INFOScxx . . $ `Descent3/lightmap_info.h:74:9`! / 8);
124
125 for (i = 0; i < 16; i++)
126 Light_component_scalarcxx . . $ Light_component_scalar.[i] = i / 15.0;
127 for (i = 16; i < 32; i++)
128 Light_component_scalarcxx . . $ Light_component_scalar.[i] = 1.0;
129
130 for (cl = 0, size = 128; size >= 2; size >>= 1, cl++) {
131 Specular_mapscxx . . $ Specular_maps.[cl] = lm_AllocLightmapcxx . . $ lm_AllocLightmap(9b79fb6aee4c0440).(size, size);
132 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Specular_maps[cl] != BAD_LM_INDEXcxx . . $ `lib/lightmap.h:25:9`!);
133 }
134
135 // Init our dynamic memory
136 if (Mem_low_memory_modecxx . . $ Mem_low_memory_mode.) {
137 // Just use 1 meg if in low mem mode
138 DYNAMIC_LIGHTMAP_MEMORYcxx . . $ DYNAMIC_LIGHTMAP_MEMORY. = 500000;
139 }
140
141 Dynamic_lightmap_memorycxx . . $ Dynamic_lightmap_memory. = (uint16_tcxx . . $ uint16_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(DYNAMIC_LIGHTMAP_MEMORY);
142
143 // Init our records list
144 Dynamic_lightmapscxx . . $ Dynamic_lightmaps. = (dynamic_lightmapcxx . . $ dynamic_lightmap# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(dynamic_lightmap) * MAX_DYNAMIC_LIGHTMAPScxx . . $ `Descent3/lighting.cpp:57:9`!);
145 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Dynamic_lightmaps);
146
147 for (i = 0; i < MAX_DYNAMIC_LIGHTMAPScxx . . $ `Descent3/lighting.cpp:57:9`!; i++) {
148 memset(&Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[i], 0, sizeof(dynamic_lightmapcxx . . $ dynamic_lightmap#));
149 }
150
151 // Setup uint8_t to float
152 for (i = 0; i < 256; i++)
153 Ubyte_to_floatcxx . . $ Ubyte_to_float.[i] = (float)i / 255.0;
154
155 // Setup specular tables
156 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Building specular tables.\n");
157
158 for (i = 0; i < MAX_SPECULAR_INCREMENTScxx . . $ `Descent3/lighting.h:38:9`!; i++) {
159 float val = (float)i / (float)(MAX_SPECULAR_INCREMENTScxx . . $ `Descent3/lighting.h:38:9`! - 1);
160
161 // Metal
162 Specular_tablescxx . . $ Specular_tables.[0][i] = (val * val * val * val * val * val);
163
164 // Plastic
165 Specular_tablescxx . . $ Specular_tables.[1][i] = (val * val * val * val * val * val * val * val * val * val * val * val * val * val);
166
167 // Marble
168 Specular_tablescxx . . $ Specular_tables.[2][i] = (val * val * val * val);
169 }
170
171 atexit(FreeLightingcxx . . $ FreeLighting(49f6e7a06ebc5aa8).);
172 }
173
174 // Given a float, returns the index into the Ubyte_float_table that this number
175 // corresponds to
176 uint8_tcxx . . $ uint8_t# Float_to_ubytecxx . . $ Float_to_ubyte(ed6d3b1f37918c01).(float fnum) {
177 int i;
178
179 if (fnum < 0 || fnum > 1.0) {
180 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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_floatcxx . . $ 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 GetFreeDynamicLightmapcxx . . $ GetFreeDynamicLightmap(9b79fb6aee4c0440).(int w, int h) {
194 int total = w * h * 2;
195
196 if (Num_dynamic_lightmapscxx . . $ Num_dynamic_lightmaps. == MAX_DYNAMIC_LIGHTMAPScxx . . $ `Descent3/lighting.cpp:57:9`!)
197 return -1;
198
199 if (total + Cur_dynamic_mem_ptrcxx . . $ Cur_dynamic_mem_ptr. > DYNAMIC_LIGHTMAP_MEMORYcxx . . $ DYNAMIC_LIGHTMAP_MEMORY.) {
200 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Ran out of lightmap memory (%d)\n", DYNAMIC_LIGHTMAP_MEMORY);
201 return -1;
202 }
203
204 int n = Num_dynamic_lightmapscxx . . $ Num_dynamic_lightmaps.++;
205 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Dynamic_lightmaps[n].used == 0);
206
207 Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[n].usedcxx . . $ dynamic_lightmap#used. = 1;
208 Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[n].mem_ptrcxx . . $ dynamic_lightmap#mem_ptr. = &Dynamic_lightmap_memorycxx . . $ Dynamic_lightmap_memory.[Cur_dynamic_mem_ptrcxx . . $ Cur_dynamic_mem_ptr. / 2];
209 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Dynamic_lightmaps[n].mem_ptr);
210
211 Cur_dynamic_mem_ptrcxx . . $ 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 BlendLightingEdgescxx . . $ BlendLightingEdges(d2f3fbe01c9a6be1).(lightmap_infocxx . . $ lightmap_info# *lmi_ptr) {
218 int lmi_handle = lmi_ptr - LightmapInfocxx . . $ LightmapInfo.;
219 int h = lmi_hcxx . . $ lmi_h(7864480464b09eea).(lmi_handle);
220 int w = lmi_wcxx . . $ lmi_w(7864480464b09eea).(lmi_handle);
221 int lm_handle = lmi_ptr->lm_handlecxx . . $ lightmap_info#lm_handle.;
222 uint16_tcxx . . $ uint16_t# *dest_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(lm_handle);
223 uint16_tcxx . . $ uint16_t# *src_data = dest_data;
224 int dest_x = lmi_ptr->x1cxx . . $ lightmap_info#x1. - 1;
225 int dest_y = lmi_ptr->y1cxx . . $ lightmap_info#y1. - 1;
226 int i;
227
228 ASSERTcxx . . $ `misc/pserror.h:246:9`!(dest_data);
229 if (!dest_data)
230 return;
231
232 ASSERTcxx . . $ `misc/pserror.h:246:9`!(dest_x >= 0 && dest_y >= 0);
233 ASSERTcxx . . $ `misc/pserror.h:246:9`!(dest_x + w < 127);
234 ASSERTcxx . . $ `misc/pserror.h:246:9`!(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 ApplyLightingToExternalRoomcxx . . $ ApplyLightingToExternalRoom(31a30e6d6b9dd744).(vectorcxx . . $ vector# *pos, int roomnum, float light_dist, float red_scale, float green_scale,
263 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range) {
264 int i, lm_handle, t;
265 uint16_tcxx . . $ uint16_t# *dest_data;
266 vectorcxx . . $ vector# rad;
267 roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[roomnum];
268 vectorcxx . . $ vector# Light_min_xyz;
269 vectorcxx . . $ vector# Light_max_xyz;
270 uint16_tcxx . . $ uint16_t# lmilist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
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.xcxx . . $ vector#x. = light_dist;
278 rad.ycxx . . $ vector#y. = light_dist;
279 rad.zcxx . . $ vector#z. = light_dist;
280
281 Light_min_xyz =cxx . . $ vector#`operator=`(c0842353a20857d). *pos -cxx . . $ operator-(b77b703cd1e43e33). rad;
282 Light_max_xyz =cxx . . $ vector#`operator=`(c0842353a20857d). *pos +cxx . . $ operator+(b77b703cd1e43e33). rad;
283
284 // Now the Light_position is in the submodels frame of reference
285 for (i = 0; i < rp->num_facescxx . . $ room#num_faces.; i++) {
286 facecxx . . $ face# *fp = &rp->facescxx . . $ room#faces.[i];
287
288 // check to see if this face is within reach
289 if (Light_max_xyz.ycxx . . $ vector#y. < fp->min_xyzcxx . . $ face#min_xyz..ycxx . . $ vector#y. || fp->max_xyzcxx . . $ face#max_xyz..ycxx . . $ vector#y. < Light_min_xyz.ycxx . . $ vector#y. || Light_max_xyz.xcxx . . $ vector#x. < fp->min_xyzcxx . . $ face#min_xyz..xcxx . . $ vector#x. ||
290 fp->max_xyzcxx . . $ face#max_xyz..xcxx . . $ vector#x. < Light_min_xyz.xcxx . . $ vector#x. || Light_max_xyz.zcxx . . $ vector#z. < fp->min_xyzcxx . . $ face#min_xyz..zcxx . . $ vector#z. || fp->max_xyzcxx . . $ face#max_xyz..zcxx . . $ vector#z. < Light_min_xyz.zcxx . . $ vector#z.)
291 continue;
292
293 // Make sure face was rendered
294 if (fp->renderframecxx . . $ face#renderframe. != ((FrameCountcxx . . $ FrameCount. - 1) % 256))
295 continue;
296
297 if (Num_dynamic_facescxx . . $ Num_dynamic_faces. >= MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!) {
298 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Too many dynamic faces!\n");
299 return;
300 }
301
302 // Make sure there already is a lightmap for this face
303 if (!(fp->flagscxx . . $ face#flags. & FF_LIGHTMAPcxx . . $ `Descent3/room_external.h:112:9`!))
304 continue;
305
306 if (Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8)))
307 continue;
308
309 int xres = lmi_wcxx . . $ lmi_w(7864480464b09eea).(fp->lmi_handlecxx . . $ face#lmi_handle.);
310 int yres = lmi_hcxx . . $ lmi_h(7864480464b09eea).(fp->lmi_handlecxx . . $ face#lmi_handle.);
311
312 lightmap_infocxx . . $ lightmap_info# *lmi_ptr = &LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.];
313 // Lightmap width and height
314 int lmw = lm_wcxx . . $ lm_w(7864480464b09eea).(lmi_ptr->lm_handlecxx . . $ lightmap_info#lm_handle.);
315
316 // Check for backfaces
317 vectorcxx . . $ vector# subvec = *pos -cxx . . $ operator-(b77b703cd1e43e33). lmi_ptr->upper_leftcxx . . $ lightmap_info#upper_left.;
318 float dist_from_plane = fabscxx . . $ fabs(fbcf3211dfe1bfd1).(vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &lmi_ptr->normalcxx . . $ lightmap_info#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 vectorcxx . . $ vector# norm_vec = *pos -cxx . . $ operator-(b77b703cd1e43e33). rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[0]];
326
327 if ((norm_vec *cxx . . $ `operator*`(4a721543d64e126a). lmi_ptr->normalcxx . . $ lightmap_info#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_vertscxx . . $ face#num_verts. && in_front == 0; t++) {
334 norm_vec =cxx . . $ vector#`operator=`(c0842353a20857d). rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[t]] -cxx . . $ operator-(b77b703cd1e43e33). *pos;
335 if (norm_vec *cxx . . $ `operator*`(4a721543d64e126a). *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 matrixcxx . . $ matrix# facematrix;
345 vectorcxx . . $ vector# fvec = -cxx . . $ operator-(9e07539e219c6f20).lmi_ptr->normalcxx . . $ lightmap_info#normal.;
346 vm_VectorToMatrixcxx . . $ vm_VectorToMatrix(543233b88555916d).(&facematrix, &fvec, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
347
348 // Get upper left vector of face
349 vectorcxx . . $ vector# base_vector = lmi_ptr->upper_leftcxx . . $ lightmap_info#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 vectorcxx . . $ vector# touch_vector = *pos -cxx . . $ operator-(b77b703cd1e43e33). (lmi_ptr->normalcxx . . $ lightmap_info#normal. *cxx . . $ `operator*`(252247219a31502c). dist_from_plane);
354
355 subvec =cxx . . $ vector#`operator=`(c0842353a20857d). touch_vector -cxx . . $ operator-(b77b703cd1e43e33). base_vector;
356 float rdist = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.rveccxx . . $ matrix#rvec.);
357 float udist = -vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.uveccxx . . $ matrix#uvec.);
358 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing. / 2)) / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
359 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing. / 2)) / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
360
361 // Get the rectangle of the lightmap that we want to light
362 int start_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) - x_add;
363 int start_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.) - y_add;
364 int end_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) + x_add + .5;
365 int end_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#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 ASSERTcxx . . $ `misc/pserror.h:246:9`!(width > 0);
388 ASSERTcxx . . $ `misc/pserror.h:246:9`!(height > 0);
389
390 if (lmi_ptr->dynamiccxx . . $ lightmap_info#dynamic. != BAD_LM_INDEXcxx . . $ `lib/lightmap.h:25:9`!) // already lit, so just adjust, not start over
391 {
392 lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
393 dest_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(lm_handle);
394
395 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
396 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
397 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
398 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
399
400 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
401 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
402 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
403 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
404
405 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`! | LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!);
406
407 if (!(Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8)))) {
408 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ face#lmi_handle.;
409 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ face#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 = GetFreeDynamicLightmapcxx . . $ GetFreeDynamicLightmap(9b79fb6aee4c0440).(xres, yres);
416
417 if (dynamic_handle < 0) {
418 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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_tcxx . . $ uint16_t# *src_data;
424
425 src_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.);
426 dest_data = Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[dynamic_handle].mem_ptrcxx . . $ dynamic_lightmap#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->y1cxx . . $ lightmap_info#y1. + y) * lmw) + lmi_ptr->x1cxx . . $ lightmap_info#x1. + x];
431 }
432 }
433
434 dest_data = src_data;
435 lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
436
437 // Mark it as changed
438 lmi_ptr->dynamiccxx . . $ lightmap_info#dynamic. = dynamic_handle;
439
440 if (!(GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. & LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!)) {
441 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
442 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
443 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
444 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
445 } else {
446 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
447 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
448 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
449 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
450
451 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
452 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
453 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
454 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
455 }
456
457 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
458
459 Dynamic_face_listcxx . . $ Dynamic_face_list.[Num_dynamic_facescxx . . $ Num_dynamic_faces.].lmi_handlecxx . . $ dynamic_face#lmi_handle. = fp->lmi_handlecxx . . $ face#lmi_handle.;
460 Num_dynamic_facescxx . . $ Num_dynamic_faces.++;
461
462 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ face#lmi_handle.;
463 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8));
464 num_spoken_for++;
465
466 // Setup edge blending
467 Edges_to_blendcxx . . $ Edges_to_blend.[Num_edges_to_blendcxx . . $ Num_edges_to_blend.++] = fp->lmi_handlecxx . . $ face#lmi_handle.;
468 }
469
470 vectorcxx . . $ vector# element_vec;
471
472 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (start_y *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.));
473 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (start_x *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.));
474
475 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). ((facematrix.uveccxx . . $ matrix#uvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
476 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). ((facematrix.rveccxx . . $ matrix#rvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
477
478 // Go through and change each element of this lightmap
479 // SLOW!
480
481 int texel_num = ((start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.) * lmw) + start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
482 for (int y = 0; y < height; y++, base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.), texel_num += lmw) {
483 element_vec =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). base_vector;
484
485 for (int x = 0; x < width; x++, element_vec +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.)) {
486 int lightmap_texel_num = texel_num + x;
487
488 uint16_tcxx . . $ uint16_t# lightmap_texel = dest_data[lightmap_texel_num];
489
490 if (!(lightmap_texel & OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`!))
491 continue;
492
493 float dist = vm_VectorDistanceQuickcxx . . $ vm_VectorDistanceQuick(a0b9c8659ecc16b8).(&element_vec, pos);
494 float scalar = 1.0 - (dist / light_dist);
495
496 if (light_direction) {
497 vectorcxx . . $ vector# lsubvec = element_vec -cxx . . $ operator-(b77b703cd1e43e33). *pos;
498 vm_NormalizeVectorFastcxx . . $ vm_NormalizeVectorFast(4939a7e530383cd4).(&lsubvec);
499 float dp = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&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 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, r + (scalar * red_scale * 31));
519 } else {
520 if (r < red_limit)
521 r = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(red_limit, r + (scalar * red_scale * 31));
522 }
523
524 if (green_scale < 0) {
525 // we are subtracting light
526 g = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, g + (scalar * green_scale * 31));
527 } else {
528 if (g < green_limit)
529 g = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(green_limit, g + (scalar * green_scale * 31));
530 }
531
532 if (blue_scale < 0) {
533 // we are subtracting light
534 b = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, b + (scalar * blue_scale * 31));
535 } else {
536 if (b < blue_limit)
537 b = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(blue_limit, b + (scalar * blue_scale * 31));
538 }
539
540 lightmap_texel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | (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_forcxx . . $ Lmi_spoken_for.[index / 8] = 0;
550 }
551 }
552
553 struct light_instance_contextcxx . . $ light_instance_context# {
554 vectorcxx . . $ vector# pcxx . . $ light_instance_context#p.;
555 vectorcxx . . $ vector# dircxx . . $ light_instance_context#dir.;
556 } Light_instance_stackcxx . . $ Light_instance_stack.[MAX_SUBOBJECTScxx . . $ `lib/polymodel_external.h:76:9`!];
557
558 int Light_instance_depthcxx . . $ Light_instance_depth. = 0;
559 uint8_tcxx . . $ uint8_t# Use_light_directioncxx . . $ Use_light_direction. = 0;
560
561 vectorcxx . . $ vector# Light_positioncxx . . $ Light_position.;
562 vectorcxx . . $ vector# Light_directioncxx . . $ Light_direction.;
563
564 // instance at specified point with specified orientation
565 void StartLightingInstancecxx . . $ StartLightingInstance(bc315f98e1febb02).(vectorcxx . . $ vector# *pos, matrixcxx . . $ matrix# *orient) {
566 vectorcxx . . $ vector# tempv;
567
568 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Light_instance_depth < MAX_SUBOBJECTScxx . . $ `lib/polymodel_external.h:76:9`!);
569
570 Light_instance_stackcxx . . $ Light_instance_stack.[Light_instance_depthcxx . . $ Light_instance_depth.].pcxx . . $ light_instance_context#p. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Light_positioncxx . . $ Light_position.;
571 if (Use_light_directioncxx . . $ Use_light_direction.)
572 Light_instance_stackcxx . . $ Light_instance_stack.[Light_instance_depthcxx . . $ Light_instance_depth.].dircxx . . $ light_instance_context#dir. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Light_directioncxx . . $ Light_direction.;
573 Light_instance_depthcxx . . $ Light_instance_depth.++;
574
575 // Step 1: subtract position from light position
576 tempv =cxx . . $ vector#`operator=`(c0842353a20857d). Light_positioncxx . . $ Light_position. -cxx . . $ operator-(b77b703cd1e43e33). *pos;
577 // step 2: rotate view vector through object matrix
578
579 Light_positioncxx . . $ Light_position. =cxx . . $ vector#`operator=`(c0842353a20857d). tempv *cxx . . $ `operator*`(14b8f9ac5c1e7b85). *orient;
580
581 // Rotate the light direction through this matrix
582 if (Use_light_directioncxx . . $ Use_light_direction.) {
583 tempv =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Light_directioncxx . . $ Light_direction.;
584 Light_directioncxx . . $ Light_direction. =cxx . . $ vector#`operator=`(c0842353a20857d). tempv *cxx . . $ `operator*`(14b8f9ac5c1e7b85). *orient;
585 }
586 }
587
588 // pops the old context
589 void DoneLightingInstancecxx . . $ DoneLightingInstance(49f6e7a06ebc5aa8).() {
590 Light_instance_depthcxx . . $ Light_instance_depth.--;
591
592 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Light_instance_depth >= 0);
593
594 Light_positioncxx . . $ Light_position. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Light_instance_stackcxx . . $ Light_instance_stack.[Light_instance_depthcxx . . $ Light_instance_depth.].pcxx . . $ light_instance_context#p.;
595 if (Use_light_directioncxx . . $ Use_light_direction.)
596 Light_directioncxx . . $ Light_direction. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Light_instance_stackcxx . . $ Light_instance_stack.[Light_instance_depthcxx . . $ Light_instance_depth.].dircxx . . $ light_instance_context#dir.;
597 }
598
599 // Applies lighting to a submodel and all its children
600 void ApplyLightingToSubmodelcxx . . $ ApplyLightingToSubmodel(164d4140fb0d0402).(objectcxx . . $ object# *obj, poly_modelcxx . . $ poly_model# *pm, bsp_infocxx . . $ bsp_info# *sm, float light_dist, float red_scale,
601 float green_scale, float blue_scale, float dot_range) {
602 matrixcxx . . $ matrix# mat;
603 vectorcxx . . $ vector# Light_min_xyz;
604 vectorcxx . . $ vector# Light_max_xyz;
605 vectorcxx . . $ vector# rad;
606 int i, t;
607 int subnum = sm - pm->submodelcxx . . $ poly_model#submodel.;
608 int lm_handle;
609 uint16_tcxx . . $ uint16_t# *dest_data;
610 uint16_tcxx . . $ uint16_t# lmilist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
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 (IsNonRenderableSubmodelcxx . . $ IsNonRenderableSubmodel(bb32db98789388a4).(pm, subnum))
618 return; // Don't do shells, frontfaces, etc
619
620 // Start instance stuff
621 vectorcxx . . $ vector# temp_vec = sm->mod_poscxx . . $ bsp_info#mod_pos. +cxx . . $ operator+(b77b703cd1e43e33). sm->offsetcxx . . $ bsp_info#offset.;
622 vm_AnglesToMatrixcxx . . $ vm_AnglesToMatrix(654fa9e3c9c3be99).(&mat, sm->angscxx . . $ bsp_info#angs..pcxx . . $ angvec#p., sm->angscxx . . $ bsp_info#angs..hcxx . . $ angvec#h., sm->angscxx . . $ bsp_info#angs..bcxx . . $ angvec#b.);
623 StartLightingInstancecxx . . $ StartLightingInstance(bc315f98e1febb02).(&temp_vec, &mat);
624
625 rad.xcxx . . $ vector#x. = light_dist;
626 rad.ycxx . . $ vector#y. = light_dist;
627 rad.zcxx . . $ vector#z. = light_dist;
628
629 vectorcxx . . $ vector# light_pos = Light_positioncxx . . $ Light_position.;
630 vectorcxx . . $ vector# light_dir = Light_directioncxx . . $ Light_direction.;
631
632 Light_min_xyz =cxx . . $ vector#`operator=`(c0842353a20857d). light_pos -cxx . . $ operator-(b77b703cd1e43e33). rad;
633 Light_max_xyz =cxx . . $ vector#`operator=`(c0842353a20857d). light_pos +cxx . . $ operator+(b77b703cd1e43e33). rad;
634
635 // Now the Light_position is in the submodels frame of reference
636 for (i = 0; i < sm->num_facescxx . . $ bsp_info#num_faces.; i++) {
637 // check to see if this face is within reach
638 if (Light_max_xyz.ycxx . . $ vector#y. < sm->face_mincxx . . $ bsp_info#face_min.[i].ycxx . . $ vector#y. || sm->face_maxcxx . . $ bsp_info#face_max.[i].ycxx . . $ vector#y. < Light_min_xyz.ycxx . . $ vector#y. ||
639 Light_max_xyz.xcxx . . $ vector#x. < sm->face_mincxx . . $ bsp_info#face_min.[i].xcxx . . $ vector#x. || sm->face_maxcxx . . $ bsp_info#face_max.[i].xcxx . . $ vector#x. < Light_min_xyz.xcxx . . $ vector#x. ||
640 Light_max_xyz.zcxx . . $ vector#z. < sm->face_mincxx . . $ bsp_info#face_min.[i].zcxx . . $ vector#z. || sm->face_maxcxx . . $ bsp_info#face_max.[i].zcxx . . $ vector#z. < Light_min_xyz.zcxx . . $ vector#z.)
641 continue;
642
643 // Ok, now we know that this light touches this face
644 if (Num_dynamic_facescxx . . $ Num_dynamic_faces. >= MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!) {
645 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Too many dynamic faces!\n");
646 DoneLightingInstancecxx . . $ DoneLightingInstance(49f6e7a06ebc5aa8).();
647 return;
648 }
649
650 lightmap_object_facecxx . . $ lightmap_object_face# *fp = &obj->lm_objectcxx . . $ object#lm_object..lightmap_facescxx . . $ lightmap_object#lightmap_faces.[subnum][i];
651
652 if (Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. % 8)))
653 continue;
654
655 int xres = lmi_wcxx . . $ lmi_w(7864480464b09eea).(fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.);
656 int yres = lmi_hcxx . . $ lmi_h(7864480464b09eea).(fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.);
657
658 lightmap_infocxx . . $ lightmap_info# *lmi_ptr = &LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.];
659 // Lightmap width and height
660 int lmw = lm_wcxx . . $ lm_w(7864480464b09eea).(lmi_ptr->lm_handlecxx . . $ lightmap_info#lm_handle.);
661
662 // Check for backfaces
663 // Get upper left vector of face
664 vectorcxx . . $ vector# base_vector = lmi_ptr->upper_leftcxx . . $ lightmap_info#upper_left.;
665 vectorcxx . . $ vector# subvec = light_pos -cxx . . $ operator-(b77b703cd1e43e33). lmi_ptr->upper_leftcxx . . $ lightmap_info#upper_left.;
666 float dist_from_plane = fabscxx . . $ fabs(fbcf3211dfe1bfd1).(vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &lmi_ptr->normalcxx . . $ lightmap_info#normal.));
667
668 if (dist_from_plane > light_dist)
669 continue;
670
671 if (Use_light_directioncxx . . $ Use_light_direction.) {
672 // If this is a directional light, check for backfaces
673 vectorcxx . . $ vector# norm_vec = light_pos -cxx . . $ operator-(b77b703cd1e43e33). base_vector;
674
675 if ((norm_vec *cxx . . $ `operator*`(4a721543d64e126a). lmi_ptr->normalcxx . . $ lightmap_info#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 polyfacecxx . . $ polyface# *poly_fp = &sm->facescxx . . $ bsp_info#faces.[i];
682 int in_front = 0; //
683 for (t = 0; t < fp->num_vertscxx . . $ lightmap_object_face#num_verts. && in_front == 0; t++) {
684 norm_vec =cxx . . $ vector#`operator=`(c0842353a20857d). sm->vertscxx . . $ bsp_info#verts.[poly_fp->vertnumscxx . . $ polyface#vertnums.[t]] -cxx . . $ operator-(b77b703cd1e43e33). light_pos;
685 if (norm_vec *cxx . . $ `operator*`(4a721543d64e126a). 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 matrixcxx . . $ matrix# facematrix;
695 vectorcxx . . $ vector# fvec = -cxx . . $ operator-(9e07539e219c6f20).lmi_ptr->normalcxx . . $ lightmap_info#normal.;
696 vm_VectorToMatrixcxx . . $ vm_VectorToMatrix(543233b88555916d).(&facematrix, &fvec, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
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 vectorcxx . . $ vector# touch_vector = light_pos -cxx . . $ operator-(b77b703cd1e43e33). (lmi_ptr->normalcxx . . $ lightmap_info#normal. *cxx . . $ `operator*`(252247219a31502c). dist_from_plane);
701
702 subvec =cxx . . $ vector#`operator=`(c0842353a20857d). touch_vector -cxx . . $ operator-(b77b703cd1e43e33). base_vector;
703 float rdist = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.rveccxx . . $ matrix#rvec.);
704 float udist = -vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.uveccxx . . $ matrix#uvec.);
705 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing. / 2)) / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
706 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing. / 2)) / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
707
708 // Get the rectangle of the lightmap that we want to light
709 int start_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) - x_add;
710 int start_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.) - y_add;
711 int end_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) + x_add + .5;
712 int end_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#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 ASSERTcxx . . $ `misc/pserror.h:246:9`!(width > 0);
735 ASSERTcxx . . $ `misc/pserror.h:246:9`!(height > 0);
736
737 if (lmi_ptr->dynamiccxx . . $ lightmap_info#dynamic. != BAD_LM_INDEXcxx . . $ `lib/lightmap.h:25:9`!) // already lit, so just adjust, not start over
738 {
739 lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
740 dest_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(lm_handle);
741
742 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
743 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
744 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
745 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
746
747 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
748 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
749 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
750 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
751
752 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`! | LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!);
753
754 if (!(Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. % 8)))) {
755 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.;
756 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ lightmap_object_face#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 = GetFreeDynamicLightmapcxx . . $ GetFreeDynamicLightmap(9b79fb6aee4c0440).(xres, yres);
763
764 if (dynamic_handle < 0) {
765 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "No free dynamic maps!\n");
766 DoneLightingInstancecxx . . $ DoneLightingInstance(49f6e7a06ebc5aa8).();
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_tcxx . . $ uint16_t# *src_data;
773
774 src_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.);
775 dest_data = Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[dynamic_handle].mem_ptrcxx . . $ dynamic_lightmap#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->y1cxx . . $ lightmap_info#y1. + y) * lmw) + lmi_ptr->x1cxx . . $ lightmap_info#x1. + x];
780 }
781 }
782
783 dest_data = src_data;
784 lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
785
786 // Mark it as changed
787 lmi_ptr->dynamiccxx . . $ lightmap_info#dynamic. = dynamic_handle;
788
789 if (!(GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. & LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!)) {
790 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
791 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
792 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
793 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
794 } else {
795 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
796 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
797 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
798 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
799
800 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
801 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
802 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
803 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
804 }
805
806 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
807
808 Dynamic_face_listcxx . . $ Dynamic_face_list.[Num_dynamic_facescxx . . $ Num_dynamic_faces.].lmi_handlecxx . . $ dynamic_face#lmi_handle. = fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.;
809 Num_dynamic_facescxx . . $ Num_dynamic_faces.++;
810
811 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.;
812 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle. % 8));
813 num_spoken_for++;
814
815 // Setup edge blending
816 Edges_to_blendcxx . . $ Edges_to_blend.[Num_edges_to_blendcxx . . $ Num_edges_to_blend.++] = fp->lmi_handlecxx . . $ lightmap_object_face#lmi_handle.;
817 }
818
819 vectorcxx . . $ vector# element_vec;
820
821 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (start_y *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.));
822 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (start_x *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.));
823
824 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). ((facematrix.uveccxx . . $ matrix#uvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
825 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). ((facematrix.rveccxx . . $ matrix#rvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
826
827 // Go through and change each element of this lightmap
828 // SLOW!
829
830 int texel_num = ((start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.) * lmw) + start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
831 for (int y = 0; y < height; y++, base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.), texel_num += lmw) {
832 element_vec =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). base_vector;
833
834 for (int x = 0; x < width; x++, element_vec +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.)) {
835 int lightmap_texel_num = texel_num + x;
836
837 uint16_tcxx . . $ uint16_t# lightmap_texel = dest_data[lightmap_texel_num];
838
839 if (!(lightmap_texel & OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`!))
840 continue;
841
842 float dist = vm_VectorDistanceQuickcxx . . $ vm_VectorDistanceQuick(a0b9c8659ecc16b8).(&element_vec, &light_pos);
843 float scalar = 1.0 - (dist / light_dist);
844
845 if (Use_light_directioncxx . . $ Use_light_direction.) {
846 vectorcxx . . $ vector# lsubvec = element_vec -cxx . . $ operator-(b77b703cd1e43e33). light_pos;
847 vm_NormalizeVectorFastcxx . . $ vm_NormalizeVectorFast(4939a7e530383cd4).(&lsubvec);
848 float dp = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&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 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, r + (scalar * red_scale * 31));
867 } else {
868 if (r < red_limit)
869 r = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(red_limit, r + (scalar * red_scale * 31));
870 }
871
872 if (green_scale < 0) {
873 g = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, g + (scalar * green_scale * 31));
874 } else {
875 if (g < green_limit)
876 g = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(green_limit, g + (scalar * green_scale * 31));
877 }
878
879 if (blue_scale < 0) {
880 b = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, b + (scalar * blue_scale * 31));
881 } else {
882 if (b < blue_limit)
883 b = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(blue_limit, b + (scalar * blue_scale * 31));
884 }
885
886 lightmap_texel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | (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_forcxx . . $ Lmi_spoken_for.[index / 8] = 0;
896 }
897
898 for (i = 0; i < sm->num_childrencxx . . $ bsp_info#num_children.; i++)
899 ApplyLightingToSubmodelcxx . . $ ApplyLightingToSubmodel(164d4140fb0d0402).(obj, pm, &pm->submodelcxx . . $ poly_model#submodel.[sm->childrencxx . . $ bsp_info#children.[i]], light_dist, red_scale, green_scale, blue_scale,
900 dot_range);
901
902 DoneLightingInstancecxx . . $ DoneLightingInstance(49f6e7a06ebc5aa8).();
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 ApplyVolumeLightToObjectcxx . . $ ApplyVolumeLightToObject(1ebef0143163e7d6).(vectorcxx . . $ vector# *pos, objectcxx . . $ object# *obj, float light_dist, float red_scale, float green_scale,
934 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range) {
935 vectorcxx . . $ vector# subvec = *pos -cxx . . $ operator-(b77b703cd1e43e33). obj->poscxx . . $ object#pos.;
936 float mag = vm_GetMagnitudeFastcxx . . $ vm_GetMagnitudeFast(4939a7e530383cd4).(&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 vectorcxx . . $ vector# dir_vec = obj->poscxx . . $ object#pos. -cxx . . $ operator-(b77b703cd1e43e33). *pos;
947 float mag = vm_NormalizeVectorFastcxx . . $ vm_NormalizeVectorFast(4939a7e530383cd4).(&dir_vec);
948 float dp = dir_vec *cxx . . $ `operator*`(4a721543d64e126a). *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 ASSERTcxx . . $ `misc/pserror.h:246:9`!(obj->effect_info != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
961
962 if (!obj->effect_infocxx . . $ object#effect_info.->dynamic_this_framecxx . . $ effect_info_s#dynamic_this_frame.) {
963 if (Num_volume_objectscxx . . $ Num_volume_objects. < MAX_VOLUME_OBJECTScxx . . $ `Descent3/lighting.cpp:51:9`!) {
964 Dynamic_volume_object_listcxx . . $ Dynamic_volume_object_list.[Num_volume_objectscxx . . $ Num_volume_objects.].handlecxx . . $ volume_object#handle. = obj->handlecxx . . $ object#handle.;
965 Dynamic_volume_object_listcxx . . $ Dynamic_volume_object_list.[Num_volume_objectscxx . . $ Num_volume_objects.++].objnumcxx . . $ volume_object#objnum. = obj - Objectscxx . . $ Objects.;
966 obj->effect_infocxx . . $ object#effect_info.->spec_magcxx . . $ effect_info_s#spec_mag. = -100000;
967
968 obj->effect_infocxx . . $ object#effect_info.->dynamic_this_framecxx . . $ effect_info_s#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_infocxx . . $ object#effect_info.->spec_magcxx . . $ effect_info_s#spec_mag. && Detail_settingscxx . . $ Detail_settings..Specular_lightingcxx . . $ tDetailSettings#Specular_lighting. &&
974 !(Object_infocxx . . $ Object_info.[obj->idcxx . . $ object#id.].lighting_infocxx . . $ object_info#lighting_info..flagscxx . . $ light_info#flags. & OLF_NO_SPECULARITYcxx . . $ `Descent3/object.h:677:9`!)) {
975 // mprintf(0,"Setting specular!\n");
976 obj->effect_infocxx . . $ object#effect_info.->type_flagscxx . . $ effect_info_s#type_flags. |= EF_SPECULARcxx . . $ `Descent3/object_external.h:205:9`!;
977 obj->effect_infocxx . . $ object#effect_info.->spec_magcxx . . $ effect_info_s#spec_mag. = light_dist - mag;
978 obj->effect_infocxx . . $ object#effect_info.->spec_poscxx . . $ effect_info_s#spec_pos. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *pos;
979 obj->effect_infocxx . . $ object#effect_info.->spec_rcxx . . $ effect_info_s#spec_r. = red_scale;
980 obj->effect_infocxx . . $ object#effect_info.->spec_gcxx . . $ effect_info_s#spec_g. = green_scale;
981 obj->effect_infocxx . . $ object#effect_info.->spec_bcxx . . $ effect_info_s#spec_b. = blue_scale;
982 }
983
984 if (obj->effect_infocxx . . $ object#effect_info.->type_flagscxx . . $ effect_info_s#type_flags. & EF_VOLUME_LITcxx . . $ `Descent3/object_external.h:201:9`!) {
985 obj->effect_infocxx . . $ object#effect_info.->dynamic_redcxx . . $ effect_info_s#dynamic_red. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(1, obj->effect_infocxx . . $ object#effect_info.->dynamic_redcxx . . $ effect_info_s#dynamic_red. + (scalar * red_scale));
986 obj->effect_infocxx . . $ object#effect_info.->dynamic_greencxx . . $ effect_info_s#dynamic_green. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(1, obj->effect_infocxx . . $ object#effect_info.->dynamic_greencxx . . $ effect_info_s#dynamic_green. + (scalar * green_scale));
987 obj->effect_infocxx . . $ object#effect_info.->dynamic_bluecxx . . $ effect_info_s#dynamic_blue. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(1, obj->effect_infocxx . . $ object#effect_info.->dynamic_bluecxx . . $ effect_info_s#dynamic_blue. + (scalar * blue_scale));
988 }
989 }
990
991 // Applies lighting to all objects in a certain distance
992 void ApplyLightingToObjectscxx . . $ ApplyLightingToObjects(31a30e6d6b9dd744).(vectorcxx . . $ vector# *pos, int roomnum, float light_dist, float red_scale, float green_scale,
993 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range) {
994 int16_t objlist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
995 int num_objects, i;
996 float normalized_time[MAX_SUBOBJECTScxx . . $ `lib/polymodel_external.h:76:9`!];
997
998 num_objects = fvi_QuickDistObjectListcxx . . $ fvi_QuickDistObjectList(85643dfbd17d1ac8).(pos, roomnum, light_dist, objlist, MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!, false, false, true);
999
1000 for (i = 0; i < num_objects; i++) {
1001 objectcxx . . $ object# *obj = &Objectscxx . . $ Objects.[objlist[i]];
1002
1003 if (obj->typecxx . . $ object#type. == OBJ_ROOMcxx . . $ `Descent3/object_external.h:178:9`!) {
1004 ApplyLightingToExternalRoomcxx . . $ ApplyLightingToExternalRoom(31a30e6d6b9dd744).(pos, obj->idcxx . . $ object#id., light_dist, red_scale, green_scale, blue_scale, light_direction,
1005 dot_range);
1006 continue;
1007 }
1008
1009 if (obj->renderframecxx . . $ object#renderframe. != ((FrameCountcxx . . $ FrameCount. - 1) % 65536)) {
1010 if (obj != Viewer_objectcxx . . $ Viewer_object.)
1011 continue;
1012 }
1013
1014 if (obj->lm_objectcxx . . $ object#lm_object..usedcxx . . $ lightmap_object#used. == 0) {
1015 if (obj->effect_infocxx . . $ object#effect_info. && ((obj->effect_infocxx . . $ object#effect_info.->type_flagscxx . . $ effect_info_s#type_flags. & EF_VOLUME_LITcxx . . $ `Descent3/object_external.h:201:9`!) ||
1016 obj->lighting_render_typecxx . . $ object#lighting_render_type. == LRT_GOURAUDcxx . . $ `Descent3/object.h:650:9`! || obj->typecxx . . $ object#type. == OBJ_POWERUPcxx . . $ `Descent3/object_external.h:167:9`!)) {
1017 ApplyVolumeLightToObjectcxx . . $ ApplyVolumeLightToObject(1ebef0143163e7d6).(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->rtypecxx . . $ object#rtype..pobj_infocxx . . $ object#$anonymous_type_10#pobj_info..model_numcxx . . $ polyobj_info#model_num.;
1024 poly_modelcxx . . $ poly_model# *pm = &Poly_modelscxx . . $ Poly_models.[model_num];
1025
1026 // Set our light position
1027 Light_positioncxx . . $ Light_position. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *pos;
1028
1029 // Set up light direction if this is a directional light
1030 if (light_direction) {
1031 Light_directioncxx . . $ Light_direction. =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *light_direction;
1032 Use_light_directioncxx . . $ Use_light_direction. = 1;
1033 } else
1034 Use_light_directioncxx . . $ Use_light_direction. = 0;
1035
1036 StartLightingInstancecxx . . $ StartLightingInstance(bc315f98e1febb02).(&obj->poscxx . . $ object#pos., &obj->orientcxx . . $ object#orient.);
1037
1038 // Now, get the lights position in the objects submodel space
1039 SetNormalizedTimeObjcxx . . $ SetNormalizedTimeObj(8af4de573afdac78).(obj, normalized_time);
1040 SetModelAnglesAndPoscxx . . $ SetModelAnglesAndPos(a00e94110fc4ea4c).(pm, normalized_time);
1041
1042 for (int t = 0; t < pm->n_modelscxx . . $ poly_model#n_models.; t++) {
1043 bsp_infocxx . . $ bsp_info# *sm = &pm->submodelcxx . . $ poly_model#submodel.[t];
1044 if (sm->parentcxx . . $ bsp_info#parent. == -1)
1045 ApplyLightingToSubmodelcxx . . $ ApplyLightingToSubmodel(164d4140fb0d0402).(obj, pm, sm, light_dist, red_scale, green_scale, blue_scale, dot_range);
1046 }
1047
1048 DoneLightingInstancecxx . . $ DoneLightingInstance(49f6e7a06ebc5aa8).();
1049 ASSERTcxx . . $ `misc/pserror.h:246:9`!(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 ApplyLightingToRoomscxx . . $ ApplyLightingToRooms(31a30e6d6b9dd744).(vectorcxx . . $ vector# *pos, int roomnum, float light_dist, float red_scale, float green_scale,
1056 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range) {
1057 fvi_face_room_listcxx . . $ fvi_face_room_list# facelist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
1058 uint16_tcxx . . $ uint16_t# lmilist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
1059 int num_spoken_for = 0;
1060
1061 int num_faces, i, t, lm_handle;
1062 uint16_tcxx . . $ uint16_t# *dest_data;
1063 int faces_misreported = 0;
1064
1065 if (Dedicated_servercxx . . $ Dedicated_server.)
1066 return;
1067
1068 ApplyLightingToObjectscxx . . $ ApplyLightingToObjects(31a30e6d6b9dd744).(pos, roomnum, light_dist, red_scale, green_scale, blue_scale, light_direction, dot_range);
1069
1070 num_faces = fvi_QuickDistFaceListcxx . . $ fvi_QuickDistFaceList(a662bbcbc4f3d9bd).(roomnum, pos, light_dist, facelist, MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!);
1071
1072 #ifdef _DEBUGcxx . . $ `<built-in>:482:9`!
1073 if (num_faces == MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!) {
1074 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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 roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[facelist[i].room_indexcxx . . $ fvi_face_room_list#room_index.];
1087 facecxx . . $ face# *fp = &rp->facescxx . . $ room#faces.[facelist[i].face_indexcxx . . $ fvi_face_room_list#face_index.];
1088
1089 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Rooms[facelist[i].room_index].used);
1090 ASSERTcxx . . $ `misc/pserror.h:246:9`!(facelist[i].face_index < Rooms[facelist[i].room_index].num_faces);
1091
1092 // Make sure face was rendered
1093 if (fp->renderframecxx . . $ face#renderframe. != ((FrameCountcxx . . $ FrameCount. - 1) % 256))
1094 continue;
1095
1096 if (Num_dynamic_facescxx . . $ Num_dynamic_faces. >= MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!) {
1097 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Too many dynamic faces!\n");
1098 return;
1099 }
1100
1101 // Make sure there already is a lightmap for this face
1102 if (!(fp->flagscxx . . $ face#flags. & FF_LIGHTMAPcxx . . $ `Descent3/room_external.h:112:9`!))
1103 continue;
1104
1105 if (Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8)))
1106 continue;
1107
1108 int xres = lmi_wcxx . . $ lmi_w(7864480464b09eea).(fp->lmi_handlecxx . . $ face#lmi_handle.);
1109 int yres = lmi_hcxx . . $ lmi_h(7864480464b09eea).(fp->lmi_handlecxx . . $ face#lmi_handle.);
1110
1111 lightmap_infocxx . . $ lightmap_info# *lmi_ptr = &LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.];
1112 // Lightmap width and height
1113 int lmw = lm_wcxx . . $ lm_w(7864480464b09eea).(lmi_ptr->lm_handlecxx . . $ lightmap_info#lm_handle.);
1114
1115 // Check for backfaces
1116 vectorcxx . . $ vector# subvec = *pos -cxx . . $ operator-(b77b703cd1e43e33). lmi_ptr->upper_leftcxx . . $ lightmap_info#upper_left.;
1117 float dist_from_plane = fabscxx . . $ fabs(fbcf3211dfe1bfd1).(vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &lmi_ptr->normalcxx . . $ lightmap_info#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 vectorcxx . . $ vector# norm_vec = *pos -cxx . . $ operator-(b77b703cd1e43e33). rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[0]];
1125
1126 if ((norm_vec *cxx . . $ `operator*`(4a721543d64e126a). lmi_ptr->normalcxx . . $ lightmap_info#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_vertscxx . . $ face#num_verts. && in_front == 0; t++) {
1133 norm_vec =cxx . . $ vector#`operator=`(c0842353a20857d). rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[t]] -cxx . . $ operator-(b77b703cd1e43e33). *pos;
1134 if (norm_vec *cxx . . $ `operator*`(4a721543d64e126a). *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 matrixcxx . . $ matrix# facematrix;
1150 vectorcxx . . $ vector# fvec = -cxx . . $ operator-(9e07539e219c6f20).lmi_ptr->normalcxx . . $ lightmap_info#normal.;
1151 vm_VectorToMatrixcxx . . $ vm_VectorToMatrix(543233b88555916d).(&facematrix, &fvec, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
1152
1153 // Get upper left vector of face
1154 vectorcxx . . $ vector# base_vector = lmi_ptr->upper_leftcxx . . $ lightmap_info#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 vectorcxx . . $ vector# touch_vector = *pos -cxx . . $ operator-(b77b703cd1e43e33). (lmi_ptr->normalcxx . . $ lightmap_info#normal. *cxx . . $ `operator*`(252247219a31502c). dist_from_plane);
1159
1160 subvec =cxx . . $ vector#`operator=`(c0842353a20857d). touch_vector -cxx . . $ operator-(b77b703cd1e43e33). base_vector;
1161 float rdist = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.rveccxx . . $ matrix#rvec.);
1162 float udist = -vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.uveccxx . . $ matrix#uvec.);
1163 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing. / 2)) / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
1164 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing. / 2)) / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
1165
1166 // Get the rectangle of the lightmap that we want to light
1167 int start_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) - x_add;
1168 int start_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.) - y_add;
1169 int end_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) + x_add + .5;
1170 int end_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#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 ASSERTcxx . . $ `misc/pserror.h:246:9`!(width > 0);
1193 ASSERTcxx . . $ `misc/pserror.h:246:9`!(height > 0);
1194
1195 if (lmi_ptr->dynamiccxx . . $ lightmap_info#dynamic. != BAD_LM_INDEXcxx . . $ `lib/lightmap.h:25:9`!) // already lit, so just adjust, not start over
1196 {
1197 lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
1198 dest_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(lm_handle);
1199
1200 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
1201 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1202 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
1203 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1204
1205 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
1206 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
1207 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
1208 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
1209
1210 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`! | LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!);
1211
1212 if (!(Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8)))) {
1213 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ face#lmi_handle.;
1214 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ face#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 = GetFreeDynamicLightmapcxx . . $ GetFreeDynamicLightmap(9b79fb6aee4c0440).(xres, yres);
1221
1222 if (dynamic_handle < 0) {
1223 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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_tcxx . . $ uint16_t# *src_data;
1229
1230 src_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.);
1231 dest_data = Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[dynamic_handle].mem_ptrcxx . . $ dynamic_lightmap#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->y1cxx . . $ lightmap_info#y1. + y) * lmw) + lmi_ptr->x1cxx . . $ lightmap_info#x1. + x];
1236 }
1237 }
1238
1239 dest_data = src_data;
1240 lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
1241
1242 // Mark it as changed
1243 lmi_ptr->dynamiccxx . . $ lightmap_info#dynamic. = dynamic_handle;
1244
1245 if (!(GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. & LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!)) {
1246 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1247 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1248 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
1249 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
1250 } else {
1251 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
1252 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1253 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
1254 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1255
1256 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
1257 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
1258 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
1259 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
1260 }
1261
1262 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
1263
1264 Dynamic_face_listcxx . . $ Dynamic_face_list.[Num_dynamic_facescxx . . $ Num_dynamic_faces.].lmi_handlecxx . . $ dynamic_face#lmi_handle. = fp->lmi_handlecxx . . $ face#lmi_handle.;
1265 Num_dynamic_facescxx . . $ Num_dynamic_faces.++;
1266
1267 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ face#lmi_handle.;
1268 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8));
1269 num_spoken_for++;
1270
1271 // Setup edge blending
1272 Edges_to_blendcxx . . $ Edges_to_blend.[Num_edges_to_blendcxx . . $ Num_edges_to_blend.++] = fp->lmi_handlecxx . . $ face#lmi_handle.;
1273 }
1274
1275 vectorcxx . . $ vector# element_vec;
1276
1277 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (start_y *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.));
1278 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (start_x *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.));
1279
1280 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). ((facematrix.uveccxx . . $ matrix#uvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
1281 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). ((facematrix.rveccxx . . $ matrix#rvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
1282
1283 // Go through and change each element of this lightmap
1284 // SLOW!
1285
1286 int texel_num = ((start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.) * lmw) + start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
1287 for (int y = 0; y < height; y++, base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.), texel_num += lmw) {
1288 element_vec =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). base_vector;
1289
1290 for (int x = 0; x < width; x++, element_vec +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.)) {
1291 int lightmap_texel_num = texel_num + x;
1292
1293 uint16_tcxx . . $ uint16_t# lightmap_texel = dest_data[lightmap_texel_num];
1294
1295 if (!(lightmap_texel & OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`!))
1296 continue;
1297
1298 float dist = vm_VectorDistanceQuickcxx . . $ vm_VectorDistanceQuick(a0b9c8659ecc16b8).(&element_vec, pos);
1299 float scalar = 1.0 - (dist / light_dist);
1300
1301 if (light_direction) {
1302 vectorcxx . . $ vector# lsubvec = element_vec -cxx . . $ operator-(b77b703cd1e43e33). *pos;
1303 vm_NormalizeVectorFastcxx . . $ vm_NormalizeVectorFast(4939a7e530383cd4).(&lsubvec);
1304 float dp = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&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 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, r + (scalar * red_scale * 31));
1324 } else {
1325 // we are adding light
1326 if (r < red_limit)
1327 r = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(red_limit, r + (scalar * red_scale * 31));
1328 }
1329
1330 if (green_scale < 0) {
1331 // we are subtracting light
1332 g = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, g + (scalar * green_scale * 31));
1333 } else {
1334 // we are adding light
1335 if (g < green_limit)
1336 g = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(green_limit, g + (scalar * green_scale * 31));
1337 }
1338
1339 if (blue_scale < 0) {
1340 // we are subtracting light
1341 b = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, b + (scalar * blue_scale * 31));
1342 } else {
1343 if (b < blue_limit)
1344 b = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(blue_limit, b + (scalar * blue_scale * 31));
1345 }
1346
1347 lightmap_texel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | (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_forcxx . . $ Lmi_spoken_for.[index / 8] = 0;
1357 }
1358 }
1359
1360 // Blends all the edges that need blending for this frame
1361 void BlendAllLightingEdgescxx . . $ BlendAllLightingEdges(49f6e7a06ebc5aa8).() {
1362 for (int i = 0; i < Num_edges_to_blendcxx . . $ Num_edges_to_blend.; i++) {
1363 if (LightmapInfocxx . . $ LightmapInfo.[Edges_to_blendcxx . . $ Edges_to_blend.[i]].usedcxx . . $ lightmap_info#used. < 1)
1364 continue; // this face was killed last frame. This can happen with objects
1365 BlendLightingEdgescxx . . $ BlendLightingEdges(d2f3fbe01c9a6be1).(&LightmapInfocxx . . $ LightmapInfo.[Edges_to_blendcxx . . $ Edges_to_blend.[i]]);
1366 }
1367 }
1368
1369 // Clears the used flag for the dynamic lightmaps and restores colors on the terrain
1370 void ClearDynamicLightmapscxx . . $ ClearDynamicLightmaps(49f6e7a06ebc5aa8).() {
1371 int i;
1372
1373 // First clear dynamic lightmap list
1374 for (i = 0; i < Num_dynamic_lightmapscxx . . $ Num_dynamic_lightmaps.; i++)
1375 Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[i].usedcxx . . $ dynamic_lightmap#used. = 0;
1376
1377 // Reset volume lights
1378 for (i = 0; i < Num_volume_objectscxx . . $ Num_volume_objects.; i++) {
1379 objectcxx . . $ object# *obj = &Objectscxx . . $ Objects.[Dynamic_volume_object_listcxx . . $ Dynamic_volume_object_list.[i].objnumcxx . . $ volume_object#objnum.];
1380 if (obj->typecxx . . $ object#type. == OBJ_NONEcxx . . $ `Descent3/object_external.h:159:9`! || obj->handlecxx . . $ object#handle. != Dynamic_volume_object_listcxx . . $ Dynamic_volume_object_list.[i].handlecxx . . $ volume_object#handle.)
1381 continue; // object was destroyed this frame
1382
1383 obj->effect_infocxx . . $ object#effect_info.->type_flagscxx . . $ effect_info_s#type_flags. &= ~EF_SPECULARcxx . . $ `Descent3/object_external.h:205:9`!;
1384 obj->effect_infocxx . . $ object#effect_info.->dynamic_this_framecxx . . $ effect_info_s#dynamic_this_frame. = 0;
1385 obj->effect_infocxx . . $ object#effect_info.->dynamic_redcxx . . $ effect_info_s#dynamic_red. = 0;
1386 obj->effect_infocxx . . $ object#effect_info.->dynamic_greencxx . . $ effect_info_s#dynamic_green. = 0;
1387 obj->effect_infocxx . . $ object#effect_info.->dynamic_bluecxx . . $ effect_info_s#dynamic_blue. = 0;
1388 }
1389
1390 Num_volume_objectscxx . . $ Num_volume_objects. = 0;
1391
1392 // Reset specular faces
1393 for (i = 0; i < Num_specular_facescxx . . $ Num_specular_faces.; i++) {
1394 int n = Specular_face_listcxx . . $ Specular_face_list.[i];
1395 SpecialFacescxx . . $ SpecialFaces.[n].flagscxx . . $ special_face#flags. = 0;
1396 // LightmapInfo[n].spec_map=-1;
1397 }
1398
1399 Num_specular_facescxx . . $ Num_specular_faces. = 0;
1400
1401 // Clear diffuse faces
1402 for (i = 0; i < Num_dynamic_facescxx . . $ Num_dynamic_faces.; i++) {
1403 if (LightmapInfocxx . . $ LightmapInfo.[Dynamic_face_listcxx . . $ Dynamic_face_list.[i].lmi_handlecxx . . $ dynamic_face#lmi_handle.].usedcxx . . $ lightmap_info#used. == 0)
1404 continue; // this face was killed last frame. This can happen with objects
1405
1406 int dynamic_handle = LightmapInfocxx . . $ LightmapInfo.[Dynamic_face_listcxx . . $ Dynamic_face_list.[i].lmi_handlecxx . . $ dynamic_face#lmi_handle.].dynamiccxx . . $ lightmap_info#dynamic.;
1407 int lmi_handle = Dynamic_face_listcxx . . $ Dynamic_face_list.[i].lmi_handlecxx . . $ dynamic_face#lmi_handle.;
1408 int lm_handle = LightmapInfocxx . . $ LightmapInfo.[lmi_handle].lm_handlecxx . . $ lightmap_info#lm_handle.;
1409 lightmap_infocxx . . $ lightmap_info# *lmi_ptr = &LightmapInfocxx . . $ LightmapInfo.[lmi_handle];
1410
1411 uint16_tcxx . . $ uint16_t# *src_data = Dynamic_lightmapscxx . . $ Dynamic_lightmaps.[dynamic_handle].mem_ptrcxx . . $ dynamic_lightmap#mem_ptr.;
1412 uint16_tcxx . . $ uint16_t# *dest_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(lm_handle);
1413
1414 int lmw = lm_wcxx . . $ lm_w(7864480464b09eea).(lm_handle);
1415
1416 dest_data += (lmi_ptr->y1cxx . . $ lightmap_info#y1. * lmw);
1417
1418 for (int y = 0; y < lmi_ptr->heightcxx . . $ lightmap_info#height.; y++, dest_data += lmw) {
1419 for (int x = 0; x < lmi_ptr->widthcxx . . $ lightmap_info#width.; x++) {
1420 dest_data[lmi_ptr->x1cxx . . $ lightmap_info#x1. + x] = src_data[y * lmi_ptr->widthcxx . . $ lightmap_info#width. + x];
1421 }
1422 }
1423
1424 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
1425 LightmapInfocxx . . $ LightmapInfo.[lmi_handle].dynamiccxx . . $ lightmap_info#dynamic. = BAD_LMI_INDEXcxx . . $ `Descent3/lightmap_info.h:46:9`!;
1426 }
1427
1428 Num_dynamic_facescxx . . $ Num_dynamic_faces. = 0;
1429
1430 // Now do terrain
1431
1432 for (i = 0; i < Num_dynamic_cellscxx . . $ Num_dynamic_cells.; i++) {
1433 int cellnum = Dynamic_cell_listcxx . . $ Dynamic_cell_list.[i].cellnumcxx . . $ dynamic_cell#cellnum.;
1434 Terrain_segcxx . . $ Terrain_seg.[cellnum].flagscxx . . $ terrain_segment#flags. &= ~TF_DYNAMICcxx . . $ `Descent3/terrain.h:73:9`!;
1435 Terrain_segcxx . . $ Terrain_seg.[cellnum].rcxx . . $ terrain_segment#r. = Dynamic_cell_listcxx . . $ Dynamic_cell_list.[i].rcxx . . $ dynamic_cell#r.;
1436 Terrain_segcxx . . $ Terrain_seg.[cellnum].gcxx . . $ terrain_segment#g. = Dynamic_cell_listcxx . . $ Dynamic_cell_list.[i].gcxx . . $ dynamic_cell#g.;
1437 Terrain_segcxx . . $ Terrain_seg.[cellnum].bcxx . . $ terrain_segment#b. = Dynamic_cell_listcxx . . $ Dynamic_cell_list.[i].bcxx . . $ dynamic_cell#b.;
1438
1439 int seg_x = cellnum % TERRAIN_WIDTHcxx . . $ `Descent3/terrain.h:31:9`!;
1440 int seg_z = cellnum / TERRAIN_WIDTHcxx . . $ `Descent3/terrain.h:31:9`!;
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 = TerrainLightmapscxx . . $ TerrainLightmaps.[whichmap];
1447
1448 uint16_tcxx . . $ uint16_t# color = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | GR_RGB16cxx . . $ GR_RGB16(6461fafb79bad5b9).(Terrain_segcxx . . $ Terrain_seg.[cellnum].rcxx . . $ terrain_segment#r., Terrain_segcxx . . $ Terrain_seg.[cellnum].gcxx . . $ terrain_segment#g., Terrain_segcxx . . $ Terrain_seg.[cellnum].bcxx . . $ terrain_segment#b.);
1449 uint16_tcxx . . $ uint16_t# *data = lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(whichmap);
1450
1451 data[subz * 128 + subx] = color;
1452
1453 GameLightmapscxx . . $ GameLightmaps.[whichmap].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
1454 }
1455
1456 Num_dynamic_cellscxx . . $ Num_dynamic_cells. = 0;
1457
1458 Num_dynamic_lightmapscxx . . $ Num_dynamic_lightmaps. = 0;
1459 Cur_dynamic_mem_ptrcxx . . $ Cur_dynamic_mem_ptr. = 0;
1460
1461 BlendAllLightingEdgescxx . . $ BlendAllLightingEdges(49f6e7a06ebc5aa8).();
1462 Num_edges_to_blendcxx . . $ Num_edges_to_blend. = 0;
1463 }
1464
1465 // Changes the terrain shading to approximate lighting
1466 void ApplyLightingToTerraincxx . . $ ApplyLightingToTerrain(31a30e6d6b9dd744).(vectorcxx . . $ vector# *pos, int cellnum, float light_dist, float red_scale, float green_scale,
1467 float blue_scale, vectorcxx . . $ vector# *light_direction, float dot_range) {
1468 int celllist[MAX_DYNAMIC_CELLScxx . . $ `Descent3/lighting.cpp:50:9`!];
1469 int num_cells, i;
1470
1471 if (Dedicated_servercxx . . $ Dedicated_server.)
1472 return;
1473
1474 ApplyLightingToObjectscxx . . $ ApplyLightingToObjects(31a30e6d6b9dd744).(pos, MAKE_ROOMNUMcxx . . $ `lib/object_external_struct.h:124:9`!(cellnum), light_dist, red_scale, green_scale, blue_scale, light_direction,
1475 dot_range);
1476
1477 num_cells = fvi_QuickDistCellListcxx . . $ fvi_QuickDistCellList(2280b6e28a61436a).(cellnum, pos, light_dist, celllist, MAX_DYNAMIC_CELLScxx . . $ `Descent3/lighting.cpp:50:9`!);
1478
1479 if (num_cells < 1)
1480 return;
1481
1482 if (num_cells == MAX_DYNAMIC_CELLScxx . . $ `Descent3/lighting.cpp:50:9`!) {
1483 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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_segmentcxx . . $ terrain_segment# *tseg = &Terrain_segcxx . . $ Terrain_seg.[cellnum];
1493
1494 int seg_x = cellnum % TERRAIN_WIDTHcxx . . $ `Descent3/terrain.h:31:9`!;
1495 int seg_z = cellnum / TERRAIN_WIDTHcxx . . $ `Descent3/terrain.h:31:9`!;
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 = TerrainLightmapscxx . . $ TerrainLightmaps.[whichmap];
1502
1503 // Make sure face was rendered
1504 // if (tseg->renderframe!=((FrameCount-1)%256))
1505 // continue;
1506
1507 if (Num_dynamic_cellscxx . . $ Num_dynamic_cells. >= MAX_DYNAMIC_CELLScxx . . $ `Descent3/lighting.cpp:50:9`!) {
1508 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Too many dynamic cells!\n");
1509 return;
1510 }
1511
1512 // Check for backfaces
1513 vectorcxx . . $ vector# tpos;
1514
1515 tpos.xcxx . . $ vector#x. = (seg_x * TERRAIN_SIZEcxx . . $ `Descent3/terrain.h:33:9`!) + (TERRAIN_SIZEcxx . . $ `Descent3/terrain.h:33:9`! / 2);
1516 tpos.zcxx . . $ vector#z. = (seg_z * TERRAIN_SIZEcxx . . $ `Descent3/terrain.h:33:9`!) + (TERRAIN_SIZEcxx . . $ `Descent3/terrain.h:33:9`! / 2);
1517 tpos.ycxx . . $ vector#y. = tseg->ycxx . . $ terrain_segment#y.;
1518
1519 vectorcxx . . $ vector# subvec = *pos -cxx . . $ operator-(b77b703cd1e43e33). tpos;
1520
1521 float dist = vm_GetMagnitudeFastcxx . . $ vm_GetMagnitudeFast(4939a7e530383cd4).(&subvec);
1522 float scalar = 1.0 - (dist / light_dist);
1523
1524 if (light_direction) {
1525 vectorcxx . . $ vector# lsubvec = -cxx . . $ operator-(9e07539e219c6f20).subvec;
1526 lsubvec /=cxx . . $ `operator/=`(f132838b2cd11d6). dist;
1527 float dp = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&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->flagscxx . . $ terrain_segment#flags. & TF_DYNAMICcxx . . $ `Descent3/terrain.h:73:9`!)) {
1541 Dynamic_cell_listcxx . . $ Dynamic_cell_list.[Num_dynamic_cellscxx . . $ Num_dynamic_cells.].cellnumcxx . . $ dynamic_cell#cellnum. = cellnum;
1542 Dynamic_cell_listcxx . . $ Dynamic_cell_list.[Num_dynamic_cellscxx . . $ Num_dynamic_cells.].rcxx . . $ dynamic_cell#r. = tseg->rcxx . . $ terrain_segment#r.;
1543 Dynamic_cell_listcxx . . $ Dynamic_cell_list.[Num_dynamic_cellscxx . . $ Num_dynamic_cells.].gcxx . . $ dynamic_cell#g. = tseg->gcxx . . $ terrain_segment#g.;
1544 Dynamic_cell_listcxx . . $ Dynamic_cell_list.[Num_dynamic_cellscxx . . $ Num_dynamic_cells.].bcxx . . $ dynamic_cell#b. = tseg->bcxx . . $ terrain_segment#b.;
1545 tseg->flagscxx . . $ terrain_segment#flags. |= TF_DYNAMICcxx . . $ `Descent3/terrain.h:73:9`!;
1546 Num_dynamic_cellscxx . . $ Num_dynamic_cells.++;
1547
1548 if (GameLightmapscxx . . $ GameLightmaps.[whichmap].flagscxx . . $ bms_lightmap#flags. & LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!) {
1549 if (subx < GameLightmapscxx . . $ GameLightmaps.[whichmap].cx1cxx . . $ bms_lightmap#cx1.)
1550 GameLightmapscxx . . $ GameLightmaps.[whichmap].cx1cxx . . $ bms_lightmap#cx1. = subx;
1551 if (subx > GameLightmapscxx . . $ GameLightmaps.[whichmap].cx2cxx . . $ bms_lightmap#cx2.)
1552 GameLightmapscxx . . $ GameLightmaps.[whichmap].cx2cxx . . $ bms_lightmap#cx2. = subx;
1553
1554 if (subz < GameLightmapscxx . . $ GameLightmaps.[whichmap].cy1cxx . . $ bms_lightmap#cy1.)
1555 GameLightmapscxx . . $ GameLightmaps.[whichmap].cy1cxx . . $ bms_lightmap#cy1. = subz;
1556 if (subz > GameLightmapscxx . . $ GameLightmaps.[whichmap].cy2cxx . . $ bms_lightmap#cy2.)
1557 GameLightmapscxx . . $ GameLightmaps.[whichmap].cy2cxx . . $ bms_lightmap#cy2. = subz;
1558
1559 } else {
1560 GameLightmapscxx . . $ GameLightmaps.[whichmap].cx1cxx . . $ bms_lightmap#cx1. = subx;
1561 GameLightmapscxx . . $ GameLightmaps.[whichmap].cx2cxx . . $ bms_lightmap#cx2. = subx + 1;
1562 GameLightmapscxx . . $ GameLightmaps.[whichmap].cy1cxx . . $ bms_lightmap#cy1. = subz;
1563 GameLightmapscxx . . $ GameLightmaps.[whichmap].cy2cxx . . $ bms_lightmap#cy2. = subz + 1;
1564 GameLightmapscxx . . $ GameLightmaps.[whichmap].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
1565 }
1566 }
1567
1568 int r = tseg->rcxx . . $ terrain_segment#r.;
1569 int g = tseg->gcxx . . $ terrain_segment#g.;
1570 int b = tseg->bcxx . . $ terrain_segment#b.;
1571
1572 if (red_scale < 0) {
1573 // we are subtracting light
1574 tseg->rcxx . . $ terrain_segment#r. = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, r + (scalar * red_scale * 255));
1575 } else {
1576 if (r < red_limit)
1577 tseg->rcxx . . $ terrain_segment#r. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(red_limit, r + (scalar * red_scale * 255));
1578 }
1579
1580 if (green_scale < 0) {
1581 // we are subtracting light
1582 tseg->gcxx . . $ terrain_segment#g. = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, g + (scalar * green_scale * 255));
1583 } else {
1584 if (g < green_limit)
1585 tseg->gcxx . . $ terrain_segment#g. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(green_limit, g + (scalar * green_scale * 255));
1586 }
1587
1588 if (blue_scale < 0) {
1589 // we are subtracting light
1590 tseg->bcxx . . $ terrain_segment#b. = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, b + (scalar * blue_scale * 255));
1591 } else {
1592 if (b < blue_limit)
1593 tseg->bcxx . . $ terrain_segment#b. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<float>(blue_limit, b + (scalar * blue_scale * 255));
1594 }
1595
1596 uint16_tcxx . . $ uint16_t# color = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | GR_RGB16cxx . . $ GR_RGB16(6461fafb79bad5b9).(tseg->rcxx . . $ terrain_segment#r., tseg->gcxx . . $ terrain_segment#g., tseg->bcxx . . $ terrain_segment#b.);
1597 uint16_tcxx . . $ uint16_t# *data = lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(whichmap);
1598
1599 ASSERTcxx . . $ `misc/pserror.h:246:9`!(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 SetRoomPulsecxx . . $ SetRoomPulse(f9b30ad62dfb5929).(roomcxx . . $ room# *rp, uint8_tcxx . . $ uint8_t# pulse_time, uint8_tcxx . . $ uint8_t# pulse_offset) {
1608 ASSERTcxx . . $ `misc/pserror.h:246:9`!(rp->used);
1609
1610 rp->pulse_timecxx . . $ room#pulse_time. = pulse_time;
1611 rp->pulse_offsetcxx . . $ room#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 GetVolumeSizeOfRoomcxx . . $ GetVolumeSizeOfRoom(bbc96c603c40b693).(roomcxx . . $ room# *rp, int *w, int *h, int *d) {
1617 int width = ((rp->max_xyzcxx . . $ room#max_xyz..xcxx . . $ vector#x. - rp->min_xyzcxx . . $ room#min_xyz..xcxx . . $ vector#x.) / VOLUME_SPACINGcxx . . $ `Descent3/lighting.h:34:9`!) + 1;
1618 int height = ((rp->max_xyzcxx . . $ room#max_xyz..ycxx . . $ vector#y. - rp->min_xyzcxx . . $ room#min_xyz..ycxx . . $ vector#y.) / VOLUME_SPACINGcxx . . $ `Descent3/lighting.h:34:9`!) + 1;
1619 int depth = ((rp->max_xyzcxx . . $ room#max_xyz..zcxx . . $ vector#z. - rp->min_xyzcxx . . $ room#min_xyz..zcxx . . $ vector#z.) / VOLUME_SPACINGcxx . . $ `Descent3/lighting.h:34:9`!) + 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_LIMITcxx . . $ `Descent3/lighting.cpp:1992:9`! .2f
1993
1994 // Kills the lighting that a face casts and dampens all the faces that light influences
1995 void DestroyLightcxx . . $ DestroyLight(3e454b8ccf442868).(int roomnum, int facenum) {
1996 vectorcxx . . $ vector# vecs[MAX_VERTS_PER_FACEcxx . . $ `Descent3/room_external.h:108:9`!], center;
1997 roomcxx . . $ room# *destroy_rp = &Roomscxx . . $ Rooms.[roomnum];
1998 facecxx . . $ face# *destroy_fp = &destroy_rp->facescxx . . $ room#faces.[facenum];
1999 float r, g, b;
2000 fvi_face_room_listcxx . . $ fvi_face_room_list# facelist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
2001 int num_faces, i;
2002 uint16_tcxx . . $ uint16_t# lmilist[MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!];
2003 int num_spoken_for = 0;
2004
2005 if (Dedicated_servercxx . . $ Dedicated_server.)
2006 return;
2007
2008 float mul = ((float)destroy_fp->light_multiplecxx . . $ face#light_multiple.) / 4.0;
2009
2010 r = GameTexturescxx . . $ GameTextures.[destroy_fp->tmapcxx . . $ face#tmap.].rcxx . . $ texture#r. * mul;
2011 g = GameTexturescxx . . $ GameTextures.[destroy_fp->tmapcxx . . $ face#tmap.].gcxx . . $ texture#g. * mul;
2012 b = GameTexturescxx . . $ GameTextures.[destroy_fp->tmapcxx . . $ face#tmap.].bcxx . . $ texture#b. * mul;
2013
2014 // Get highest component
2015 float rmax = stdcxx . . $ std/::maxcxx . . $ std/max(7d979dd22a97e265).({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 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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_vertscxx . . $ face#num_verts.; i++)
2030 vecs[i] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). destroy_rp->vertscxx . . $ room#verts.[destroy_fp->face_vertscxx . . $ face#face_verts.[i]];
2031
2032 float area = vm_GetCentroidcxx . . $ vm_GetCentroid(9f2e9a768aacd0cc).(&center, vecs, destroy_fp->num_vertscxx . . $ face#num_verts.);
2033
2034 // Get the sphere of influence of this light
2035 float power = rmax * area;
2036 float temp_ignore = DESTROYABLE_IGNORE_LIMITcxx . . $ `Descent3/lighting.cpp:1992:9`! / power;
2037 float express = 1.0 / temp_ignore;
2038 express /= 3.14f;
2039 express -= area;
2040 float sphere_dist = sqrtcxx . . $ sqrt(fbcf3211dfe1bfd1).(express);
2041
2042 // ApplyLightingToObjects (pos,roomnum,light_dist,red_scale,green_scale,blue_scale,light_direction,dot_range);
2043
2044 num_faces = fvi_QuickDistFaceListcxx . . $ fvi_QuickDistFaceList(a662bbcbc4f3d9bd).(roomnum, &center, sphere_dist, facelist, MAX_DYNAMIC_FACEScxx . . $ `Descent3/lighting.cpp:49:9`!);
2045
2046 for (i = 0; i < num_faces; i++) {
2047 roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[facelist[i].room_indexcxx . . $ fvi_face_room_list#room_index.];
2048 facecxx . . $ face# *fp = &rp->facescxx . . $ room#faces.[facelist[i].face_indexcxx . . $ fvi_face_room_list#face_index.];
2049
2050 ASSERTcxx . . $ `misc/pserror.h:246:9`!(Rooms[facelist[i].room_index].used);
2051 ASSERTcxx . . $ `misc/pserror.h:246:9`!(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->flagscxx . . $ face#flags. & FF_LIGHTMAPcxx . . $ `Descent3/room_external.h:112:9`!))
2055 continue;
2056
2057 if (GameTexturescxx . . $ GameTextures.[fp->tmapcxx . . $ face#tmap.].flagscxx . . $ texture#flags. & TF_LIGHTcxx . . $ `Descent3/gametexture.h:220:9`!)
2058 continue;
2059
2060 if (Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] & (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8)))
2061 continue;
2062
2063 int xres = lmi_wcxx . . $ lmi_w(7864480464b09eea).(fp->lmi_handlecxx . . $ face#lmi_handle.);
2064 int yres = lmi_hcxx . . $ lmi_h(7864480464b09eea).(fp->lmi_handlecxx . . $ face#lmi_handle.);
2065
2066 lightmap_infocxx . . $ lightmap_info# *lmi_ptr = &LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.];
2067
2068 int lmw = lm_wcxx . . $ lm_w(7864480464b09eea).(lmi_ptr->lm_handlecxx . . $ lightmap_info#lm_handle.);
2069
2070 vectorcxx . . $ vector# subvec = center -cxx . . $ operator-(b77b703cd1e43e33). lmi_ptr->upper_leftcxx . . $ lightmap_info#upper_left.;
2071 float dist_from_plane = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &lmi_ptr->normalcxx . . $ lightmap_info#normal.);
2072
2073 // Check for backfaces
2074 if (dist_from_plane < 0)
2075 continue;
2076
2077 dist_from_plane = fabscxx . . $ fabs(fbcf3211dfe1bfd1).(dist_from_plane);
2078
2079 if (dist_from_plane > sphere_dist)
2080 continue;
2081
2082 // Compute face matrix
2083 matrixcxx . . $ matrix# facematrix;
2084 vectorcxx . . $ vector# fvec = -cxx . . $ operator-(9e07539e219c6f20).lmi_ptr->normalcxx . . $ lightmap_info#normal.;
2085 vm_VectorToMatrixcxx . . $ vm_VectorToMatrix(543233b88555916d).(&facematrix, &fvec, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
2086
2087 // Get upper left vector of face
2088 vectorcxx . . $ vector# base_vector = lmi_ptr->upper_leftcxx . . $ lightmap_info#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 vectorcxx . . $ vector# touch_vector = center -cxx . . $ operator-(b77b703cd1e43e33). (lmi_ptr->normalcxx . . $ lightmap_info#normal. *cxx . . $ `operator*`(252247219a31502c). dist_from_plane);
2093
2094 subvec =cxx . . $ vector#`operator=`(c0842353a20857d). touch_vector -cxx . . $ operator-(b77b703cd1e43e33). base_vector;
2095 float rdist = vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.rveccxx . . $ matrix#rvec.);
2096 float udist = -vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(&subvec, &facematrix.uveccxx . . $ matrix#uvec.);
2097 int x_add = 1 + ((area_of_effect + (lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing. / 2)) / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
2098 int y_add = 1 + ((area_of_effect + (lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing. / 2)) / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
2099
2100 // Get the rectangle of the lightmap that we want to light
2101 int start_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) - x_add;
2102 int start_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.) - y_add;
2103 int end_x = (rdist / lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.) + x_add + .5;
2104 int end_y = (udist / lmi_ptr->yspacingcxx . . $ lightmap_info#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 ASSERTcxx . . $ `misc/pserror.h:246:9`!(width > 0);
2135 ASSERTcxx . . $ `misc/pserror.h:246:9`!(height > 0);
2136
2137 // Now copy our source data to our dest data so we have a base to work with
2138 uint16_tcxx . . $ uint16_t# *dest_data;
2139
2140 dest_data = (uint16_tcxx . . $ uint16_t# *)lm_datacxx . . $ lm_data(2fbd6a760478ca0f).(LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.);
2141 int lm_handle = LightmapInfocxx . . $ LightmapInfo.[fp->lmi_handlecxx . . $ face#lmi_handle.].lm_handlecxx . . $ lightmap_info#lm_handle.;
2142
2143 if (!(GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. & LF_LIMITScxx . . $ `lib/lightmap.h:29:9`!)) {
2144 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
2145 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
2146 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
2147 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
2148 } else {
2149 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1.)
2150 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx1cxx . . $ bms_lightmap#cx1. = start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
2151 if (start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1. + width > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2.)
2152 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cx2cxx . . $ bms_lightmap#cx2. = start_x + width + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
2153
2154 if (start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1. < GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1.)
2155 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy1cxx . . $ bms_lightmap#cy1. = start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
2156 if (lmi_ptr->y1cxx . . $ lightmap_info#y1. + start_y + height > GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2.)
2157 GameLightmapscxx . . $ GameLightmaps.[lm_handle].cy2cxx . . $ bms_lightmap#cy2. = start_y + height + lmi_ptr->y1cxx . . $ lightmap_info#y1.;
2158 }
2159
2160 GameLightmapscxx . . $ GameLightmaps.[lm_handle].flagscxx . . $ bms_lightmap#flags. |= (LF_LIMITScxx . . $ `lib/lightmap.h:29:9`! | LF_CHANGEDcxx . . $ `lib/lightmap.h:28:9`!);
2161
2162 lmilist[num_spoken_for] = fp->lmi_handlecxx . . $ face#lmi_handle.;
2163 Lmi_spoken_forcxx . . $ Lmi_spoken_for.[fp->lmi_handlecxx . . $ face#lmi_handle. / 8] |= (1 << (fp->lmi_handlecxx . . $ face#lmi_handle. % 8));
2164 num_spoken_for++;
2165
2166 vectorcxx . . $ vector# element_vec;
2167
2168 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (start_y *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.));
2169 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (start_x *cxx . . $ `operator*`(9d6577ff3e9faa29). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.));
2170
2171 base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). ((facematrix.uveccxx . . $ matrix#uvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.);
2172 base_vector +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). ((facematrix.rveccxx . . $ matrix#rvec. /cxx . . $ `operator/`(252247219a31502c). 2) *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.);
2173
2174 // Go through and change each element of this lightmap
2175 // SLOW!
2176
2177 int texel_num = ((start_y + lmi_ptr->y1cxx . . $ lightmap_info#y1.) * lmw) + start_x + lmi_ptr->x1cxx . . $ lightmap_info#x1.;
2178 for (int y = 0; y < height; y++, base_vector -=cxx . . $ `operator-=`(801a8d6f7ab7bef9). (facematrix.uveccxx . . $ matrix#uvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->yspacingcxx . . $ lightmap_info#yspacing.), texel_num += lmw) {
2179 element_vec =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). base_vector;
2180
2181 for (int x = 0; x < width; x++, element_vec +=cxx . . $ `operator+=`(801a8d6f7ab7bef9). (facematrix.rveccxx . . $ matrix#rvec. *cxx . . $ `operator*`(252247219a31502c). lmi_ptr->xspacingcxx . . $ lightmap_info#xspacing.)) {
2182 int lightmap_texel_num = texel_num + x;
2183
2184 uint16_tcxx . . $ uint16_t# lightmap_texel = dest_data[lightmap_texel_num];
2185
2186 if (!(lightmap_texel & OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`!))
2187 continue;
2188
2189 float dist = vm_VectorDistanceQuickcxx . . $ vm_VectorDistanceQuick(a0b9c8659ecc16b8).(&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 = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, r - (scalar * red_scale * 31));
2201
2202 if (g > 0)
2203 g = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, g - (scalar * green_scale * 31));
2204
2205 if (b > 0)
2206 b = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).<float>(0, b - (scalar * blue_scale * 31));
2207
2208 lightmap_texel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | (r << 10) | (g << 5) | b;
2209
2210 dest_data[lightmap_texel_num] = lightmap_texel;
2211 }
2212 }
2213
2214 BlendLightingEdgescxx . . $ BlendLightingEdges(d2f3fbe01c9a6be1).(lmi_ptr);
2215 }
2216
2217 for (i = 0; i < num_spoken_for; i++) {
2218 int index = lmilist[i];
2219 Lmi_spoken_forcxx . . $ 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 AddToDestroyableLightListcxx . . $ AddToDestroyableLightList(3e454b8ccf442868).(int roomnum, int facenum) {
2226 if (Num_destroyed_lights_this_framecxx . . $ Num_destroyed_lights_this_frame. >= MAX_DESTROYED_LIGHTS_PER_FRAMEcxx . . $ `Descent3/lighting.cpp:90:9`!) {
2227 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Ran out of destroyable light slots!\n");
2228 return;
2229 }
2230
2231 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Destroying light. Room=%d face=%d\n", roomnum, facenum);
2232
2233 Destroyed_light_rooms_this_framecxx . . $ Destroyed_light_rooms_this_frame.[Num_destroyed_lights_this_framecxx . . $ Num_destroyed_lights_this_frame.] = roomnum;
2234 Destroyed_light_faces_this_framecxx . . $ Destroyed_light_faces_this_frame.[Num_destroyed_lights_this_framecxx . . $ Num_destroyed_lights_this_frame.++] = facenum;
2235 }
2236
2237 // Goes through our destroyable light list and actually kills all the lights
2238 void DoDestroyedLightsForFramecxx . . $ DoDestroyedLightsForFrame(49f6e7a06ebc5aa8).() {
2239 int i;
2240 for (i = 0; i < Num_destroyed_lights_this_framecxx . . $ Num_destroyed_lights_this_frame.; i++) {
2241 int roomnum = Destroyed_light_rooms_this_framecxx . . $ Destroyed_light_rooms_this_frame.[i];
2242 int facenum = Destroyed_light_faces_this_framecxx . . $ Destroyed_light_faces_this_frame.[i];
2243 DestroyLightcxx . . $ DestroyLight(3e454b8ccf442868).(roomnum, facenum);
2244 }
2245
2246 Num_destroyed_lights_this_framecxx . . $ Num_destroyed_lights_this_frame. = 0;
2247 }