Radium Engine  1.5.0
SpotLight.hpp
1 #pragma once
2 
3 #include <Core/Math/Math.hpp>
4 #include <Engine/RaEngine.hpp>
5 #include <Engine/Scene/Light.hpp>
6 
7 namespace Ra {
8 namespace Engine {
9 
10 namespace Scene {
11 class Entity;
15 class RA_ENGINE_API SpotLight final : public Ra::Engine::Scene::Light
16 {
17 
18  public:
19  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
20 
21  explicit SpotLight( Entity* entity, const std::string& name = "spotlight" );
22  ~SpotLight() override = default;
23 
24  void setPosition( const Eigen::Matrix<Scalar, 3, 1>& position ) override;
25  inline const Eigen::Matrix<Scalar, 3, 1>& getPosition() const;
26 
27  void setDirection( const Eigen::Matrix<Scalar, 3, 1>& direction ) override;
28  inline const Eigen::Matrix<Scalar, 3, 1>& getDirection() const;
29 
30  inline void setInnerAngleInRadians( Scalar angle );
31  inline void setOuterAngleInRadians( Scalar angle );
32  inline void setInnerAngleInDegrees( Scalar angle );
33  inline void setOuterAngleInDegrees( Scalar angle );
34 
35  inline Scalar getInnerAngle() const;
36  inline Scalar getOuterAngle() const;
37 
38  inline void setAttenuation( const Attenuation& attenuation );
39  inline void setAttenuation( Scalar constant, Scalar linear, Scalar quadratic );
40  inline const Attenuation& getAttenuation() const;
41 
42  std::string getShaderInclude() const override;
43 
44  private:
45  Eigen::Matrix<Scalar, 3, 1> m_position { 0, 0, 0 };
46  Eigen::Matrix<Scalar, 3, 1> m_direction { 0, -1, 0 };
47 
48  Scalar m_innerAngle { Core::Math::PiDiv4 };
49  Scalar m_outerAngle { Core::Math::PiDiv2 };
50 
51  Attenuation m_attenuation { 1, 0, 0 };
52 };
53 
54 // ---------------------------------------------------------------------------------------------
55 // ---- inline methods implementation
56 
57 inline void SpotLight::setPosition( const Eigen::Matrix<Scalar, 3, 1>& position ) {
58  m_position = position;
59  getRenderParameters().addParameter( "light.spot.position", m_position );
60 }
61 
62 inline const Eigen::Matrix<Scalar, 3, 1>& Scene::SpotLight::getPosition() const {
63  return m_position;
64 }
65 
66 inline void SpotLight::setDirection( const Eigen::Matrix<Scalar, 3, 1>& direction ) {
67  m_direction = direction.normalized();
68  getRenderParameters().addParameter( "light.spot.direction", m_direction );
69 }
70 
71 inline const Eigen::Matrix<Scalar, 3, 1>& SpotLight::getDirection() const {
72  return m_direction;
73 }
74 
75 inline void SpotLight::setInnerAngleInRadians( Scalar angle ) {
76  m_innerAngle = angle;
77  getRenderParameters().addParameter( "light.spot.innerAngle", m_innerAngle );
78 }
79 
80 inline void SpotLight::setOuterAngleInRadians( Scalar angle ) {
81  m_outerAngle = angle;
82  getRenderParameters().addParameter( "light.spot.outerAngle", m_outerAngle );
83 }
84 
85 inline void SpotLight::setInnerAngleInDegrees( Scalar angle ) {
86  m_innerAngle = angle * Core::Math::toRad;
87 }
88 
89 inline void SpotLight::setOuterAngleInDegrees( Scalar angle ) {
90  m_outerAngle = Core::Math::toRad * angle;
91 }
92 
93 inline Scalar SpotLight::getInnerAngle() const {
94  return m_innerAngle;
95 }
96 
97 inline Scalar SpotLight::getOuterAngle() const {
98  return m_outerAngle;
99 }
100 
101 inline void SpotLight::setAttenuation( const Attenuation& attenuation ) {
102  m_attenuation = attenuation;
103  getRenderParameters().addParameter( "light.spot.attenuation.constant", m_attenuation.constant );
104  getRenderParameters().addParameter( "light.spot.attenuation.linear", m_attenuation.linear );
105  getRenderParameters().addParameter( "light.spot.attenuation.quadratic",
106  m_attenuation.quadratic );
107 }
108 
109 inline void SpotLight::setAttenuation( Scalar constant, Scalar linear, Scalar quadratic ) {
110  setAttenuation( { constant, linear, quadratic } );
111 }
112 
113 inline const SpotLight::Attenuation& SpotLight::getAttenuation() const {
114  return m_attenuation;
115 }
116 
117 } // namespace Scene
118 } // namespace Engine
119 } // namespace Ra
void addParameter(const std::string &name, T value, typename std::enable_if<!std::is_class< T > {}, bool >::type=true)
Add a parameter by value.
An entity is an scene element. It ties together components with a transform.
Definition: Entity.hpp:23
const Data::RenderParameters & getRenderParameters() const
Gives read-only access to the renderParameters of the light.
Definition: Light.hpp:95
void setDirection(const Eigen::Matrix< Scalar, 3, 1 > &direction) override
Definition: SpotLight.hpp:66
void setPosition(const Eigen::Matrix< Scalar, 3, 1 > &position) override
Definition: SpotLight.hpp:57
Scalar angle(const Vector_ &v1, const Vector_ &v2)
Definition: Cage.cpp:3