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
40class LIGHT
41{
42public:
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
62protected:
64};
65
66
70class POINT_LIGHT : public LIGHT
71{
72public:
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
102private:
105
109};
110
111
116{
117public:
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
142private:
145};
146
147
148#endif // LIGHT_H
A light source based only on a directional vector.
Definition: light.h:116
SFVEC3F m_inv_direction
opposite direction of the light
Definition: light.h:143
SFVEC3F m_color
light color
Definition: light.h:144
void SetDirection(const SFVEC3F &aDir)
Set directional light orientation.
Definition: light.h:131
void GetLightParameters(const SFVEC3F &, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
Get parameters from this light.
Definition: light.h:134
DIRECTIONAL_LIGHT(const SFVEC3F &aDir, const SFVEC3F &aColor)
Definition: light.h:118
A base light class to derive to implement other light classes.
Definition: light.h:41
bool GetCastShadows() const
Definition: light.h:60
bool m_castShadow
Definition: light.h:63
LIGHT()
Definition: light.h:43
virtual ~LIGHT()
Definition: light.h:45
void SetCastShadows(bool aCastShadow)
Definition: light.h:59
virtual void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const =0
Get parameters from this light.
Point light source based on http://ogldev.atspace.co.uk/www/tutorial20/tutorial20....
Definition: light.h:71
float m_att_exp
Definition: light.h:108
SFVEC3F m_color
Definition: light.h:104
float m_att_linear
Definition: light.h:107
void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
Get parameters from this light.
Definition: light.h:84
POINT_LIGHT(const SFVEC3F &aPos, const SFVEC3F &aColor)
Definition: light.h:73
SFVEC3F m_position
Definition: light.h:103
float m_att_constant
Definition: light.h:106
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44