NGL  6.5
The NCCA Graphics Library
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
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.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  GNU General Public License for more details.
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <>.
16 */
18 #include "Light.h"
19 #include "ShaderLib.h"
21 //----------------------------------------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------------------------------------
25 namespace ngl
26 {
28 //----------------------------------------------------------------------------------------------------------------------
29 Light::Light(const Vec3 &_pos,const Colour& _col, LightModes _lightMode ) noexcept
30 {
31  // zero now means un-assigned
32  m_position.set(_pos);
33  m_diffuse.set(_col);
34  m_specular.set(_col);
35  m_lightMode = _lightMode;
36  m_constantAtten=1.0f;
37  m_linearAtten=0.0f;
38  m_quadraticAtten=0.0f;
39  m_active=true;
40  // set the w for our light modes
41  m_position.m_w=static_cast<Real>(m_lightMode);
42  // as per the orange book we use this value along with pos.m_w to determine the light
43  // type.
44  m_cutoffAngle=180.0f;
45 }
47 Light::Light(const Light &_l) noexcept
48 {
49  m_position=_l.m_position;
50  m_diffuse=_l.m_diffuse;
51  m_specular=_l.m_specular;
52  m_lightMode = _l.m_lightMode;
53  m_constantAtten=_l.m_constantAtten;
54  m_linearAtten=_l.m_linearAtten;
55  m_quadraticAtten=_l.m_quadraticAtten;
56  m_active=_l.m_active;
57  // set the w for our light modes
58  m_position.m_w=static_cast<Real>(_l.m_lightMode);
59  // as per the orange book we use this value along with pos.m_w to determine the light
60  // type.
61  m_cutoffAngle=_l.m_cutoffAngle;
62 }
64 //----------------------------------------------------------------------------------------------------------------------
65 Light::Light(const Vec3 &_pos,const Colour& _col, const Colour& _specColour,LightModes _lightMode ) noexcept
66 {
67  // zero now means un-assigned
68  m_position.set(_pos);
69  m_diffuse.set(_col);
70  m_specular.set(_specColour);
71  m_lightMode=_lightMode;
72  m_constantAtten=1.0f;
73  m_linearAtten=0.0f;
74  m_quadraticAtten=0.0f;
75  m_active=true;
76  // set the w for our light modes
77  m_position.m_w=static_cast<Real>(m_lightMode);
78  m_cutoffAngle=180.0f;
81 }
83 //----------------------------------------------------------------------------------------------------------------------
84 void Light::enable() noexcept
85 {
86  m_active=true;
87 }
89 //----------------------------------------------------------------------------------------------------------------------
90 void Light::disable() noexcept
91 {
92  m_active=false;
93 }
95 //----------------------------------------------------------------------------------------------------------------------
96 void Light::setAttenuation(Real _constant, Real _linear, Real _quadratic ) noexcept
97 {
98  m_constantAtten=_constant;
99  m_linearAtten=_linear;
100  m_quadraticAtten=_quadratic;
101 }
105 void Light::loadToShader(std::string _uniformName )const noexcept
106 {
109 /*
117  */
118  if(m_active==true)
119  {
121  shader->setShaderParam4f(_uniformName+".position",pos.m_x,pos.m_y,pos.m_z,Real(m_lightMode));
122  shader->setShaderParam4f(_uniformName+".ambient",m_ambient.m_r,m_ambient.m_g,m_ambient.m_b,m_ambient.m_a);
123  shader->setShaderParam4f(_uniformName+".diffuse",m_diffuse.m_r,m_diffuse.m_g,m_diffuse.m_b,m_diffuse.m_a);
124  shader->setShaderParam4f(_uniformName+".specular",m_specular.m_r,m_specular.m_g,m_specular.m_b,m_specular.m_a);
125  shader->setShaderParam1f(_uniformName+".constantAttenuation",m_constantAtten);
126  shader->setShaderParam1f(_uniformName+".linearAttenuation",m_linearAtten);
127  shader->setShaderParam1f(_uniformName+".quadraticAttenuation",m_quadraticAtten);
128  shader->setShaderParam1f(_uniformName+".spotCosCutoff",m_cutoffAngle);
130  }
131  else
132  {
133  // turn light off by setting 0 values
134  shader->setShaderParam4f(_uniformName+".position",0,0,0,Real(m_lightMode));
135  shader->setShaderParam4f(_uniformName+".ambient",0,0,0,0);
136  shader->setShaderParam4f(_uniformName+".diffuse",0,0,0,0);
137  shader->setShaderParam4f(_uniformName+".specular",0,0,0,0);
138  }
139 }
141 void Light::setTransform(Mat4 &_t) noexcept
142 {
143  m_transform=_t;
144 }
146 } // end ngl namespacee
bool m_active
active if true else off
Definition: Light.h:206
Real m_linearAtten
attenuation value for linear falloff
Definition: Light.h:198
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
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
void setTransform(Mat4 &_t) noexcept
set a transform so that the light position is multiplied by this value (default is identity matrix) ...
Definition: Light.cpp:141
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
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
Real m_g
green component of the colour tuple
Definition: Colour.h:164
virtual void enable() noexcept
Enables the light and automatically allocates an OpenGL light ID.
Definition: Light.cpp:84
GLuint shader
Definition: glew.h:1816
an encapsulation of an OpenGL Light
Real m_z
z component
Definition: Vec4.h:323
void loadToShader(std::string _uniformName) const noexcept
method to load the current light values to the shader that is active at present
Definition: Light.cpp:105
void setAttenuation(Real _constant=1.0, Real _linear=0.0, Real _quadratic=0.0) noexcept
set the light attenuation
Definition: Light.cpp:96
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
void disable() noexcept
Disable the light by calling glDisable(GL_LIGHT0+LightNo);.
Definition: Light.cpp:90
Colour m_specular
specular highlight colour used for the lights
Definition: Light.h:182
Real m_x
x component
Definition: Vec4.h:321
static ShaderLib * instance()
Get the instance.
Light() noexcept
default constructor this set nothing so the light will not illuminate
Definition: Light.h:103
Real m_a
alpha the transparent element
Definition: Colour.h:172
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
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
Definition: Light.h:40