-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlroom_manager.cpp
2268 lines (1745 loc) · 53.8 KB
/
lroom_manager.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_manager.h"
#include "core/engine.h"
#include "scene/3d/camera.h"
#include "scene/3d/mesh_instance.h"
#include "lroom_converter.h"
#include "ldebug.h"
#include "scene/3d/immediate_geometry.h"
#include "scene/3d/light.h"
#include "scene/3d/baked_lightmap.h"
#include "lroom.h"
#include "lhelper.h"
#include "lscene_saver.h"
#include "ldae_exporter.h"
#define LROOMLIST m_pRoomList
#define CHECK_ROOM_LIST if (!CheckRoomList())\
{\
WARN_PRINT_ONCE("rooms is unset");\
return false;\
}
LRoomManager::LRoomManager()
{
// m_ID_camera = 0;
m_DOB_id_camera = -1;
m_ID_DebugPlanes = 0;
m_ID_DebugBounds = 0;
m_ID_DebugLights = 0;
m_ID_DebugLightVolumes = 0;
m_ID_DebugFrustums = 0;
m_ID_RoomList = 0;
m_uiFrameCounter = 0;
m_iLoggingLevel = 2;
m_bActive = true;
m_bFrustumOnly = false;
m_bPortalPlane_Convention = false;
// to know which rooms to hide we keep track of which were shown this, and the previous frame
m_pCurr_VisibleRoomList = &m_VisibleRoomList_A;
m_pPrev_VisibleRoomList = &m_VisibleRoomList_B;
m_bDebugPlanes = false;
m_bDebugBounds = false;
m_bDebugLights = false;
m_bDebugLightVolumes = false;
m_bDebugFrustums = false;
m_bDebugFrameString = false;
m_pRoomList = 0;
// loses detail at approx .. 0.00001
// dot 0.9999
// however, this will depend on the map so we will use some more approximate figures, and allow
// the user to change in rare cases where this fails
m_fLightmapUnMerge_ThresholdDist = 0.001f;
m_fLightmapUnMerge_ThresholdDot = 0.99f;
if (!Engine::get_singleton()->is_editor_hint())
{
CreateDebug();
}
}
int LRoomManager::FindClosestRoom(const Vector3 &pt) const
{
//print_line("FindClosestRoom");
int closest = -1;
float closest_dist = FLT_MAX;
// uses bounds if this is available
int closest_within = -1;
float within_dist = FLT_MAX;
for (int n=0; n<m_Rooms.size(); n++)
{
const LRoom &lroom = m_Rooms[n];
float d = pt.distance_squared_to(lroom.m_ptCentre);
if (d < closest_dist)
{
closest = n;
closest_dist = d;
}
// is there a bound?
if (lroom.m_Bound.IsActive())
{
// is it within the aabb?
if (lroom.m_AABB.has_point(pt))
{
// is it within the convex hull?
float dist = lroom.m_Bound.GetSmallestPenetrationDistance(pt);
// find the lowest within distance of the nearby room convex hulls
if (dist < within_dist)
{
closest_within = n;
within_dist = dist;
}
}
}
}
// some logic whether to use the hulls or the closest dist
if (within_dist < 1.0f)
{
return closest_within;
}
return closest;
}
const LRoom * LRoomManager::GetRoom(int i) const
{
if ((unsigned int) i >= (unsigned int) m_Rooms.size())
{
WARN_PRINT_ONCE("LRoomManager::GetRoom out of range");
return 0;
}
return &m_Rooms[i];
}
LRoom * LRoomManager::GetRoom(int i)
{
if ((unsigned int) i >= (unsigned int) m_Rooms.size())
{
WARN_PRINT_ONCE("LRoomManager::GetRoom out of range");
return 0;
}
return &m_Rooms[i];
}
LRoom &LRoomManager::Portal_GetLinkedRoom(const LPortal &port)
{
return m_Rooms[port.m_iRoomNum];
}
// for lights we store the light ID in the metadata
void LRoomManager::Meta_SetLightID(Node * pNode, int id)
{
pNode->set_meta("_llight", id);
}
int LRoomManager::Meta_GetLightID(Node * pNode) const
{
//assert (pNode->has_meta("_lroom"));
Variant v = pNode->get_meta("_llight");
if (v.get_type() == Variant::NIL)
return -1;
return v;
}
/*
CANT BE TRUSTED
void LRoomManager::Meta_SetRoomNum(Node * pNode, int num)
{
pNode->set_meta("_lroom", num);
assert (Meta_GetRoomNum(pNode) == num);
}
int LRoomManager::Meta_GetRoomNum(Node * pNode) const
{
//assert (pNode->has_meta("_lroom"));
Variant v = pNode->get_meta("_lroom");
if (v.get_type() == Variant::NIL)
return -1;
return v;
}
*/
/*
// register but let LPortal know which room the dob should start in
bool LRoomManager::dob_register_hint(Node * pDOB, float radius, Node * pRoom)
{
CHECK_ROOM_LIST
if (!pDOB)
{
WARN_PRINT_ONCE("dob_register_hint : pDOB is NULL");
return false;
}
LPRINT(3, "dob_register_hint " + pDOB->get_name());
if (!pRoom)
{
WARN_PRINT_ONCE("dob_register_hint : pRoom is NULL");
return false;
}
int iRoom = Meta_GetRoomNum(pRoom);
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
if (!pSpat)
{
WARN_PRINT_ONCE("dob_register_hint : DOB is not a spatial");
return false;
}
return DobRegister(pSpat, radius, iRoom);
}
*/
void LRoomManager::CreateDebug()
{
ImmediateGeometry * p = memnew(ImmediateGeometry);
p->set_name("debug_planes");
add_child(p);
move_child(p, get_child_count()-1);
m_ID_DebugPlanes = p->get_instance_id();
// m_mat_Debug_Planes->set_as_toplevel(true);
m_mat_Debug_Planes = Ref<SpatialMaterial>(memnew(SpatialMaterial));
m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
// m_mat_Debug_Planes->set_line_width(6.0);
// m_mat_Debug_Planes->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
m_mat_Debug_Planes->set_albedo(Color(1, 0, 1, 1));
p->set_material_override(m_mat_Debug_Planes);
p->hide();
ImmediateGeometry * b = memnew(ImmediateGeometry);
b->set_name("debug_bounds");
add_child(b);
move_child(b, get_child_count()-1);
m_ID_DebugBounds = b->get_instance_id();
m_mat_Debug_Bounds = Ref<SpatialMaterial>(memnew(SpatialMaterial));
//m_mat_Debug_Bounds->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
m_mat_Debug_Bounds->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
m_mat_Debug_Bounds->set_cull_mode(SpatialMaterial::CULL_DISABLED);
m_mat_Debug_Bounds->set_albedo(Color(0, 0, 1, 0.4));
b->set_material_override(m_mat_Debug_Bounds);
b->hide();
{
ImmediateGeometry * b = memnew(ImmediateGeometry);
b->set_name("debug_lights");
add_child(b);
move_child(b, get_child_count()-1);
m_ID_DebugLights = b->get_instance_id();
b->set_material_override(m_mat_Debug_Bounds);
b->hide();
}
{
ImmediateGeometry * p = memnew(ImmediateGeometry);
p->set_name("debug_lightvolumes");
add_child(p);
move_child(p, get_child_count()-1);
m_ID_DebugLightVolumes = p->get_instance_id();
// m_mat_Debug_Planes->set_as_toplevel(true);
m_mat_Debug_LightVolumes = Ref<SpatialMaterial>(memnew(SpatialMaterial));
m_mat_Debug_LightVolumes->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
// m_mat_Debug_Planes->set_line_width(6.0);
// m_mat_Debug_Planes->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
m_mat_Debug_LightVolumes->set_albedo(Color(0, 1, 1, 1));
p->set_material_override(m_mat_Debug_LightVolumes);
p->hide();
}
{
ImmediateGeometry * p = memnew(ImmediateGeometry);
p->set_name("debug_frustums");
add_child(p);
move_child(p, get_child_count()-1);
m_ID_DebugFrustums = p->get_instance_id();
// m_mat_Debug_Planes->set_as_toplevel(true);
m_mat_Debug_Frustums = Ref<SpatialMaterial>(memnew(SpatialMaterial));
m_mat_Debug_Frustums->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
// m_mat_Debug_Planes->set_line_width(6.0);
// m_mat_Debug_Planes->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
// m_mat_Debug_Planes->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
m_mat_Debug_Frustums->set_albedo(Color(1, 0, 0, 1));
p->set_material_override(m_mat_Debug_Frustums);
p->hide();
}
}
ObjectID LRoomManager::DobRegister_FindVIRecursive(Node * pNode) const
{
// is the node a VI?
VisualInstance * pVI = Object::cast_to<VisualInstance>(pNode);
if (pVI)
{
// take away layer 0 from the dob, so it can be culled effectively
pVI->set_layer_mask(0);
return pVI->get_instance_id();
}
// try the children
for (int n=0; n<pNode->get_child_count(); n++)
{
ObjectID res = DobRegister_FindVIRecursive(pNode->get_child(n));
if (res)
return res;
}
return 0;
}
int LRoomManager::DobRegister(Spatial * pDOB, const Vector3 &pos, float radius, int iRoom)
{
//LPRINT(3, "register_dob " + pDOB->get_name());
if (iRoom == -1)
{
WARN_PRINT_ONCE("LRoomManager::DobRegister : room ID is -1");
return false;
}
int did = m_DobList.Request();
LDob &dob = m_DobList.GetDob(did);
dob.m_iRoomID = iRoom;
dob.m_ID_Spatial = pDOB->get_instance_id();
dob.m_fRadius = radius;
// LRoom * pRoom = GetRoom(iRoom);
// if (!pRoom)
// return false;
// The dob is derived from spatial, but the visual instances may be children of the dob
// rather than the node itself .. we need visual instances for layer culling for shadows
// LDob dob;
// dob.m_ID_Spatial = pDOB->get_instance_id();
// dob.m_fRadius = radius;
dob.m_ID_VI = DobRegister_FindVIRecursive(pDOB);
// pRoom->DOB_Add(dob);
// save the room ID on the dob metadata
// Meta_SetRoomNum(pDOB, iRoom);
#ifdef LPORTAL_DOBS_NO_SOFTSHOW
VisualInstance * pVI = dob.GetVI();
if (pVI)
{
uint32_t mask = 0;
mask = LRoom::LAYER_MASK_CAMERA | LRoom::LAYER_MASK_LIGHT;
LRoom::SoftShow(pVI, mask);
}
#endif
// change visibility
m_DobList.UpdateDob(*this, did, pos);
// DobUpdateVisibility(pDOB, pRoom);
return did;
}
int LRoomManager::dob_register(Node * pDOB, const Vector3 &pos, float radius)
{
CHECK_ROOM_LIST
if (!pDOB)
{
WARN_PRINT_ONCE("dob_register : pDOB is NULL");
return -1;
}
LPRINT(3, "dob_register " + pDOB->get_name() + " instance ID " + itos(pDOB->get_instance_id()));
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
if (!pSpat)
{
WARN_PRINT_ONCE("dob_register : DOB is not a spatial");
return -1;
}
// Vector3 pt = pSpat->get_global_transform().origin;
int iRoomNum = FindClosestRoom(pos);
LPRINT(2, "dob_register closest room " + itos(iRoomNum));
return DobRegister(pSpat, pos, radius, iRoomNum);
}
/*
// where pRoom is current room
int LRoomManager::DobUpdate(Spatial * pDOB_Spatial, LRoom * pRoom)
{
LRoom * pNewRoom = pRoom->DOB_Update(*this, pDOB_Spatial);
// // update visibility
// if (pNewRoom)
// DobUpdateVisibility(pSpat, pNewRoom);
// else
// DobUpdateVisibility(pSpat, pRoom);
if (pNewRoom)
{
// remove from the list in old room and add to list in new room, and change the metadata
int iRoomNum = pNewRoom->m_RoomID;
// get dob data to move to new room
//unsigned int uidob_instance_id = pDOB->get_instance_id();
unsigned int dob_id = pRoom->DOB_Find(pDOB_Spatial);
// if (dob_id == -1)
// {
// WARN_PRINT_ONCE("DOB is not found in room");
// return -1;
// }
assert (dob_id != -1);
print_line("dob " + itos(dob_id) + " entering room " + pNewRoom->get_name());
// copy across data before removing
const LDob &data = pRoom->DOB_Get(dob_id);
pNewRoom->DOB_Add(data);
// remove from old room
pRoom->DOB_Remove(dob_id);
// change visibility
DobUpdateVisibility(pDOB_Spatial, pNewRoom);
// save the room ID on the dob metadata
Meta_SetRoomNum(pDOB_Spatial, iRoomNum);
// new room number
return iRoomNum;
}
//#ifdef LDEBUG_DOB_VISIBILITY
//#pragma message ("LPortal LDEBUG_DOB_VISIBILITY, dobs will flicker when hidden")
// bool bVis = pRoom->IsVisible();
// bool bSpatVisible = pSpat->is_visible_in_tree();
// if (bVis != bSpatVisible)
// {
// WARN_PRINT("DOB visibility incorrect");
// }
// bool bShow = true;
// if (!bVis)
// {
// if ((Engine::get_singleton()->get_frames_drawn() % 2) == 0)
// {
// pSpat->show();
// }
// else
// {
// pSpat->hide();
// }
// }
// else
// {
// pSpat->show();
// }
//#endif
// still in the same room
return pRoom->m_RoomID;
}
*/
int LRoomManager::dob_update(int dob_id, const Vector3 &pos)
{
#ifdef LPORTAL_DOBS_AUTO_UPDATE
return -1;
#endif
return m_DobList.UpdateDob(*this, dob_id, pos);
// find the room the object is attached to
// LRoom * pRoom = GetRoomFromDOB(pDOB);
// if (!pRoom)
// return -1;
// Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
// if (!pSpat)
// return -1;
// return DobUpdate(pSpat, pRoom);
}
/*
bool LRoomManager::dob_teleport_hint(Node * pDOB, Node * pRoom)
{
CHECK_ROOM_LIST
if (!pDOB)
{
WARN_PRINT_ONCE("dob_teleport_hint : pDOB is NULL");
return false;
}
LPRINT(1, "dob_teleport_hint " + pDOB->get_name());
if (!pRoom)
{
WARN_PRINT_ONCE("dob_teleport_hint : pRoom is NULL");
return false;
}
int iRoom = Meta_GetRoomNum(pRoom);
Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
if (!pSpat)
{
WARN_PRINT_ONCE("dob_teleport_hint : DOB is not a spatial");
return false;
}
return DobTeleport(pSpat, iRoom);
}
*/
/*
bool LRoomManager::DobTeleport(Spatial * pDOB, int iNewRoomID)
{
LPRINT(5, "teleporting " + pDOB->get_name() + " to room " + itos(iNewRoomID));
// old room
LRoom * pOldRoom = GetRoomFromDOB(pDOB);
if (!pOldRoom)
{
WARN_PRINT_ONCE("LRoomManager::DobTeleport : pOldRoom is NULL");
return false;
}
if (iNewRoomID == -1)
{
WARN_PRINT_ONCE("LRoomManager::DobTeleport : iNewRoomID is -1");
return false;
}
LRoom * pNewRoom = GetRoom(iNewRoomID);
if (!pNewRoom)
return false;
// special case, if teleporting within the same room, no op
// (if we do try and move from same room to same room we get data corruption because of
// const ref)
if (pOldRoom == pNewRoom)
return true;
// detach from old room, add to new room
// get dob data to move to new room
unsigned int dob_id = pOldRoom->DOB_Find(pDOB);
assert (dob_id != -1);
// copy across data before removing
const LDob &data = pOldRoom->DOB_Get(dob_id);
pNewRoom->DOB_Add(data);
// remove from old room
pOldRoom->DOB_Remove(dob_id);
// save the room ID on the dob metadata
Meta_SetRoomNum(pDOB, iNewRoomID);
// change visibility
DobUpdateVisibility(pDOB, pNewRoom);
return true;
}
*/
// not tested...
/*
bool LRoomManager::dob_teleport(Node * pDOB)
{
CHECK_ROOM_LIST
return true;
// Spatial * pSpat = Object::cast_to<Spatial>(pDOB);
// if (!pSpat)
// return false;
// Vector3 pt = pSpat->get_global_transform().origin;
// int iRoomNum = FindClosestRoom(pt);
// //print_line("dob_teleport closest room " + itos(iRoomNum));
// if (iRoomNum == -1)
// return false;
// return DobTeleport(pSpat, iRoomNum);
}
*/
bool LRoomManager::dob_unregister(int dob_id)
{
CHECK_ROOM_LIST
// LRoom * pRoom = GetRoomFromDOB(pDOB);
// // change the meta data on the DOB .. this will catch trying to update an unregistered DOB
// Meta_SetRoomNum(pDOB, -2);
// if (pRoom)
// {
// unsigned int dob_id = pRoom->DOB_Find(pDOB);
// return pRoom->DOB_Remove(dob_id);
// }
m_DobList.DeleteDob(dob_id);
return true;
}
void LRoomManager::Light_FrameProcess(int lightID)
{
if (!m_BF_ProcessedLights.GetBit(lightID))
{
m_BF_ProcessedLights.SetBit(lightID, true);
// some lights may be processed but found not to intersect the camera frustum
if (Light_FindCasters(lightID))
{
m_BF_ActiveLights.SetBit(lightID, true);
m_ActiveLights.push_back(lightID);
}
}
}
// now we are centralizing the tracing out from static and dynamic lights for each frame to this function
// returns false if the entire light should be culled
bool LRoomManager::Light_FindCasters(int lightID)
{
// add all shadow casters for this light (new method)
const LLight &light = m_Lights[lightID];
/*
if (light.m_eClass == LLight::LT_STATIC)
{
int last_caster = light.m_FirstCaster + light.m_NumCasters;
for (int c=light.m_FirstCaster; c<last_caster; c++)
{
int sobID = m_LightCasters_SOB[c];
// only add to the caster list if not in it already (does this check need to happen, can this ever occur?)
if (!m_BF_caster_SOBs.GetBit(sobID))
{
LPRINT_RUN(2, "\t" + itos(sobID) + ", " + m_SOBs[sobID].GetSpatial()->get_name());
m_BF_caster_SOBs.SetBit(sobID, true);
m_CasterList_SOBs.push_back(sobID);
}
else
{
//LPRINT(2, "\t" + itos(sobID) + ", ALREADY CASTER " + manager.m_SOBs[sobID].GetSpatial()->get_name());
}
} // for c through caster
} // static lights have a list of SOB casters
*/
// special case of global area lights
if (light.m_iArea != -1)
{
// special trace for area light
if (m_Trace.Trace_Light(*this, light, LTrace::LR_ALL) == false)
return false;
}
else
{
// can only deal with lights in rooms for now
if (light.m_Source.m_RoomID == -1)
{
return true;
}
LRoom * pRoom = GetRoom(light.m_Source.m_RoomID);
if (!pRoom)
return true;
if (m_Trace.Trace_Light(*this, light, LTrace::LR_ALL) == false)
return false;
} // non-area light
/*
// we now need to trace either just DOBs (in the case of static lights)
// or SOBs and DOBs (in the case of dynamic lights)
m_LightRender.m_BF_Temp_SOBs.Blank();
m_LightRender.m_Temp_Visible_SOBs.clear();
const LSource &cam = light.m_Source;
// cam.Source_SetDefaults();
// cam.m_ptPos = light.m_ptPos;
// cam.m_ptDir = light.m_ptDir;
m_Trace.Trace_Prepare(*this, cam, m_LightRender.m_BF_Temp_SOBs, m_BF_visible_rooms, m_LightRender.m_Temp_Visible_SOBs, *m_pCurr_VisibleRoomList);
unsigned int pool_member = m_Pool.Request();
assert (pool_member != -1);
LVector<Plane> &planes = m_Pool.Get(pool_member);
planes.clear();
// create subset planes of light frustum and camera frustum
m_MainCamera.AddCameraLightPlanes(*this, cam, planes);
m_Trace.Trace_Begin(*pRoom, planes);
// we no longer need these planes
m_Pool.Free(pool_member);
*/
// process the sobs that were visible
for (int n=0; n<m_LightRender.m_Temp_Visible_SOBs.size(); n++)
{
int sobID = m_LightRender.m_Temp_Visible_SOBs[n];
// only add to the caster list if not in it already (does this check need to happen, can this ever occur?)
if (!m_BF_caster_SOBs.GetBit(sobID))
{
LPRINT_RUN(2, "\t" + itos(sobID) + ", " + m_SOBs[sobID].GetSpatial()->get_name());
m_BF_caster_SOBs.SetBit(sobID, true);
m_CasterList_SOBs.push_back(sobID);
}
else
{
//LPRINT(2, "\t" + itos(sobID) + ", ALREADY CASTER " + manager.m_SOBs[sobID].GetSpatial()->get_name());
}
}
return true;
}
void LRoomManager::Light_UpdateTransform(LLight &light, const Light &glight) const
{
if (!glight.get_parent())
{
WARN_PRINT_ONCE("LRoomManager::Light_UpdateTransform Light is not in tree");
return;
}
// get global transform only works if glight is in the tree
Transform tr = glight.get_global_transform();
light.m_Source.m_ptPos = tr.origin;
light.m_Source.m_ptDir = -tr.basis.get_axis(2); // or possibly get_axis .. z is what we want
}
// common stuff for global and local light creation
bool LRoomManager::LightCreate(Light * pLight, int roomID, String szArea)
{
// set culling flag for light
// 1 is for lighting objects outside the room system
pLight->set_cull_mask(1 | LRoom::LAYER_MASK_LIGHT);
// create new light
LLight l;
l.Light_SetDefaults();
l.Hidable_Create(pLight);
l.m_GodotID = pLight->get_instance_id();
//l.m_iArea = areaID;
// store the area name as a string if an area light
// as the areas aren't actually created until calling convert
if (szArea != "")
l.m_szArea = szArea;
LSource &lsource = l.m_Source;
// direction
Light_UpdateTransform(l, *pLight);
lsource.m_RoomID = roomID;
bool bOK = false;
// what kind of light?
SpotLight * pSL = Object::cast_to<SpotLight>(pLight);
if (pSL)
{
LPRINT(2, "\tSPOTLIGHT detected " + pLight->get_name());
lsource.m_eType = LSource::ST_SPOTLIGHT;
lsource.m_fSpread = pSL->get_param(Light::PARAM_SPOT_ANGLE);
lsource.m_fRange = pLight->get_param(Light::PARAM_RANGE);
bOK = true;
}
OmniLight * pOL = Object::cast_to<OmniLight>(pLight);
if (pOL)
{
LPRINT(2, "\tOMNILIGHT detected " + pLight->get_name());
lsource.m_eType = LSource::ST_OMNI;
lsource.m_fRange = pLight->get_param(Light::PARAM_RANGE);
bOK = true;
}
DirectionalLight * pDL = Object::cast_to<DirectionalLight>(pLight);
if (pDL)
{
LPRINT(2, "\tDIRECTIONALLIGHT detected " + pLight->get_name());
lsource.m_eType = LSource::ST_DIRECTIONAL;
// no range but max distance? NYI
bOK = true;
}
// don't add if not recognised
if (!bOK)
{
LPRINT(2, "\tLIGHT type unrecognised " + pLight->get_name());
return false;
}
// turn the local light off to start with
if (!lsource.IsGlobal())
{
// l.Show(false);
//pLight->hide();
}
m_Lights.push_back(l);
return true;
}
int LRoomManager::dynamic_light_register(Node * pLightNode, float radius)
{
CHECK_ROOM_LIST
if (!pLightNode)
{
WARN_PRINT_ONCE("dynamic_light_register : pLightNode is NULL");
return -1;
}
ObjectID light_id = pLightNode->get_instance_id();
// does the light already exist in the light list? I.e. was it imported as part of the roomlist?
for (int n=0; n<m_Lights.size(); n++)
{
if (m_Lights[n].m_GodotID == light_id)
{
m_Lights[n].m_Source.m_eClass = LSource::SC_DYNAMIC;
// do an update on the light
//dynamic_light_update(n, pos, dir);
// store the light ID in the metadata for the node
//Meta_SetLightID(pLightNode, n);
return n;
}
}
return -1;
}
bool LRoomManager::dynamic_light_unregister(int light_id)
{
// NYI
return true;
}
// returns room within or -1 if no dob
int LRoomManager::dynamic_light_update(int light_id, const Vector3 &pos, const Vector3 &dir) // returns room within
{
// doesn't now matter if not in tree as position and dir are passed directly
if ((unsigned int) light_id >= (unsigned int) m_Lights.size())
{
WARN_PRINT_ONCE("dynamic_light_update : meta light ID out of range");
return -1;
}
LLight &light = m_Lights[light_id];
int iRoom = light.m_Source.m_RoomID;
if (iRoom == -1)
{
WARN_PRINT_ONCE("dynamic_light_update : can't update global light");
return -1;
}
light.m_Source.m_ptPos = pos;
light.m_Source.m_ptDir = dir.normalized();
// update dob
if (light.m_DOB_id != -1)
{
int iNewRoom = m_DobList.UpdateDob(*this, light.m_DOB_id, pos);
light.m_Source.m_RoomID = iNewRoom;
}
// update with a new Trace (we are assuming update is only called if the light has moved)
// remove the old local lights
for (int n=0; n<light.m_NumAffectedRooms; n++)
{
int r = light.m_AffectedRooms[n];
GetRoom(r)->RemoveLocalLight(light_id);
}
light.ClearAffectedRooms();
// now do a new trace, and add all the rooms that are hit
m_Trace.Trace_Light(*this, light, LTrace::LR_ROOMS);
// we should now have a list of the rooms hit in m_LightRender.m_Temp_Visible_Rooms
for (int n=0; n<m_LightRender.m_Temp_Visible_Rooms.size(); n++)
{
int r = m_LightRender.m_Temp_Visible_Rooms[n];
// add to the list on the light
light.AddAffectedRoom(r);
// add to the list of local lights in the room
GetRoom(r)->AddLocalLight(light_id);
}
// this may or may not have changed
return light.m_Source.m_RoomID;
/*
if (!pLightNode)
{
WARN_PRINT_ONCE("dynamic_light_update : pLightNode is NULL");
return -1;
}
if (pLightNode->is_inside_tree() == false)
{
#ifdef LDEBUG_LIGHT_AFFECTED_ROOMS
if (m_bDebugFrameString)
DebugString_Add("DynamicLight not in tree\n");
#endif
return -1;
}
// find the light ID from meta data
int light_id = Meta_GetLightID(pLightNode);