KiCad PCB EDA Suite
clight.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #ifndef _CLIGHT_H_
31 #define _CLIGHT_H_
32 
33 #include "ray.h"
34 #include "hitinfo.h"
35 
37 class CLIGHT
38 {
39 public:
40  CLIGHT() { m_castShadow = true; }
41 
42  virtual ~CLIGHT() {}
43 
52  virtual void GetLightParameters( const SFVEC3F &aHitPoint,
53  SFVEC3F &aOutVectorToLight,
54  SFVEC3F &aOutLightColor,
55  float &aOutDistance ) const = 0;
56 
57  void SetCastShadows( bool aCastShadow ) { m_castShadow = aCastShadow; }
58  bool GetCastShadows() const { return m_castShadow; }
59 
60 protected:
62 };
63 
64 
67 class CPOINTLIGHT : public CLIGHT
68 {
69 
70 public:
71  CPOINTLIGHT( const SFVEC3F &aPos, const SFVEC3F &aColor )
72  {
73  m_position = aPos;
74  m_color = aColor;
75  m_att_constant = 0.9f;
76  m_att_linear = 0.0005f;
77  m_att_exp = 0.001f;
78  m_castShadow = true;
79  }
80 
81  // Imported functions from CLIGHT
82 
83  void GetLightParameters( const SFVEC3F &aHitPoint,
84  SFVEC3F &aOutVectorToLight,
85  SFVEC3F &aOutLightColor,
86  float &aOutDistance ) const override
87  {
88  const SFVEC3F vectorLight = m_position - aHitPoint;
89 
90  aOutDistance = glm::length( vectorLight );
91  aOutVectorToLight = vectorLight / aOutDistance; // normalize
92 
93  const float att = 1.0f / ( m_att_constant +
94  m_att_linear * aOutDistance +
95  m_att_exp * aOutDistance * aOutDistance );
96 
97  if( att <= 0.0f )
98  aOutLightColor = SFVEC3F( 0.0f, 0.0f, 0.0f );
99  else
100  aOutLightColor = m_color * att;
101  }
102 
103 private:
106 
109  float m_att_exp;
110 };
111 
112 
114 class CDIRECTIONALLIGHT : public CLIGHT
115 {
116 public:
117  CDIRECTIONALLIGHT( const SFVEC3F &aDir, const SFVEC3F &aColor )
118  {
119  // Invert light direction and make sure it is normalized
120  m_inv_direction = glm::normalize( -aDir );
121  m_color = aColor;
122  m_castShadow = true; // Set as default to cast shadows
123  }
124 
129  void SetDirection( const SFVEC3F &aDir ) { m_inv_direction = -aDir; }
130 
131  // Imported functions from CLIGHT
132 
133  void GetLightParameters( const SFVEC3F &aHitPoint,
134  SFVEC3F &aOutVectorToLight,
135  SFVEC3F &aOutLightColor,
136  float &aOutDistance ) const override
137  {
138  (void)aHitPoint; // unused
139 
140  aOutVectorToLight = m_inv_direction;
141  aOutDistance = std::numeric_limits<float>::infinity();
142  aOutLightColor = m_color;
143  }
144 
145 private:
148 };
149 
150 
151 typedef std::list< CLIGHT * > LIST_LIGHT;
152 
153 
156 {
157 public:
159 
161 
165  void Clear()
166  {
167  if( !m_lights.empty() )
168  {
169  for( LIST_LIGHT::iterator ii = m_lights.begin();
170  ii != m_lights.end();
171  --ii )
172  {
173  delete *ii;
174  *ii = NULL;
175  }
176 
177  m_lights.clear();
178  }
179  }
180 
181 
186  void Add( CLIGHT *aLight )
187  {
188  if( aLight )
189  m_lights.push_back( aLight );
190  }
191 
196  const LIST_LIGHT &GetList() const { return m_lights; }
197 
198 private:
200 };
201 
202 #endif // _CLIGHT_H_
Directional light - a light based only on a direction vector.
Definition: clight.h:114
~CLIGHTCONTAINER()
Definition: clight.h:160
const LIST_LIGHT & GetList() const
GetList - get light list of this container.
Definition: clight.h:196
bool m_castShadow
Definition: clight.h:61
void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
GetLightParameters - Get parameters from this light.
Definition: clight.h:83
CPOINTLIGHT(const SFVEC3F &aPos, const SFVEC3F &aColor)
Definition: clight.h:71
void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
GetLightParameters - Get parameters from this light.
Definition: clight.h:133
SFVEC3F m_inv_direction
oposite direction of the light
Definition: clight.h:146
float m_att_constant
Definition: clight.h:107
float m_att_exp
Definition: clight.h:109
SFVEC3F m_position
Definition: clight.h:104
void Add(CLIGHT *aLight)
Add - Add a light to the container.
Definition: clight.h:186
Point light based on: http://ogldev.atspace.co.uk/www/tutorial20/tutorial20.html. ...
Definition: clight.h:67
CDIRECTIONALLIGHT(const SFVEC3F &aDir, const SFVEC3F &aColor)
Definition: clight.h:117
A base light class to derive to implement other light classes.
Definition: clight.h:37
A light contariner. It will add lights and remove it in the end.
Definition: clight.h:155
bool GetCastShadows() const
Definition: clight.h:58
void SetDirection(const SFVEC3F &aDir)
SetDirection - Set directional light orientation.
Definition: clight.h:129
CLIGHT()
Definition: clight.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
SFVEC3F m_color
Definition: clight.h:105
virtual ~CLIGHT()
Definition: clight.h:42
void SetCastShadows(bool aCastShadow)
Definition: clight.h:57
virtual void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const =0
GetLightParameters - Get parameters from this light.
std::list< CLIGHT * > LIST_LIGHT
Definition: clight.h:151
float m_att_linear
Definition: clight.h:108
void Clear()
Clear - Remove all lights from the container.
Definition: clight.h:165
SFVEC3F m_color
light color
Definition: clight.h:147
LIST_LIGHT m_lights
list of lights
Definition: clight.h:199