-
Notifications
You must be signed in to change notification settings - Fork 0
/
effect_blobs.cpp
548 lines (432 loc) · 21.3 KB
/
effect_blobs.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include "main.h"
#include "objloader.h"
#define BLOB_EXTENT 20
#define BLOB_COUNT 3
static GLuint shaderProgram;
static GLuint floorShaderProgram;
static GLuint houseShaderProgram;
static GLuint blurShaderProgram;
static GLuint composeShaderProgram;
static GLuint cubesBO;
static GLuint floorBO;
static GLuint houseBO;
static size_t houseVertCount;
static GLuint projectionLoc;
static GLuint modelviewLoc;
static GLuint normalviewLoc;
static GLuint vertexInLoc;
static GLuint normalInLoc;
static GLuint texcoordsInLoc;
static GLuint blobglowInLoc;
static const sync_track* camRotX;
static const sync_track* camRotY;
static const sync_track* camRotZ;
static GLuint glowyTexture;
static GLuint brickTexture;
static GLuint brickNormalTexture;
static GLuint houseTexture;
static GLuint postprocTextureInitial;
static GLuint postprocFBOInitial;
static GLuint floorTexture;
static GLuint postprocTextureA;
static GLuint postprocFBOA;
static GLuint postprocTextureB;
static GLuint postprocFBOB;
typedef struct blobInfo {
const sync_track* t;
glm::vec3 pos;
} blobInfo;
static blobInfo blobs[3];
static const sync_track* radius;
typedef struct vertexInfo {
glm::vec3 pos;
glm::vec3 normal;
glm::vec2 texcoord;
float glow;
} vertexInfo;
static vertexInfo* blobVertices;
// Too-lazy-for-element-buffer quads
vertexInfo cubeVertices[] = {
// Front
{{-1, -1, 1}, {0, 0, 1}, {0, 0}, 1.0},
{{ 1, -1, 1}, {0, 0, 1}, {1, 0}, 1.0},
{{-1, 1, 1}, {0, 0, 1}, {0, 1}, 1.0},
{{ 1, -1, 1}, {0, 0, 1}, {1, 0}, 1.0},
{{ 1, 1, 1}, {0, 0, 1}, {1, 1}, 1.0},
{{-1, 1, 1}, {0, 0, 1}, {0, 1}, 1.0},
// Back
{{-1, -1, -1}, {0, 0, -1}, {0, 0}, 1.0},
{{-1, 1, -1}, {0, 0, -1}, {0, 1}, 1.0},
{{ 1, -1, -1}, {0, 0, -1}, {1, 0}, 1.0},
{{ 1, -1, -1}, {0, 0, -1}, {1, 0}, 1.0},
{{-1, 1, -1}, {0, 0, -1}, {0, 1}, 1.0},
{{ 1, 1, -1}, {0, 0, -1}, {1, 1}, 1.0},
// Left
{{-1, -1, -1}, {-1, 0, 0}, {0, 0}, 1.0},
{{-1, -1, 1}, {-1, 0, 0}, {0, 1}, 1.0},
{{-1, 1, -1}, {-1, 0, 0}, {1, 0}, 1.0},
{{-1, 1, -1}, {-1, 0, 0}, {1, 0}, 1.0},
{{-1, -1, 1}, {-1, 0, 0}, {0, 1}, 1.0},
{{-1, 1, 1}, {-1, 0, 0}, {1, 1}, 1.0},
// Right
{{ 1, -1, -1}, { 1, 0, 0}, {0, 0}, 1.0},
{{ 1, 1, -1}, { 1, 0, 0}, {1, 0}, 1.0},
{{ 1, -1, 1}, { 1, 0, 0}, {0, 1}, 1.0},
{{ 1, 1, -1}, { 1, 0, 0}, {1, 0}, 1.0},
{{ 1, 1, 1}, { 1, 0, 0}, {1, 1}, 1.0},
{{ 1, -1, 1}, { 1, 0, 0}, {0, 1}, 1.0},
// Top
{{-1, -1, -1}, { 0, -1, 0}, {0, 0}, 1.0},
{{ 1, -1, -1}, { 0, -1, 0}, {1, 0}, 1.0},
{{-1, -1, 1}, { 0, -1, 0}, {0, 1}, 1.0},
{{ 1, -1, -1}, { 0, -1, 0}, {1, 0}, 1.0},
{{ 1, -1, 1}, { 0, -1, 0}, {1, 1}, 1.0},
{{-1, -1, 1}, { 0, -1, 0}, {0, 1}, 1.0},
// Bottom
{{-1, 1, -1}, { 0, 1, 0}, {0, 0}, 0.0},
{{-1, 1, 1}, { 0, 1, 0}, {0, 1}, 0.0},
{{ 1, 1, -1}, { 0, 1, 0}, {1, 0}, 0.0},
{{ 1, 1, -1}, { 0, 1, 0}, {1, 0}, 0.0},
{{-1, 1, 1}, { 0, 1, 0}, {0, 1}, 0.0},
{{ 1, 1, 1}, { 0, 1, 0}, {1, 1}, 0.0},
};
float planeVertices[] = {
-1000, 0, -1000,
-1000, 0, 1000,
1000, 0, -1000,
1000, 0, -1000,
-1000, 0, 1000,
1000, 0, 1000,
};
void cubeAt(vertexInfo* buffer, glm::vec3 at, glm::vec3 size, float glow) {
for(int i = 0; i < 36; i++) {
vertexInfo cubeVert = cubeVertices[i];
cubeVert.pos = cubeVert.pos * size + at;
cubeVert.glow = glow;
buffer[i] = cubeVert;
}
}
void effectBlobsInitialize() {
// Basic OpenGL state
glClearColor(0.08f, 0.04f, 0.04f, 0.0f);
// Cube drawing shader
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, "shaders/glowy_blobs.vert.glsl");
GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, "shaders/glowy_blobs_lighting.frag.glsl");
shaderProgram = makeShaderProgram(fragmentShader, vertexShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// Floor drawing shader
vertexShader = loadShader(GL_VERTEX_SHADER, "shaders/basic.vert.glsl");
fragmentShader = loadShader(GL_FRAGMENT_SHADER, "shaders/normalmap.frag.glsl");
const char* targets[] = {"outColor", "outFloor"};
floorShaderProgram = makeShaderProgramMRT(fragmentShader, vertexShader, targets, 2);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// House drawing shader
vertexShader = loadShader(GL_VERTEX_SHADER, "shaders/basic.vert.glsl");
fragmentShader = loadShader(GL_FRAGMENT_SHADER, "shaders/house_lighting.frag.glsl");
houseShaderProgram = makeShaderProgram(fragmentShader, vertexShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// Postprocessing
blurShaderProgram = loadSaqShaderProgram("shaders/blur.frag.glsl");
composeShaderProgram = loadSaqShaderProgram("shaders/glow_compose.frag.glsl");
// Uniforms
projectionLoc = glGetUniformLocation(shaderProgram, "projection");
modelviewLoc = glGetUniformLocation(shaderProgram, "modelview");
normalviewLoc = glGetUniformLocation(shaderProgram, "normalview");
// Attributes
vertexInLoc = glGetAttribLocation(shaderProgram, "vertexIn");
normalInLoc = glGetAttribLocation(shaderProgram, "normalIn");
texcoordsInLoc = glGetAttribLocation(shaderProgram, "texcoordsIn");
blobglowInLoc = glGetAttribLocation(shaderProgram, "blobglowIn");
// Geometry
blobVertices = (vertexInfo*)malloc(sizeof(vertexInfo) * 36 * BLOB_EXTENT * BLOB_EXTENT * BLOB_EXTENT);
cubesBO = makeBO(GL_ARRAY_BUFFER, blobVertices, sizeof(vertexInfo) * 36 * BLOB_EXTENT * BLOB_EXTENT * BLOB_EXTENT, GL_DYNAMIC_DRAW);
floorBO = makeBO(GL_ARRAY_BUFFER, planeVertices, sizeof(float) * 6 * 3, GL_STATIC_DRAW);
std::vector<glm::vec3> houseVerts;
std::vector<glm::vec2> houseUVs;
std::vector<glm::vec3> houseNormals;
loadOBJ("models/house2.obj", houseVerts, houseUVs, houseNormals);
houseVertCount = houseVerts.size();
vertexInfo* houseVertices = (vertexInfo*)malloc(sizeof(vertexInfo) * houseVertCount);
for (int i = 0; i < houseVertCount; i++) {
houseVertices[i].pos = houseVerts[i];
houseVertices[i].texcoord = houseUVs[i];
houseVertices[i].normal = houseNormals[i];
houseVertices[i].glow = 1.0f;
}
houseBO = makeBO(GL_ARRAY_BUFFER, houseVertices, (GLsizei)(sizeof(vertexInfo) * houseVertCount), GL_STATIC_DRAW);
// Sync
camRotX = sync_get_track(rocket, "blobs:rot.x");
camRotY = sync_get_track(rocket, "blobs:rot.y");
camRotZ = sync_get_track(rocket, "blobs:rot.z");
blobs[0].t = sync_get_track(rocket, "blobs:blob1.t");
blobs[1].t = sync_get_track(rocket, "blobs:blob2.t");
blobs[2].t = sync_get_track(rocket, "blobs:blob3.t");
radius = sync_get_track(rocket, "blobs:radius");
// Textures
glowyTexture = loadTexture("texture/glowy.tga");
brickTexture = loadTexture("texture/bricks_colour.tga");
brickNormalTexture = loadTexture("texture/bricks_normals.tga");
houseTexture = loadTexture("texture/house2.tga");
postprocTextureInitial = makeTextureBuffer(screenWidth, screenHeight, GL_RGBA32F);
postprocFBOInitial = makeFBO(postprocTextureInitial);
floorTexture = makeTextureBuffer(screenWidth, screenHeight, GL_RGBA32F);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, floorTexture, 0);
postprocTextureA = makeTextureBuffer(screenWidth, screenHeight, GL_RGBA32F);
postprocFBOA = makeFBO(postprocTextureA);
postprocTextureB = makeTextureBuffer(screenWidth, screenHeight, GL_RGBA32F);
postprocFBOB = makeFBO(postprocTextureB);
}
// Trefoil knot pathing
glm::vec3 blobPos(float t, int i) {
if(i == 0) {
return glm::vec3(sin(t) + 2.0f * sin(2.0f * t), cos(t) + 2.0f * cos(2.0f * t), -sin(3.0f * t)) * 2.85;
}
else if (i == 1) {
return glm::vec3(-sin(3.0f * t), sin(t) + 2.0f * sin(2.0f * t), cos(t) + 2.0f * cos(2.0f * t)) * 2.85;
}
return glm::vec3(cos(t) + 2.0f * cos(2.0f * t), -sin(3.0f * t), sin(t) + 2.0f * sin(2.0f * t)) * 2.85;
}
// Draw a house
void drawHouse(glm::mat4 projection, glm::mat4 modelview, glm::mat4 cameraTransform) {
// Calculate matrix for mirroring along the xz plane
glm::mat4 modelviewMirrored = modelview;
modelviewMirrored[1][1] = -modelviewMirrored[1][1];
modelviewMirrored[3][1] = -modelviewMirrored[3][1];
// Apply camera transform
modelview = cameraTransform * modelview;
modelviewMirrored = cameraTransform * modelviewMirrored;
// Calculate normalview matrices
glm::mat4 normalview = glm::transpose(glm::inverse(modelview));
glm::mat4 normalviewMirrored = glm::transpose(glm::inverse(modelviewMirrored));
glCullFace(GL_BACK);
glUseProgram(houseShaderProgram);
glUniformMatrix4fv(glGetUniformLocation(houseShaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(houseShaderProgram, "modelview"), 1, GL_FALSE, glm::value_ptr(modelview));
glUniformMatrix4fv(glGetUniformLocation(houseShaderProgram, "normalview"), 1, GL_FALSE, glm::value_ptr(normalview));
glUniformMatrix4fv(glGetUniformLocation(houseShaderProgram, "cameraTransform"), 1, GL_FALSE, glm::value_ptr(cameraTransform));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, houseTexture);
glUniform1i(glGetUniformLocation(houseShaderProgram, "textureCol"), 0);
glBindBuffer(GL_ARRAY_BUFFER, houseBO);
GLuint houseVertexInLoc = glGetAttribLocation(houseShaderProgram, "vertexIn");
GLuint houseNormalInLoc = glGetAttribLocation(houseShaderProgram, "normalIn");
GLuint houseTexcoordInLoc = glGetAttribLocation(houseShaderProgram, "texcoordsIn");
glEnableVertexAttribArray(houseVertexInLoc);
glVertexAttribPointer(houseVertexInLoc, 3, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 0));
glEnableVertexAttribArray(houseNormalInLoc);
glVertexAttribPointer(houseNormalInLoc, 3, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 3));
glEnableVertexAttribArray(houseTexcoordInLoc);
glVertexAttribPointer(houseTexcoordInLoc, 2, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 6));
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)houseVertCount);
// Draw mirrored
glUniformMatrix4fv(glGetUniformLocation(houseShaderProgram, "modelview"), 1, GL_FALSE, glm::value_ptr(modelviewMirrored));
glUniformMatrix4fv(glGetUniformLocation(houseShaderProgram, "normalview"), 1, GL_FALSE, glm::value_ptr(normalviewMirrored));
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)houseVertCount);
glDisableVertexAttribArray(houseVertexInLoc);
glDisableVertexAttribArray(houseNormalInLoc);
glDisableVertexAttribArray(houseTexcoordInLoc);
}
void drawMetaballs(glm::mat4 projection, glm::mat4 modelview, glm::mat4 normalview, glm::mat4 modelviewMirrored, glm::mat4 normalviewMirrored, float bassRow) {
// Bind cube shader and set up uniforms
glUseProgram(shaderProgram);
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(modelviewLoc, 1, GL_FALSE, glm::value_ptr(modelview));
glUniformMatrix4fv(normalviewLoc, 1, GL_FALSE, glm::value_ptr(normalview));
glUniform1i(glGetUniformLocation(shaderProgram, "textureIn"), 0);
// Update blob pos
for(int i = 0; i < BLOB_COUNT; i++) {
blobs[i].pos = blobPos((float)sync_get_val(blobs[i].t, bassRow), i);
}
float blobRadius = (float)sync_get_val(radius, bassRow);
// Update geometry
srand(666);
for(int x = 0; x < BLOB_EXTENT; x++) {
for(int y = 0; y < BLOB_EXTENT; y++) {
for(int z = 0; z < BLOB_EXTENT; z++) {
glm::vec3 cubeCenter = glm::vec3(
x - BLOB_EXTENT / 2 + randFloatUnit() * 0.3,
y - BLOB_EXTENT / 2 + randFloatUnit() * 0.3,
z - BLOB_EXTENT / 2 + randFloatUnit() * 0.3
);
float blobValue = 0.0;
for(int i = 0; i < BLOB_COUNT; i++) {
blobValue += blobRadius / (float)glm::length(cubeCenter + blobs[i].pos);
}
blobValue = pow(fmin(blobValue, 1.0f), 10.0f);
blobValue = blobValue < 0.2f ? 0.0f : blobValue;
int blobState = (int)floor(((float)((int)bassRow % 16)) / 4.0);
float blobGlow = 0.0f;
if(rand() % 4 == blobState) {
blobGlow = bassRow / 2.0f;
blobGlow = 1.0f - (blobGlow - floor(blobGlow));
blobGlow = fmax(0.0f, blobGlow);
}
cubeAt(
&blobVertices[(x + y * BLOB_EXTENT + z * BLOB_EXTENT * BLOB_EXTENT) * 36],
cubeCenter,
glm::vec3((randFloat() * 0.3f + 0.5f) * blobValue),
blobGlow
);
}
}
}
// Set up texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, glowyTexture);
// Bind buffers and draw main thing
glBindBuffer(GL_ARRAY_BUFFER, cubesBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexInfo) * 36 * BLOB_EXTENT * BLOB_EXTENT * BLOB_EXTENT, blobVertices, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(vertexInLoc);
glVertexAttribPointer(vertexInLoc, 3, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 0));
glEnableVertexAttribArray(normalInLoc);
glVertexAttribPointer(normalInLoc, 3, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 3));
glEnableVertexAttribArray(texcoordsInLoc);
glVertexAttribPointer(texcoordsInLoc, 2, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 6));
glEnableVertexAttribArray(blobglowInLoc);
glVertexAttribPointer(blobglowInLoc, 1, GL_FLOAT, GL_FALSE, sizeof(vertexInfo), (void*)(sizeof(float) * 8));
glDrawArrays(GL_TRIANGLES, 0, 36 * BLOB_EXTENT * BLOB_EXTENT * BLOB_EXTENT);
// Draw the same again but mirrored
glCullFace(GL_FRONT);
glUniformMatrix4fv(modelviewLoc, 1, GL_FALSE, glm::value_ptr(modelviewMirrored));
glUniformMatrix4fv(normalviewLoc, 1, GL_FALSE, glm::value_ptr(normalviewMirrored));
glDrawArrays(GL_TRIANGLES, 0, 36 * BLOB_EXTENT * BLOB_EXTENT * BLOB_EXTENT);
glDisableVertexAttribArray(vertexInLoc);
glDisableVertexAttribArray(normalInLoc);
glDisableVertexAttribArray(texcoordsInLoc);
glDisableVertexAttribArray(blobglowInLoc);
}
// Draw a floor and blend
void drawFloor(glm::mat4 projection, glm::mat4 cameraTransform, glm::mat4 normalviewCamera) {
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
glCullFace(GL_BACK);
glUseProgram(floorShaderProgram);
glUniformMatrix4fv(glGetUniformLocation(floorShaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(floorShaderProgram, "modelview"), 1, GL_FALSE, glm::value_ptr(cameraTransform));
glUniformMatrix4fv(glGetUniformLocation(floorShaderProgram, "normalview"), 1, GL_FALSE, glm::value_ptr(normalviewCamera));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, brickTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, brickNormalTexture);
glUniform1i(glGetUniformLocation(floorShaderProgram, "textureCol"), 0);
glUniform1i(glGetUniformLocation(floorShaderProgram, "textureNorm"), 1);
glBindBuffer(GL_ARRAY_BUFFER, floorBO);
GLuint floorVertexInLoc = glGetAttribLocation(floorShaderProgram, "vertexIn");
glEnableVertexAttribArray(floorVertexInLoc);
glVertexAttribPointer(floorVertexInLoc, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)(sizeof(float) * 0));
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisable(GL_BLEND);
glDisableVertexAttribArray(floorVertexInLoc);
}
// Actual effect
void effectBlobsRender() {
glBindFramebuffer(GL_FRAMEBUFFER, postprocFBOInitial);
GLenum targets[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, targets);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
float bassRow = (float)bassGetRow(stream);
// Standard perspective projection
glm::mat4 projection = glm::perspective(90.0f, (float)screenWidth / (float)screenHeight, 0.1f, 200.0f);
// Modelview shifts scene up and into the frame
glm::mat4 modelview = glm::translate(glm::vec3(0.0f, 11.0f, 0.0f));
// Calculate matrix for mirroring along the xz plane
glm::mat4 modelviewMirrored = modelview;
modelviewMirrored[1][1] = -modelviewMirrored[1][1];
modelviewMirrored[3][1] = -modelviewMirrored[3][1];
// Camera transform
glm::mat4 cameraTransform = glm::translate(glm::vec3(0.0f, 0.0f, -25.0f)) * (glm::mat4)glm::quat(glm::vec3(sync_get_val(camRotX, bassRow), sync_get_val(camRotY, bassRow), sync_get_val(camRotZ, bassRow)));
modelview = cameraTransform * modelview;
modelviewMirrored = cameraTransform * modelviewMirrored;
// Normalview -> Inverse transpose
glm::mat4 normalview = glm::transpose(glm::inverse(modelview));
glm::mat4 normalviewMirrored = glm::transpose(glm::inverse(modelviewMirrored));
glm::mat4 normalviewCamera = glm::transpose(glm::inverse(cameraTransform));
// Draw balls
drawMetaballs(projection, modelview, normalview, modelviewMirrored, normalviewMirrored, bassRow);
// Draw some houses
int housesPerRing = 9;
int rings = 40;
for(int ring = 0; ring < rings; ring++) {
float ringOffset = randFloat() * 2.0f * 3.141592f;
for(int house = 0; house < housesPerRing; house++) {
float angle = ((float)house / (float)housesPerRing) + (randFloat() * (0.8f / (float)housesPerRing)) + ringOffset;
angle = angle * 2.0f * 3.141592f;
float radius = ring * 6.5f + 30.0f;
glm::mat4 houseModelview = glm::translate(glm::vec3(sin(angle) * radius, 0.0f, cos(angle) * radius));
drawHouse(projection, houseModelview, cameraTransform);
}
}
// Draw and blend floor
drawFloor(projection, cameraTransform, normalviewCamera);
// Do postproc
glm::vec2 resolution = glm::vec2(screenWidth, screenHeight);
glDisable(GL_DEPTH_TEST);
for(int i = 0; i < 10; i++) {
glBindFramebuffer(GL_FRAMEBUFFER, postprocFBOB);
glActiveTexture(GL_TEXTURE0);
if (i == 0) {
glBindTexture(GL_TEXTURE_2D, postprocTextureInitial);
}
else {
glBindTexture(GL_TEXTURE_2D, postprocTextureA);
}
glUseProgram(blurShaderProgram);
glUniform2f(glGetUniformLocation(blurShaderProgram, "resolution"), (float)screenWidth, (float)screenHeight);
glUniform2f(glGetUniformLocation(blurShaderProgram, "direction"), 0.0, 1.0);
glUniform1i(glGetUniformLocation(blurShaderProgram, "tex"), 0);
renderSAQ(blurShaderProgram);
glBindFramebuffer(GL_FRAMEBUFFER, postprocFBOA);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, postprocTextureB);
glUseProgram(blurShaderProgram);
glUniform1i(glGetUniformLocation(blurShaderProgram, "tex"), 0);
glUniform2f(glGetUniformLocation(blurShaderProgram, "resolution"), (float)screenWidth, (float)screenHeight);
glUniform2f(glGetUniformLocation(blurShaderProgram, "direction"), 1.0, 0.0);
renderSAQ(blurShaderProgram);
}
// Draw postproc quad to actual framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, postprocTextureInitial);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, postprocTextureA);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, floorTexture);
glUseProgram(composeShaderProgram);
glUniform1i(glGetUniformLocation(composeShaderProgram, "baseTex"), 0);
glUniform1i(glGetUniformLocation(composeShaderProgram, "glowTex"), 1);
glUniform1i(glGetUniformLocation(composeShaderProgram, "floorTex"), 2);
glUniformMatrix4fv(glGetUniformLocation(composeShaderProgram, "normalviewCamera"), 1, GL_FALSE, glm::value_ptr(normalviewCamera));
glUniform1f(glGetUniformLocation(composeShaderProgram, "bassRow"), bassRow);
renderSAQ(composeShaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
}
void effectBlobsTerminate() {
// TODO proper this up
// Textures
glDeleteTextures(1, &brickTexture);
glDeleteTextures(1, &brickNormalTexture);
glDeleteTextures(1, &glowyTexture);
// VBOs
glDeleteBuffers(1, &cubesBO);
glDeleteBuffers(1, &floorBO);
// Shaders
glDeleteProgram(shaderProgram);
glDeleteProgram(blurShaderProgram);
glDeleteProgram(composeShaderProgram);
// TODO FBOS
// Other data
free(blobVertices);
}