forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalComposite.cpp
377 lines (334 loc) · 13.2 KB
/
FinalComposite.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "FinalComposite.hpp"
#include "PresetState.hpp"
#include <cstddef>
static std::string const defaultCompositeShader = "shader_body\n{\nret = tex2D(sampler_main, uv).xyz;\n}";
FinalComposite::FinalComposite()
{
RenderItem::Init();
}
void FinalComposite::InitVertexAttrib()
{
glGenBuffers(1, &m_elementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), nullptr); // Positions
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), reinterpret_cast<void*>(offsetof(MeshVertex, r))); // Colors
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), reinterpret_cast<void*>(offsetof(MeshVertex, u))); // Textures
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(MeshVertex), reinterpret_cast<void*>(offsetof(MeshVertex, radius))); // Radius/Angle
}
void FinalComposite::LoadCompositeShader(const PresetState& presetState)
{
if (presetState.compositeShaderVersion > 0)
{
m_compositeShader = std::make_unique<MilkdropShader>(MilkdropShader::ShaderType::CompositeShader);
if (!presetState.warpShader.empty())
{
try
{
m_compositeShader->LoadCode(presetState.compositeShader);
#ifdef MILKDROP_PRESET_DEBUG
std::cerr << "[Composite Shader] Loaded composite shader code." << std::endl;
#endif
}
catch (ShaderException& ex)
{
#ifdef MILKDROP_PRESET_DEBUG
std::cerr << "[Composite Shader] Error loading composite warp shader code:" << ex.message() << std::endl;
std::cerr << "[Composite Shader] Using fallback shader." << std::endl;
#endif
// Fall back to default shader
m_compositeShader = std::make_unique<MilkdropShader>(MilkdropShader::ShaderType::CompositeShader);
m_compositeShader->LoadCode(defaultCompositeShader);
}
}
else
{
m_compositeShader->LoadCode(defaultCompositeShader);
#ifdef MILKDROP_PRESET_DEBUG
std::cerr << "[Composite Shader] Loaded default composite shader code." << std::endl;
#endif
}
}
else
{
// Video echo OR gamma adjustment with random hue.
m_videoEcho = std::make_unique<VideoEcho>(presetState);
if (presetState.brighten ||
presetState.darken ||
presetState.solarize ||
presetState.invert)
{
m_filters = std::make_unique<Filters>(presetState);
}
}
}
void FinalComposite::CompileCompositeShader(PresetState& presetState)
{
if (m_compositeShader)
{
try
{
m_compositeShader->LoadTexturesAndCompile(presetState);
#ifdef MILKDROP_PRESET_DEBUG
std::cerr << "[Composite Shader] Successfully compiled composite shader code." << std::endl;
#endif
}
catch (ShaderException& ex)
{
#ifdef MILKDROP_PRESET_DEBUG
std::cerr << "[Composite Shader] Error compiling composite warp shader code:" << ex.message() << std::endl;
std::cerr << "[Composite Shader] Using fallback shader." << std::endl;
#endif
// Fall back to default shader
m_compositeShader = std::make_unique<MilkdropShader>(MilkdropShader::ShaderType::CompositeShader);
m_compositeShader->LoadCode(defaultCompositeShader);
m_compositeShader->LoadTexturesAndCompile(presetState);
}
}
}
void FinalComposite::Draw(const PresetState& presetState, const PerFrameContext& perFrameContext)
{
if (m_compositeShader)
{
InitializeMesh(presetState);
ApplyHueShaderColors(presetState);
// Render the grid
glDisable(GL_BLEND);
glBindVertexArray(m_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(MeshVertex) * vertexCount, m_vertices.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_compositeShader->LoadVariables(presetState, perFrameContext);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr);
}
else
{
// Apply old-school filters
m_videoEcho->Draw();
if (m_filters)
{
m_filters->Draw();
}
}
glBindVertexArray(0);
Shader::Unbind();
}
auto FinalComposite::HasCompositeShader() const -> bool
{
return m_compositeShader != nullptr;
}
void FinalComposite::InitializeMesh(const PresetState& presetState)
{
if (m_viewportWidth == presetState.renderContext.viewportSizeX &&
m_viewportHeight == presetState.renderContext.viewportSizeY)
{
return;
}
float const halfTexelWidth = 0.5f / static_cast<float>(presetState.renderContext.viewportSizeX);
float const halfTexelHeight = 0.5f / static_cast<float>(presetState.renderContext.viewportSizeY);
float const dividedByX = 1.0f / static_cast<float>(compositeGridWidth - 2);
float const dividedByY = 1.0f / static_cast<float>(compositeGridHeight - 2);
constexpr float PI = 3.1415926535898f;
for (int gridY = 0; gridY < compositeGridHeight; gridY++)
{
int const gridY2 = gridY - gridY / (compositeGridHeight / 2);
float const v = SquishToCenter(gridY2 * dividedByY, 3.0f);
float const sy = -((v - halfTexelHeight) * 2.0f - 1.0f);
for (int gridX = 0; gridX < compositeGridWidth; gridX++)
{
int const gridX2 = gridX - gridX / (compositeGridWidth / 2);
float const u = SquishToCenter(gridX2 * dividedByX, 3.0f);
float const sx = (u - halfTexelWidth) * 2.0f - 1.0f;
auto& vertex = m_vertices.at(gridX + gridY * compositeGridWidth);
vertex.x = sx;
vertex.y = sy;
float rad;
float ang;
UvToMathSpace(presetState.renderContext.aspectX,
presetState.renderContext.aspectY,
u, v, rad, ang);
// fix-ups:
if (gridX == compositeGridWidth / 2 - 1)
{
if (gridY < compositeGridHeight / 2 - 1)
{
ang = PI * 1.5f;
}
else if (gridY == compositeGridHeight / 2 - 1)
{
ang = PI * 1.25f;
}
else if (gridY == compositeGridHeight / 2)
{
ang = PI * 0.75f;
}
else
{
ang = PI * 0.5f;
}
}
else if (gridX == compositeGridWidth / 2)
{
if (gridY < compositeGridHeight / 2 - 1)
{
ang = PI * 1.5f;
}
else if (gridY == compositeGridHeight / 2 - 1)
{
ang = PI * 1.75f;
}
else if (gridY == compositeGridHeight / 2)
{
ang = PI * 0.25f;
}
else
{
ang = PI * 0.5f;
}
}
else if (gridY == compositeGridHeight / 2 - 1)
{
if (gridX < compositeGridWidth / 2 - 1)
{
ang = PI * 1.0f;
}
else
{
ang = PI * 2.0f;
}
}
else if (gridY == compositeGridHeight / 2)
{
if (gridX < compositeGridWidth / 2 - 1)
{
ang = PI * 1.0f;
}
else
{
ang = PI * 0.0f;
}
}
vertex.u = u;
vertex.v = v;
vertex.radius = rad;
vertex.angle = ang;
}
}
// build index list for final composite blit -
// order should be friendly for interpolation of 'ang' value!
int currentIndex = 0;
for (int gridY = 0; gridY < compositeGridHeight - 1; gridY++)
{
if (gridY == compositeGridHeight / 2 - 1)
{
continue;
}
for (int gridX = 0; gridX < compositeGridWidth - 1; gridX++)
{
if (gridX == compositeGridWidth / 2 - 1)
{
continue;
}
bool const leftHalf = (gridX < compositeGridWidth / 2);
bool const topHalf = (gridY < compositeGridHeight / 2);
bool const center4 = ((gridX == compositeGridWidth / 2) &&
(gridY == compositeGridHeight / 2));
if ((static_cast<int>(leftHalf) + static_cast<int>(topHalf) + static_cast<int>(center4)) % 2 == 1)
{
m_indices[currentIndex + 0] = gridY * compositeGridWidth + gridX;
m_indices[currentIndex + 1] = gridY * compositeGridWidth + gridX + 1;
m_indices[currentIndex + 2] = (gridY + 1) * compositeGridWidth + gridX + 1;
m_indices[currentIndex + 3] = (gridY + 1) * compositeGridWidth + gridX + 1;
m_indices[currentIndex + 4] = (gridY + 1) * compositeGridWidth + gridX;
m_indices[currentIndex + 5] = gridY * compositeGridWidth + gridX;
}
else
{
m_indices[currentIndex + 0] = (gridY + 1) * compositeGridWidth + (gridX);
m_indices[currentIndex + 1] = (gridY) *compositeGridWidth + (gridX);
m_indices[currentIndex + 2] = (gridY) *compositeGridWidth + (gridX + 1);
m_indices[currentIndex + 3] = (gridY) *compositeGridWidth + (gridX + 1);
m_indices[currentIndex + 4] = (gridY + 1) * compositeGridWidth + (gridX + 1);
m_indices[currentIndex + 5] = (gridY + 1) * compositeGridWidth + (gridX);
}
currentIndex += 6;
}
}
// Store indices.
// ToDo: Probably don't need to store m_indices
glBindVertexArray(m_vaoID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * m_indices.size(), m_indices.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
}
float FinalComposite::SquishToCenter(float x, float exponent)
{
if (x > 0.5f)
{
return powf(x * 2.0f - 1.0f, exponent) * 0.5f + 0.5f;
}
return (1.0f - powf(1.0f - x * 2.0f, exponent)) * 0.5f;
}
void FinalComposite::UvToMathSpace(float aspectX, float aspectY,
float u, float v, float& rad, float& ang)
{
// (screen space = -1..1 on both axes; corresponds to UV space)
// uv space = [0..1] on both axes
// "math" space = what the preset authors are used to:
// upper left = [0,0]
// bottom right = [1,1]
// rad == 1 at corners of screen
// ang == 0 at three o'clock, and increases counter-clockwise (to 6.28).
float const px = (u * 2.0f - 1.0f) * aspectX; // probably 1.0
float const py = (v * 2.0f - 1.0f) * aspectY; // probably <1
rad = sqrtf(px * px + py * py) / sqrtf(aspectX * aspectX + aspectY * aspectY);
ang = atan2f(py, px);
if (ang < 0)
{
ang += 6.2831853071796f;
}
}
void FinalComposite::ApplyHueShaderColors(const PresetState& presetState)
{
std::array<std::array<float, 3>, 4> shade = {{{1.0f, 1.0f, 1.0f},
{1.0f, 1.0f, 1.0f},
{1.0f, 1.0f, 1.0f},
{1.0f, 1.0f, 1.0f}}};
for (int i = 0; i < 4; i++)
{
auto const indexFloat = static_cast<float>(i);
shade[i][0] = 0.6f + 0.3f * sinf(presetState.renderContext.time * 30.0f * 0.0143f + 3 + indexFloat * 21 + presetState.hueRandomOffsets[3]);
shade[i][1] = 0.6f + 0.3f * sinf(presetState.renderContext.time * 30.0f * 0.0107f + 1 + indexFloat * 13 + presetState.hueRandomOffsets[1]);
shade[i][2] = 0.6f + 0.3f * sinf(presetState.renderContext.time * 30.0f * 0.0129f + 6 + indexFloat * 9 + presetState.hueRandomOffsets[2]);
float const max = std::max(shade[i][0], std::max(shade[i][1], shade[i][2]));
for (int k = 0; k < 3; k++)
{
shade[i][k] /= max;
shade[i][k] = 0.5f + 0.5f * shade[i][k];
}
}
// Interpolate and apply to all grid vertices.
for (int gridY = 0; gridY < compositeGridHeight; gridY++)
{
for (int gridX = 0; gridX < compositeGridWidth; gridX++)
{
auto& vertex = m_vertices[gridX + gridY * compositeGridWidth];
float x = vertex.x * 0.5f + 0.5f;
float y = vertex.y * 0.5f + 0.5f;
std::array<float, 3> color{{1.0f, 1.0f, 1.0f}};
for (int col = 0; col < 3; col++)
{
color[col] = shade[0][col] * (x) * (y) +
shade[1][col] * (1 - x) * (y) +
shade[2][col] * (x) * (1 - y) +
shade[3][col] * (1 - x) * (1 - y);
}
vertex.r = color[0];
vertex.g = color[1];
vertex.b = color[2];
vertex.a = 1.0f;
}
}
}