NGL  6.5
The NCCA Graphics Library
SpotLight.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 //---------------------------------------------------------------------------
18 #include "SpotLight.h"
19 #include "ShaderLib.h"
20 //----------------------------------------------------------------------------------------------------------------------
23 //----------------------------------------------------------------------------------------------------------------------
24 
25 namespace ngl
26 {
27 
28 //----------------------------------------------------------------------------------------------------------------------
29 SpotLight::SpotLight(const Vec3& _pos, const Vec3& _aim, const Colour& _col ) noexcept:
30  Light( _pos, _col,LightModes::SPOTLIGHT )
31 {
32  // set up m_direction and default values
33  m_dir=_aim-_pos;
34  m_dir.normalize();
35  m_dir[3]=0;
36 
37  // set some good default values
38  m_cutoffAngle = 45.0f;
39  m_innerCutoffAngle=25.0f;
40  m_spotExponent = 2.0f;
41 
42  m_constantAtten = 1.5f;
43  m_linearAtten = 0.0f;
44  m_quadraticAtten = 0.0f;
45  m_position=_pos;
46  m_aim=_aim;
47  m_transform.identity();
48  m_lightMode=LightModes::SPOTLIGHT;
49 }
50 
51 SpotLight::SpotLight(const SpotLight &_l) noexcept: Light(_l)
52 {
53  m_aim=_l.m_aim;
54  m_transform.identity();
55  m_lightMode=LightModes::SPOTLIGHT;
56 
57 }
58 
59 
60 //----------------------------------------------------------------------------------------------------------------------
61 void SpotLight::set(const Vec3 &_pos, const Vec3 &_dir,const Colour& _col ) noexcept
62 {
63  // set up m_direction and default values
64  m_dir = _dir;
65  m_dir.normalize();
66  m_dir[3]=0;
67  m_diffuse=_col;
68  m_position=_pos;
69  m_position[3]=1;
70  // set some good default values
71  m_cutoffAngle = 45.0f;
72  m_spotExponent = 0.0f;
73  m_constantAtten = 1.5f;
74  m_linearAtten = 0.0f;
75  m_quadraticAtten = 0.0f;
76  m_transform.identity();
77  m_lightMode=LightModes::SPOTLIGHT;
78 
79 
80 }
81 
82 
83 //----------------------------------------------------------------------------------------------------------------------
84 void SpotLight::aim( const Vec4& _pos ) noexcept
85 {
86  Vec4 dir= _pos-m_position;
87  // this is a vector so set 0 component
88  dir[3]=0;
89  dir.normalize();
90  m_aim=_pos;
91  m_dir=dir;
92 }
93 
94 
95 
96 //----------------------------------------------------------------------------------------------------------------------
97 void SpotLight::enable() noexcept
98 {
99  m_active=true;
100 }
101 
102 //----------------------------------------------------------------------------------------------------------------------
103 void SpotLight::setParams( const Real _cutoff,const Real _exponent, const Real _constant, const Real _linear, const Real _quadratic ) noexcept
104 {
105  // we need to convert this to the correct values
106  m_cutoffAngle=cos(radians(_cutoff));
107  m_spotExponent=_exponent;
108  m_constantAtten=_constant;
109  m_linearAtten=_linear;
110  m_quadraticAtten=_quadratic;
111 }
112 
113 void SpotLight::setCutoff(const Real &_cutoff) noexcept
114 {
115  m_cutoffAngle=cos(radians(_cutoff));
116 }
117 void SpotLight::setInnerCutoff(const Real &_cutoff) noexcept
118 {
119  m_innerCutoffAngle=cos(radians(_cutoff));
120 }
121 void SpotLight::loadToShader( std::string _uniformName)const noexcept
122 {
123 
137  if(m_active==true)
138  {
140  Vec4 dir=m_transform*m_dir;
141  shader->setShaderParam4f(_uniformName+".position",pos.m_x,pos.m_y,pos.m_z,float(m_lightMode));
142  shader->setShaderParam3f(_uniformName+".direction",dir.m_x,dir.m_y,dir.m_z);
143  shader->setShaderParam4f(_uniformName+".ambient",m_ambient.m_r,m_ambient.m_g,m_ambient.m_b,m_ambient.m_a);
144  shader->setShaderParam4f(_uniformName+".diffuse",m_diffuse.m_r,m_diffuse.m_g,m_diffuse.m_b,m_diffuse.m_a);
145  shader->setShaderParam4f(_uniformName+".specular",m_specular.m_r,m_specular.m_g,m_specular.m_b,m_specular.m_a);
146  shader->setShaderParam1f(_uniformName+".spotCosCutoff",m_cutoffAngle);
147  shader->setShaderParam1f(_uniformName+".spotCosInnerCutoff",m_innerCutoffAngle);
148  shader->setShaderParam1f(_uniformName+".spotExponent",m_spotExponent);
149  shader->setShaderParam1f(_uniformName+".constantAttenuation",m_constantAtten);
150  shader->setShaderParam1f(_uniformName+".linearAttenuation",m_linearAtten);
151  shader->setShaderParam1f(_uniformName+".quadraticAttenuation",m_quadraticAtten);
152  }
153  else
154  {
155  // turn light off by setting 0 values
156  shader->setShaderParam4f(_uniformName+".position",0,0,0,Real(m_lightMode));
157  shader->setShaderParam4f(_uniformName+".ambient",0,0,0,0);
158  shader->setShaderParam4f(_uniformName+".diffuse",0,0,0,0);
159  shader->setShaderParam4f(_uniformName+".specular",0,0,0,0);
160  }
161 }
162 void SpotLight::setTransform(Mat4 &_t) noexcept
163 {
164  m_transform=_t;
165 }
166 
167 
168 
169 } // end ngl namespace
170 
171 //----------------------------------------------------------------------------------------------------------------------
bool m_active
active if true else off
Definition: Light.h:206
void loadToShader(std::string _uniformName) const noexcept
this method loads the spotlight values to the shader
Definition: SpotLight.cpp:121
Real m_linearAtten
attenuation value for linear falloff
Definition: Light.h:198
Real m_spotExponent
the SpotExponent value
Definition: SpotLight.h:149
Real m_cutoffAngle
the spot cutoff angle for directional and point lights this is set to 180 by default, and other values are cos(radians(angle))
Definition: Light.h:211
Real m_constantAtten
attenuation value for constant falloff
Definition: Light.h:194
Vec4 & normalize() noexcept
Normalize the vector using .
Definition: Vec4.cpp:99
simple class to hold colour information and set the basic opengl colour state. also has overloaded me...
Definition: Colour.h:40
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
Vec4 m_position
m_pos is used to store the light position w used for point / dir light values
Definition: Light.h:174
Real m_r
red component of the colour tuple
Definition: Colour.h:160
void setShaderParam1f(const std::string &_paramName, float _p1) noexcept
set a shader param by name for 1 float param note that the shader must be the currently active shader...
Definition: ShaderLib.cpp:153
Real m_b
blue component of the colour tuple
Definition: Colour.h:168
Simple class to encapsulate OpenGL Light functions this will fill in the following structure...
Definition: Light.h:68
Singleton Class to init and Use GLSL Shaders the class stores the shaders as a map of shader objects ...
Definition: ShaderLib.h:55
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
main shader loader / manager class for GLSL shaders
implementation files for RibExport class
Definition: AABB.cpp:22
void enable() noexcept
enables the spot light
Definition: SpotLight.cpp:97
Encapsulation of OpenGL spotlight inherits from the Light class.
Definition: SpotLight.h:58
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
void set(const Vec3 &_pos, const Vec3 &_dir, const Colour &_col) noexcept
a method to set the spotlight parameters
Definition: SpotLight.cpp:61
Real m_g
green component of the colour tuple
Definition: Colour.h:164
GLuint shader
Definition: glew.h:1816
void setCutoff(const Real &_cutoff) noexcept
sets the spot cutoff
Definition: SpotLight.cpp:113
Real m_z
z component
Definition: Vec4.h:323
void setInnerCutoff(const Real &_cutoff) noexcept
sets the spot inner cutoff
Definition: SpotLight.cpp:117
Colour m_ambient
ambient light colour used for the lights
Definition: Light.h:186
Colour m_diffuse
Colour used to give the light a colour.
Definition: Light.h:178
Encapsulation of OpenGL spotlight inherits from the Light class.
Colour m_specular
specular highlight colour used for the lights
Definition: Light.h:182
void setShaderParam3f(const std::string &_paramName, float _p1, float _p2, float _p3) noexcept
set a shader param by name for 3 float params note that the shader must be the currently active shade...
Definition: ShaderLib.cpp:114
Real m_x
x component
Definition: Vec4.h:321
Vec4 m_dir
the spot light direction vector
Definition: SpotLight.h:157
static ShaderLib * instance()
Get the instance.
Real m_a
alpha the transparent element
Definition: Colour.h:172
void setParams(const Real _cutoff, const Real _exponent, const Real _constant, const Real _linear, const Real _quadratic) noexcept
sets the various spot light params
Definition: SpotLight.cpp:103
SpotLight() noexcept
default ctor
Definition: SpotLight.h:65
NGL_DLLEXPORT Real radians(const Real _deg)
converts Degrees to Radians
Definition: Util.cpp:89
Real m_innerCutoffAngle
the spot inner cutoff angle
Definition: SpotLight.h:145
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
void setShaderParam4f(const std::string &_paramName, float _p1, float _p2, float _p3, float _p4) noexcept
set a shader param by name for 4 float params note that the shader must be the currently active shade...
Definition: ShaderLib.cpp:102
void setTransform(Mat4 &_t) noexcept
set the transform of the light this will be multiplied by the position Vec4 pos=m_transform*m_positio...
Definition: SpotLight.cpp:162
void aim(const Vec4 &_pos) noexcept
This function sets the light to aim at the specified point.
Definition: SpotLight.cpp:84
Real m_y
y component
Definition: Vec4.h:322
GLsizei const GLchar *const * string
Definition: glew.h:1847
Mat4 m_transform
the transform applied to the light before loading to the shader this is usually the inverse projectio...
Definition: Light.h:217
Real m_quadraticAtten
attenuation value for Quadratic falloff
Definition: Light.h:202
LightModes m_lightMode
used to hold the current lights mode
Definition: Light.h:190