Debian Codegraph @

similar/editor/seguvs.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * u,v coordinate computation for segment faces
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <math.h>
30 #include <string.h>
31 #include "inferno.h"
32 #include "segment.h"
33 #include "editor/editor.h"
34 #include "editor/esegment.h"
35 #include "gameseg.h"
36 #include "maths.h"
37 #include "dxxerror.h"
38 #include "wall.h"
39 #include "editor/kdefs.h"
40 #include "bm.h" // Needed for TmapInfo
41 #include "fvi.h"
42 #include "render.h"
43 #include "seguvs.h"
44
45 #include "compiler-range_for.h"
46 #include "d_enumerate.h"
47 #include "d_range.h"
48 #include "d_zip.h"
49
50 namespace dcx {
51 namespace {
52 static void cast_all_light_in_mine(int quick_flag);
53 }
54 }
55 //--rotate_uvs-- vms_vector Rightvec;
56
57 #define MAX_LIGHT_SEGS 16
58
59 namespace {
60
61 // ---------------------------------------------------------------------------------------------
62 // Scan all polys in all segments, return average light value for vnum.
63 // segs = output array for segments containing vertex, terminated by -1.
64 static fix get_average_light_at_vertex(const vertnum_t vnum, segnum_t *segs)
65 {
66 fix total_light;
67 int num_occurrences;
68 // #ifndef NDEBUG //Removed this ifdef because the version of Assert that I used to get it to compile doesn't work without this symbol. -KRB
69 segnum_t *original_segs;
70
71 original_segs = segs;
72 // #endif
73
74
75 num_occurrences = 0;
76 total_light = 0;
77
78 range_for (const auto &&segp, vcsegptridx)
79 {
80 auto e = end(segp->verts);
81 auto it = std::find(begin(segp->verts), e, vnum);
82 if (it == e)
83 continue;
84 auto relvnum = static_cast<segment_relative_vertnum>(std::distance(it, e));
85 {
86 *segs++ = segp;
87 Assert(segs - original_segs < MAX_LIGHT_SEGS);
88 (void)original_segs;
89
90 for (const auto &&[child_segnum, uside, vp] : zip(segp->children, segp->unique_segment::sides, Side_to_verts))
91 {
92 if (!IS_CHILD(child_segnum))
93 {
94 const auto vb = begin(vp);
95 const auto ve = end(vp);
96 const auto vi = std::find(vb, ve, relvnum);
97 if (vi != ve)
98 {
99 const auto v = static_cast<side_relative_vertnum>(std::distance(vb, vi));
100 total_light += uside.uvls[v].l;
101 num_occurrences++;
102 }
103 } // end if
104 } // end sidenum
105 }
106 } // end segnum
107
108 *segs = segment_none;
109
110 if (num_occurrences)
111 return total_light/num_occurrences;
112 else
113 return 0;
114
115 }
116
117 static void set_average_light_at_vertex(vertnum_t vnum)
118 {
119 segnum_t Segment_indices[MAX_LIGHT_SEGS];
120 int segind;
121
122 fix average_light;
123
124 average_light = get_average_light_at_vertex(vnum, Segment_indices);
125
126 if (!average_light)
127 return;
128
129 segind = 0;
130 while (Segment_indices[segind] != segment_none) {
131 auto segnum = Segment_indices[segind++];
132
133 auto &ssegp = *vcsegptr(segnum);
134 unique_segment &usegp = *vmsegptr(segnum);
135
136 for (const auto &&[relvnum, vert] : enumerate(ssegp.verts))
137 if (vert == vnum)
138 {
139 for (const auto &&[child_segnum, sidep, vp] : zip(ssegp.children, usegp.sides, Side_to_verts))
140 {
141 if (!IS_CHILD(child_segnum))
142 {
143 const auto vb = begin(vp);
144 const auto ve = end(vp);
145 const auto vi = std::find(vb, ve, relvnum);
146 if (vi != ve)
147 {
148 const auto v = static_cast<side_relative_vertnum>(std::distance(vb, vi));
149 sidep.uvls[v].l = average_light;
150 }
151 } // end if
152 } // end sidenum
153 break;
154 } // end if
155 } // end while
156
157 Update_flags |= UF_WORLD_CHANGED;
158 }
159
160 static void set_average_light_on_side(const shared_segment &segp, const sidenum_t sidenum)
161 {
162 if (!IS_CHILD(segp.children[sidenum]))
163 range_for (const auto v, Side_to_verts[sidenum])
164 {
165 set_average_light_at_vertex(segp.verts[v]);
166 }
167 }
168
169 }
170
171 int set_average_light_on_curside(void)
172 {
173 set_average_light_on_side(Cursegp, Curside);
174 return 0;
175 }
176
177 int set_average_light_on_all(void)
178 {
179 Doing_lighting_hack_flag = lighting_hack::ignore_out_of_mine_location;
180 cast_all_light_in_mine(0);
181 Doing_lighting_hack_flag = lighting_hack::normal;
182 Update_flags |= UF_WORLD_CHANGED;
183
184 // int seg, side;
185
186 // for (seg=0; seg<=Highest_segment_index; seg++)
187 // for (side=0; side<MAX_SIDES_PER_SEGMENT; side++)
188 // if (Segments[seg].segnum != -1)
189 // set_average_light_on_side(&Segments[seg], side);
190 return 0;
191 }
192
193 int set_average_light_on_all_quick(void)
194 {
195 cast_all_light_in_mine(1);
196 Update_flags |= UF_WORLD_CHANGED;
197
198 return 0;
199 }
200
201 namespace {
202
203 // ---------------------------------------------------------------------------------------------
204 // Given a polygon, compress the uv coordinates so that they are as close to 0 as possible.
205 // Do this by adding a constant u and v to each uv pair.
206 static void compress_uv_coordinates(std::array<uvl, 4> &uvls)
207 {
208 fix uc, vc;
209
210 uc = 0;
211 vc = 0;
212
213 range_for (auto &uvl, uvls)
214 {
215 uc += uvl.u;
216 vc += uvl.v;
217 }
218
219 uc /= 4;
220 vc /= 4;
221 uc = uc & 0xffff0000;
222 vc = vc & 0xffff0000;
223
224 range_for (auto &uvl, uvls)
225 {
226 uvl.u -= uc;
227 uvl.v -= vc;
228 }
229 }
230
231 static void assign_default_lighting_on_side(std::array<uvl, 4> &uvls)
232 {
233 range_for (auto &uvl, uvls)
234 uvl.l = DEFAULT_LIGHTING;
235 }
236
237 static void assign_default_lighting(unique_segment &segp)
238 {
239 range_for (auto &side, segp.sides)
240 assign_default_lighting_on_side(side.uvls);
241 }
242
243 }
244
245 void assign_default_lighting_all(void)
246 {
247 for (auto &seg : vmsegptr)
248 {
249 if (seg.segnum != segment_none)
250 assign_default_lighting(seg);
251 }
252 }
253
254 namespace {
255
256 // ---------------------------------------------------------------------------------------------
257 static void validate_uv_coordinates(unique_segment &segp)
258 {
259 range_for (auto &side, segp.sides)
260 {
261 compress_uv_coordinates(side.uvls);
262 }
263
264 }
265
266 #ifdef __WATCOMC__
267 fix zhypot(fix a,fix b);
268 #pragma aux zhypot parm [eax] [ebx] value [eax] modify [eax ebx ecx edx] = \
269 "imul eax" \
270 "xchg eax,ebx" \
271 "mov ecx,edx" \
272 "imul eax" \
273 "add eax,ebx" \
274 "adc edx,ecx" \
275 "call quad_sqrt";
276 #else
277 static fix zhypot(fix a,fix b) {
278 double x = static_cast<double>(a) / 65536;
279 double y = static_cast<double>(b) / 65536;
280 return static_cast<long>(sqrt(x * x + y * y) * 65536);
281 }
282 #endif
283
284 }
285
286 // ---------------------------------------------------------------------------------------------
287 // Assign lighting value to side, a function of the normal vector.
288 void assign_light_to_side(unique_side &s)
289 {
290 range_for (auto &v, s.uvls)
291 v.l = DEFAULT_LIGHTING;
292 }
293
294 fix Stretch_scale_x = F1_0;
295 fix Stretch_scale_y = F1_0;
296
297 namespace {
298
299 // ---------------------------------------------------------------------------------------------
300 // Given u,v coordinates at two vertices, assign u,v coordinates to other two vertices on a side.
301 // (Actually, assign them to the coordinates in the faces.)
302 // va, vb = face-relative vertex indices corresponding to uva, uvb. Ie, they are always in 0..3 and should be looked up in
303 // Side_to_verts[side] to get the segment relative index.
304 static void assign_uvs_to_side(fvcvertptr &vcvertptr, const vmsegptridx_t segp, const sidenum_t sidenum, const uvl &uva, const uvl &uvb, const side_relative_vertnum va, const side_relative_vertnum vb)
305 {
306 uvl ruvmag,fuvmag;
307 const uvl *uvlo, *uvhi;
308 fix fmag,mag01;
309 #ifndef NDEBUG
310 {
311 const auto distance_between_side_vertices = underlying_value(va) - underlying_value(vb);
312 const auto abs_distance_between_side_vertices = std::abs(distance_between_side_vertices);
313 // make sure the vertices specify an edge
314 assert(abs_distance_between_side_vertices == 1 || abs_distance_between_side_vertices == 3);
315 }
316 #endif
317
318 auto &vp = Side_to_verts[sidenum];
319
320 side_relative_vertnum vlo, vhi;
321 // We want vlo precedes vhi, ie vlo < vhi, or vlo = 3, vhi = 0
322 if (va == next_side_vertex(vb))
323 { // va = vb + 1
324 vlo = vb;
325 vhi = va;
326 uvlo = &uvb;
327 uvhi = &uva;
328 } else {
329 vlo = va;
330 vhi = vb;
331 uvlo = &uva;
332 uvhi = &uvb;
333 }
334
335 assert(next_side_vertex(vlo) == vhi); // If we are on an edge, then uvhi is one more than uvlo (mod 4)
336 enumerated_array<uvl, 4, side_relative_vertnum> uvls;
337 uvls[vlo] = *uvlo;
338 uvls[vhi] = *uvhi;
339
340 // Now we have vlo precedes vhi, compute vertices ((vhi+1) % 4) and ((vhi+2) % 4)
341
342 // Assign u,v scale to a unit length right vector.
343 fmag = zhypot(uvhi->v - uvlo->v, uvhi->u - uvlo->u);
344 if (fmag < 64) { // this is a fix, so 64 = 1/1024
345 ruvmag.u = F1_0*256;
346 ruvmag.v = F1_0*256;
347 fuvmag.u = F1_0*256;
348 fuvmag.v = F1_0*256;
349 } else {
350 ruvmag.u = uvhi->v - uvlo->v;
351 ruvmag.v = uvlo->u - uvhi->u;
352
353 fuvmag.u = uvhi->u - uvlo->u;
354 fuvmag.v = uvhi->v - uvlo->v;
355 }
356
357 const auto v0 = segp->verts[vp[vlo]];
358 const auto v1 = segp->verts[vp[vhi]];
359 const auto vhi1 = next_side_vertex(vhi);
360 const auto vhi2 = next_side_vertex(vhi, 2);
361 const auto v2 = segp->verts[vp[vhi1]];
362 const auto v3 = segp->verts[vp[vhi2]];
363
364 // Compute right vector by computing orientation matrix from:
365 // forward vector = vlo:vhi
366 // right vector = vlo:(vhi+2) % 4
367 const auto &&vp0 = vcvertptr(v0);
368 const auto &vv1v0{vm_vec_build_sub(vcvertptr(v1), vp0)};
369 mag01 = vm_vec_mag(vv1v0);
370 mag01 = fixmul(mag01, (va == side_relative_vertnum::_0 || va == side_relative_vertnum::_2) ? Stretch_scale_x : Stretch_scale_y);
371
372 if (unlikely(mag01 < F1_0/1024))
373 editor_status_fmt("U, V bogosity in segment #%hu, probably on side #%i. CLEAN UP YOUR MESS!", segp.get_unchecked_index(), underlying_value(sidenum));
374 else {
375 struct frvec {
376 vms_vector fvec, rvec;
377 frvec(const vms_vector &tfvec, const vms_vector &trvec) {
378 if (tfvec == vms_vector{} || trvec == vms_vector{})
379 {
380 fvec = vmd_identity_matrix.fvec;
381 rvec = vmd_identity_matrix.rvec;
382 }
383 else
384 {
385 const auto m{vm_vector_to_matrix_r(tfvec, trvec)};
386 fvec = m.fvec;
387 rvec = m.rvec;
388 }
389 vm_vec_negate(rvec);
390 }
391 };
392 const auto &vv3v0{vm_vec_build_sub(vcvertptr(v3), vp0)};
393 const frvec fr{
394 vv1v0,
395 vv3v0
396 };
397 const auto build_uvl = [&](const vms_vector &tvec, const uvl &uvi) {
398 const auto drt = vm_vec_build_dot(fr.rvec, tvec);
399 const auto dft = vm_vec_build_dot(fr.fvec, tvec);
400 return uvl{
401 uvi.u +
402 fixdiv(fixmul(ruvmag.u, drt), mag01) +
403 fixdiv(fixmul(fuvmag.u, dft), mag01),
404 uvi.v +
405 fixdiv(fixmul(ruvmag.v, drt), mag01) +
406 fixdiv(fixmul(fuvmag.v, dft), mag01),
407 uvi.l
408 };
409 };
410 uvls[vhi1] = build_uvl(vm_vec_build_sub(vcvertptr(v2), vcvertptr(v1)), *uvhi);
411 uvls[vhi2] = build_uvl(vv3v0, *uvlo);
412 // For all faces in side, copy uv coordinates from uvs array to face.
413 segp->unique_segment::sides[sidenum].uvls = uvls;
414 }
415 }
416
417 }
418
419
420 int Vmag = VMAG;
421
422 namespace dsx {
423
424 // -----------------------------------------------------------------------------------------------------------
425 // Assign default uvs to side.
426 // This means:
427 // v0 = 0,0
428 // v1 = k,0 where k is 3d size dependent
429 // v2, v3 assigned by assign_uvs_to_side
430 void assign_default_uvs_to_side(const vmsegptridx_t segp, const sidenum_t side)
431 {
432 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
433 auto &Vertices = LevelSharedVertexState.get_vertices();
434 uvl uv0,uv1;
435 uv0.u = 0;
436 uv0.v = 0;
437 auto &vp = Side_to_verts[side];
438 uv1.u = 0;
439 auto &vcvertptr = Vertices.vcptr;
440 uv1.v = Num_tilings * fixmul(Vmag, vm_vec_dist(vcvertptr(segp->verts[vp[side_relative_vertnum::_1]]), vcvertptr(segp->verts[vp[side_relative_vertnum::_0]])));
441
442 assign_uvs_to_side(vcvertptr, segp, side, uv0, uv1, side_relative_vertnum::_0, side_relative_vertnum::_1);
443 }
444
445 // -----------------------------------------------------------------------------------------------------------
446 // Assign default uvs to side.
447 // This means:
448 // v0 = 0,0
449 // v1 = k,0 where k is 3d size dependent
450 // v2, v3 assigned by assign_uvs_to_side
451 void stretch_uvs_from_curedge(const vmsegptridx_t segp, const sidenum_t side)
452 {
453 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
454 auto &Vertices = LevelSharedVertexState.get_vertices();
455 uvl uv0,uv1;
456
457 const auto v0{Curedge};
458 const auto v1 = next_side_vertex(v0);
459
460 const auto &uvls = segp->unique_segment::sides[side].uvls;
461 uv0.u = uvls[v0].u;
462 uv0.v = uvls[v0].v;
463
464 uv1.u = uvls[v1].u;
465 uv1.v = uvls[v1].v;
466
467 auto &vcvertptr = Vertices.vcptr;
468 assign_uvs_to_side(vcvertptr, segp, side, uv0, uv1, v0, v1);
469 }
470
471 // --------------------------------------------------------------------------------------------------------------
472 // Assign default uvs to a segment.
473 void assign_default_uvs_to_segment(const vmsegptridx_t segp)
474 {
475 for (const auto s : MAX_SIDES_PER_SEGMENT)
476 {
477 assign_default_uvs_to_side(segp,s);
478 assign_light_to_side(segp, s);
479 }
480 }
481
482
483 // -- mk021394 -- // --------------------------------------------------------------------------------------------------------------
484 // -- mk021394 -- // Find the face:poly:vertex index in base_seg:base_common_side which is segment relative vertex v1
485 // -- mk021394 -- // This very specific routine is subsidiary to med_assign_uvs_to_side.
486 // -- mk021394 -- void get_face_and_vert(segment *base_seg, int base_common_side, int v1, int *ff, int *vv, int *pi)
487 // -- mk021394 -- {
488 // -- mk021394 -- int p,f,v;
489 // -- mk021394 --
490 // -- mk021394 -- for (f=0; f<base_seg->sides[base_common_side].num_faces; f++) {
491 // -- mk021394 -- face *fp = &base_seg->sides[base_common_side].faces[f];
492 // -- mk021394 -- for (p=0; p<fp->num_polys; p++) {
493 // -- mk021394 -- poly *pp = &fp->polys[p];
494 // -- mk021394 -- for (v=0; v<pp->num_vertices; v++)
495 // -- mk021394 -- if (pp->verts[v] == v1) {
496 // -- mk021394 -- *ff = f;
497 // -- mk021394 -- *vv = v;
498 // -- mk021394 -- *pi = p;
499 // -- mk021394 -- return;
500 // -- mk021394 -- }
501 // -- mk021394 -- }
502 // -- mk021394 -- }
503 // -- mk021394 --
504 // -- mk021394 -- Assert(0); // Error -- Couldn't find face:vertex which matched vertex v1 on base_seg:base_common_side
505 // -- mk021394 -- }
506
507 // -- mk021394 -- // --------------------------------------------------------------------------------------------------------------
508 // -- mk021394 -- // Find the vertex index in base_seg:base_common_side which is segment relative vertex v1
509 // -- mk021394 -- // This very specific routine is subsidiary to med_assign_uvs_to_side.
510 // -- mk021394 -- void get_side_vert(segment *base_seg,int base_common_side,int v1,int *vv)
511 // -- mk021394 -- {
512 // -- mk021394 -- int p,f,v;
513 // -- mk021394 --
514 // -- mk021394 -- Assert((base_seg->sides[base_common_side].tri_edge == 0) || (base_seg->sides[base_common_side].tri_edge == 1));
515 // -- mk021394 -- Assert(base_seg->sides[base_common_side].num_faces <= 2);
516 // -- mk021394 --
517 // -- mk021394 -- for (f=0; f<base_seg->sides[base_common_side].num_faces; f++) {
518 // -- mk021394 -- face *fp = &base_seg->sides[base_common_side].faces[f];
519 // -- mk021394 -- for (p=0; p<fp->num_polys; p++) {
520 // -- mk021394 -- poly *pp = &fp->polys[p];
521 // -- mk021394 -- for (v=0; v<pp->num_vertices; v++)
522 // -- mk021394 -- if (pp->verts[v] == v1) {
523 // -- mk021394 -- if (pp->num_vertices == 4) {
524 // -- mk021394 -- *vv = v;
525 // -- mk021394 -- return;
526 // -- mk021394 -- }
527 // -- mk021394 --
528 // -- mk021394 -- if (base_seg->sides[base_common_side].tri_edge == 0) { // triangulated 012, 023, so if f==0, *vv = v, if f==1, *vv = v if v=0, else v+1
529 // -- mk021394 -- if ((f == 1) && (v > 0))
530 // -- mk021394 -- v++;
531 // -- mk021394 -- *vv = v;
532 // -- mk021394 -- return;
533 // -- mk021394 -- } else { // triangulated 013, 123
534 // -- mk021394 -- if (f == 0) {
535 // -- mk021394 -- if (v == 2)
536 // -- mk021394 -- v++;
537 // -- mk021394 -- } else
538 // -- mk021394 -- v++;
539 // -- mk021394 -- *vv = v;
540 // -- mk021394 -- return;
541 // -- mk021394 -- }
542 // -- mk021394 -- }
543 // -- mk021394 -- }
544 // -- mk021394 -- }
545 // -- mk021394 --
546 // -- mk021394 -- Assert(0); // Error -- Couldn't find face:vertex which matched vertex v1 on base_seg:base_common_side
547 // -- mk021394 -- }
548
549 //--rotate_uvs-- // --------------------------------------------------------------------------------------------------------------
550 //--rotate_uvs-- // Rotate uvl coordinates uva, uvb about their center point by heading
551 //--rotate_uvs-- void rotate_uvs(uvl *uva, uvl *uvb, vms_vector *rvec)
552 //--rotate_uvs-- {
553 //--rotate_uvs-- uvl uvc, uva1, uvb1;
554 //--rotate_uvs--
555 //--rotate_uvs-- uvc.u = (uva->u + uvb->u)/2;
556 //--rotate_uvs-- uvc.v = (uva->v + uvb->v)/2;
557 //--rotate_uvs--
558 //--rotate_uvs-- uva1.u = fixmul(uva->u - uvc.u, rvec->x) - fixmul(uva->v - uvc.v, rvec->z);
559 //--rotate_uvs-- uva1.v = fixmul(uva->u - uvc.u, rvec->z) + fixmul(uva->v - uvc.v, rvec->x);
560 //--rotate_uvs--
561 //--rotate_uvs-- uva->u = uva1.u + uvc.u;
562 //--rotate_uvs-- uva->v = uva1.v + uvc.v;
563 //--rotate_uvs--
564 //--rotate_uvs-- uvb1.u = fixmul(uvb->u - uvc.u, rvec->x) - fixmul(uvb->v - uvc.v, rvec->z);
565 //--rotate_uvs-- uvb1.v = fixmul(uvb->u - uvc.u, rvec->z) + fixmul(uvb->v - uvc.v, rvec->x);
566 //--rotate_uvs--
567 //--rotate_uvs-- uvb->u = uvb1.u + uvc.u;
568 //--rotate_uvs-- uvb->v = uvb1.v + uvc.v;
569 //--rotate_uvs-- }
570
571 namespace {
572
573 // --------------------------------------------------------------------------------------------------------------
574 // Assign u,v coordinates to con_seg, con_common_side from base_seg, base_common_side
575 // They are connected at the edge defined by the vertices abs_id1, abs_id2.
576 static void med_assign_uvs_to_side(const vmsegptridx_t con_seg, const sidenum_t con_common_side, const cscusegment base_seg, const sidenum_t base_common_side, const vertnum_t abs_id1, const vertnum_t abs_id2)
577 {
578 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
579 auto &Vertices = LevelSharedVertexState.get_vertices();
580 constexpr segment_relative_vertnum invalid_vertnum{0xff};
581 segment_relative_vertnum cv1 = invalid_vertnum, cv2 = invalid_vertnum;
582 segment_relative_vertnum bv1 = invalid_vertnum, bv2 = invalid_vertnum;
583
584 // Find which vertices in segment match abs_id1, abs_id2
585 for (const auto &&[v, b, c] : enumerate(zip(base_seg.s.verts, con_seg->verts), segment_relative_vertnum{}))
586 {
587 if (b == abs_id1)
588 bv1 = v;
589 if (b == abs_id2)
590 bv2 = v;
591 if (c == abs_id1)
592 cv1 = v;
593 if (c == abs_id2)
594 cv2 = v;
595 }
596
597 // Now, bv1, bv2 are segment relative vertices in base segment which are the same as absolute vertices abs_id1, abs_id2
598 // cv1, cv2 are segment relative vertices in conn segment which are the same as absolute vertices abs_id1, abs_id2
599
600 assert(bv1 != invalid_vertnum);
601 assert(bv2 != invalid_vertnum);
602 assert(cv1 != invalid_vertnum);
603 assert(cv2 != invalid_vertnum);
604
605 // Now, scan 4 vertices in base side and 4 vertices in connected side.
606 // Set uv1, uv2 to uv coordinates from base side which correspond to vertices bv1, bv2.
607 // Set vv1, vv2 to relative vertex ids (in 0..3) in connecting side which correspond to cv1, cv2
608 std::optional<side_relative_vertnum> vv1, vv2;
609 auto &base_uvls = base_seg.u.sides[base_common_side].uvls;
610 const uvl *uv1 = nullptr, *uv2 = nullptr;
611 auto &svbase = Side_to_verts[base_common_side];
612 auto &svconn = Side_to_verts[con_common_side];
613 for (auto &&[v, base_uvl, svb, svc] : enumerate(zip(base_uvls, svbase, svconn)))
614 {
615 if (bv1 == svb)
616 uv1 = &base_uvl;
617 if (bv2 == svb)
618 uv2 = &base_uvl;
619 if (cv1 == svc)
620 vv1 = v;
621 if (cv2 == svc)
622 vv2 = v;
623 }
624
625 assert(uv1->u != uv2->u || uv1->v != uv2->v);
626 assert(vv1.has_value() && vv2.has_value());
627 auto &vcvertptr = Vertices.vcptr;
628 assign_uvs_to_side(vcvertptr, con_seg, con_common_side, *uv1, *uv2, *vv1, *vv2);
629 }
630
631
632 // -----------------------------------------------------------------------------
633 // Given a base and a connecting segment, a side on each of those segments and two global vertex ids,
634 // determine which side in each of the segments shares those two vertices.
635 // This is used to propagate a texture map id to a connecting segment in an expected and desired way.
636 // Since we can attach any side of a segment to any side of another segment, and do so in each case in
637 // four different rotations (for a total of 6*6*4 = 144 ways), not having this nifty function will cause
638 // great confusion.
639 static std::pair<sidenum_t, sidenum_t> get_side_ids(const shared_segment &base_seg, const shared_segment &con_seg, sidenum_t base_side, sidenum_t con_side, const vertnum_t abs_id1, const vertnum_t abs_id2)
640 {
641 if (&base_seg == &con_seg)
642 return {base_side, con_side};
643 std::optional<sidenum_t> base_common_side;
644
645 // Find side in base segment which contains the two global vertex ids.
646 for (const auto &&[idx, base_vp] : enumerate(Side_to_verts))
647 {
648 if (idx != base_side) {
649 for (const auto v0 : MAX_VERTICES_PER_SIDE)
650 {
651 auto &verts = base_seg.verts;
652 const auto candidate_abs_v0 = verts[base_vp[v0]];
653 const auto candidate_abs_v1 = verts[base_vp[next_side_vertex(v0)]];
654 if ((candidate_abs_v0 == abs_id1 && candidate_abs_v1 == abs_id2) ||
655 (candidate_abs_v0 == abs_id2 && candidate_abs_v1 == abs_id1))
656 {
657 assert(!base_common_side); // This means two different sides shared the same edge with base_side == impossible!
658 base_common_side = idx;
659 }
660 }
661 }
662 }
663
664 // Note: For connecting segment, process vertices in reversed order.
665 std::optional<sidenum_t> con_common_side;
666
667 // Find side in connecting segment which contains the two global vertex ids.
668 for (const auto &&[idx, con_vp] : enumerate(Side_to_verts))
669 {
670 if (idx != con_side) {
671 for (const auto v0 : MAX_VERTICES_PER_SIDE)
672 {
673 auto &verts = con_seg.verts;
674 const auto candidate_abs_v0 = verts[con_vp[next_side_vertex(v0)]];
675 const auto candidate_abs_v1 = verts[con_vp[v0]];
676 if ((candidate_abs_v0 == abs_id1 && candidate_abs_v1 == abs_id2) ||
677 (candidate_abs_v0 == abs_id2 && candidate_abs_v1 == abs_id1))
678 {
679 assert(!con_common_side); // This means two different sides shared the same edge with con_side == impossible!
680 con_common_side = idx;
681 }
682 }
683 }
684 }
685 return {*base_common_side, *con_common_side};
686 }
687
688 // -----------------------------------------------------------------------------
689 // Propagate texture map u,v coordinates from base_seg:base_side to con_seg:con_side.
690 // The two vertices abs_id1 and abs_id2 are the only two vertices common to the two sides.
691 // If uv_only_flag is 1, then don't assign texture map ids, only update the uv coordinates
692 // If uv_only_flag is -1, then ONLY assign texture map ids, don't update the uv coordinates
693 static void propagate_tmaps_to_segment_side(const vcsegptridx_t base_seg, const sidenum_t base_side, const vmsegptridx_t con_seg, const sidenum_t con_side, const vertnum_t abs_id1, const vertnum_t abs_id2, const int uv_only_flag)
694 {
695 Assert ((uv_only_flag == -1) || (uv_only_flag == 0) || (uv_only_flag == 1));
696
697 // Set base_common_side = side in base_seg which contains edge abs_id1:abs_id2
698 // Set con_common_side = side in con_seg which contains edge abs_id1:abs_id2
699 const auto [base_common_side, con_common_side] = get_side_ids(base_seg, con_seg, base_side, con_side, abs_id1, abs_id2);
700
701 // Now, all faces in con_seg which are on side con_common_side get their tmap_num set to whatever tmap is assigned
702 // to whatever face I find which is on side base_common_side.
703 // First, find tmap_num for base_common_side. If it doesn't exist (ie, there is a connection there), look at the segment
704 // that is connected through it.
705 if (!IS_CHILD(con_seg->children[con_common_side])) {
706 if (!IS_CHILD(base_seg->children[base_common_side])) {
707 // There is at least one face here, so get the tmap_num from there.
708
709 // Now assign all faces in the connecting segment on side con_common_side to tmap_num.
710 if ((uv_only_flag == -1) || (uv_only_flag == 0))
711 {
712 const auto tmap_num = base_seg->unique_segment::sides[base_common_side].tmap_num;
713 con_seg->unique_segment::sides[con_common_side].tmap_num = tmap_num;
714 }
715
716 if (uv_only_flag != -1)
717 med_assign_uvs_to_side(con_seg, static_cast<sidenum_t>(con_common_side), base_seg, static_cast<sidenum_t>(base_common_side), abs_id1, abs_id2);
718
719 } else { // There are no faces here, there is a connection, trace through the connection.
720 const auto &&csegp = base_seg.absolute_sibling(base_seg->children[base_common_side]);
721 auto cside = find_connect_side(base_seg, csegp);
722 propagate_tmaps_to_segment_side(csegp, cside, con_seg, con_side, abs_id1, abs_id2, uv_only_flag);
723 }
724 }
725
726 }
727
728 }
729
730 // -----------------------------------------------------------------------------
731 // Propagate texture map u,v coordinates to base_seg:back_side from base_seg:some-other-side
732 // There is no easy way to figure out which side is adjacent to another side along some edge, so we do a bit of searching.
733 void med_propagate_tmaps_to_back_side(const vmsegptridx_t base_seg, const sidenum_t back_side, int uv_only_flag)
734 {
735 if (IS_CHILD(base_seg->children[back_side]))
736 return; // connection, so no sides here.
737
738 // Scan all sides, look for an occupied side which is not back_side or Side_opposite[back_side]
739 for (const auto &&[s, ebs] : enumerate(Two_sides_to_edge))
740 {
741 if ((s != back_side) && (s != Side_opposite[back_side])) {
742 const auto v1 = ebs[back_side][0];
743 const auto v2 = ebs[back_side][1];
744 if (!base_seg->verts.valid_index(v1))
745 // This means there was no shared edge between the two sides.
746 return;
747 if (!base_seg->verts.valid_index(v2))
748 return;
749 propagate_tmaps_to_segment_side(base_seg, s, base_seg, back_side, base_seg->verts[v1], base_seg->verts[v2], uv_only_flag);
750 goto found1;
751 }
752 }
753 return; // Error -- couldn't find edge != back_side and Side_opposite[back_side]
754 found1: ;
755
756 // Assign an unused tmap id to the back side.
757 // Note that this can get undone by the caller if this was not part of a new attach, but a rotation or a scale (which
758 // both do attaches).
759 // First see if tmap on back side is anywhere else.
760 if (!uv_only_flag) {
761 const auto back_side_tmap = base_seg->unique_segment::sides[back_side].tmap_num;
762 for (const auto &&[idx, value] : enumerate(base_seg->unique_segment::sides))
763 {
764 if (idx != back_side)
765 if (value.tmap_num == back_side_tmap)
766 {
767 for (const auto tmap_num : MAX_SIDES_PER_SEGMENT)
768 {
769 for (const auto ss : MAX_SIDES_PER_SEGMENT)
770 if (ss != back_side)
771 if (base_seg->unique_segment::sides[ss].tmap_num == New_segment.unique_segment::sides[tmap_num].tmap_num)
772 goto found2; // current texture map (tmap_num) is used on current (ss) side, so try next one
773 // Current texture map (tmap_num) has not been used, assign to all faces on back_side.
774 base_seg->unique_segment::sides[back_side].tmap_num = New_segment.unique_segment::sides[tmap_num].tmap_num;
775 goto done1;
776 found2: ;
777 }
778 }
779 }
780 done1: ;
781 }
782
783 }
784
785 int fix_bogus_uvs_on_side(void)
786 {
787 med_propagate_tmaps_to_back_side(Cursegp, Curside, 1);
788 return 0;
789 }
790
791 namespace {
792
793 static void fix_bogus_uvs_on_side1(const vmsegptridx_t sp, const sidenum_t sidenum, const int uvonly_flag)
794 {
795 auto &uvls = sp->unique_segment::sides[sidenum].uvls;
796 if (uvls[side_relative_vertnum::_0].u == 0 && uvls[side_relative_vertnum::_1].u == 0 && uvls[side_relative_vertnum::_2].u == 0)
797 {
798 med_propagate_tmaps_to_back_side(sp, static_cast<sidenum_t>(sidenum), uvonly_flag);
799 }
800 }
801
802 static void fix_bogus_uvs_seg(const vmsegptridx_t segp)
803 {
804 for (const auto &&[idx, value] : enumerate(segp->children))
805 {
806 if (!IS_CHILD(value))
807 fix_bogus_uvs_on_side1(segp, static_cast<sidenum_t>(idx), 1);
808 }
809 }
810
811 }
812
813 int fix_bogus_uvs_all(void)
814 {
815 range_for (const auto &&segp, vmsegptridx)
816 {
817 if (segp->segnum != segment_none)
818 fix_bogus_uvs_seg(segp);
819 }
820 return 0;
821 }
822
823 namespace {
824
825 // -----------------------------------------------------------------------------
826 // Segment base_seg is connected through side base_side to segment con_seg on con_side.
827 // For all walls in con_seg, find the wall in base_seg which shares an edge. Copy tmap_num
828 // from that side in base_seg to the wall in con_seg. If the wall in base_seg is not present
829 // (ie, there is another segment connected through it), follow the connection through that
830 // segment to get the wall in the connected segment which shares the edge, and get tmap_num from there.
831 static void propagate_tmaps_to_segment_sides(const vcsegptridx_t base_seg, const sidenum_t base_side, const vmsegptridx_t con_seg, const sidenum_t con_side, const int uv_only_flag)
832 {
833 auto &base_vp = Side_to_verts[base_side];
834
835 // Do for each edge on connecting face.
836 for (const auto v : MAX_VERTICES_PER_SIDE)
837 {
838 const auto abs_id1 = base_seg->verts[base_vp[v]];
839 const auto abs_id2 = base_seg->verts[base_vp[next_side_vertex(v)]];
840 propagate_tmaps_to_segment_side(base_seg, base_side, con_seg, con_side, abs_id1, abs_id2, uv_only_flag);
841 }
842 }
843
844 }
845
846 // -----------------------------------------------------------------------------
847 // Propagate texture maps in base_seg to con_seg.
848 // For each wall in con_seg, find the wall in base_seg which shared an edge. Copy tmap_num from that
849 // wall in base_seg to the wall in con_seg. If the wall in base_seg is not present, then look at the
850 // segment connected through base_seg through the wall. The wall with a common edge is the new wall
851 // of interest. Continue searching in this way until a wall of interest is present.
852 void med_propagate_tmaps_to_segments(const vcsegptridx_t base_seg, const vmsegptridx_t con_seg, const int uv_only_flag)
853 {
854 for (const auto &&[idx, value] : enumerate(base_seg->children))
855 if (value == con_seg)
856 propagate_tmaps_to_segment_sides(base_seg, static_cast<sidenum_t>(idx), con_seg, find_connect_side(base_seg, con_seg), uv_only_flag);
857
858 const unique_segment &ubase = base_seg;
859 unique_segment &ucon = con_seg;
860 ucon.static_light = ubase.static_light;
861
862 validate_uv_coordinates(con_seg);
863 }
864
865
866 // -------------------------------------------------------------------------------
867 // Copy texture map uvs from srcseg to destseg.
868 // If two segments have different face structure (eg, destseg has two faces on side 3, srcseg has only 1)
869 // then assign uvs according to side vertex id, not face vertex id.
870 void copy_uvs_seg_to_seg(unique_segment &destseg, const unique_segment &srcseg)
871 {
872 range_for (const auto &&z, zip(destseg.sides, srcseg.sides))
873 {
874 auto &ds = std::get<0>(z);
875 auto &ss = std::get<1>(z);
876 ds.tmap_num = ss.tmap_num;
877 ds.tmap_num2 = ss.tmap_num2;
878 }
879
880 destseg.static_light = srcseg.static_light;
881 }
882
883 }
884
885 namespace dcx {
886
887 // _________________________________________________________________________________________________________________________
888 // Maximum distance between a segment containing light to a segment to receive light.
889 #define LIGHT_DISTANCE_THRESHOLD (F1_0*80)
890 fix Magical_light_constant = (F1_0*16);
891
892 // int Seg0, Seg1;
893
894 //int Bugseg = 27;
895
896 namespace {
897
898 struct hash_info {
899 sbyte flag;
900 fvi_hit_type hit_type;
901 vms_vector vector;
902 };
903
904 #define FVI_HASH_SIZE 8
905 #define FVI_HASH_AND_MASK (FVI_HASH_SIZE - 1)
906
907 // Note: This should be malloced.
908 // Also, the vector should not be 12 bytes, you should only care about some smaller portion of it.
909 static std::array<hash_info, FVI_HASH_SIZE> fvi_cache;
910 static int Hash_hits=0, Hash_retries=0, Hash_calcs=0;
911
912 // -----------------------------------------------------------------------------------------
913 // Set light from a light source.
914 // Light incident on a surface is defined by the light incident at its points.
915 // Light at a point = K * (V . N) / d
916 // where:
917 // K = some magical constant to make everything look good
918 // V = normalized vector from light source to point
919 // N = surface normal at point
920 // d = distance from light source to point
921 // (Note that the above equation can be simplified to K * (VV . N) / d^2 where VV = non-normalized V)
922 // Light intensity emitted from a light source is defined to be cast from four points.
923 // These four points are 1/64 of the way from the corners of the light source to the center
924 // of its segment. By assuming light is cast from these points, rather than from on the
925 // light surface itself, light will be properly cast on the light surface. Otherwise, the
926 // vector V would be the null vector.
927 // If quick_light set, then don't use find_vector_intersection
928 static void cast_light_from_side(const vmsegptridx_t segp, const sidenum_t light_side, fix light_intensity, int quick_light)
929 {
930 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
931 auto &Vertices = LevelSharedVertexState.get_vertices();
932 auto &vcvertptr = Vertices.vcptr;
933 auto &Walls = LevelUniqueWallSubsystemState.Walls;
934 auto &vcwallptr = Walls.vcptr;
935 const auto segment_center{compute_segment_center(vcvertptr, segp)};
936 // Do for four lights, one just inside each corner of side containing light.
937 range_for (const auto lightnum, Side_to_verts[light_side])
938 {
939 // fix inverse_segment_magnitude;
940
941 const auto light_vertex_num = segp->verts[lightnum];
942 auto light_location = *vcvertptr(light_vertex_num);
943
944 // New way, 5/8/95: Move towards center irrespective of size of segment.
945 const auto vector_to_center = vm_vec_normalized_quick(vm_vec_build_sub(segment_center, light_location));
946 vm_vec_add2(light_location, vector_to_center);
947
948 // -- Old way, before 5/8/95 -- // -- This way was kind of dumb. In larger segments, you move LESS towards the center.
949 // -- Old way, before 5/8/95 -- // Main problem, though, is vertices don't illuminate themselves well in oblong segments because the dot product is small.
950 // -- Old way, before 5/8/95 -- vm_vec_sub(&vector_to_center, &segment_center, &light_location);
951 // -- Old way, before 5/8/95 -- inverse_segment_magnitude = fixdiv(F1_0/5, vm_vec_mag(&vector_to_center));
952 // -- Old way, before 5/8/95 -- vm_vec_scale_add(&light_location, &light_location, &vector_to_center, inverse_segment_magnitude);
953
954 for (auto &rseg : vmsegptr)
955 {
956 fix dist_to_rseg;
957
958 range_for (auto &i, fvi_cache)
959 i.flag = 0;
960
961 // efficiency hack (I hope!), for faraway segments, don't check each point.
962 const auto r_segment_center{compute_segment_center(vcvertptr, rseg)};
963 dist_to_rseg = vm_vec_dist_quick(r_segment_center, segment_center);
964
965 if (dist_to_rseg <= LIGHT_DISTANCE_THRESHOLD) {
966 for (const auto &&[sidenum, srside, urside] : enumerate(zip(rseg.shared_segment::sides, rseg.unique_segment::sides)))
967 {
968 if (WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, rseg, static_cast<sidenum_t>(sidenum)) != wall_is_doorway_result::no_wall)
969 {
970 auto &side_normalp = srside.normals[0]; // kinda stupid? always use vector 0.
971
972 auto &sv_side = Side_to_verts[static_cast<sidenum_t>(sidenum)];
973 for (const auto vertnum : MAX_VERTICES_PER_SIDE)
974 {
975 const auto segment_relative_vert = sv_side[vertnum];
976 const auto abs_vertnum{rseg.verts[segment_relative_vert]};
977 vms_vector vert_location = *vcvertptr(abs_vertnum);
978 const fix distance_to_point = vm_vec_dist_quick(vert_location, light_location);
979 const auto vector_to_light = vm_vec_normalized(vm_vec_build_sub(light_location, vert_location));
980
981 // Hack: In oblong segments, it's possible to get a very small dot product
982 // but the light source is very nearby (eg, illuminating light itself!).
983 fix light_dot = vm_vec_build_dot(vector_to_light, side_normalp);
984 if (distance_to_point < F1_0)
985 if (light_dot > 0)
986 light_dot = (light_dot + F1_0)/2;
987
988 if (light_dot > 0) {
989 fix light_at_point = fixdiv(fixmul(light_dot, light_dot), distance_to_point);
990 light_at_point = fixmul(light_at_point, Magical_light_constant);
991 if (light_at_point >= 0) {
992 fvi_info hit_data;
993 fvi_hit_type hit_type;
994
995 const auto r_vector_to_center{vm_vec_build_sub(r_segment_center, vert_location)};
996 const auto inverse_segment_magnitude = fixdiv(F1_0/3, vm_vec_mag(r_vector_to_center));
997 const auto vert_location_1{vm_vec_scale_add(vert_location, r_vector_to_center, inverse_segment_magnitude)};
998 vert_location = vert_location_1;
999
1000 if (!quick_light) {
1001 auto hash_value = underlying_value(segment_relative_vert);
1002 hash_info *hashp = &fvi_cache[hash_value];
1003 while (1) {
1004 if (hashp->flag) {
1005 if (hashp->vector == vector_to_light)
1006 {
1007 hit_type = hashp->hit_type;
1008 Hash_hits++;
1009 break;
1010 } else {
1011 Int3(); // How is this possible? Should be no hits!
1012 Hash_retries++;
1013 hash_value = (hash_value+1) & FVI_HASH_AND_MASK;
1014 hashp = &fvi_cache[hash_value];
1015 }
1016 } else {
1017 Hash_calcs++;
1018 hashp->vector = vector_to_light;
1019 hashp->flag = 1;
1020
1021 hit_type = find_vector_intersection(fvi_query{
1022 light_location,
1023 vert_location,
1024 fvi_query::unused_ignore_obj_list,
1025 fvi_query::unused_LevelUniqueObjectState,
1026 fvi_query::unused_Robot_info,
1027 0,
1028 object_none,
1029 }, segp, 0, hit_data);
1030 hashp->hit_type = hit_type;
1031 break;
1032 }
1033 }
1034 } else
1035 hit_type = fvi_hit_type::None;
1036 switch (hit_type) {
1037 case fvi_hit_type::None:
1038 light_at_point = fixmul(light_at_point, light_intensity);
1039 urside.uvls[vertnum].l += light_at_point;
1040 if (urside.uvls[vertnum].l > F1_0)
1041 urside.uvls[vertnum].l = F1_0;
1042 break;
1043 case fvi_hit_type::Wall:
1044 break;
1045 case fvi_hit_type::Object:
1046 Int3(); // Hit object, should be ignoring objects!
1047 break;
1048 case fvi_hit_type::BadP0:
1049 Int3(); // Ugh, this thing again, what happened, what does it mean?
1050 break;
1051 }
1052 } // end if (light_at_point...
1053 } // end if (light_dot >...
1054 } // end for (vertnum=0...
1055 } // end if (rseg...
1056 } // end for (sidenum=0...
1057 } // end if (dist_to_rseg...
1058
1059 } // end for (segnum=0...
1060
1061 } // end for (lightnum=0...
1062 }
1063
1064
1065 // ------------------------------------------------------------------------------------------
1066 // Zero all lighting values.
1067 static void calim_zero_light_values(void)
1068 {
1069 range_for (unique_segment &segp, vmsegptr)
1070 {
1071 range_for (auto &side, segp.sides)
1072 {
1073 range_for (auto &uvl, side.uvls)
1074 uvl.l = F1_0/64; // Put a tiny bit of light here.
1075 }
1076 segp.static_light = F1_0 / 64;
1077 }
1078 }
1079
1080
1081 // ------------------------------------------------------------------------------------------
1082 // Used in setting average light value in a segment, cast light from a side to the center
1083 // of all segments.
1084 static void cast_light_from_side_to_center(const vmsegptridx_t segp, const sidenum_t light_side, fix light_intensity, int quick_light)
1085 {
1086 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
1087 auto &Vertices = LevelSharedVertexState.get_vertices();
1088 auto &vcvertptr = Vertices.vcptr;
1089 const auto segment_center{compute_segment_center(vcvertptr, segp)};
1090 // Do for four lights, one just inside each corner of side containing light.
1091 range_for (const auto lightnum, Side_to_verts[light_side])
1092 {
1093 const auto light_vertex_num = segp->verts[lightnum];
1094 auto &vert_light_location = *vcvertptr(light_vertex_num);
1095 const auto light_location{vm_vec_scale_add(vert_light_location, /* vector_to_center = */ vm_vec_build_sub(segment_center, vert_light_location), F1_0 / 64)};
1096
1097 for (const csmusegment rsegp : vmsegptr)
1098 {
1099 fix dist_to_rseg;
1100 //if ((segp == &Segments[Bugseg]) && (rsegp == &Segments[Bugseg]))
1101 // Int3();
1102 const auto r_segment_center{compute_segment_center(vcvertptr, rsegp)};
1103 dist_to_rseg = vm_vec_dist_quick(r_segment_center, segment_center);
1104
1105 if (dist_to_rseg <= LIGHT_DISTANCE_THRESHOLD) {
1106 fix light_at_point;
1107 if (dist_to_rseg > F1_0)
1108 light_at_point = fixdiv(Magical_light_constant, dist_to_rseg);
1109 else
1110 light_at_point = Magical_light_constant;
1111
1112 if (light_at_point >= 0) {
1113 fvi_hit_type hit_type;
1114
1115 if (!quick_light) {
1116 fvi_info hit_data;
1117
1118 hit_type = find_vector_intersection(fvi_query{
1119 light_location,
1120 r_segment_center,
1121 fvi_query::unused_ignore_obj_list,
1122 fvi_query::unused_LevelUniqueObjectState,
1123 fvi_query::unused_Robot_info,
1124 0,
1125 object_none,
1126 }, segp, 0, hit_data);
1127 }
1128 else
1129 hit_type = fvi_hit_type::None;
1130
1131 switch (hit_type) {
1132 case fvi_hit_type::None:
1133 light_at_point = fixmul(light_at_point, light_intensity);
1134 if (light_at_point >= F1_0)
1135 light_at_point = F1_0-1;
1136 {
1137 auto &static_light = rsegp.u.static_light;
1138 static_light += light_at_point;
1139 if (static_light < 0) // if it went negative, saturate
1140 static_light = 0;
1141 }
1142 break;
1143 case fvi_hit_type::Wall:
1144 break;
1145 case fvi_hit_type::Object:
1146 Int3(); // Hit object, should be ignoring objects!
1147 break;
1148 case fvi_hit_type::BadP0:
1149 Int3(); // Ugh, this thing again, what happened, what does it mean?
1150 break;
1151 }
1152 } // end if (light_at_point...
1153 } // end if (dist_to_rseg...
1154
1155 } // end for (segnum=0...
1156
1157 } // end for (lightnum=0...
1158
1159 }
1160
1161 // ------------------------------------------------------------------------------------------
1162 // Process all lights.
1163 static void calim_process_all_lights(int quick_light)
1164 {
1165 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
1166 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1167 auto &vcwallptr = Walls.vcptr;
1168 range_for (const auto &&segp, vmsegptridx)
1169 {
1170 for (const auto &&[sidenum, value] : enumerate(segp->unique_segment::sides))
1171 {
1172 if (WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, segp, static_cast<sidenum_t>(sidenum)) != wall_is_doorway_result::no_wall)
1173 {
1174 const auto sidep = &value;
1175 fix light_intensity;
1176
1177 light_intensity = TmapInfo[get_texture_index(sidep->tmap_num)].lighting + TmapInfo[get_texture_index(sidep->tmap_num2)].lighting;
1178
1179 // if (segp->sides[sidenum].wall_num != -1) {
1180 // int wall_num, bitmap_num, effect_num;
1181 // wall_num = segp->sides[sidenum].wall_num;
1182 // effect_num = Walls[wall_num].type;
1183 // bitmap_num = effects_bm_num[effect_num];
1184 //
1185 // light_intensity += TmapInfo[bitmap_num].lighting;
1186 // }
1187
1188 if (light_intensity) {
1189 light_intensity /= 4; // casting light from four spots, so divide by 4.
1190 cast_light_from_side(segp, static_cast<sidenum_t>(sidenum), light_intensity, quick_light);
1191 cast_light_from_side_to_center(segp, static_cast<sidenum_t>(sidenum), light_intensity, quick_light);
1192 }
1193 }
1194 }
1195 }
1196 }
1197
1198 // ------------------------------------------------------------------------------------------
1199 // Apply static light in mine.
1200 // First, zero all light values.
1201 // Then, for all light sources, cast their light.
1202 static void cast_all_light_in_mine(int quick_flag)
1203 {
1204 validate_segment_all(LevelSharedSegmentState);
1205 calim_zero_light_values();
1206
1207 calim_process_all_lights(quick_flag);
1208 }
1209
1210 }
1211
1212 }
1213
1214 // int Fvit_num = 1000;
1215 //
1216 // fix find_vector_intersection_test(void)
1217 // {
1218 // int i;
1219 // fvi_info hit_data;
1220 // int p0_seg, p1_seg, this_objnum, ignore_obj, check_obj_flag;
1221 // fix rad;
1222 // int start_time = timer_get_milliseconds();;
1223 // vms_vector p0,p1;
1224 //
1225 // ignore_obj = 1;
1226 // check_obj_flag = 0;
1227 // this_objnum = -1;
1228 // rad = F1_0/4;
1229 //
1230 // for (i=0; i<Fvit_num; i++) {
1231 // p0_seg = d_rand()*(Highest_segment_index+1)/32768;
1232 // compute_segment_center(&p0, &Segments[p0_seg]);
1233 //
1234 // p1_seg = d_rand()*(Highest_segment_index+1)/32768;
1235 // compute_segment_center(&p1, &Segments[p1_seg]);
1236 //
1237 // find_vector_intersection(&hit_data, &p0, p0_seg, &p1, rad, this_objnum, ignore_obj, check_obj_flag);
1238 // }
1239 //
1240 // return timer_get_milliseconds() - start_time;
1241 // }