-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightHandler.cpp
More file actions
156 lines (120 loc) · 4.29 KB
/
Copy pathLightHandler.cpp
File metadata and controls
156 lines (120 loc) · 4.29 KB
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
#include "LightHandler.h"
#include <vector>
#include "Debugger.h"
#include "Scene.h"
#include "ErrorHandler.h"
namespace LightHandler
{
namespace
{
//for rendering the "lights". They are just squares. Uses a geometry shader
Shader* lightShader;
//for generating the depthMap for multiple lights
Shader* depthShader;
std::vector<Light*> lights;
//for multi light shadows
unsigned int worldFBO, worldShadows;
bool fullyBuilt = false;
MovingLight* movingLight;
void InitWorldShadows()
{
glGenTextures(1, &worldShadows);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, worldShadows);
//note: maxlights needs to be multiplied by 6 because there are 6 sides to a cube map
glTexImage3D(GL_TEXTURE_CUBE_MAP_ARRAY, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, MAX_LIGHTS * 6, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glGenFramebuffers(1, &worldFBO);
glBindFramebuffer(GL_FRAMEBUFFER, worldFBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, worldShadows, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
//rebind back to normal
glBindFramebuffer(GL_FRAMEBUFFER, 0);
ErrorHandler::ErrCheck("World shadow init");
}
}
void Init()
{
lightShader = new Shader("Shaders/Light.vert", "Shaders/Light.frag", "Shaders/Light.geom");
depthShader = new Shader("Shaders/Voxel.vert", "Shaders/Depth.frag", "Shaders/Voxel.geom");
movingLight = new MovingLight(glm::vec3(37, 104, 37));
AddLight(movingLight);
InitWorldShadows();
}
void RenderLights(glm::mat4 projection, glm::mat4 view)
{
lightShader->use();
lightShader->setMat4("projection", projection);
lightShader->setMat4("view", view);
for(auto light : lights)
{
light->Render(lightShader);
}
}
void BuildShadows(Scene* scene)
{
//move movinglight to camera position
if(Input::GetKeyPressed(KeyCode::M))
{
movingLight->ChangeOffset(Camera::GetPosition());
}
//build multi light shadows
depthShader->use();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//index for each lights within the cubemap array
unsigned int i = 0;
for(auto light : lights)
{
ErrorHandler::ErrCheck("light handler: lights");
//only update lights that are either realtime lights of when a light needs to rebuild its shadow map
if(light->IsRealtime() || !fullyBuilt)
{
light->BuildShadowMap(depthShader, scene, worldFBO, worldShadows, i);
}
//increase for each light.
++i;
}
fullyBuilt = true;
}
void SetUpLights(Shader* shader)
{
shader->setInt("totalLights", lights.size());
for(unsigned int i = 0; i < lights.size(); ++i)
{
lights[i]->SetUpLight(shader, i);
}
shader->setFloat("far_plane", FAR_PLANE);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, worldShadows);
}
void AddLight(Light* light)
{
//limit total lights
if(lights.size() < MAX_LIGHTS)
{
lights.push_back(light);
}
}
void RemoveLight(Light* light)
{
for(unsigned int i = 0; i < lights.size(); ++i)
{
if(lights[i] == light)
{
lights.erase(lights.begin() + i);
}
}
}
int GetTotalLightsInUse()
{
return lights.size();
}
void FullRebuildRequired()
{
fullyBuilt = false;
}
}