-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.cpp
173 lines (130 loc) · 3.86 KB
/
Light.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Let There Be Light
Copyright (C) 2012 Eric Laukien
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "Light.h"
#include <assert.h>
namespace ltbl
{
Light::Light()
: m_intensity(1.0f),
m_radius(100.0f),
m_center(0.0f, 0.0f),
m_color(1.0f, 1.0f, 1.0f),
m_size(40.0f),
m_updateRequired(true), m_alwaysUpdate(true), m_pStaticTexture(NULL), // For static light
m_pWin(NULL), m_pLightSystem(NULL), m_shaderAttenuation(true),
m_bleed(1.0f), m_linearizeFactor(0.2f)
{
}
Light::~Light()
{
// Destroy the static Texture if one exists
if(m_alwaysUpdate)
delete m_pStaticTexture;
}
AABB* Light::GetAABB()
{
return &m_aabb;
}
bool Light::AlwaysUpdate()
{
return m_alwaysUpdate;
}
void Light::SetAlwaysUpdate(bool always)
{
// Must add to the light system before calling this
assert(m_pWin != NULL);
if(!always && m_alwaysUpdate) // If previously set to false, create a render Texture for the static texture
{
Vec2f dims(m_aabb.GetDims());
// Check if large enough textures are supported
unsigned int maxDim = sf::Texture::getMaximumSize();
if(maxDim <= dims.x || maxDim <= dims.y)
{
std::cout << "Attempted to create a too large static light. Switching to dynamic." << std::endl;
return;
}
// Create the render Texture based on aabb dims
m_pStaticTexture = new sf::RenderTexture();
unsigned int uiDimsX = static_cast<unsigned int>(dims.x);
unsigned int uiDimsY = static_cast<unsigned int>(dims.y);
m_pStaticTexture->create(uiDimsX, uiDimsY, false);
m_pStaticTexture->setSmooth(true);
m_pStaticTexture->setActive();
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
m_pWin->setActive();
m_updateRequired = true;
}
else if(always && !m_alwaysUpdate) // If previously set to true, destroy the render Texture
delete m_pStaticTexture;
m_alwaysUpdate = always;
}
void Light::SwitchStaticTexture()
{
Vec2f dims(m_aabb.GetDims());
m_pStaticTexture->setActive();
glViewport(0, 0, static_cast<unsigned int>(dims.x), static_cast<unsigned int>(dims.y));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Flip the projection, since SFML draws the bound textures (on the FBO) upside down
glOrtho(0, static_cast<unsigned int>(dims.x), static_cast<unsigned int>(dims.y), 0, -100.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
void Light::SetRadius(float radius)
{
assert(m_alwaysUpdate);
m_radius = radius;
CalculateAABB();
TreeUpdate();
}
void Light::IncRadius(float increment)
{
assert(m_alwaysUpdate);
m_radius += increment;
CalculateAABB();
TreeUpdate();
}
float Light::GetRadius()
{
return m_radius;
}
void Light::SetCenter(Vec2f center)
{
Vec2f difference(center - m_center);
m_center = center;
m_aabb.IncCenter(difference);
TreeUpdate();
m_updateRequired = true;
}
void Light::IncCenter(Vec2f increment)
{
m_center += increment;
m_aabb.IncCenter(increment);
TreeUpdate();
m_updateRequired = true;
}
Vec2f Light::GetCenter()
{
return m_center;
}
void Light::CalculateAABB()
{
m_aabb.SetCenter(m_center);
m_aabb.SetDims(Vec2f(m_radius, m_radius));
}
}