/
LightInteractions.cpp
157 lines (115 loc) · 4.71 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
#include "LightInteractions.h"
#include "OpenGLShader.h"
namespace render
{
namespace detail
{
inline void submitSurface(IRenderableSurface& surface)
{
if (surface.getSurfaceTransform().getHandedness() == Matrix4::RIGHTHANDED)
{
glFrontFace(GL_CW);
}
else
{
glFrontFace(GL_CCW);
}
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glMultMatrixd(surface.getSurfaceTransform());
const auto& vertices = surface.getVertices();
const auto& indices = surface.getIndices();
glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().vertex);
glVertexAttribPointer(ATTR_NORMAL, 3, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().normal);
glVertexAttribPointer(ATTR_TEXCOORD, 2, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().texcoord);
glVertexAttribPointer(ATTR_TANGENT, 3, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().tangent);
glVertexAttribPointer(ATTR_BITANGENT, 3, GL_DOUBLE, 0, sizeof(ArbitraryMeshVertex), &vertices.front().bitangent);
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indices.size()), GL_UNSIGNED_INT, &indices.front());
glPopMatrix();
}
}
void LightInteractions::addSurface(IRenderableSurface& surface, IRenderEntity& entity, OpenGLShader& shader)
{
auto& surfacesByMaterial = _surfacesByEntity.emplace(
&entity, SurfacesByMaterial{}).first->second;
auto& surfaces = surfacesByMaterial.emplace(
&shader, SurfaceList{}).first->second;
surfaces.emplace_back(std::ref(surface));
}
void LightInteractions::fillDepthBuffer(OpenGLState& state, RenderStateFlags globalFlagsMask,
const IRenderView& view, std::size_t renderTime)
{
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
for (auto& pair : _surfacesByEntity)
{
auto entity = pair.first;
for (auto& pair : pair.second)
{
auto shader = pair.first;
auto& surfaceList = pair.second;
if (!shader->isVisible()) continue;
// Skip translucent materials
if (shader->getMaterial()->getCoverage() == Material::MC_TRANSLUCENT) continue;
if (!shader->getDepthFillPass()) continue;
// Reset the texture matrix
glMatrixMode(GL_TEXTURE);
glLoadMatrixd(Matrix4::getIdentity());
glMatrixMode(GL_MODELVIEW);
// Apply our state to the current state object
shader->getDepthFillPass()->applyState(state, globalFlagsMask, view.getViewer(), renderTime, entity);
RenderInfo info(state.getRenderFlags(), view.getViewer(), state.cubeMapMode);
for (auto surface : surfaceList)
{
detail::submitSurface(surface.get());
++_drawCalls;
}
}
}
glDisableClientState(GL_VERTEX_ARRAY);
}
void LightInteractions::render(OpenGLState& state, RenderStateFlags globalFlagsMask, const IRenderView& view, std::size_t renderTime)
{
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
for (auto& pair : _surfacesByEntity)
{
auto entity = pair.first;
for (auto& pair : pair.second)
{
auto shader = pair.first;
auto& surfaceList = pair.second;
if (!shader->isVisible()) continue;
shader->foreachPassWithoutDepthPass([&](OpenGLShaderPass& pass)
{
if (!pass.stateIsActive())
{
return;
}
// Reset the texture matrix
glMatrixMode(GL_TEXTURE);
glLoadMatrixd(Matrix4::getIdentity());
glMatrixMode(GL_MODELVIEW);
// Apply our state to the current state object
pass.applyState(state, globalFlagsMask, view.getViewer(), renderTime, entity);
RenderInfo info(state.getRenderFlags(), view.getViewer(), state.cubeMapMode);
for (auto surface : surfaceList)
{
if (state.glProgram)
{
OpenGLShaderPass::setUpLightingCalculation(state, &_light, _worldToLight,
view.getViewer(), surface.get().getSurfaceTransform(), renderTime, state.isColourInverted());
}
detail::submitSurface(surface.get());
++_drawCalls;
}
});
}
}
glDisableClientState(GL_VERTEX_ARRAY);
}
}