KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
25
26#ifndef LIGHT_H
27#define LIGHT_H
28
29#include "ray.h"
30#include "hitinfo.h"
31
32
36class LIGHT
37{
38public:
39 LIGHT() { m_castShadow = true; }
40
41 virtual ~LIGHT() {}
42
52 virtual void GetLightParameters( const SFVEC3F& aHitPoint, SFVEC3F& aOutVectorToLight,
53 SFVEC3F& aOutLightColor, float& aOutDistance ) const = 0;
54
55 void SetCastShadows( bool aCastShadow ) { m_castShadow = aCastShadow; }
56 bool GetCastShadows() const { return m_castShadow; }
57
58protected:
60};
61
62
66class POINT_LIGHT : public LIGHT
67{
68public:
69 POINT_LIGHT( const SFVEC3F& aPos, const SFVEC3F& aColor )
70 {
71 m_position = aPos;
72 m_color = aColor;
73 m_att_constant = 0.9f;
74 m_att_linear = 0.0005f;
75 m_att_exp = 0.001f;
76 m_castShadow = true;
77 }
78
79 // Imported functions from LIGHT
80 void GetLightParameters( const SFVEC3F& aHitPoint, SFVEC3F& aOutVectorToLight,
81 SFVEC3F& aOutLightColor, float& aOutDistance ) const override
82 {
83 const SFVEC3F vectorLight = m_position - aHitPoint;
84
85 aOutDistance = glm::length( vectorLight );
86 aOutVectorToLight = vectorLight / aOutDistance; // normalize
87
88 const float att = 1.0f / ( m_att_constant +
89 m_att_linear * aOutDistance +
90 m_att_exp * aOutDistance * aOutDistance );
91
92 if( att <= 0.0f )
93 aOutLightColor = SFVEC3F( 0.0f, 0.0f, 0.0f );
94 else
95 aOutLightColor = m_color * att;
96 }
97
98private:
101
105};
106
107
112{
113public:
114 DIRECTIONAL_LIGHT( const SFVEC3F& aDir, const SFVEC3F& aColor )
115 {
116 // Invert light direction and make sure it is normalized
117 m_inv_direction = glm::normalize( -aDir );
118 m_color = aColor;
119 m_castShadow = true; // Set as default to cast shadows
120 }
121
127 void SetDirection( const SFVEC3F& aDir ) { m_inv_direction = -aDir; }
128
129 // Imported functions from LIGHT
130 void GetLightParameters( const SFVEC3F& /* aHitPoint */, SFVEC3F& aOutVectorToLight,
131 SFVEC3F& aOutLightColor, float& aOutDistance ) const override
132 {
133 aOutVectorToLight = m_inv_direction;
134 aOutDistance = std::numeric_limits<float>::infinity();
135 aOutLightColor = m_color;
136 }
137
138private:
141};
142
143
144#endif // LIGHT_H
SFVEC3F m_inv_direction
opposite direction of the light
Definition light.h:139
SFVEC3F m_color
light color
Definition light.h:140
void SetDirection(const SFVEC3F &aDir)
Set directional light orientation.
Definition light.h:127
void GetLightParameters(const SFVEC3F &, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
Get parameters from this light.
Definition light.h:130
DIRECTIONAL_LIGHT(const SFVEC3F &aDir, const SFVEC3F &aColor)
Definition light.h:114
bool GetCastShadows() const
Definition light.h:56
bool m_castShadow
Definition light.h:59
LIGHT()
Definition light.h:39
virtual ~LIGHT()
Definition light.h:41
void SetCastShadows(bool aCastShadow)
Definition light.h:55
virtual void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const =0
Get parameters from this light.
float m_att_exp
Definition light.h:104
SFVEC3F m_color
Definition light.h:100
float m_att_linear
Definition light.h:103
void GetLightParameters(const SFVEC3F &aHitPoint, SFVEC3F &aOutVectorToLight, SFVEC3F &aOutLightColor, float &aOutDistance) const override
Get parameters from this light.
Definition light.h:80
POINT_LIGHT(const SFVEC3F &aPos, const SFVEC3F &aColor)
Definition light.h:69
SFVEC3F m_position
Definition light.h:99
float m_att_constant
Definition light.h:102
glm::vec3 SFVEC3F
Definition xv3d_types.h:40