KiCad PCB EDA Suite
light.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 <[email protected]>
5  * Copyright (C) 2015-2021 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 LIGHT_H
31 #define LIGHT_H
32 
33 #include "ray.h"
34 #include "hitinfo.h"
35 
36 
40 class LIGHT
41 {
42 public:
43  LIGHT() { m_castShadow = true; }
44 
45  virtual ~LIGHT() {}
46 
56  virtual void GetLightParameters( const SFVEC3F& aHitPoint, SFVEC3F& aOutVectorToLight,
57  SFVEC3F& aOutLightColor, float& aOutDistance ) const = 0;
58 
59  void SetCastShadows( bool aCastShadow ) { m_castShadow = aCastShadow; }
60  bool GetCastShadows() const { return m_castShadow; }
61 
62 protected:
64 };
65 
66 
70 class POINT_LIGHT : public LIGHT
71 {
72 public:
73  POINT_LIGHT( const SFVEC3F& aPos, const SFVEC3F& aColor )
74  {
75  m_position = aPos;
76  m_color = aColor;
77  m_att_constant = 0.9f;
78  m_att_linear = 0.0005f;
79  m_att_exp = 0.001f;
80  m_castShadow = true;
81  }
82 
83  // Imported functions from LIGHT
84  void GetLightParameters( const SFVEC3F& aHitPoint, SFVEC3F& aOutVectorToLight,
85  SFVEC3F& aOutLightColor, float& aOutDistance ) const override
86  {
87  const SFVEC3F vectorLight = m_position - aHitPoint;
88 
89  aOutDistance = glm::length( vectorLight );
90  aOutVectorToLight = vectorLight / aOutDistance; // normalize
91 
92  const float att = 1.0f / ( m_att_constant +
93  m_att_linear * aOutDistance +
94  m_att_exp * aOutDistance * aOutDistance );
95 
96  if( att <= 0.0f )
97  aOutLightColor = SFVEC3F( 0.0f, 0.0f, 0.0f );
98  else
99  aOutLightColor = m_color * att;
100  }
101 
102 private:
105 
108  float m_att_exp;
109 };
110 
111 
115 class DIRECTIONAL_LIGHT : public LIGHT
116 {
117 public:
118  DIRECTIONAL_LIGHT( const SFVEC3F& aDir, const SFVEC3F& aColor )
119  {
120  // Invert light direction and make sure it is normalized
121  m_inv_direction = glm::normalize( -aDir );
122  m_color = aColor;
123  m_castShadow = true; // Set as default to cast shadows
124  }
125 
131  void SetDirection( const SFVEC3F& aDir ) { m_inv_direction = -aDir; }
132 
133  // Imported functions from LIGHT
134  void GetLightParameters( const SFVEC3F& /* aHitPoint */, SFVEC3F& aOutVectorToLight,
135  SFVEC3F& aOutLightColor, float& aOutDistance ) const override
136  {
137  aOutVectorToLight = m_inv_direction;
138  aOutDistance = std::numeric_limits<float>::infinity();
139  aOutLightColor = m_color;
140  }
141 
142 private:
145 };
146 
147 
148 #endif // LIGHT_H
A base light class to derive to implement other light classes.
Definition: light.h:40
virtual ~LIGHT()
Definition: light.h:45
virtual void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const =0
Get parameters from this light.
float m_att_linear
Definition: light.h:107
Point light source based on http://ogldev.atspace.co.uk/www/tutorial20/tutorial20....
Definition: light.h:70
bool GetCastShadows() const
Definition: light.h:60
bool m_castShadow
Definition: light.h:63
DIRECTIONAL_LIGHT(const SFVEC3F &aDir, const SFVEC3F &aColor)
Definition: light.h:118
void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
Get parameters from this light.
Definition: light.h:84
SFVEC3F m_color
Definition: light.h:104
SFVEC3F m_position
Definition: light.h:103
SFVEC3F m_inv_direction
opposite direction of the light
Definition: light.h:143
void SetCastShadows(bool aCastShadow)
Definition: light.h:59
float m_att_exp
Definition: light.h:108
A light source based only on a directional vector.
Definition: light.h:115
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
SFVEC3F m_color
light color
Definition: light.h:144
LIGHT()
Definition: light.h:43
void SetDirection(const SFVEC3F &aDir)
Set directional light orientation.
Definition: light.h:131
float m_att_constant
Definition: light.h:106
void GetLightParameters(const SFVEC3F &, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
Get parameters from this light.
Definition: light.h:134
POINT_LIGHT(const SFVEC3F &aPos, const SFVEC3F &aColor)
Definition: light.h:73