forked from projectM-visualizer/projectm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMilkdropNoise.cpp
357 lines (298 loc) · 12.3 KB
/
MilkdropNoise.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
#include "MilkdropNoise.hpp"
#include "projectM-opengl.h"
#include <chrono>
#include <random>
namespace libprojectM {
namespace Renderer {
auto MilkdropNoise::LowQuality() -> std::shared_ptr<Texture>
{
GLuint texture{};
{
auto textureData = generate2D(256, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GetPreferredInternalFormat(), GL_UNSIGNED_BYTE, textureData.data());
}
return std::make_shared<Texture>("noise_lq", texture, GL_TEXTURE_2D, 256, 256, false);
}
auto MilkdropNoise::LowQualityLite() -> std::shared_ptr<Texture>
{
GLuint texture{};
{
auto textureData = generate2D(32, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 32, 32, 0, GetPreferredInternalFormat(), GL_UNSIGNED_BYTE, textureData.data());
}
return std::make_shared<Texture>("noise_lq_lite", texture, GL_TEXTURE_2D, 32, 32, false);
}
auto MilkdropNoise::MediumQuality() -> std::shared_ptr<Texture>
{
GLuint texture{};
{
auto textureData = generate2D(256, 4);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GetPreferredInternalFormat(), GL_UNSIGNED_BYTE, textureData.data());
}
return std::make_shared<Texture>("noise_mq", texture, GL_TEXTURE_2D, 256, 256, false);
}
auto MilkdropNoise::HighQuality() -> std::shared_ptr<Texture>
{
GLuint texture{};
{
auto textureData = generate2D(256, 8);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GetPreferredInternalFormat(), GL_UNSIGNED_BYTE, textureData.data());
}
return std::make_shared<Texture>("noise_hq", texture, GL_TEXTURE_2D, 256, 256, false);
}
auto MilkdropNoise::LowQualityVolume() -> std::shared_ptr<Texture>
{
GLuint texture{};
{
auto textureData = generate3D(32, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 32, 32, 32, 0, GetPreferredInternalFormat(), GL_UNSIGNED_BYTE, textureData.data());
}
return std::make_shared<Texture>("noisevol_lq", texture, GL_TEXTURE_3D, 32, 32, false);
}
auto MilkdropNoise::HighQualityVolume() -> std::shared_ptr<Texture>
{
GLuint texture{};
{
auto textureData = generate3D(32, 4);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_3D, texture);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, 32, 32, 32, 0, GetPreferredInternalFormat(), GL_UNSIGNED_BYTE, textureData.data());
}
return std::make_shared<Texture>("noisevol_hq", texture, GL_TEXTURE_3D, 32, 32, false);
}
auto MilkdropNoise::GetPreferredInternalFormat() -> int
{
#ifndef USE_GLES
// We use GL_BGRA, as this is the best general-use format according to Khronos.
return GL_BGRA;
#else
// GLES only supports GL_RGB and GL_RGBA, so we always use the latter.
return GL_RGBA;
#endif
}
auto MilkdropNoise::generate2D(int size, int zoomFactor) -> std::vector<uint32_t>
{
uint32_t randomSeed = static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count());
std::default_random_engine randomGenerator(randomSeed);
std::uniform_int_distribution<int> randomDistribution(0, INT32_MAX);
std::vector<uint32_t> textureData;
textureData.resize(size * size);
// write to the bits...
auto dst = textureData.data();
auto RANGE = (zoomFactor > 1) ? 216 : 256;
for (auto y = 0; y < size; y++)
{
for (auto x = 0; x < size; x++)
{
dst[x] = (static_cast<uint32_t>((randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 24) |
(static_cast<uint32_t>((randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 16) |
(static_cast<uint32_t>((randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 8) |
(static_cast<uint32_t>((randomDistribution(randomGenerator) % RANGE) + RANGE / 2));
}
// swap some pixels randomly, to improve 'randomness'
for (auto x = 0; x < size; x++)
{
auto x1 = randomDistribution(randomGenerator) % size;
auto x2 = randomDistribution(randomGenerator) % size;
auto temp = dst[x2];
dst[x2] = dst[x1];
dst[x1] = temp;
}
dst += size;
}
// smoothing
if (zoomFactor > 1)
{
dst = textureData.data();
// first go ACROSS, blending cubically on X, but only on the main lines.
for (auto y = 0; y < size; y += zoomFactor)
{
for (auto x = 0; x < size; x++)
{
if (x % zoomFactor)
{
auto base_x = (x / zoomFactor) * zoomFactor + size;
auto base_y = y * size;
auto y0 = dst[base_y + ((base_x - zoomFactor) % size)];
auto y1 = dst[base_y + ((base_x) % size)];
auto y2 = dst[base_y + ((base_x + zoomFactor) % size)];
auto y3 = dst[base_y + ((base_x + zoomFactor * 2) % size)];
auto t = static_cast<float>(x % zoomFactor) / static_cast<float>(zoomFactor);
auto result = dwCubicInterpolate(y0, y1, y2, y3, t);
dst[y * size + x] = result;
}
}
}
// next go down, doing cubic interp along Y, on every line.
for (auto x = 0; x < size; x++)
{
for (auto y = 0; y < size; y++)
{
if (y % zoomFactor)
{
auto base_y = (y / zoomFactor) * zoomFactor + size;
auto y0 = dst[((base_y - zoomFactor) % size) * size + x];
auto y1 = dst[((base_y) % size) * size + x];
auto y2 = dst[((base_y + zoomFactor) % size) * size + x];
auto y3 = dst[((base_y + zoomFactor * 2) % size) * size + x];
auto t = static_cast<float>(y % zoomFactor) / static_cast<float>(zoomFactor);
auto result = dwCubicInterpolate(y0, y1, y2, y3, t);
dst[y * size + x] = result;
}
}
}
}
return textureData;
}
auto MilkdropNoise::generate3D(int size, int zoomFactor) -> std::vector<uint32_t>
{
uint32_t randomSeed = static_cast<uint32_t>(std::chrono::system_clock::now().time_since_epoch().count());
std::default_random_engine randomGenerator(randomSeed);
std::uniform_int_distribution<int> randomDistribution(0, INT32_MAX);
std::vector<uint32_t> textureData;
textureData.resize(size * size * size);
// write to the bits...
int RANGE = (zoomFactor > 1) ? 216 : 256;
for (auto z = 0; z < size; z++)
{
auto dst = (textureData.data()) + z * size * size;
for (auto y = 0; y < size; y++)
{
for (auto x = 0; x < size; x++)
{
dst[x] = ((static_cast<uint32_t>(randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 24) |
((static_cast<uint32_t>(randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 16) |
((static_cast<uint32_t>(randomDistribution(randomGenerator) % RANGE) + RANGE / 2) << 8) |
((static_cast<uint32_t>(randomDistribution(randomGenerator) % RANGE) + RANGE / 2));
}
// swap some pixels randomly, to improve 'randomness'
for (auto x = 0; x < size; x++)
{
auto x1 = randomDistribution(randomGenerator) % size;
auto x2 = randomDistribution(randomGenerator) % size;
auto temp = dst[x2];
dst[x2] = dst[x1];
dst[x1] = temp;
}
dst += size;
}
}
// smoothing
if (zoomFactor > 1)
{
// first go ACROSS, blending cubically on X, but only on the main lines.
auto dst = textureData.data();
for (auto z = 0; z < size; z += zoomFactor)
{
for (auto y = 0; y < size; y += zoomFactor)
{
for (auto x = 0; x < size; x++)
{
if (x % zoomFactor)
{
auto base_x = (x / zoomFactor) * zoomFactor + size;
auto base_y = z * size + y * size;
auto y0 = dst[base_y + ((base_x - zoomFactor) % size)];
auto y1 = dst[base_y + ((base_x) % size)];
auto y2 = dst[base_y + ((base_x + zoomFactor) % size)];
auto y3 = dst[base_y + ((base_x + zoomFactor * 2) % size)];
auto t = static_cast<float>(x % zoomFactor) / static_cast<float>(zoomFactor);
auto result = dwCubicInterpolate(y0, y1, y2, y3, t);
dst[z * size + y * size + x] = result;
}
}
}
}
// next go down, doing cubic interp along Y, on the main slices.
for (auto z = 0; z < size; z += zoomFactor)
{
for (auto x = 0; x < size; x++)
{
for (auto y = 0; y < size; y++)
{
if (y % zoomFactor)
{
auto base_y = (y / zoomFactor) * zoomFactor + size;
auto base_z = z * size;
auto y0 = dst[((base_y - zoomFactor) % size) * size + base_z + x];
auto y1 = dst[((base_y) % size) * size + base_z + x];
auto y2 = dst[((base_y + zoomFactor) % size) * size + base_z + x];
auto y3 = dst[((base_y + zoomFactor * 2) % size) * size + base_z + x];
auto t = static_cast<float>(y % zoomFactor) / static_cast<float>(zoomFactor);
auto result = dwCubicInterpolate(y0, y1, y2, y3, t);
dst[y * size + base_z + x] = result;
}
}
}
}
// next go through, doing cubic interp along Z, everywhere.
for (auto x = 0; x < size; x++)
{
for (auto y = 0; y < size; y++)
{
for (auto z = 0; z < size; z++)
{
if (z % zoomFactor)
{
auto base_y = y * size;
auto base_z = (z / zoomFactor) * zoomFactor + size;
auto y0 = dst[((base_z - zoomFactor) % size) * size + base_y + x];
auto y1 = dst[((base_z) % size) * size + base_y + x];
auto y2 = dst[((base_z + zoomFactor) % size) * size + base_y + x];
auto y3 = dst[((base_z + zoomFactor * 2) % size) * size + base_y + x];
auto t = static_cast<float>(z % zoomFactor) / static_cast<float>(zoomFactor);
auto result = dwCubicInterpolate(y0, y1, y2, y3, t);
dst[z * size + base_y + x] = result;
}
}
}
}
}
return textureData;
}
float MilkdropNoise::fCubicInterpolate(float y0, float y1, float y2, float y3, float t)
{
auto t2 = t * t;
auto a0 = y3 - y2 - y0 + y1;
auto a1 = y0 - y1 - a0;
auto a2 = y2 - y0;
auto a3 = y1;
return (a0 * t * t2 + a1 * t2 + a2 * t + a3);
}
uint32_t MilkdropNoise::dwCubicInterpolate(uint32_t y0, uint32_t y1, uint32_t y2, uint32_t y3, float t)
{
uint32_t ret = 0;
uint32_t shift = 0;
for (auto i = 0; i < 4; i++)
{
auto f = fCubicInterpolate(
static_cast<float>((y0 >> shift) & 0xFF) / 255.0f,
static_cast<float>((y1 >> shift) & 0xFF) / 255.0f,
static_cast<float>((y2 >> shift) & 0xFF) / 255.0f,
static_cast<float>((y3 >> shift) & 0xFF) / 255.0f,
t);
if (f < 0)
{
f = 0;
}
if (f > 1)
{
f = 1;
}
ret |= ((uint32_t) (f * 255)) << shift;
shift += 8;
}
return ret;
}
} // namespace Renderer
} // namespace libprojectM