-
Notifications
You must be signed in to change notification settings - Fork 47
/
RegularLight.h
190 lines (147 loc) · 5.13 KB
/
RegularLight.h
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
#pragma once
#include <map>
#include <vector>
#include <set>
#include "irender.h"
#include "irenderableobject.h"
#include "iobjectrenderer.h"
#include "irenderview.h"
#include "render/Rectangle.h"
#include "InteractionPass.h"
namespace render
{
class OpenGLState;
class OpenGLShader;
class DepthFillAlphaProgram;
class InteractionProgram;
class ShadowMapProgram;
class DepthFillPass;
/**
* Depth-buffer filling light with diffuse/bump/specular interactions
* between this light and one or more entity renderables.
* Objects are grouped by entity, then by shader.
*
* Instances only live through the course of a single render pass, therefore direct
* references without ref-counting are used.
*/
class RegularLight
{
public:
// A flat list of renderables
using ObjectList = std::vector<std::reference_wrapper<IRenderableObject>>;
private:
RendererLight& _light;
IGeometryStore& _store;
IObjectRenderer& _objectRenderer;
AABB _lightBounds;
// All objects, grouped by material
using ObjectsByMaterial = std::map<OpenGLShader*, ObjectList>;
// object mappings, grouped by entity
std::map<IRenderEntity*, ObjectsByMaterial> _objectsByEntity;
std::size_t _interactionDrawCalls;
std::size_t _depthDrawCalls;
std::size_t _objectCount;
std::size_t _shadowMapDrawCalls;
int _shadowLightIndex;
bool _isShadowCasting;
// Helper object submitting a DBS interaction draw call
class InteractionDrawCall
{
private:
OpenGLState& _state;
InteractionProgram& _program;
IObjectRenderer& _objectRenderer;
const Vector3& _worldLightOrigin;
const Vector3& _viewer;
const InteractionPass::Stage* _bump;
const InteractionPass::Stage* _diffuse;
const InteractionPass::Stage* _specular;
std::vector<IGeometryStore::Slot> _untransformedObjects;
InteractionPass::Stage _defaultBumpStage;
InteractionPass::Stage _defaultDiffuseStage;
InteractionPass::Stage _defaultSpecularStage;
std::size_t _interactionDrawCalls;
public:
InteractionDrawCall(OpenGLState& state, InteractionProgram& program,
IObjectRenderer& objectRenderer, const Vector3& worldLightOrigin, const Vector3& viewer);
std::size_t getInteractionDrawCalls() const
{
return _interactionDrawCalls;
}
void prepare(InteractionPass& pass)
{
_bump = nullptr;
_diffuse = nullptr;
_specular = nullptr;
_defaultBumpStage.texture = pass.getDefaultInteractionTextureBinding(IShaderLayer::BUMP);
_defaultDiffuseStage.texture = pass.getDefaultInteractionTextureBinding(IShaderLayer::DIFFUSE);
_defaultSpecularStage.texture = pass.getDefaultInteractionTextureBinding(IShaderLayer::SPECULAR);
}
bool hasBump() const
{
return _bump != nullptr;
}
bool hasDiffuse() const
{
return _diffuse != nullptr;
}
bool hasSpecular() const
{
return _specular != nullptr;
}
void setBump(const InteractionPass::Stage* bump);
void setDiffuse(const InteractionPass::Stage* diffuse);
void setSpecular(const InteractionPass::Stage* specular);
void submit(const ObjectList& objects);
};
public:
RegularLight(RendererLight& light, IGeometryStore& store, IObjectRenderer& objectRenderer);
RegularLight(RegularLight&& other) = default;
const Vector3& getBoundsCenter() const
{
return _lightBounds.getOrigin();
}
int getShadowLightIndex() const
{
return _shadowLightIndex;
}
void setShadowLightIndex(int index)
{
_shadowLightIndex = index;
}
const RendererLight& getLight() const
{
return _light;
}
std::size_t getInteractionDrawCalls() const
{
return _interactionDrawCalls;
}
std::size_t getDepthDrawCalls() const
{
return _depthDrawCalls;
}
std::size_t getShadowMapDrawCalls() const
{
return _shadowMapDrawCalls;
}
std::size_t getObjectCount() const
{
return _objectCount;
}
std::size_t getEntityCount() const
{
return _objectsByEntity.size();
}
void addObject(IRenderableObject& object, IRenderEntity& entity, OpenGLShader* shader);
bool isInView(const IRenderView& view);
bool isShadowCasting() const;
void collectSurfaces(const IRenderView& view, const std::set<IRenderEntityPtr>& entities);
void fillDepthBuffer(OpenGLState& state, DepthFillAlphaProgram& program,
std::size_t renderTime, std::vector<IGeometryStore::Slot>& untransformedObjectsWithoutAlphaTest);
void drawShadowMap(OpenGLState& state, const Rectangle& rectangle, ShadowMapProgram& program, std::size_t renderTime);
void drawInteractions(OpenGLState& state, InteractionProgram& program, const IRenderView& view, std::size_t renderTime);
void setupAlphaTest(OpenGLState& state, OpenGLShader* shader, DepthFillPass* depthFillPass,
ISupportsAlphaTest& alphaTestProgram, std::size_t renderTime, IRenderEntity* entity);
};
}