/
LightNode.cpp
1270 lines (1048 loc) · 41.2 KB
/
LightNode.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
#include "LightNode.h"
#include "igrid.h"
#include "ishaders.h"
#include "icolourscheme.h"
#include "../EntitySettings.h"
#include <functional>
#include "registry/CachedKey.h"
namespace entity {
// Store the static default shader string (it's here because LightShader does not have a .cpp file)
std::string LightShader::m_defaultShader = "";
// --------- LightNode implementation ------------------------------------
LightNode::LightNode(const IEntityClassPtr& eclass)
: EntityNode(eclass),
m_originKey(std::bind(&LightNode::originChanged, this)),
_originTransformed(ORIGINKEY_IDENTITY),
m_rotationKey(std::bind(&LightNode::rotationChanged, this)),
m_transformChanged(std::bind(&scene::Node::transformChanged, this)),
m_boundsChanged(std::bind(&scene::Node::boundsChanged, this)),
_instances(getDoom3Radius().m_centerTransformed, _projVectors.transformed,
sigc::mem_fun(*this, &LightNode::selectedChangedComponent)),
_dragPlanes(std::bind(&LightNode::selectedChangedComponent, this, std::placeholders::_1)),
_renderableOctagon(*this, 0.5), // transparent
_renderableOctagonOutline(*this, 1.0), // opaque lines
_renderableLightVolume(*this),
_renderableVertices(*this, _instances, _projUseFlags),
_showLightVolumeWhenUnselected(EntitySettings::InstancePtr()->getShowAllLightRadii()),
_overrideColKey(colours::RKEY_OVERRIDE_LIGHTCOL)
{
m_doom3Radius.m_changed = std::bind(&LightNode::onLightRadiusChanged, this);
}
LightNode::LightNode(const LightNode& other)
: EntityNode(other), ILightNode(other),
m_originKey(std::bind(&LightNode::originChanged, this)),
_originTransformed(ORIGINKEY_IDENTITY),
m_rotationKey(std::bind(&LightNode::rotationChanged, this)),
m_transformChanged(std::bind(&Node::transformChanged, this)),
m_boundsChanged(std::bind(&Node::boundsChanged, this)),
_instances(getDoom3Radius().m_centerTransformed, _projVectors.transformed,
sigc::mem_fun(*this, &LightNode::selectedChangedComponent)),
_dragPlanes(std::bind(&LightNode::selectedChangedComponent, this, std::placeholders::_1)),
_renderableOctagon(*this, 0.5), // transparent
_renderableOctagonOutline(*this, 1.0), // opaque lines
_renderableLightVolume(*this),
_renderableVertices(*this, _instances, _projUseFlags),
_showLightVolumeWhenUnselected(other._showLightVolumeWhenUnselected),
_overrideColKey(colours::RKEY_OVERRIDE_LIGHTCOL)
{
m_doom3Radius.m_changed = std::bind(&LightNode::onLightRadiusChanged, this);
}
LightNodePtr LightNode::Create(const IEntityClassPtr& eclass)
{
LightNodePtr instance(new LightNode(eclass));
instance->construct();
return instance;
}
void LightNode::construct()
{
EntityNode::construct();
m_rotation.setIdentity();
_lightBox.origin = Vector3(0, 0, 0);
_lightBox.extents = Vector3(8, 8, 8);
_originTransformed = ORIGINKEY_IDENTITY;
// Observe position and rotation spawnargs
static_assert(std::is_base_of<sigc::trackable, OriginKey>::value);
static_assert(std::is_base_of<sigc::trackable, RotationKey>::value);
observeKey("origin", sigc::mem_fun(m_originKey, &OriginKey::onKeyValueChanged));
observeKey("angle", sigc::mem_fun(m_rotationKey, &RotationKey::angleChanged));
observeKey("rotation", sigc::mem_fun(m_rotationKey, &RotationKey::rotationChanged));
// Observe light-specific spawnargs
static_assert(std::is_base_of<sigc::trackable, Doom3LightRadius>::value);
static_assert(std::is_base_of<sigc::trackable, LightNode>::value);
static_assert(std::is_base_of<sigc::trackable, LightShader>::value);
observeKey("light_radius",
sigc::mem_fun(m_doom3Radius, &Doom3LightRadius::lightRadiusChanged));
observeKey("light_center",
sigc::mem_fun(m_doom3Radius, &Doom3LightRadius::lightCenterChanged));
observeKey("light_rotation", sigc::mem_fun(this, &LightNode::lightRotationChanged));
observeKey("light_target", sigc::mem_fun(this, &LightNode::lightTargetChanged));
observeKey("light_up", sigc::mem_fun(this, &LightNode::lightUpChanged));
observeKey("light_right", sigc::mem_fun(this, &LightNode::lightRightChanged));
observeKey("light_start", sigc::mem_fun(this, &LightNode::lightStartChanged));
observeKey("light_end", sigc::mem_fun(this, &LightNode::lightEndChanged));
observeKey("texture", sigc::mem_fun(m_shader, &LightShader::valueChanged));
_projectionChanged = true;
_spawnArgs.setIsContainer(true);
// Load the light colour (might be inherited)
m_shader.valueChanged(_spawnArgs.getKeyValue("texture"));
}
const Frustum& LightNode::getLightFrustum() const
{
if (!isProjected()) throw std::logic_error("getLightFrustum can be called on projected lights only");
return _frustum;
}
const Vector3& LightNode::getLightStart() const
{
if (!isProjected()) throw std::logic_error("getLightStart can be called on projected lights only");
return _projVectors.transformed.start;
}
const Vector3& LightNode::getLightRadius() const
{
if (isProjected()) throw std::logic_error("getLightRadius can be called on point lights only");
return m_doom3Radius.m_radiusTransformed;
}
AABB LightNode::getSelectAABB() const
{
// Use the light origin as select AABB centerpoint
return AABB(getLightOrigin(), Vector3(8, 8, 8));
}
void LightNode::onLightRadiusChanged()
{
// Light radius changed, mark bounds as dirty
boundsChanged();
updateRenderables();
}
void LightNode::transformChanged()
{
EntityNode::transformChanged();
updateRenderables();
}
float LightNode::getShaderParm(int parmNum) const
{
return EntityNode::getShaderParm(parmNum);
}
void LightNode::onRemoveFromScene(scene::IMapRootNode& root)
{
// Call the base class first
EntityNode::onRemoveFromScene(root);
// De-select all child components as well
setSelectedComponents(false, selection::ComponentSelectionMode::Vertex);
setSelectedComponents(false, selection::ComponentSelectionMode::Face);
clearRenderables();
}
void LightNode::testSelect(Selector& selector, SelectionTest& test)
{
// Generic entity selection
EntityNode::testSelect(selector, test);
// Light specific selection
test.BeginMesh(localToWorld());
SelectionIntersection best;
aabb_testselect(_lightBox, test, best);
if (best.isValid())
{
selector.addIntersection(best);
}
}
// greebo: Returns true if drag planes or one or more light vertices are selected
bool LightNode::isSelectedComponents() const {
return (_dragPlanes.isSelected() || _instances.center.isSelected() ||
_instances.target.isSelected() || _instances.right.isSelected() ||
_instances.up.isSelected() || _instances.start.isSelected() ||
_instances.end.isSelected() );
}
// greebo: Selects/deselects all components, depending on the chosen componentmode
void LightNode::setSelectedComponents(bool select, selection::ComponentSelectionMode mode)
{
if (mode == selection::ComponentSelectionMode::Face) {
_dragPlanes.setSelected(false);
}
if (mode == selection::ComponentSelectionMode::Vertex) {
_instances.center.setSelected(false);
_instances.target.setSelected(false);
_instances.right.setSelected(false);
_instances.up.setSelected(false);
_instances.start.setSelected(false);
_instances.end.setSelected(false);
}
}
void LightNode::invertSelectedComponents(selection::ComponentSelectionMode mode)
{
if (mode == selection::ComponentSelectionMode::Vertex)
{
_instances.center.invertSelected();
_instances.target.invertSelected();
_instances.right.invertSelected();
_instances.up.invertSelected();
_instances.start.invertSelected();
_instances.end.invertSelected();
}
}
void LightNode::testSelectComponents(Selector& selector, SelectionTest& test, selection::ComponentSelectionMode mode)
{
if (mode == selection::ComponentSelectionMode::Vertex)
{
// Use the full rotation matrix for the test
test.BeginMesh(localToWorld());
if (isProjected())
{
// Test the projection components for selection
_instances.target.testSelect(selector, test);
_instances.right.testSelect(selector, test);
_instances.up.testSelect(selector, test);
_instances.start.testSelect(selector, test);
_instances.end.testSelect(selector, test);
}
else
{
// Test if the light center is hit by the click
_instances.center.testSelect(selector, test);
}
}
}
const AABB& LightNode::getSelectedComponentsBounds() const {
// Create a new axis aligned bounding box
m_aabb_component = AABB();
if (isProjected()) {
// Include the according vertices in the AABB
m_aabb_component.includePoint(_instances.target.getVertex());
m_aabb_component.includePoint(_instances.right.getVertex());
m_aabb_component.includePoint(_instances.up.getVertex());
m_aabb_component.includePoint(_instances.start.getVertex());
m_aabb_component.includePoint(_instances.end.getVertex());
}
else {
// Just include the light center, this is the only vertex that may be out of the light volume
m_aabb_component.includePoint(_instances.center.getVertex());
}
return m_aabb_component;
}
void LightNode::snapComponents(float snap) {
if (isProjected()) {
// Check, if any components are selected and snap the selected ones to the grid
if (isSelectedComponents()) {
if (_instances.target.isSelected()) {
_projVectors.transformed.target.snap(snap);
}
if (_instances.right.isSelected()) {
_projVectors.transformed.right.snap(snap);
}
if (_instances.up.isSelected()) {
_projVectors.transformed.up.snap(snap);
}
if (useStartEnd()) {
if (_instances.end.isSelected()) {
_projVectors.transformed.end.snap(snap);
}
if (_instances.start.isSelected()) {
_projVectors.transformed.start.snap(snap);
}
}
}
else {
// None are selected, snap them all
_projVectors.transformed.target.snap(snap);
_projVectors.transformed.right.snap(snap);
_projVectors.transformed.up.snap(snap);
if (useStartEnd()) {
_projVectors.transformed.end.snap(snap);
_projVectors.transformed.start.snap(snap);
}
}
}
else {
// There is only one vertex for point lights, namely the light_center, always snap it
getDoom3Radius().m_centerTransformed.snap(snap);
}
freezeLightTransform();
}
void LightNode::selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback) {
test.BeginMesh(localToWorld());
// greebo: Make sure to use the local lightAABB() for the selection test, excluding the light center
AABB localLightAABB(Vector3(0,0,0), getDoom3Radius().m_radiusTransformed);
_dragPlanes.selectPlanes(localLightAABB, selector, test, selectedPlaneCallback);
}
void LightNode::selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
{
AABB localLightAABB(Vector3(0,0,0), getDoom3Radius().m_radiusTransformed);
_dragPlanes.selectReversedPlanes(localLightAABB, selector, selectedPlanes);
}
scene::INodePtr LightNode::clone() const
{
LightNodePtr node(new LightNode(*this));
node->construct();
node->constructClone(*this);
return node;
}
void LightNode::selectedChangedComponent(const ISelectable& selectable)
{
// add the selectable to the list of selected components (see RadiantSelectionSystem::onComponentSelection)
GlobalSelectionSystem().onComponentSelection(Node::getSelf(), selectable);
_renderableVertices.queueUpdate();
}
void LightNode::onPreRender(const VolumeTest& volume)
{
EntityNode::onPreRender(volume);
// Octagon faces (camera only)
_renderableOctagon.update(_crystalFillShader);
// Octagon outlines (for both camera and ortho)
_renderableOctagonOutline.update(_crystalOutlineShader);
bool lightIsSelected = isSelected();
// Depending on the selected status or the entity settings, we need to update the wireframe volume
if (_showLightVolumeWhenUnselected || lightIsSelected)
{
if (isProjected())
{
updateProjection();
}
_renderableLightVolume.update(_crystalOutlineShader);
// Update vertices when the light is selected
if (lightIsSelected)
{
_renderableVertices.setComponentMode(GlobalSelectionSystem().ComponentMode());
_renderableVertices.update(_vertexShader);
}
else
{
_renderableVertices.clear();
}
}
else
{
// Light volume is not visible, hide it
_renderableLightVolume.clear();
_renderableVertices.clear();
}
}
void LightNode::renderHighlights(IRenderableCollector& collector, const VolumeTest& volume)
{
collector.addHighlightRenderable(_renderableOctagon, Matrix4::getIdentity());
collector.addHighlightRenderable(_renderableLightVolume, Matrix4::getIdentity());
EntityNode::renderHighlights(collector, volume);
}
void LightNode::setRenderSystem(const RenderSystemPtr& renderSystem)
{
EntityNode::setRenderSystem(renderSystem);
// Clear the geometry from any previous shader
clearRenderables();
m_shader.setRenderSystem(renderSystem);
if (renderSystem)
{
_vertexShader = renderSystem->capture(BuiltInShaderType::BigPoint);
auto renderColour = getEntityColour();
_crystalOutlineShader = renderSystem->capture(ColourShaderType::CameraAndOrthoViewOutline, renderColour);
_crystalFillShader = renderSystem->capture(ColourShaderType::CameraTranslucent, renderColour);
_renderableVertices.queueUpdate();
}
else
{
_crystalFillShader.reset();
_crystalOutlineShader.reset();
_vertexShader.reset();
}
}
Vector4 LightNode::getEntityColour() const
{
// Pick the colour shader according to our settings
return _overrideColKey.get() ? EntityNode::getEntityColour() : Vector4(_colourKey.getColour(), 1.0);
}
void LightNode::evaluateTransform()
{
if (getType() == TRANSFORM_PRIMITIVE)
{
translate(getTranslation());
rotate(getRotation());
}
else
{
// Check if the light center is selected, if yes, transform it, if not, it's a drag plane operation
if (GlobalSelectionSystem().ComponentMode() == selection::ComponentSelectionMode::Vertex)
{
// When the user is mouse-moving a vertex in the orthoviews he/she is operating
// in world space. It's expected that the selected vertex follows the mouse.
// Since the editable light vertices are measured in local coordinates
// we have to calculate the new position in world space first and then transform
// the point back into local space.
if (_instances.center.isSelected())
{
// Retrieve the translation and apply it to the temporary light center variable
Vector3 newWorldPos = localToWorld().transformPoint(getDoom3Radius().m_center) + getTranslation();
getDoom3Radius().m_centerTransformed = localToWorld().getFullInverse().transformPoint(newWorldPos);
}
if (_instances.target.isSelected())
{
Vector3 newWorldPos = localToWorld().transformPoint(_projVectors.base.target) + getTranslation();
_projVectors.transformed.target = localToWorld().getFullInverse().transformPoint(newWorldPos);
}
if (_instances.start.isSelected())
{
Vector3 newWorldPos = localToWorld().transformPoint(_projVectors.base.start) + getTranslation();
Vector3 newLightStart = localToWorld().getFullInverse().transformPoint(newWorldPos);
// Assign the light start, perform the boundary checks
setLightStart(newLightStart);
}
if (_instances.end.isSelected())
{
Vector3 newWorldPos = localToWorld().transformPoint(_projVectors.base.end) + getTranslation();
_projVectors.transformed.end = localToWorld().getFullInverse().transformPoint(newWorldPos);
ensureLightStartConstraints();
}
// Even more footwork needs to be done for light_up and light_right since these
// are measured relatively to the light_target position.
// Extend the regular local2World by the additional light_target transform
Matrix4 local2World = localToWorld();
local2World.translateBy(_projVectors.base.target);
Matrix4 world2Local = local2World.getFullInverse();
if (_instances.right.isSelected())
{
Vector3 newWorldPos = local2World.transformPoint(_projVectors.base.right) + getTranslation();
_projVectors.transformed.right = world2Local.transformPoint(newWorldPos);
}
if (_instances.up.isSelected())
{
Vector3 newWorldPos = local2World.transformPoint(_projVectors.base.up) + getTranslation();
_projVectors.transformed.up = world2Local.transformPoint(newWorldPos);
}
// If this is a projected light, then it is likely for the according vertices to have changed, so update the projection
if (isProjected())
{
// Call projection changed, so that the recalculation can be triggered (call for projection() would be ignored otherwise)
projectionChanged();
// Recalculate the frustum
updateProjection();
}
}
else
{
// Ordinary Drag manipulator
// greebo: To evaluate the drag operation use a fresh AABB as starting point.
// We don't use the aabb() or localABB() methods, those return the bounding box
// including the light center, which may be positioned way out of the volume
_dragPlanes.m_bounds = AABB(_originTransformed, m_doom3Radius.m_radiusTransformed);
setLightRadius(_dragPlanes.evaluateResize(getTranslation(), rotation()));
}
}
}
void LightNode::_onTransformationChanged()
{
revertLightTransform();
evaluateTransform();
updateOrigin();
updateRenderables();
}
void LightNode::_applyTransformation()
{
revertLightTransform();
evaluateTransform();
freezeLightTransform();
}
void LightNode::updateOrigin() {
m_boundsChanged();
m_doom3Radius.m_changed();
// Update the projection as well, if necessary
if (isProjected())
projectionChanged();
// Update the transformation matrix
setLocalToParent(Matrix4::getTranslation(_originTransformed) * m_rotation.getMatrix4());
// Notify all child nodes
m_transformChanged();
GlobalSelectionSystem().pivotChanged();
}
const Vector3& LightNode::getUntransformedOrigin()
{
return m_originKey.get();
}
const Vector3& LightNode::getWorldPosition() const
{
return _originTransformed;
}
void LightNode::onVisibilityChanged(bool isVisibleNow)
{
EntityNode::onVisibilityChanged(isVisibleNow);
if (isVisibleNow)
{
updateRenderables();
}
else
{
clearRenderables();
}
}
void LightNode::onSelectionStatusChange(bool changeGroupStatus)
{
EntityNode::onSelectionStatusChange(changeGroupStatus);
// Volume renderable is not always prepared for rendering, queue an update
_renderableLightVolume.queueUpdate();
_renderableVertices.queueUpdate();
}
void LightNode::onEntitySettingsChanged()
{
EntityNode::onEntitySettingsChanged();
_showLightVolumeWhenUnselected = EntitySettings::InstancePtr()->getShowAllLightRadii();
_renderableLightVolume.queueUpdate();
}
void LightNode::originChanged()
{
// The "origin" key has been changed, reset the current working copy to that value
_originTransformed = m_originKey.get();
updateOrigin();
}
void LightNode::lightTargetChanged(const std::string& value)
{
_projUseFlags.target = (!value.empty());
if (_projUseFlags.target)
{
_projVectors.base.target = string::convert<Vector3>(value);
}
_projVectors.transformed.target = _projVectors.base.target;
projectionChanged();
}
void LightNode::lightUpChanged(const std::string& value)
{
_projUseFlags.up = (!value.empty());
if (_projUseFlags.up)
{
_projVectors.base.up = string::convert<Vector3>(value);
}
_projVectors.transformed.up = _projVectors.base.up;
projectionChanged();
}
void LightNode::lightRightChanged(const std::string& value)
{
_projUseFlags.right = (!value.empty());
if (_projUseFlags.right)
{
_projVectors.base.right = string::convert<Vector3>(value);
}
_projVectors.transformed.right = _projVectors.base.right;
projectionChanged();
}
void LightNode::lightStartChanged(const std::string& value) {
_projUseFlags.start = (!value.empty());
if (_projUseFlags.start)
{
_projVectors.base.start = string::convert<Vector3>(value);
}
_projVectors.transformed.start = _projVectors.base.start;
// If the light_end key is still unused, set it to a reasonable value
if (_projUseFlags.end) {
checkStartEnd();
}
projectionChanged();
}
void LightNode::lightEndChanged(const std::string& value) {
_projUseFlags.end = (!value.empty());
if (_projUseFlags.end)
{
_projVectors.base.end = string::convert<Vector3>(value);
}
_projVectors.transformed.end = _projVectors.base.end;
// If the light_start key is still unused, set it to a reasonable value
if (_projUseFlags.start) {
checkStartEnd();
}
projectionChanged();
}
/* greebo: Checks the light_start and light_end keyvals for meaningful values.
*
* If the light_end is "above" the light_start (i.e. nearer to the origin),
* the two are swapped.
*
* This also checks if the two vertices happen to be on the very same spot.
*/
void LightNode::checkStartEnd()
{
if (_projUseFlags.start && _projUseFlags.end)
{
if (_projVectors.base.end.getLengthSquared() < _projVectors.base.start.getLengthSquared())
{
// Swap the two vectors
Vector3 temp = _projVectors.base.end;
_projVectors.transformed.end = _projVectors.base.end = _projVectors.base.start;
_projVectors.transformed.start = _projVectors.base.start = temp;
}
// The light_end on the same point as the light_start is an unlucky situation, revert it
// otherwise the vertices won't be separable again for the user
if (_projVectors.base.end == _projVectors.base.start)
{
_projVectors.transformed.end = _projVectors.base.end = _projVectors.base.target;
_projVectors.transformed.start = _projVectors.base.start = Vector3(0,0,0);
}
}
}
void LightNode::rotationChanged()
{
m_rotation = m_useLightRotation ? m_lightRotation : m_rotationKey.m_rotation;
// Update the transformation matrix
setLocalToParent(Matrix4::getTranslation(_originTransformed) * m_rotation.getMatrix4());
// Notify owner about this
m_transformChanged();
GlobalSelectionSystem().pivotChanged();
}
void LightNode::lightRotationChanged(const std::string& value) {
m_useLightRotation = (!value.empty());
if(m_useLightRotation) {
m_lightRotation.readFromString(value);
}
rotationChanged();
}
/* greebo: Snaps the current light origin to the grid.
*
* Note: This gets called when the light as a whole is selected, NOT in vertex editing mode
*/
void LightNode::snapto(float snap)
{
m_originKey.snap(snap);
m_originKey.write(_spawnArgs);
_originTransformed = m_originKey.get();
updateOrigin();
}
void LightNode::setLightRadius(const AABB& aabb)
{
if (EntitySettings::InstancePtr()->getDragResizeEntitiesSymmetrically())
{
// Leave origin unchanged, calculate the new symmetrical radius
Vector3 delta = aabb.getExtents() - m_doom3Radius.m_radiusTransformed;
m_doom3Radius.m_radiusTransformed += delta*2;
// Constrain the values to barely non-zero limits (issue #1969)
for (int i = 0; i < 3; ++i)
{
if (m_doom3Radius.m_radiusTransformed[i] < 0.01f)
{
m_doom3Radius.m_radiusTransformed[i] = 0.01f;
}
}
}
else
{
// Transform the origin together with the radius (pivoted transform)
_originTransformed = aabb.origin;
// Set the new radius
m_doom3Radius.m_radiusTransformed = aabb.extents;
}
}
void LightNode::transformLightRadius(const Matrix4& transform)
{
_originTransformed = transform.transformPoint(_originTransformed);
}
void LightNode::revertLightTransform()
{
_originTransformed = m_originKey.get();
m_rotation = m_useLightRotation ? m_lightRotation : m_rotationKey.m_rotation;
m_doom3Radius.m_radiusTransformed = m_doom3Radius.m_radius;
m_doom3Radius.m_centerTransformed = m_doom3Radius.m_center;
// revert all the projection changes to the saved values
_projVectors.revertTransform();
}
void LightNode::freezeLightTransform()
{
m_originKey.set(_originTransformed);
m_originKey.write(_spawnArgs);
if (isProjected())
{
if (_projUseFlags.target)
{
_projVectors.base.target = _projVectors.transformed.target;
_spawnArgs.setKeyValue("light_target",
string::to_string(_projVectors.base.target));
}
if (_projUseFlags.up)
{
_projVectors.base.up = _projVectors.transformed.up;
_spawnArgs.setKeyValue("light_up",
string::to_string(_projVectors.base.up));
}
if (_projUseFlags.right)
{
_projVectors.base.right = _projVectors.transformed.right;
_spawnArgs.setKeyValue("light_right",
string::to_string(_projVectors.base.right));
}
// Check the start and end (if the end is "above" the start, for example)
checkStartEnd();
if (_projUseFlags.start)
{
_projVectors.base.start = _projVectors.transformed.start;
_spawnArgs.setKeyValue("light_start",
string::to_string(_projVectors.base.start));
}
if (_projUseFlags.end)
{
_projVectors.base.end = _projVectors.transformed.end;
_spawnArgs.setKeyValue("light_end",
string::to_string(_projVectors.base.end));
}
}
else
{
// Save the light center to the entity key/values
m_doom3Radius.m_center = m_doom3Radius.m_centerTransformed;
_spawnArgs.setKeyValue("light_center",
string::to_string(m_doom3Radius.m_center));
}
if(m_useLightRotation)
{
m_lightRotation = m_rotation;
m_lightRotation.writeToEntity(&_spawnArgs, "light_rotation");
}
m_rotationKey.m_rotation = m_rotation;
m_rotationKey.m_rotation.writeToEntity(&_spawnArgs);
if (!isProjected())
{
m_doom3Radius.m_radius = m_doom3Radius.m_radiusTransformed;
_spawnArgs.setKeyValue("light_radius",
string::to_string(m_doom3Radius.m_radius));
}
}
Doom3LightRadius& LightNode::getDoom3Radius() {
return m_doom3Radius;
}
void LightNode::translate(const Vector3& translation)
{
_originTransformed += translation;
}
void LightNode::ensureLightStartConstraints()
{
Vector3 assumedEnd = (_projUseFlags.end) ? _projVectors.transformed.end : _projVectors.transformed.target;
Vector3 normal = (_projVectors.transformed.start - assumedEnd).getNormalised();
// Calculate the distance to the plane going through the origin, hence the minus sign
double dist = normal.dot(_projVectors.transformed.start);
if (dist > 0)
{
// Light_Start is too "high", project it back onto the origin plane
_projVectors.transformed.start = _projVectors.transformed.start - normal*dist;
_projVectors.transformed.start.snap(GlobalGrid().getGridSize());
}
}
void LightNode::setLightStart(const Vector3& newLightStart)
{
_projVectors.transformed.start = newLightStart;
// Prevent the light_start to cause the volume form an hourglass-shaped frustum
ensureLightStartConstraints();
}
void LightNode::rotate(const Quaternion& rotation)
{
m_rotation.rotate(rotation);
}
// greebo: This returns the AABB of the WHOLE light (this includes the volume and all its selectable vertices)
// Used to test the light for selection on mouse click.
const AABB& LightNode::localAABB() const
{
if (isProjected()) {
// start with an empty AABB and include all the projection vertices
m_doom3AABB = AABB();
m_doom3AABB.includePoint(_lightBox.origin);
m_doom3AABB.includePoint(_lightBox.origin + _projVectors.transformed.target);
m_doom3AABB.includePoint(_lightBox.origin + _projVectors.transformed.target + _projVectors.transformed.right);
m_doom3AABB.includePoint(_lightBox.origin + _projVectors.transformed.target + _projVectors.transformed.up);
if (useStartEnd()) {
m_doom3AABB.includePoint(_lightBox.origin + _projVectors.transformed.start);
m_doom3AABB.includePoint(_lightBox.origin + _projVectors.transformed.end);
}
}
else {
m_doom3AABB = AABB(_lightBox.origin, m_doom3Radius.m_radiusTransformed);
// greebo: Make sure the light center (that maybe outside of the light volume) is selectable
m_doom3AABB.includePoint(_lightBox.origin + m_doom3Radius.m_centerTransformed);
}
return m_doom3AABB;
}
/* RendererLight implementation */
Matrix4 LightNode::getLightTextureTransformation() const
{
// greebo: Some notes on the world2Light matrix
// This matrix transforms a world point (i.e. relative to the 0,0,0 world origin)
// into texture coordinates that span the range [0..1] within the light volume.
// Example:
// For non-rotated point lights the world point [origin - light_radius] will be
// transformed to [0,0,0], whereas [origin + light_radius] will be [1,1,1]
if (isProjected())
{
// Ensure _localToTexture matrix is up to date
updateProjection();
// First step: subtract the light origin from the world point
Matrix4 worldTolight = Matrix4::getTranslation(-getLightOrigin());
// "Undo" the light rotation, we're now in local space
worldTolight.premultiplyBy(rotation().getTransposed());
// Transform the local coordinates into texture space and we're done
worldTolight.premultiplyBy(_localToTexture);
return worldTolight;
}
else // point light
{
AABB lightBounds = lightAABB();
// First step: subtract the light origin from the world point
Matrix4 worldTolight = Matrix4::getTranslation(-lightBounds.origin);
// "Undo" the light rotation
worldTolight.premultiplyBy(rotation().getTransposed());
// Map the point to a small [-1..1] cube around the origin
worldTolight.premultiplyBy(Matrix4::getScale(
Vector3(1.0f / lightBounds.extents.x(),
1.0f / lightBounds.extents.y(),
1.0f / lightBounds.extents.z())
));
// To get texture coordinates in the range of [0..1], we need to scale down
// one more time. [-1..1] is 2 units wide, so scale down by factor 2.
// By this time, points within the light volume have been mapped
// into a [-0.5..0.5] cube around the origin.
worldTolight.premultiplyBy(Matrix4::getScale(Vector3(0.5f, 0.5f, 0.5f)));
// Now move the [-0.5..0.5] cube to [0..1] and we're done
worldTolight.premultiplyBy(Matrix4::getTranslation(Vector3(0.5f, 0.5f, 0.5f)));
return worldTolight;
}
}
// AABB for light volume only (excluding the light_center which might be
// outside the volume), used for drag manipulator and render culling.
AABB LightNode::lightAABB() const
{
if (isProjected())
{
// Make sure our frustum is up to date
updateProjection();
// Return Frustum AABB in *world* space
return _frustum.getTransformedBy(localToParent()).getAABB();
}
else
{
// AABB ignores light_center so we can't call getLightOrigin() here.
// Just transform (0, 0, 0) by localToWorld to get the world origin for
// the AABB.
return AABB(localToWorld().transformPoint(Vector3(0, 0, 0)),
m_doom3Radius.m_radiusTransformed);
}
}
const Matrix4& LightNode::rotation() const {
m_doom3Rotation = m_rotation.getMatrix4();
return m_doom3Rotation;
}
/* greebo: This is needed by the renderer to determine the center of the light. It returns
* the centerTransformed variable as the lighting should be updated as soon as the light center
* is dragged.
*/
Vector3 LightNode::getLightOrigin() const
{
if (isProjected())
{
return _originTransformed;
}
else
{