-
Notifications
You must be signed in to change notification settings - Fork 47
/
LightInteractions.cpp
316 lines (247 loc) · 11 KB
/
LightInteractions.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "LightInteractions.h"
#include "OpenGLShader.h"
#include "ObjectRenderer.h"
#include "glprogram/GLSLDepthFillAlphaProgram.h"
#include "glprogram/ShadowMapProgram.h"
namespace render
{
void LightInteractions::addObject(IRenderableObject& object, IRenderEntity& entity, OpenGLShader* shader)
{
auto& objectsByMaterial = _objectsByEntity.emplace(
&entity, ObjectsByMaterial{}).first->second;
auto& surfaces = objectsByMaterial.emplace(
shader, ObjectList{}).first->second;
surfaces.emplace_back(std::ref(object));
++_objectCount;
}
bool LightInteractions::isInView(const IRenderView& view)
{
return view.TestAABB(_lightBounds) != VOLUME_OUTSIDE;
}
bool LightInteractions::castsShadows()
{
return _light.getLightEntity().getEntityName() == "light_1";
}
void LightInteractions::collectSurfaces(const IRenderView& view, const std::set<IRenderEntityPtr>& entities)
{
bool shadowCasting = castsShadows();
// Now check all the entities intersecting with this light
for (const auto& entity : entities)
{
entity->foreachRenderableTouchingBounds(_lightBounds,
[&](const IRenderableObject::Ptr& object, Shader* shader)
{
// Skip empty objects
if (!object->isVisible()) return;
// Don't collect invisible shaders
if (!shader->isVisible()) return;
// For non-shadow lights we can cull surfaces that are not in view
if (!shadowCasting)
{
if (object->isOriented())
{
if (view.TestAABB(object->getObjectBounds(), object->getObjectTransform()) == VOLUME_OUTSIDE)
{
return;
}
}
else if (view.TestAABB(object->getObjectBounds()) == VOLUME_OUTSIDE) // non-oriented AABB test
{
return;
}
}
auto glShader = static_cast<OpenGLShader*>(shader);
// We only consider materials designated for camera rendering
if (!glShader->isApplicableTo(RenderViewType::Camera))
{
return;
}
if (!glShader->getInteractionPass())
{
return; // This material doesn't interact with lighting
}
addObject(*object, *entity, glShader);
});
}
}
void LightInteractions::fillDepthBuffer(OpenGLState& state, GLSLDepthFillAlphaProgram& program,
std::size_t renderTime, std::vector<IGeometryStore::Slot>& untransformedObjectsWithoutAlphaTest)
{
std::vector<IGeometryStore::Slot> untransformedObjects;
untransformedObjects.reserve(1000);
for (const auto& [entity, objectsByShader] : _objectsByEntity)
{
for (const auto& [shader, objects] : objectsByShader)
{
auto depthFillPass = shader->getDepthFillPass();
if (!depthFillPass) continue;
setupAlphaTest(state, shader, depthFillPass, program, renderTime, entity);
for (const auto& object : objects)
{
// We submit all objects with an identity matrix in a single multi draw call
if (!object.get().isOriented())
{
if (shader->getMaterial()->getCoverage() == Material::MC_PERFORATED)
{
untransformedObjects.push_back(object.get().getStorageLocation());
}
else
{
// Put it on the huge pile of non-alphatest materials
untransformedObjectsWithoutAlphaTest.push_back(object.get().getStorageLocation());
}
continue;
}
program.setObjectTransform(object.get().getObjectTransform());
ObjectRenderer::SubmitGeometry(object.get().getStorageLocation(), GL_TRIANGLES, _store);
++_depthDrawCalls;
}
// All alpha-tested materials without transform need to be submitted now
if (!untransformedObjects.empty())
{
program.setObjectTransform(Matrix4::getIdentity());
ObjectRenderer::SubmitGeometry(untransformedObjects, GL_TRIANGLES, _store);
++_depthDrawCalls;
untransformedObjects.clear();
}
}
}
}
void LightInteractions::drawShadowMap(OpenGLState& state, const Rectangle& rectangle,
ShadowMapProgram& program, std::size_t renderTime)
{
// Set up the viewport to write to a specific area within the shadow map texture
glViewport(rectangle.x, rectangle.y, 6 * rectangle.width, rectangle.width);
glClear(GL_DEPTH_BUFFER_BIT);
std::vector<IGeometryStore::Slot> untransformedObjects;
untransformedObjects.reserve(1000);
program.setLightOrigin(_light.getLightOrigin());
// Set evaluated stage texture transformation matrix to the GLSL uniform
program.setDiffuseTextureTransform(Matrix4::getIdentity());
// Render all the objects that have a depth filling stage
for (const auto& [entity, objectsByShader] : _objectsByEntity)
{
for (const auto& [shader, objects] : objectsByShader)
{
auto depthFillPass = shader->getDepthFillPass();
if (!depthFillPass) continue;
setupAlphaTest(state, shader, depthFillPass, program, renderTime, entity);
for (const auto& object : objects)
{
// We submit all objects with an identity matrix in a single multi draw call
if (!object.get().isOriented())
{
untransformedObjects.push_back(object.get().getStorageLocation());
continue;
}
program.setObjectTransform(object.get().getObjectTransform());
ObjectRenderer::SubmitInstancedGeometry(object.get().getStorageLocation(), 6, GL_TRIANGLES, _store);
++_shadowMapDrawCalls;
}
if (!untransformedObjects.empty())
{
program.setObjectTransform(Matrix4::getIdentity());
ObjectRenderer::SubmitInstancedGeometry(untransformedObjects, 6, GL_TRIANGLES, _store);
++_shadowMapDrawCalls;
untransformedObjects.clear();
}
}
}
debug::assertNoGlErrors();
}
void LightInteractions::drawInteractions(OpenGLState& state, GLSLBumpProgram& program,
const IRenderView& view, std::size_t renderTime)
{
if (_objectsByEntity.empty())
{
return;
}
auto worldLightOrigin = _light.getLightOrigin();
std::vector<IGeometryStore::Slot> untransformedObjects;
untransformedObjects.reserve(10000);
// Set up textures used by this light
program.setupLightParameters(state, _light, renderTime);
for (const auto& [entity, objectsByShader] : _objectsByEntity)
{
for (const auto& [shader, objects] : objectsByShader)
{
const auto pass = shader->getInteractionPass();
if (!pass || !pass->stateIsActive()) continue;
// Evaluate the expressions in the material stages
pass->evaluateShaderStages(renderTime, entity);
// Enable alphatest if required
if (pass->state().stage0 && pass->state().stage0->hasAlphaTest())
{
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GEQUAL, pass->state().stage0->getAlphaTest());
}
else
{
glDisable(GL_ALPHA_TEST);
}
// Bind textures
OpenGLState::SetTextureState(state.texture0, pass->state().texture0, GL_TEXTURE0, GL_TEXTURE_2D);
OpenGLState::SetTextureState(state.texture1, pass->state().texture1, GL_TEXTURE1, GL_TEXTURE_2D);
OpenGLState::SetTextureState(state.texture2, pass->state().texture2, GL_TEXTURE2, GL_TEXTURE_2D);
// Load stage texture matrices
program.setDiffuseTextureTransform(pass->getDiffuseTextureTransform());
program.setBumpTextureTransform(pass->getBumpTextureTransform());
program.setSpecularTextureTransform(pass->getSpecularTextureTransform());
// Vertex colour mode and diffuse stage colour setup for this pass
program.setStageVertexColour(pass->state().getVertexColourMode(),
pass->state().stage0 ? pass->state().stage0->getColour() : Colour4::WHITE());
for (const auto& object : objects)
{
// We submit all objects with an identity matrix in a single multi draw call
if (!object.get().isOriented())
{
untransformedObjects.push_back(object.get().getStorageLocation());
continue;
}
program.setUpObjectLighting(worldLightOrigin, view.getViewer(),
object.get().getObjectTransform().getInverse());
pass->getProgram().setObjectTransform(object.get().getObjectTransform());
ObjectRenderer::SubmitGeometry(object.get().getStorageLocation(), GL_TRIANGLES, _store);
++_interactionDrawCalls;
}
if (!untransformedObjects.empty())
{
program.setUpObjectLighting(worldLightOrigin, view.getViewer(), Matrix4::getIdentity());
pass->getProgram().setObjectTransform(Matrix4::getIdentity());
ObjectRenderer::SubmitGeometry(untransformedObjects, GL_TRIANGLES, _store);
++_interactionDrawCalls;
untransformedObjects.clear();
}
}
}
// Unbind the light textures
OpenGLState::SetTextureState(state.texture3, 0, GL_TEXTURE3, GL_TEXTURE_2D);
OpenGLState::SetTextureState(state.texture4, 0, GL_TEXTURE4, GL_TEXTURE_2D);
}
void LightInteractions::setupAlphaTest(OpenGLState& state, OpenGLShader* shader, DepthFillPass* depthFillPass,
ISupportsAlphaTest& program, std::size_t renderTime, IRenderEntity* entity)
{
const auto& material = shader->getMaterial();
assert(material);
auto coverage = material->getCoverage();
// Skip translucent materials
if (coverage == Material::MC_TRANSLUCENT) return;
if (coverage == Material::MC_PERFORATED)
{
// Evaluate the shader stages of this material
depthFillPass->evaluateShaderStages(renderTime, entity);
// Apply the alpha test value, it might be affected by time and entity parms
program.setAlphaTest(depthFillPass->getAlphaTestValue());
// If there's a diffuse stage, apply the correct texture
OpenGLState::SetTextureState(state.texture0, depthFillPass->state().texture0, GL_TEXTURE0, GL_TEXTURE_2D);
// Set evaluated stage texture transformation matrix to the GLSL uniform
program.setDiffuseTextureTransform(depthFillPass->getDiffuseTextureTransform());
}
else
{
// No alpha test on this material, pass -1 to deactivate texture sampling
// in the GLSL program
program.setAlphaTest(-1);
}
}
}