-
-
Notifications
You must be signed in to change notification settings - Fork 981
/
OgreLight.cpp
971 lines (918 loc) · 37.4 KB
/
OgreLight.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
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2014 Torus Knot Software Ltd
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 "OgreStableHeaders.h"
#include "OgreLight.h"
namespace Ogre {
//-----------------------------------------------------------------------
Light::Light()
: mLightType(LT_POINT),
#ifdef OGRE_NODELESS_POSITIONING
mPosition(Vector3::ZERO),
mDirection(Vector3::NEGATIVE_UNIT_Z),
mDerivedPosition(Vector3::ZERO),
mDerivedDirection(Vector3::NEGATIVE_UNIT_Z),
mDerivedCamRelativePosition(Vector3::ZERO),
mDerivedCamRelativeDirty(false),
mDerivedTransformDirty(false),
#endif
mDiffuse(ColourValue::White),
mSpecular(ColourValue::Black),
mSpotOuter(Degree(40.0f)),
mSpotInner(Degree(30.0f)),
mSpotFalloff(1.0f),
mSpotNearClip(0.0f),
mAttenuation(100000.f, 1.f, 0.f, 0.f),
mPowerScale(1.0f),
mIndexInFrame(0),
mShadowFarDist(0),
mShadowFarDistSquared(0),
mShadowNearClipDist(-1),
mShadowFarClipDist(-1),
mCameraToBeRelativeTo(0),
mOwnShadowFarDist(false)
{
//mMinPixelSize should always be zero for lights otherwise lights will disapear
mMinPixelSize = 0;
}
//-----------------------------------------------------------------------
Light::Light(const String& name) : MovableObject(name),
mLightType(LT_POINT),
#ifdef OGRE_NODELESS_POSITIONING
mPosition(Vector3::ZERO),
mDirection(Vector3::NEGATIVE_UNIT_Z),
mDerivedPosition(Vector3::ZERO),
mDerivedDirection(Vector3::NEGATIVE_UNIT_Z),
mDerivedCamRelativeDirty(false),
mDerivedTransformDirty(false),
#endif
mDiffuse(ColourValue::White),
mSpecular(ColourValue::Black),
mSpotOuter(Degree(40.0f)),
mSpotInner(Degree(30.0f)),
mSpotFalloff(1.0f),
mSpotNearClip(0.0f),
mAttenuation(100000.f, 1.f, 0.f, 0.f),
mPowerScale(1.0f),
mIndexInFrame(0),
mShadowFarDist(0),
mShadowFarDistSquared(0),
mShadowNearClipDist(-1),
mShadowFarClipDist(-1),
mCameraToBeRelativeTo(0),
mOwnShadowFarDist(false)
{
//mMinPixelSize should always be zero for lights otherwise lights will disapear
mMinPixelSize = 0;
}
//-----------------------------------------------------------------------
Light::~Light()
{
}
//-----------------------------------------------------------------------
void Light::setType(LightTypes type)
{
mLightType = type;
}
//-----------------------------------------------------------------------
Light::LightTypes Light::getType(void) const
{
return mLightType;
}
#ifdef OGRE_NODELESS_POSITIONING
//-----------------------------------------------------------------------
void Light::setPosition(Real x, Real y, Real z)
{
mPosition.x = x;
mPosition.y = y;
mPosition.z = z;
mDerivedTransformDirty = true;
}
//-----------------------------------------------------------------------
void Light::setPosition(const Vector3& vec)
{
mPosition = vec;
mDerivedTransformDirty = true;
}
//-----------------------------------------------------------------------
const Vector3& Light::getPosition(void) const
{
return mPosition;
}
//-----------------------------------------------------------------------
void Light::setDirection(Real x, Real y, Real z)
{
mDirection.x = x;
mDirection.y = y;
mDirection.z = z;
mDerivedTransformDirty = true;
}
//-----------------------------------------------------------------------
void Light::setDirection(const Vector3& vec)
{
mDirection = vec;
mDerivedTransformDirty = true;
}
//-----------------------------------------------------------------------
const Vector3& Light::getDirection(void) const
{
return mDirection;
}
#endif
//-----------------------------------------------------------------------
void Light::setSpotlightRange(const Radian& innerAngle, const Radian& outerAngle, Real falloff)
{
mSpotInner = innerAngle;
mSpotOuter = outerAngle;
mSpotFalloff = falloff;
}
//-----------------------------------------------------------------------
void Light::setSpotlightInnerAngle(const Radian& val)
{
mSpotInner = val;
}
//-----------------------------------------------------------------------
void Light::setSpotlightOuterAngle(const Radian& val)
{
mSpotOuter = val;
}
//-----------------------------------------------------------------------
void Light::setSpotlightFalloff(Real val)
{
mSpotFalloff = val;
}
//-----------------------------------------------------------------------
const Radian& Light::getSpotlightInnerAngle(void) const
{
return mSpotInner;
}
//-----------------------------------------------------------------------
const Radian& Light::getSpotlightOuterAngle(void) const
{
return mSpotOuter;
}
//-----------------------------------------------------------------------
Real Light::getSpotlightFalloff(void) const
{
return mSpotFalloff;
}
//-----------------------------------------------------------------------
void Light::setDiffuseColour(float red, float green, float blue)
{
mDiffuse.r = red;
mDiffuse.b = blue;
mDiffuse.g = green;
}
//-----------------------------------------------------------------------
void Light::setDiffuseColour(const ColourValue& colour)
{
mDiffuse = colour;
}
//-----------------------------------------------------------------------
const ColourValue& Light::getDiffuseColour(void) const
{
return mDiffuse;
}
//-----------------------------------------------------------------------
void Light::setSpecularColour(float red, float green, float blue)
{
mSpecular.r = red;
mSpecular.b = blue;
mSpecular.g = green;
}
//-----------------------------------------------------------------------
void Light::setSpecularColour(const ColourValue& colour)
{
mSpecular = colour;
}
//-----------------------------------------------------------------------
const ColourValue& Light::getSpecularColour(void) const
{
return mSpecular;
}
//-----------------------------------------------------------------------
void Light::setPowerScale(Real power)
{
mPowerScale = power;
}
//-----------------------------------------------------------------------
Real Light::getPowerScale(void) const
{
return mPowerScale;
}
#ifdef OGRE_NODELESS_POSITIONING
//-----------------------------------------------------------------------
void Light::update(void) const
{
if (mDerivedTransformDirty)
{
if (mParentNode)
{
// Ok, update with SceneNode we're attached to
mDerivedDirection = mParentNode->convertLocalToWorldDirection(mDirection, false);
mDerivedPosition = mParentNode->convertLocalToWorldPosition(mPosition);
}
else
{
mDerivedPosition = mPosition;
mDerivedDirection = mDirection;
}
mDerivedTransformDirty = false;
//if the position has been updated we must update also the relative position
mDerivedCamRelativeDirty = true;
}
if (mCameraToBeRelativeTo && mDerivedCamRelativeDirty)
{
mDerivedCamRelativePosition = mDerivedPosition - mCameraToBeRelativeTo->getDerivedPosition();
mDerivedCamRelativeDirty = false;
}
}
//-----------------------------------------------------------------------
void Light::_notifyAttached(Node* parent, bool isTagPoint)
{
mDerivedTransformDirty = true;
MovableObject::_notifyAttached(parent, isTagPoint);
}
//-----------------------------------------------------------------------
void Light::_notifyMoved(void)
{
mDerivedTransformDirty = true;
MovableObject::_notifyMoved();
}
#endif
//-----------------------------------------------------------------------
const AxisAlignedBox& Light::getBoundingBox(void) const
{
// Null, lights are not visible
static AxisAlignedBox box;
return box;
}
//-----------------------------------------------------------------------
void Light::_updateRenderQueue(RenderQueue* queue)
{
// Do nothing
}
//-----------------------------------------------------------------------
void Light::visitRenderables(Renderable::Visitor* visitor,
bool debugRenderables)
{
// nothing to render
}
//-----------------------------------------------------------------------
const String& Light::getMovableType(void) const
{
return LightFactory::FACTORY_TYPE_NAME;
}
#ifdef OGRE_NODELESS_POSITIONING
//-----------------------------------------------------------------------
const Vector3& Light::getDerivedPosition(bool cameraRelative) const
{
update();
if (cameraRelative && mCameraToBeRelativeTo)
{
return mDerivedCamRelativePosition;
}
else
{
return mDerivedPosition;
}
}
//-----------------------------------------------------------------------
const Vector3& Light::getDerivedDirection(void) const
{
update();
return mDerivedDirection;
}
#endif
//-----------------------------------------------------------------------
Vector4 Light::getAs4DVector(bool cameraRelativeIfSet) const
{
if (mLightType == Light::LT_DIRECTIONAL)
{
return Vector4(-getDerivedDirection(), // negate direction as 'position'
0.0); // infinite distance
}
else
{
return Vector4(getDerivedPosition(cameraRelativeIfSet), 1.0);
}
}
//-----------------------------------------------------------------------
const PlaneBoundedVolume& Light::_getNearClipVolume(const Camera* const cam) const
{
// First check if the light is close to the near plane, since
// in this case we have to build a degenerate clip volume
mNearClipVolume.planes.clear();
mNearClipVolume.outside = Plane::NEGATIVE_SIDE;
Real n = cam->getNearClipDistance();
// Homogenous position
Vector4 lightPos = getAs4DVector();
// 3D version (not the same as _getDerivedPosition, is -direction for
// directional lights)
Vector3 lightPos3 = Vector3(lightPos.x, lightPos.y, lightPos.z);
// Get eye-space light position
// use 4D vector so directional lights still work
Vector4 eyeSpaceLight = cam->getViewMatrix() * lightPos;
// Find distance to light, project onto -Z axis
Real d = eyeSpaceLight.dotProduct(
Vector4(0, 0, -1, -n) );
#define THRESHOLD 1e-6
if (d > THRESHOLD || d < -THRESHOLD)
{
// light is not too close to the near plane
// First find the worldspace positions of the corners of the viewport
const Vector3 *corner = cam->getWorldSpaceCorners();
int winding = (d < 0) ^ cam->isReflected() ? +1 : -1;
// Iterate over world points and form side planes
Vector3 normal;
Vector3 lightDir;
for (unsigned int i = 0; i < 4; ++i)
{
// Figure out light dir
lightDir = lightPos3 - (corner[i] * lightPos.w);
// Cross with anticlockwise corner, therefore normal points in
normal = (corner[i] - corner[(i+winding)%4])
.crossProduct(lightDir);
normal.normalise();
mNearClipVolume.planes.push_back(Plane(normal, corner[i]));
}
// Now do the near plane plane
normal = cam->getFrustumPlane(FRUSTUM_PLANE_NEAR).normal;
if (d < 0)
{
// Behind near plane
normal = -normal;
}
const Vector3& cameraPos = cam->getDerivedPosition();
mNearClipVolume.planes.push_back(Plane(normal, cameraPos));
// Finally, for a point/spot light we can add a sixth plane
// This prevents false positives from behind the light
if (mLightType != LT_DIRECTIONAL)
{
// Direction from light perpendicular to near plane
mNearClipVolume.planes.push_back(Plane(-normal, lightPos3));
}
}
else
{
// light is close to being on the near plane
// degenerate volume including the entire scene
// we will always require light / dark caps
mNearClipVolume.planes.push_back(Plane(Vector3::UNIT_Z, -n));
mNearClipVolume.planes.push_back(Plane(-Vector3::UNIT_Z, n));
}
return mNearClipVolume;
}
//-----------------------------------------------------------------------
const PlaneBoundedVolumeList& Light::_getFrustumClipVolumes(const Camera* const cam) const
{
// Homogenous light position
Vector4 lightPos = getAs4DVector();
// 3D version (not the same as _getDerivedPosition, is -direction for
// directional lights)
Vector3 lightPos3 = Vector3(lightPos.x, lightPos.y, lightPos.z);
const Vector3 *clockwiseVerts[4];
// Get worldspace frustum corners
const Vector3* corners = cam->getWorldSpaceCorners();
int windingPt0 = cam->isReflected() ? 1 : 0;
int windingPt1 = cam->isReflected() ? 0 : 1;
bool infiniteViewDistance = (cam->getFarClipDistance() == 0);
Vector3 notSoFarCorners[4];
if(infiniteViewDistance)
{
Vector3 camPosition = cam->getRealPosition();
notSoFarCorners[0] = corners[0] + corners[0] - camPosition;
notSoFarCorners[1] = corners[1] + corners[1] - camPosition;
notSoFarCorners[2] = corners[2] + corners[2] - camPosition;
notSoFarCorners[3] = corners[3] + corners[3] - camPosition;
}
mFrustumClipVolumes.clear();
for (unsigned short n = 0; n < 6; ++n)
{
// Skip far plane if infinite view frustum
if (infiniteViewDistance && n == FRUSTUM_PLANE_FAR)
continue;
const Plane& plane = cam->getFrustumPlane(n);
Vector4 planeVec(plane.normal.x, plane.normal.y, plane.normal.z, plane.d);
// planes face inwards, we need to know if light is on negative side
Real d = planeVec.dotProduct(lightPos);
if (d < -1e-06)
{
// Ok, this is a valid one
// clockwise verts mean we can cross-product and always get normals
// facing into the volume we create
mFrustumClipVolumes.push_back(PlaneBoundedVolume());
PlaneBoundedVolume& vol = mFrustumClipVolumes.back();
switch(n)
{
case(FRUSTUM_PLANE_NEAR):
clockwiseVerts[0] = corners + 3;
clockwiseVerts[1] = corners + 2;
clockwiseVerts[2] = corners + 1;
clockwiseVerts[3] = corners + 0;
break;
case(FRUSTUM_PLANE_FAR):
clockwiseVerts[0] = corners + 7;
clockwiseVerts[1] = corners + 6;
clockwiseVerts[2] = corners + 5;
clockwiseVerts[3] = corners + 4;
break;
case(FRUSTUM_PLANE_LEFT):
clockwiseVerts[0] = infiniteViewDistance ? notSoFarCorners + 1 : corners + 5;
clockwiseVerts[1] = corners + 1;
clockwiseVerts[2] = corners + 2;
clockwiseVerts[3] = infiniteViewDistance ? notSoFarCorners + 2 : corners + 6;
break;
case(FRUSTUM_PLANE_RIGHT):
clockwiseVerts[0] = infiniteViewDistance ? notSoFarCorners + 3 : corners + 7;
clockwiseVerts[1] = corners + 3;
clockwiseVerts[2] = corners + 0;
clockwiseVerts[3] = infiniteViewDistance ? notSoFarCorners + 0 : corners + 4;
break;
case(FRUSTUM_PLANE_TOP):
clockwiseVerts[0] = infiniteViewDistance ? notSoFarCorners + 0 : corners + 4;
clockwiseVerts[1] = corners + 0;
clockwiseVerts[2] = corners + 1;
clockwiseVerts[3] = infiniteViewDistance ? notSoFarCorners + 1 : corners + 5;
break;
case(FRUSTUM_PLANE_BOTTOM):
clockwiseVerts[0] = infiniteViewDistance ? notSoFarCorners + 2 : corners + 6;
clockwiseVerts[1] = corners + 2;
clockwiseVerts[2] = corners + 3;
clockwiseVerts[3] = infiniteViewDistance ? notSoFarCorners + 3 : corners + 7;
break;
};
// Build a volume
// Iterate over world points and form side planes
Vector3 normal;
Vector3 lightDir;
unsigned int infiniteViewDistanceInt = infiniteViewDistance ? 1 : 0;
for (unsigned int i = 0; i < 4 - infiniteViewDistanceInt; ++i)
{
// Figure out light dir
lightDir = lightPos3 - (*(clockwiseVerts[i]) * lightPos.w);
Vector3 edgeDir = *(clockwiseVerts[(i+windingPt1)%4]) - *(clockwiseVerts[(i+windingPt0)%4]);
// Cross with anticlockwise corner, therefore normal points in
normal = edgeDir.crossProduct(lightDir);
normal.normalise();
vol.planes.push_back(Plane(normal, *(clockwiseVerts[i])));
}
// Now do the near plane (this is the plane of the side we're
// talking about, with the normal inverted (d is already interpreted as -ve)
vol.planes.push_back( Plane(-plane.normal, plane.d) );
// Finally, for a point/spot light we can add a sixth plane
// This prevents false positives from behind the light
if (mLightType != LT_DIRECTIONAL)
{
// re-use our own plane normal
vol.planes.push_back(Plane(plane.normal, lightPos3));
}
}
}
return mFrustumClipVolumes;
}
//-----------------------------------------------------------------------
uint32 Light::getTypeFlags(void) const
{
return SceneManager::LIGHT_TYPE_MASK;
}
//---------------------------------------------------------------------
void Light::_calcTempSquareDist(const Vector3& worldPos)
{
if (mLightType == LT_DIRECTIONAL)
{
// make sure directional lights are always in front
// even of point lights at worldPos
// tempSquareDist is just a tag for sorting, and nobody will take the sqrt
tempSquareDist = -1;
}
else
{
tempSquareDist =
(worldPos - getDerivedPosition()).squaredLength();
}
}
//-----------------------------------------------------------------------
class LightDiffuseColourValue : public AnimableValue
{
protected:
Light* mLight;
public:
LightDiffuseColourValue(Light* l) :AnimableValue(COLOUR)
{ mLight = l; }
void setValue(const ColourValue& val)
{
mLight->setDiffuseColour(val);
}
void applyDeltaValue(const ColourValue& val)
{
setValue(mLight->getDiffuseColour() + val);
}
void setCurrentStateAsBaseValue(void)
{
setAsBaseValue(mLight->getDiffuseColour());
}
};
//-----------------------------------------------------------------------
class LightSpecularColourValue : public AnimableValue
{
protected:
Light* mLight;
public:
LightSpecularColourValue(Light* l) :AnimableValue(COLOUR)
{ mLight = l; }
void setValue(const ColourValue& val)
{
mLight->setSpecularColour(val);
}
void applyDeltaValue(const ColourValue& val)
{
setValue(mLight->getSpecularColour() + val);
}
void setCurrentStateAsBaseValue(void)
{
setAsBaseValue(mLight->getSpecularColour());
}
};
//-----------------------------------------------------------------------
class LightAttenuationValue : public AnimableValue
{
protected:
Light* mLight;
public:
LightAttenuationValue(Light* l) :AnimableValue(VECTOR4)
{ mLight = l; }
void setValue(const Vector4& val)
{
mLight->setAttenuation(val.x, val.y, val.z, val.w);
}
void applyDeltaValue(const Vector4& val)
{
setValue(mLight->getAs4DVector() + val);
}
void setCurrentStateAsBaseValue(void)
{
setAsBaseValue(mLight->getAs4DVector());
}
};
//-----------------------------------------------------------------------
class LightSpotlightInnerValue : public AnimableValue
{
protected:
Light* mLight;
public:
LightSpotlightInnerValue(Light* l) :AnimableValue(REAL)
{ mLight = l; }
void setValue(Real val)
{
mLight->setSpotlightInnerAngle(Radian(val));
}
void applyDeltaValue(Real val)
{
setValue(mLight->getSpotlightInnerAngle().valueRadians() + val);
}
void setCurrentStateAsBaseValue(void)
{
setAsBaseValue(mLight->getSpotlightInnerAngle().valueRadians());
}
};
//-----------------------------------------------------------------------
class LightSpotlightOuterValue : public AnimableValue
{
protected:
Light* mLight;
public:
LightSpotlightOuterValue(Light* l) :AnimableValue(REAL)
{ mLight = l; }
void setValue(Real val)
{
mLight->setSpotlightOuterAngle(Radian(val));
}
void applyDeltaValue(Real val)
{
setValue(mLight->getSpotlightOuterAngle().valueRadians() + val);
}
void setCurrentStateAsBaseValue(void)
{
setAsBaseValue(mLight->getSpotlightOuterAngle().valueRadians());
}
};
//-----------------------------------------------------------------------
class LightSpotlightFalloffValue : public AnimableValue
{
protected:
Light* mLight;
public:
LightSpotlightFalloffValue(Light* l) :AnimableValue(REAL)
{ mLight = l; }
void setValue(Real val)
{
mLight->setSpotlightFalloff(val);
}
void applyDeltaValue(Real val)
{
setValue(mLight->getSpotlightFalloff() + val);
}
void setCurrentStateAsBaseValue(void)
{
setAsBaseValue(mLight->getSpotlightFalloff());
}
};
//-----------------------------------------------------------------------
AnimableValuePtr Light::createAnimableValue(const String& valueName)
{
if (valueName == "diffuseColour")
{
return AnimableValuePtr(
OGRE_NEW LightDiffuseColourValue(this));
}
else if(valueName == "specularColour")
{
return AnimableValuePtr(
OGRE_NEW LightSpecularColourValue(this));
}
else if (valueName == "attenuation")
{
return AnimableValuePtr(
OGRE_NEW LightAttenuationValue(this));
}
else if (valueName == "spotlightInner")
{
return AnimableValuePtr(
OGRE_NEW LightSpotlightInnerValue(this));
}
else if (valueName == "spotlightOuter")
{
return AnimableValuePtr(
OGRE_NEW LightSpotlightOuterValue(this));
}
else if (valueName == "spotlightFalloff")
{
return AnimableValuePtr(
OGRE_NEW LightSpotlightFalloffValue(this));
}
else
{
return MovableObject::createAnimableValue(valueName);
}
}
//-----------------------------------------------------------------------
void Light::setCustomShadowCameraSetup(const ShadowCameraSetupPtr& customShadowSetup)
{
mCustomShadowCameraSetup = customShadowSetup;
}
//-----------------------------------------------------------------------
void Light::resetCustomShadowCameraSetup()
{
mCustomShadowCameraSetup.reset();
}
//-----------------------------------------------------------------------
const ShadowCameraSetupPtr& Light::getCustomShadowCameraSetup() const
{
return mCustomShadowCameraSetup;
}
//-----------------------------------------------------------------------
void Light::setShadowFarDistance(Real distance)
{
mOwnShadowFarDist = true;
mShadowFarDist = distance;
mShadowFarDistSquared = distance * distance;
}
//-----------------------------------------------------------------------
void Light::resetShadowFarDistance(void)
{
mOwnShadowFarDist = false;
}
//-----------------------------------------------------------------------
Real Light::getShadowFarDistance(void) const
{
if (mOwnShadowFarDist)
return mShadowFarDist;
else
return mManager->getShadowFarDistance ();
}
//-----------------------------------------------------------------------
Real Light::getShadowFarDistanceSquared(void) const
{
if (mOwnShadowFarDist)
return mShadowFarDistSquared;
else
return mManager->getShadowFarDistanceSquared ();
}
//---------------------------------------------------------------------
void Light::_setCameraRelative(Camera* cam)
{
mCameraToBeRelativeTo = cam;
#ifdef OGRE_NODELESS_POSITONING
mDerivedCamRelativeDirty = true;
#endif
}
//---------------------------------------------------------------------
Real Light::_deriveShadowNearClipDistance(const Camera* maincam) const
{
if (mShadowNearClipDist > 0)
return mShadowNearClipDist;
else
return maincam->getNearClipDistance();
}
//---------------------------------------------------------------------
Real Light::_deriveShadowFarClipDistance() const
{
if (mShadowFarClipDist >= 0)
return mShadowFarClipDist;
else
{
if (mLightType == LT_DIRECTIONAL)
return 0;
else
return mAttenuation[0];
}
}
//-----------------------------------------------------------------------
void Light::setCustomParameter(uint16 index, const Ogre::Vector4 &value)
{
mCustomParameters[index] = value;
}
//-----------------------------------------------------------------------
const Vector4 &Light::getCustomParameter(uint16 index) const
{
CustomParameterMap::const_iterator i = mCustomParameters.find(index);
if (i != mCustomParameters.end())
{
return i->second;
}
else
{
OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"Parameter at the given index was not found.",
"Light::getCustomParameter");
}
}
//-----------------------------------------------------------------------
void Light::_updateCustomGpuParameter(uint16 paramIndex, const GpuProgramParameters::AutoConstantEntry& constantEntry, GpuProgramParameters *params) const
{
CustomParameterMap::const_iterator i = mCustomParameters.find(paramIndex);
if (i != mCustomParameters.end())
{
params->_writeRawConstant(constantEntry.physicalIndex, i->second,
constantEntry.elementCount);
}
}
//-----------------------------------------------------------------------
bool Light::isInLightRange(const Ogre::Sphere& container) const
{
bool isIntersect = true;
//directional light always intersects (check only spotlight and point)
if (mLightType != LT_DIRECTIONAL)
{
#ifndef OGRE_NODELESS_POSITIONING
const auto& mDerivedDirection = getDerivedDirection();
const auto& mDerivedPosition = mParentNode->_getDerivedPosition();
#endif
//Check that the sphere is within the sphere of the light
isIntersect = container.intersects(Sphere(mDerivedPosition, mAttenuation[0]));
//If this is a spotlight, check that the sphere is within the cone of the spot light
if ((isIntersect) && (mLightType == LT_SPOTLIGHT))
{
//check first check of the sphere surrounds the position of the light
//(this covers the case where the center of the sphere is behind the position of the light
// something which is not covered in the next test).
isIntersect = container.intersects(mDerivedPosition);
//if not test cones
if (!isIntersect)
{
//Calculate the cone that exists between the sphere and the center position of the light
Ogre::Vector3 lightSphereConeDirection = container.getCenter() - mDerivedPosition;
Ogre::Radian halfLightSphereConeAngle = Math::ASin(container.getRadius() / lightSphereConeDirection.length());
//Check that the light cone and the light-position-to-sphere cone intersect)
Radian angleBetweenConeDirections = lightSphereConeDirection.angleBetween(mDerivedDirection);
isIntersect = angleBetweenConeDirections <= halfLightSphereConeAngle + mSpotOuter * 0.5;
}
}
}
return isIntersect;
}
//-----------------------------------------------------------------------
bool Light::isInLightRange(const Ogre::AxisAlignedBox& container) const
{
#ifndef OGRE_NODELESS_POSITIONING
const auto& mDerivedDirection = getDerivedDirection();
const auto& mDerivedPosition = mParentNode->_getDerivedPosition();
#endif
bool isIntersect = true;
//Check the 2 simple / obvious situations. Light is directional or light source is inside the container
if ((mLightType != LT_DIRECTIONAL) && (container.intersects(mDerivedPosition) == false))
{
float range = mAttenuation[0];
//Check that the container is within the sphere of the light
isIntersect = Math::intersects(Sphere(mDerivedPosition, range),container);
//If this is a spotlight, do a more specific check
if ((isIntersect) && (mLightType == LT_SPOTLIGHT) && (mSpotOuter.valueRadians() <= Math::PI))
{
//Create a rough bounding box around the light and check if
Quaternion localToWorld = Vector3::NEGATIVE_UNIT_Z.getRotationTo(mDerivedDirection);
Real boxOffset = Math::Sin(mSpotOuter * 0.5) * range;
AxisAlignedBox lightBoxBound;
lightBoxBound.merge(Vector3::ZERO);
lightBoxBound.merge(localToWorld * Vector3(boxOffset, boxOffset, -range));
lightBoxBound.merge(localToWorld * Vector3(-boxOffset, boxOffset, -range));
lightBoxBound.merge(localToWorld * Vector3(-boxOffset, -boxOffset, -range));
lightBoxBound.merge(localToWorld * Vector3(boxOffset, -boxOffset, -range));
lightBoxBound.setMaximum(lightBoxBound.getMaximum() + mDerivedPosition);
lightBoxBound.setMinimum(lightBoxBound.getMinimum() + mDerivedPosition);
isIntersect = lightBoxBound.intersects(container);
//If the bounding box check succeeded do one more test
if (isIntersect)
{
//Check intersection again with the bounding sphere of the container
//Helpful for when the light is at an angle near one of the vertexes of the bounding box
isIntersect = isInLightRange(Sphere(container.getCenter(),
container.getHalfSize().length()));
}
}
}
return isIntersect;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
String LightFactory::FACTORY_TYPE_NAME = "Light";
//-----------------------------------------------------------------------
const String& LightFactory::getType(void) const
{
return FACTORY_TYPE_NAME;
}
//-----------------------------------------------------------------------
MovableObject* LightFactory::createInstanceImpl( const String& name,
const NameValuePairList* params)
{
Light* light = OGRE_NEW Light(name);
if(params)
{
NameValuePairList::const_iterator ni;
// Setting the light type first before any property specific to a certain light type
if ((ni = params->find("type")) != params->end())
{
if (ni->second == "point")
light->setType(Light::LT_POINT);
else if (ni->second == "directional")
light->setType(Light::LT_DIRECTIONAL);
else if (ni->second == "spotlight")
light->setType(Light::LT_SPOTLIGHT);
else
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Invalid light type '" + ni->second + "'.",
"LightFactory::createInstance");
}
// Common properties
//if ((ni = params->find("position")) != params->end())
// light->setPosition(StringConverter::parseVector3(ni->second));
//if ((ni = params->find("direction")) != params->end())
// light->setDirection(StringConverter::parseVector3(ni->second));
if ((ni = params->find("diffuseColour")) != params->end())
light->setDiffuseColour(StringConverter::parseColourValue(ni->second));
if ((ni = params->find("specularColour")) != params->end())
light->setSpecularColour(StringConverter::parseColourValue(ni->second));
if ((ni = params->find("attenuation")) != params->end())
{
Vector4 attenuation = StringConverter::parseVector4(ni->second);
light->setAttenuation(attenuation.x, attenuation.y, attenuation.z, attenuation.w);
}
if ((ni = params->find("castShadows")) != params->end())
light->setCastShadows(StringConverter::parseBool(ni->second));
if ((ni = params->find("visible")) != params->end())
light->setVisible(StringConverter::parseBool(ni->second));
if ((ni = params->find("powerScale")) != params->end())
light->setPowerScale(StringConverter::parseReal(ni->second));
if ((ni = params->find("shadowFarDistance")) != params->end())
light->setShadowFarDistance(StringConverter::parseReal(ni->second));
// Spotlight properties
if ((ni = params->find("spotlightInner")) != params->end())
light->setSpotlightInnerAngle(StringConverter::parseAngle(ni->second));
if ((ni = params->find("spotlightOuter")) != params->end())
light->setSpotlightOuterAngle(StringConverter::parseAngle(ni->second));
if ((ni = params->find("spotlightFalloff")) != params->end())
light->setSpotlightFalloff(StringConverter::parseReal(ni->second));
}
return light;
}
} // Namespace