-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlroom_converter.cpp
1604 lines (1235 loc) · 38.5 KB
/
lroom_converter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2019 Lawnjelly
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lroom_converter.h"
#include "lroom_manager.h"
#include "lportal.h"
#include "scene/3d/mesh_instance.h"
#include "core/math/quick_hull.h"
#include "ldebug.h"
#include "scene/3d/light.h"
// save typing, I am lazy
#define LMAN m_pManager
#define LROOMLIST m_pRoomList
void LRoomConverter::Convert(LRoomManager &manager, bool bVerbose, bool bPreparationRun, bool bDeleteLights, bool bSingleRoomMode)
{
m_bFinalRun = (bPreparationRun == false);
m_bDeleteLights = bDeleteLights;
m_bSingleRoomMode = bSingleRoomMode;
// This just is simply used to set how much debugging output .. more during conversion, less during running
// except when requested by explicitly clearing this flag.
Lawn::LDebug::m_bRunning = (bVerbose == false);
if (!m_bFinalRun)
{
LPRINT(5, "running convert PREPARATION RUN");
}
else
{
LPRINT(5, "running convert");
}
LMAN = &manager;
LROOMLIST = manager.GetRoomList();
// force clear all arrays
manager.ReleaseResources(true);
int count = CountRooms();
//int num_global_lights = LMAN->m_Lights.size();
// make sure bitfield is right size for number of rooms
LMAN->m_BF_visible_rooms.Create(count);
LMAN->m_LightRender.m_BF_Temp_Visible_Rooms.Create(count);
LMAN->m_Rooms.resize(count);
m_TempRooms.clear(true);
m_TempRooms.resize(count);
Convert_Rooms();
Convert_Portals();
Convert_Bounds();
// make sure manager bitfields are the correct size for number of objects
int num_sobs = LMAN->m_SOBs.size();
LPRINT(5,"Total SOBs " + itos(num_sobs));
LMAN->m_BF_caster_SOBs.Create(num_sobs);
LMAN->m_BF_visible_SOBs.Create(num_sobs);
LMAN->m_BF_master_SOBs.Create(num_sobs);
LMAN->m_BF_master_SOBs_prev.Create(num_sobs);
LMAN->m_LightRender.m_BF_Temp_SOBs.Create(num_sobs);
LMAN->m_BF_ActiveLights.Create(LMAN->m_Lights.size());
LMAN->m_BF_ActiveLights_prev.Create(LMAN->m_Lights.size());
LMAN->m_BF_ProcessedLights.Create(LMAN->m_Lights.size());
// must be done after the bitfields
Convert_Lights();
Convert_ShadowCasters();
Convert_AreaLights();
// hide all in preparation for first frame
//LMAN->ShowAll(false);
// temp rooms no longer needed
m_TempRooms.clear(true);
// clear out the local room lights, leave only global lights
//LMAN->m_Lights.resize(num_global_lights);
Lawn::LDebug::m_bRunning = true;
}
int LRoomConverter::Convert_Rooms_Recursive(Node * pParent, int count, int area)
{
for (int n=0; n<pParent->get_child_count(); n++)
{
Node * pChild = pParent->get_child(n);
if (Node_IsRoom(pChild))
{
Spatial * pSpat = Object::cast_to<Spatial>(pChild);
assert (pSpat);
Convert_Room(pSpat, count++, area);
}
else if (Node_IsArea(pChild))
{
// get the area name
String szArea = LPortal::FindNameAfter(pChild, "area_");
// find or create an area with this name
int area_child = Area_FindOrCreate(szArea);
count = Convert_Rooms_Recursive(pChild, count, area_child);
}
}
return count;
}
void LRoomConverter::Convert_Rooms()
{
LPRINT(5,"Convert_Rooms");
// allow faking a single room in single room mode
if (m_bSingleRoomMode)
{
Spatial * pSpat = Object::cast_to<Spatial>(LROOMLIST);
if (!pSpat)
return;
// add a default area
int area = Area_FindOrCreate("default");
Convert_Room(pSpat, 0, area);
return;
}
// first find all room empties and convert to LRooms
int count = 0;
int area = -1;
count = Convert_Rooms_Recursive(LROOMLIST, count, area);
}
int LRoomConverter::Area_FindOrCreate(String szName)
{
for (int n=0; n<LMAN->m_Areas.size(); n++)
{
if (LMAN->m_Areas[n].m_szName == szName)
return n;
}
// create
LArea area;
area.Create(szName);
LMAN->m_Areas.push_back(area);
return LMAN->m_Areas.size() - 1;
}
int LRoomConverter::FindRoom_ByName(String szName) const
{
for (int n=0; n<LMAN->m_Rooms.size(); n++)
{
if (LMAN->m_Rooms[n].m_szName == szName)
return n;
}
return -1;
}
void LRoomConverter::Convert_Room_SetDefaultCullMask_Recursive(Node * pParent)
{
int nChildren = pParent->get_child_count();
for (int n=0; n<nChildren; n++)
{
Node * pChild = pParent->get_child(n);
// default cull mask should always be visible to camera and lights
VisualInstance * pVI = Object::cast_to<VisualInstance>(pChild);
if (pVI)
{
// LRoom::SoftShow(pVI, LRoom::LAYER_MASK_CAMERA | LRoom::LAYER_MASK_LIGHT);
}
Convert_Room_SetDefaultCullMask_Recursive(pChild);
}
}
void LRoomConverter::Convert_Room_FindObjects_Recursive(Node * pParent, LRoom &lroom, LAABB &bb_room)
{
int nChildren = pParent->get_child_count();
for (int n=0; n<nChildren; n++)
{
Node * pChild = pParent->get_child(n);
// ignore invisible
Spatial * pSpatialChild = Object::cast_to<Spatial>(pChild);
if (pSpatialChild && (Convert_IsVisibleInRooms(pSpatialChild) == false))
{
pSpatialChild->queue_delete();
continue;
}
// we are not interested in portal meshes, as they will be deleted later in conversion
if (Node_IsPortal(pChild))
continue;
// we can optionally ignore nodes (they will still be shown / hidden with the room though)
if (Node_IsIgnore(pChild))
continue;
// not interested in bounds
if (Node_IsBound(pChild))
continue;
// lights
if (Node_IsLight(pChild))
{
LRoom_DetectedLight(lroom, pChild);
continue;
}
// area
if (Node_IsArea(pChild))
{
LRoom_DetectedArea(lroom, pChild);
continue;
}
VisualInstance * pVI = Object::cast_to<VisualInstance>(pChild);
if (pVI)
{
LPRINT(2, "\t\tFound VI : " + pVI->get_name());
// update bound to find centre of room roughly
AABB bb = pVI->get_transformed_aabb();
bb_room.ExpandToEnclose(bb);
// store some info about the static object for use at runtime
LSob sob;
sob.m_ID = pVI->get_instance_id();
sob.m_aabb = bb;
sob.Hidable_Create(pChild);
//lroom.m_SOBs.push_back(sob);
LRoom_PushBackSOB(lroom, sob);
// take away layer 0 from the sob, so it can be culled effectively
if (m_bFinalRun)
{
pVI->set_layer_mask(0);
}
}
else
{
// not visual instances
}
// does it have further children?
Convert_Room_FindObjects_Recursive(pChild, lroom, bb_room);
}
}
bool LRoomConverter::Convert_IsVisibleInRooms(const Node * pNode) const
{
const Spatial * pS = Object::cast_to<Spatial>(pNode);
const Spatial * pRoomList = Object::cast_to<Spatial>(LROOMLIST);
while (pS)
{
if (!pS->is_visible())
{
return false;
}
pS = pS->get_parent_spatial();
// terminate
if (pS == pRoomList)
return true;
}
return true;
}
// areaID could be -1 if unset
bool LRoomConverter::Convert_Room(Spatial * pNode, int lroomID, int areaID)
{
// get the room part of the name
String szFullName = pNode->get_name();
String szRoom = LPortal::FindNameAfter(pNode, "room_");
if (areaID == -1)
{
LPRINT(4, "Convert_Room : " + szFullName);
}
else
{
LPRINT(4, "Convert_Room : " + szFullName + " area_id " + itos(areaID));
}
// get a reference to the lroom we are writing to
LRoom &lroom = LMAN->m_Rooms[lroomID];
// store the godot room
lroom.m_GodotID = pNode->get_instance_id();
lroom.m_RoomID = lroomID;
// save the room ID on the godot room metadata
// This is used when registering DOBs and teleporting them with hints
// i.e. the Godot room is used to lookup the room ID of the startroom.
// LMAN->Meta_SetRoomNum(pNode, lroomID);
// create a new LRoom to exchange the children over to, and delete the original empty
lroom.m_szName = szRoom;
// area
if (areaID != -1)
lroom.m_Areas.push_back(areaID);
// keep a running bounding volume as we go through the visual instances
// to determine the overall bound of the room
LAABB bb_room;
bb_room.SetToMaxOpposite();
// set default cull masks
Convert_Room_SetDefaultCullMask_Recursive(pNode);
// recursively find statics
Convert_Room_FindObjects_Recursive(pNode, lroom, bb_room);
// store the lroom centre and bound
lroom.m_ptCentre = bb_room.FindCentre();
// bound (untested)
lroom.m_AABB.position = bb_room.m_ptMins;
lroom.m_AABB.size = bb_room.m_ptMaxs - bb_room.m_ptMins;
LPRINT(2, "\t\t\t" + String(lroom.m_szName) + " centre " + lroom.m_ptCentre);
return true;
}
bool LRoomConverter::Bound_AddPlaneIfUnique(LVector<Plane> &planes, const Plane &p)
{
for (int n=0; n<planes.size(); n++)
{
const Plane &o = planes[n];
// this is a fudge factor for how close planes can be to be considered the same ...
// to prevent ridiculous amounts of planes
const float d = 0.08f;
if (fabs(p.d - o.d) > d) continue;
float dot = p.normal.dot(o.normal);
if (dot < 0.98f) continue;
// match!
return false;
}
// test
// Vector3 va(1, 0, 0);
// Vector3 vb(1, 0.2, 0);
// vb.normalize();
// float dot = va.dot(vb);
// print("va dot vb is " + String(Variant(dot)));
// is unique
// print("\t\t\t\tAdding bound plane : " + p);
planes.push_back(p);
return true;
}
bool LRoomConverter::Convert_Bound_FromPoints(LRoom &lroom, const Vector<Vector3> &points)
{
if (points.size() > 3)
{
Geometry::MeshData md;
Error err = QuickHull::build(points, md);
if (err == OK)
{
// get the planes
for (int n=0; n<md.faces.size(); n++)
{
const Plane &p = md.faces[n].plane;
Bound_AddPlaneIfUnique(lroom.m_Bound.m_Planes, p);
}
LPRINT(2, "\t\t\tcontained " + itos(lroom.m_Bound.m_Planes.size()) + " planes.");
// make a copy of the mesh data for debugging
// note this could be avoided in release builds? NYI
lroom.m_Bound_MeshData = md;
// for (int f=0; f<md.faces.size(); f++)
// {
// String sz;
// sz = "face " + itos (f) + ", indices ";
// for (int i=0; i<md.faces[f].indices.size(); i++)
// {
// sz += itos(md.faces[f].indices[i]) + ", ";
// }
// LPRINT(2, sz);
// }
return true;
}
}
return false;
}
void LRoomConverter::GetWorldVertsFromMesh(const MeshInstance &mi, Vector<Vector3> &pts) const
{
// some godot jiggery pokery to get the mesh verts in local space
Ref<Mesh> rmesh = mi.get_mesh();
Array arrays = rmesh->surface_get_arrays(0);
// possible to have a meshinstance with no geometry .. don't want to crash
if (!arrays.size())
{
WARN_PRINT_ONCE("Warning : LRoomConverter::GetWorldVertsFromMesh MeshInstance with no mesh, ignoring");
return;
}
PoolVector<Vector3> p_vertices = arrays[VS::ARRAY_VERTEX];
// convert to world space
Transform trans = mi.get_global_transform();
for (int n=0; n<p_vertices.size(); n++)
{
Vector3 ptWorld = trans.xform(p_vertices[n]);
pts.push_back(ptWorld);
}
}
bool LRoomConverter::Convert_ManualBound(LRoom &lroom, MeshInstance * pMI)
{
LPRINT(2, "\tCONVERT_MANUAL_BOUND : '" + pMI->get_name() + "' for room '" + lroom.get_name() + "'");
Vector<Vector3> points;
GetWorldVertsFromMesh(*pMI, points);
for (int n=0; n<points.size(); n++)
{
// expand the room AABB to make sure it encompasses the bound
lroom.m_AABB.expand_to(points[n]);
}
return Convert_Bound_FromPoints(lroom, points);
}
// hide all in preparation for first frame
//void LRoomConverter::Convert_HideAll()
//{
// for (int n=0; n<LMAN->m_SOBs.size(); n++)
// {
// LSob &sob = LMAN->m_SOBs[n];
// sob.Show(false);
// }
// // hide all lights that are non global
// for (int n=0; n<LMAN->m_Lights.size(); n++)
// {
// LLight &light = LMAN->m_Lights[n];
// if (!light.IsGlobal())
// light.Show(false);
// }
//}
void LRoomConverter::Convert_AreaLights()
{
// list the rooms in each area
for (int a=0; a<LMAN->m_Areas.size(); a++)
{
LArea &area = LMAN->m_Areas[a];
// add every room in this area to the light affected rooms list
for (int r=0; r<LMAN->m_Rooms.size(); r++)
{
LRoom &room = LMAN->m_Rooms[r];
if (room.IsInArea(a))
{
// add the room to the area room list
if (area.m_iNumRooms == 0)
area.m_iFirstRoom = LMAN->m_AreaRooms.size();
area.m_iNumRooms += 1;
LMAN->m_AreaRooms.push_back(r);
}
}
}
// first identify which lights are area lights, and match area strings to area IDs
for (int n=0; n<LMAN->m_Lights.size(); n++)
{
LLight &l = LMAN->m_Lights[n];
// global area light?
if (!l.m_Source.IsGlobal())
continue;
assert (l.m_iArea == -1);
// match area string to area
// find the area
for (int n=0; n<LMAN->m_Areas.size(); n++)
{
if (LMAN->m_Areas[n].m_szName == l.m_szArea)
{
l.m_iArea = n;
break;
}
}
// area not found?
if (l.m_iArea == -1)
{
LWARN(2, "Convert_AreaLights area not found : " + l.m_szArea);
}
else
{
LPRINT(5,"Area light " + itos (n) + " area " + l.m_szArea + " found area_id " + itos(l.m_iArea));
}
}
// add each light within an area to the area light list
for (int a=0; a<LMAN->m_Areas.size(); a++)
{
LArea &area = LMAN->m_Areas[a];
for (int n=0; n<LMAN->m_Lights.size(); n++)
{
LLight &l = LMAN->m_Lights[n];
int areaID = l.m_iArea;
if (areaID != a)
continue;
// this light affects this area
if (area.m_iNumLights == 0)
area.m_iFirstLight = LMAN->m_AreaLights.size();
LMAN->m_AreaLights.push_back(n);
area.m_iNumLights++;
} // for n
} // for a
// for each global light we can calculate the affected rooms
for (int n=0; n<LMAN->m_Lights.size(); n++)
{
LLight &l = LMAN->m_Lights[n];
int areaID = l.m_iArea;
// not a global light
if (areaID == -1)
continue;
LPRINT(5,"Area light " + itos (n) + " affected rooms:");
// add every room in this area to the light affected rooms list
for (int r=0; r<LMAN->m_Rooms.size(); r++)
{
LRoom &room = LMAN->m_Rooms[r];
if (room.IsInArea(areaID))
{
//l.AddAffectedRoom(r); // no need as this is now done by area
LPRINT(5,"\t" + itos (r));
// store the global lights on the room
room.m_GlobalLights.push_back(n);
}
}
}
}
void LRoomConverter::Convert_Lights()
{
// trace local lights out from rooms and add to each room the light affects
for (int n=0; n<LMAN->m_Lights.size(); n++)
{
LLight &l = LMAN->m_Lights[n];
if (l.m_Source.IsGlobal())
continue; // ignore globals .. affect all rooms
Light_Trace(n);
}
}
void LRoomConverter::Light_Trace(int iLightID)
{
// get the light
LLight &l = LMAN->m_Lights[iLightID];
LPRINT(5,"_____________________________________________________________");
LPRINT(5,"\nLight_Trace " + itos (iLightID));
LMAN->m_Trace.Trace_Light(*LMAN, l, LTrace::LR_CONVERT);
// now save the data from the trace
LRoomManager::LLightRender &lr = LMAN->m_LightRender;
// visible rooms
for (int n=0; n<lr.m_Temp_Visible_Rooms.size(); n++)
{
int room_id = lr.m_Temp_Visible_Rooms[n];
LRoom &room = *LMAN->GetRoom(room_id);
room.AddLocalLight(iLightID);
// store the affected room on the light
l.AddAffectedRoom(room_id);
}
// sobs
for (int n=0; n<lr.m_Temp_Visible_SOBs.size(); n++)
{
int sob_id = lr.m_Temp_Visible_SOBs[n];
// first?
if (!l.m_NumCasters)
l.m_FirstCaster = LMAN->m_LightCasters_SOB.size();
LMAN->m_LightCasters_SOB.push_back(sob_id);
l.m_NumCasters++;
}
LPRINT(5, itos(lr.m_Temp_Visible_Rooms.size()) + " visible rooms, " + itos (lr.m_Temp_Visible_SOBs.size()) + " visible SOBs.\n");
/*
// blank this each time as it is used to create the list of casters
LMAN->m_BF_caster_SOBs.Blank();
// reset the planes pool for each render out from the source room
LMAN->m_Pool.Reset();
// the first set of planes are blank
unsigned int pool_member = LMAN->m_Pool.Request();
assert (pool_member != -1);
LVector<Plane> &planes = LMAN->m_Pool.Get(pool_member);
planes.clear();
Lawn::LDebug::m_iTabDepth = 0;
Light_TraceRecursive(0, LMAN->m_Rooms[l.m_Source.m_RoomID], l, iLightID, planes);
*/
}
/*
void LRoomConverter::Light_TraceRecursive(int depth, LRoom &lroom, LLight &light, int iLightID, const LVector<Plane> &planes)
{
// prevent too much depth
if (depth > 8)
{
LPRINT_RUN(2, "\t\t\tLight_TraceRecursive DEPTH LIMIT REACHED");
return;
}
Lawn::LDebug::m_iTabDepth = depth;
LPRINT_RUN(2, "ROOM " + lroom.get_name() + " affected by local light");
// add to the local lights affecting this room
// already in list?
bool bAlreadyInList = false;
for (int n=0; n<lroom.m_LocalLights.size(); n++)
{
if (lroom.m_LocalLights[n] == iLightID)
{
bAlreadyInList = true;
break;
}
}
// add to local lights if not already in list
if (!bAlreadyInList)
{
lroom.m_LocalLights.push_back(iLightID);
// store the affected room on the light
light.AddAffectedRoom(lroom.m_RoomID);
}
// add each light caster that is within the planes to the light caster list
// clip all objects in this room to the clipping planes
int last_sob = lroom.m_iFirstSOB + lroom.m_iNumSOBs;
for (int n=lroom.m_iFirstSOB; n<last_sob; n++)
{
LSob &sob = LMAN->m_SOBs[n];
//LPRINT_RUN(2, "sob " + itos(n) + " " + sob.GetSpatial()->get_name());
// already determined to be visible through another portal
// if (LMAN->m_BF_caster_SOBs.GetBit(n))
// {
// //LPRINT_RUN(2, "\talready visible");
// continue;
// }
bool bShow = true;
// estimate the radius .. for now
const AABB &bb = sob.m_aabb;
// print("\t\t\tculling object " + pObj->get_name());
for (int p=0; p<planes.size(); p++)
{
// float dist = planes[p].distance_to(pt);
// print("\t\t\t\t" + itos(p) + " : dist " + String(Variant(dist)));
float r_min, r_max;
bb.project_range_in_plane(planes[p], r_min, r_max);
// print("\t\t\t\t" + itos(p) + " : r_min " + String(Variant(r_min)) + ", r_max " + String(Variant(r_max)));
if (r_min > 0.0f)
{
bShow = false;
break;
}
}
if (bShow)
{
Light_AddCaster_SOB(light, n);
}
} // for through sobs
// look through every portal out
for (int n=0; n<lroom.m_iNumPortals; n++)
{
int portalID = lroom.m_iFirstPortal + n;
const LPortal &port = LMAN->m_Portals[portalID];
LPRINT_RUN(2, "\tPORTAL " + itos (n) + " (" + itos(portalID) + ") " + port.get_name() + " normal " + port.m_Plane.normal);
float dot = port.m_Plane.normal.dot(light.m_Source.m_ptDir);
if (dot <= 0.0f)
{
LPRINT_RUN(2, "\t\tCULLED (wrong direction)");
continue;
}
// is it culled by the planes?
LPortal::eClipResult overall_res = LPortal::eClipResult::CLIP_INSIDE;
// cull portal with planes
for (int l=0; l<planes.size(); l++)
{
LPortal::eClipResult res = port.ClipWithPlane(planes[l]);
switch (res)
{
case LPortal::eClipResult::CLIP_OUTSIDE:
overall_res = res;
break;
case LPortal::eClipResult::CLIP_PARTIAL:
overall_res = res;
break;
default: // suppress warning
break;
}
if (overall_res == LPortal::eClipResult::CLIP_OUTSIDE)
break;
}
// this portal is culled
if (overall_res == LPortal::eClipResult::CLIP_OUTSIDE)
{
LPRINT_RUN(2, "\t\tCULLED (outside planes)");
continue;
}
LRoom &linked_room = LMAN->Portal_GetLinkedRoom(port);
// recurse into that portal
unsigned int uiPoolMem = LMAN->m_Pool.Request();
if (uiPoolMem != -1)
{
// get a vector of planes from the pool
LVector<Plane> &new_planes = LMAN->m_Pool.Get(uiPoolMem);
// copy the existing planes
new_planes.copy_from(planes);
// add the planes for the portal
port.AddLightPlanes(*LMAN, light, new_planes, false);
Light_TraceRecursive(depth + 1, linked_room, light, iLightID, new_planes);
// for debugging need to reset tab depth
Lawn::LDebug::m_iTabDepth = depth;
// we no longer need these planes
LMAN->m_Pool.Free(uiPoolMem);
}
else
{
// planes pool is empty!
// This will happen if the view goes through shedloads of portals
// The solution is either to increase the plane pool size, or build levels
// with views through multiple portals. Looking through multiple portals is likely to be
// slow anyway because of the number of planes to test.
WARN_PRINT_ONCE("LRoom_FindShadowCasters_Recursive : Planes pool is empty");
}
}
}
*/
void LRoomConverter::Convert_ShadowCasters()
{
int nLights = LMAN->m_Lights.size();
LPRINT(5,"\nConvert_ShadowCasters ... numlights " + itos (nLights));
for (int l=0; l<nLights; l++)
{
const LLight &light = LMAN->m_Lights[l];
String sz = "Light " + itos (l);
if (light.m_Source.IsGlobal())
sz += " GLOBAL";
else
sz += " LOCAL from room " + itos(light.m_Source.m_RoomID);
LPRINT(5, sz + " direction " + light.m_Source.m_ptDir);
for (int n=0; n<LMAN->m_Rooms.size(); n++)
{
LRoom &lroom = LMAN->m_Rooms[n];
// global lights affect every room
bool bAffectsRoom = false; // true
// if the light is local, does it affect this room?
if (!light.m_Source.IsGlobal())
{
// a local light .. does it affect this room?
bAffectsRoom = false;
for (int i=0; i<lroom.m_LocalLights.size(); i++)
{
// if the light id is found among the local lights for this room
if (lroom.m_LocalLights[i] == l)
{
bAffectsRoom = true;
break;
}
}
}
if (bAffectsRoom)
{
LPRINT(2,"\n\tAFFECTS room " + itos(n) + ", " + lroom.get_name());
LRoom_FindShadowCasters_FromLight(lroom, light);
//LRoom_FindShadowCasters(lroom, l, light);
}
}
}
}
void LRoomConverter::Convert_Bounds()
{
for (int n=0; n<LMAN->m_Rooms.size(); n++)
{
LRoom &lroom = LMAN->m_Rooms[n];
//print("DetectBounds from room " + lroom.get_name());
Spatial * pGRoom = lroom.GetGodotRoom();
assert (pGRoom);
for (int n=0; n<pGRoom->get_child_count(); n++)
{
Node * pChild = pGRoom->get_child(n);
if (Node_IsBound(pChild))
{
MeshInstance * pMesh = Object::cast_to<MeshInstance>(pChild);
assert (pMesh);
Convert_ManualBound(lroom, pMesh);
// delete the mesh
pGRoom->remove_child(pChild);
pChild->queue_delete();
break;
}
}
// if no manual bound is found, we will create one by using qhull on all the points
if (!lroom.m_Bound.IsActive())
{
Vector<Vector3> pts;
Bound_FindPoints_Recursive(pGRoom, pts);
LPRINT(2, "\tCONVERT_AUTO_BOUND room : '" + lroom.get_name() + "' (" + itos(pts.size()) + " verts)");
// use qhull
Convert_Bound_FromPoints(lroom, pts);
}
}
}
void LRoomConverter::Bound_FindPoints_Recursive(Node * pNode, Vector<Vector3> &pts)
{
// is it a mesh instance?
MeshInstance * pMI = Object::cast_to<MeshInstance>(pNode);
if (pMI)
{
// get the points in world space
GetWorldVertsFromMesh(*pMI, pts);
}
for (int n=0; n<pNode->get_child_count(); n++)
{
Node * pChild = pNode->get_child(n);
Bound_FindPoints_Recursive(pChild, pts);
}
}
void LRoomConverter::Convert_Portals()
{
for (int pass=0; pass<3; pass++)
{
LPRINT(2, "Convert_Portals pass " + itos(pass));
LPRINT(2, "");
for (int n=0; n<LMAN->m_Rooms.size(); n++)
{
LRoom &lroom = LMAN->m_Rooms[n];
LTempRoom &troom = m_TempRooms[n];
switch (pass)
{
case 0:
LRoom_DetectPortalMeshes(lroom, troom);
break;
case 1:
LRoom_MakePortalsTwoWay(lroom, troom, n);
break;
case 2:
LRoom_MakePortalFinalList(lroom, troom);
break;
}
}