Continuous distribution
[clouds.git] / clouds.cpp
1 #include "debug.hpp"
2 #include "program.hpp"
3 #include "simulation.h"
4 #include <GL/glew.h>
5 #include <GLUT/glut.h>
6 #include <array>
7 #include <cmath>
8 #include <cstdio>
9 #include <cstdlib>
10 #include <glm/ext.hpp>
11 #include <glm/glm.hpp>
12 #include <sys/stat.h>
13 #include <vector>
14
15 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
16
17 bool debugContDist = false;
18
19 using namespace std;
20 using namespace glm;
21
22 const float metaballR = 1.5f;
23 inline float metaballField(float r) {
24   if (r > metaballR)
25     return 0;
26   const float a = r / metaballR;
27   return (-4.f / 9.f * powf(a, 6)) + (17.f / 9.f * powf(a, 4)) -
28          (22.f / 9.f * powf(a, 2)) + 1;
29 }
30
31 const float normalizationFactor = 748.f / 405.f * M_PI * metaballR;
32
33 void checkError() {
34   if (GLenum e = glGetError()) {
35     fprintf(stderr, "%s\n", gluErrorString(e));
36     abort();
37   }
38 }
39
40 GLuint bbProg;
41 GLuint bbVao;
42
43 // Here we need to generate n_q textures for different densities of metaballs
44 // These textures then go on the billboards
45 // The texture stores attenuation ratio?
46
47 #define NQ 64
48 GLuint bbTexIds[NQ];
49
50 // Stores attenuation ratio inside r channel
51 // Should be highest value at center
52 void precalculateBillboardTextures() {
53   fprintf(stderr, "Calculating billboard textures...\n");
54   glGenTextures(NQ, bbTexIds);
55
56   for (int d = 0; d < NQ; d++) {
57     float data[32 * 32];
58     for (int j = 0; j < 32; j++) {
59       for (int i = 0; i < 32; i++) {
60         // TODO: properly calculate this instead of whatever this is
61         float r = distance(vec2(i, j), vec2(16, 16)) / 16;
62         float density = (float)d / NQ;
63         data[i + j * 32] =
64             1 - (density * metaballField(r) / normalizationFactor);
65       }
66     }
67
68     mkdir("bbtex", 0777);
69     char path[32];
70     snprintf(path, 32, "bbtex/%i.tga", d);
71     saveGrayscale(data, 32, 32, path);
72
73     glBindTexture(GL_TEXTURE_2D, bbTexIds[d]);
74     checkError();
75
76     glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 32, 32, 0, GL_RED, GL_FLOAT, data);
77     glGenerateMipmap(GL_TEXTURE_2D); // required, otherwise texture is blank
78
79     checkError();
80
81     fprintf(stderr, "\r%i out of %i densities calculated%s", d + 1, NQ,
82             d == NQ - 1 ? "\n" : "");
83   }
84 }
85
86 struct Metaball {
87   vec3 pos;
88   /** Radius, density */
89   float r, d;
90   vec4 col;
91 };
92
93 array<Metaball, CLOUD_DIM * CLOUD_DIM * CLOUD_DIM> metaballs;
94
95 Clouds cs;
96
97 void calculateMetaballs() {
98   stepClouds(&cs);
99   /* for (int i = 0; i < 256; i++) { */
100   /*   float x = ((float)rand()/(float)(RAND_MAX) - 0.5) * 2; */
101   /*   float y = ((float)rand()/(float)(RAND_MAX) - 0.5) * 2; */
102   /*   float z = ((float)rand()/(float)(RAND_MAX) - 0.5) * 2; */
103   /*   float r = (float)rand()/(float)(RAND_MAX) * 1; */
104   /*   Metaball m = {{x,y,z}, r}; */
105   /*   metaballs.push_back(m); */
106   /* } */
107   for (int i = 0; i < CLOUD_DIM; i++) {
108     for (int j = 0; j < CLOUD_DIM; j++) {
109       for (int k = 0; k < CLOUD_DIM; k++) {
110         Metaball m = {
111             {i / (float)CLOUD_DIM, j / (float)CLOUD_DIM, k / (float)CLOUD_DIM},
112             1.f / (float)CLOUD_DIM};
113         m.pos = (m.pos * vec3(2)) - vec3(1);
114         m.d = cs.q[i][j][k];
115         metaballs[i * CLOUD_DIM * CLOUD_DIM + j * CLOUD_DIM + k] = m;
116       }
117     }
118   }
119 }
120
121 vec3 sunPos = {0, 2, 2}, sunDir = {0, -1, -1};
122 vec3 camPos = {0, 0, -5}, viewPos = {0, 0, 0};
123 mat4 proj; // projection matrix
124 mat4 view; // view matrix
125 float znear = 0.001, zfar = 1000;
126 float width = 600, height = 400;
127 float aspect = width / height;
128
129 void setProjectionAndViewUniforms(GLuint progId) {
130   GLuint projLoc = glGetUniformLocation(progId, "projection");
131   glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
132
133   GLuint viewLoc = glGetUniformLocation(progId, "view");
134   glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
135 }
136
137 /** Orientates the transformation matrix to face the camera in the view matrix
138  */
139 mat4 faceView(mat4 m) {
140   m[0][0] = view[0][0];
141   m[0][1] = view[1][0];
142   m[0][2] = view[2][0];
143   m[1][0] = view[0][1];
144   m[1][1] = view[1][1];
145   m[1][2] = view[2][1];
146   m[2][0] = view[0][2];
147   m[2][1] = view[1][2];
148   m[2][2] = view[2][2];
149   return m;
150 }
151
152 GLuint attenuationTex;
153
154 void shadeClouds() {
155   glDisable(GL_DEPTH_TEST);
156   // shaderOutput * 0 + buffer * shader alpha
157   glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
158   glEnable(GL_BLEND);
159
160   // sort by ascending distance from the sun
161   sort(metaballs.begin(), metaballs.end(), [](Metaball &a, Metaball &b) {
162     return distance(sunPos, a.pos) < distance(sunPos, b.pos);
163   });
164
165   glActiveTexture(GL_TEXTURE0);
166   glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
167
168   GLuint modelLoc = glGetUniformLocation(bbProg, "model");
169
170   for (auto &k : metaballs) {
171     // place the billboard at the center of k
172     mat4 model = scale(translate(mat4(1), k.pos), vec3(k.r) * 2.f);
173
174     // rotate the billboard so that its normal is oriented to the sun
175     model = faceView(model);
176
177     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
178
179     // Set the billboard color as RGBA = (1.0, 1.0, 1.0, 1.0).
180     vec4 color = {1, 1, 1, 1};
181     glUniform4fv(glGetUniformLocation(bbProg, "color"), 1,
182                  glm::value_ptr(color));
183
184     // Map the billboard texture with GL_MODULATE.
185     // i.e. multiply rather than add
186     // but glTexEnv is for the old fixed function pipeline --
187     // need to just tell our fragment shader then to modulate
188     int dIdx = k.d * NQ;
189     glBindTexture(GL_TEXTURE_2D, bbTexIds[dIdx]);
190     glUniform1i(glGetUniformLocation(bbProg, "modulate"), 1);
191
192     // Render the billboard.
193     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
194
195     // Read the pixel value corresponding to the center of metaball k.
196     // 1. First get position in opengl screen space: from [-1,1]
197     // 2. Normalize to [0,1]
198     // 3. Multiply by (width * height)
199     vec2 screenPos =
200         ((vec2(proj * view * model * vec4(0, 0, 0, 1)) + vec2(1)) / vec2(2)) *
201         vec2(width, height);
202     vec4 pixel;
203     // TODO: This is a huge bottleneck
204     glReadPixels(screenPos.x, screenPos.y, 1, 1, GL_RGBA, GL_FLOAT,
205                  value_ptr(pixel));
206
207     // Multiply the pixel value by the sunlight color.
208     vec4 sunColor = {1, 1, 0.9, 1};
209     pixel *= sunColor;
210
211     // Store the color into an array C[k] as the color of the billboard.
212     k.col = pixel;
213   }
214
215   saveFBO();
216   checkError();
217 }
218
219 void renderObject() {}
220
221 void renderClouds() {
222   // Sort metaballs in descending order from the viewpoint
223   sort(metaballs.begin(), metaballs.end(), [](Metaball &a, Metaball &b) {
224     return distance(camPos, a.pos) > distance(camPos, b.pos);
225   });
226
227   glUniform1i(glGetUniformLocation(bbProg, "debugContDist"), debugContDist);
228
229   glDisable(GL_DEPTH_TEST);
230   glEnable(GL_BLEND);
231   // shaderOutput * 1 + buffer * shader alpha
232   glBlendFunc(GL_ONE, GL_SRC_ALPHA);
233
234   glActiveTexture(GL_TEXTURE0);
235   glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
236
237   for (int i = 0; i < metaballs.size(); i++) {
238     Metaball k = metaballs[i];
239
240     GLuint modelLoc = glGetUniformLocation(bbProg, "model");
241
242     // Place the billboard at the center of the corresponding metaball n.
243     mat4 model = scale(translate(mat4(1), k.pos), vec3(k.r) * 2.f);
244     // Rotate the billboard so that its normal is oriented to the viewpoint.
245     model = faceView(model);
246
247     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
248
249     // Set the billboard color as C[n].
250     k.col.w = 1;
251     glUniform4fv(glGetUniformLocation(bbProg, "color"), 1,
252                  glm::value_ptr(k.col));
253
254     // Map the billboard texture.
255     int dIdx = k.d * NQ;
256     glBindTexture(GL_TEXTURE_2D, bbTexIds[dIdx]);
257
258     // Don't modulate it -- blend it
259     glUniform1i(glGetUniformLocation(bbProg, "modulate"), 0);
260
261     if (debugContDist) {
262       glUniform1f(glGetUniformLocation(bbProg, "density"), k.d);
263       glDisable(GL_BLEND);
264       model = scale(model, vec3(0.02));
265       glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
266     }
267
268     // Render the billboard with the blending function.
269     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
270   }
271 }
272
273 bool needsReshading = true;
274 void display() {
275   if (needsReshading) {
276     // TODO: find a way to make sure there's no clipping
277     view = glm::lookAt(sunPos + sunDir * vec3(20), sunPos, {0, 1, 0});
278     proj = glm::ortho(1.f * aspect, -1.f * aspect, -1.f, 1.f, znear, zfar);
279     setProjectionAndViewUniforms(bbProg);
280
281     glClearColor(1, 1, 1, 1);
282     glClear(GL_COLOR_BUFFER_BIT);
283     shadeClouds();
284     needsReshading = false;
285   }
286
287   view = glm::lookAt(camPos, viewPos, {0, 1, 0});
288   proj = glm::perspective(45.f, aspect, znear, zfar);
289   setProjectionAndViewUniforms(bbProg);
290
291   glClearColor(0.83, 1, 1, 1); // background color
292   glClear(GL_COLOR_BUFFER_BIT);
293   renderObject(); // render things that aren't clouds
294   renderClouds();
295
296   glutSwapBuffers();
297 }
298
299 bool needsRedisplay = false;
300 void timer(int _) {
301   /* calculateMetaballs(); */
302   if (needsRedisplay) {
303     glutPostRedisplay();
304     needsRedisplay = false;
305   }
306   glutTimerFunc(16, timer, 0);
307 }
308
309 void keyboard(unsigned char key, int x, int y) {
310   if (key == ' ') {
311     calculateMetaballs();
312     needsRedisplay = true;
313     needsReshading = true;
314   }
315   if (key == 'd') {
316     debugContDist = !debugContDist;
317     needsRedisplay = true;
318   }
319 }
320
321 int prevMouseX, prevMouseY;
322 bool firstMouse = true;
323 void motion(int x, int y) {
324   if (firstMouse) {
325     prevMouseX = x;
326     prevMouseY = y;
327     firstMouse = false;
328   }
329   float dx = x - prevMouseX, dy = y - prevMouseY;
330   prevMouseX = x;
331   prevMouseY = y;
332   const vec3 origin(0, 18, 0);
333   const float sensitivity = 0.003f;
334   auto camMat = translate(mat4(1), origin + camPos);
335   auto rotation = rotate(rotate(mat4(1), -dx * sensitivity, {0, 1, 0}),
336                          -dy * sensitivity, {1, 0, 0});
337   auto rotAroundOrig = camMat * rotation * translate(mat4(1), origin - camPos);
338   camPos = rotAroundOrig * glm::vec4(camPos, 0);
339   needsRedisplay = true;
340 }
341
342 void passiveMotion(int x, int y) {
343   prevMouseX = x;
344   prevMouseY = y;
345 }
346
347 int main(int argc, char **argv) {
348   glutInit(&argc, argv);
349   glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB |
350                       GLUT_3_2_CORE_PROFILE);
351   glutInitWindowSize(width, height);
352   glutCreateWindow("Clouds");
353   glutDisplayFunc(display);
354
355   glewInit();
356
357   Program prog("billboardvert.glsl", "billboardfrag.glsl");
358
359   bbProg = prog.progId;
360   glUseProgram(bbProg);
361
362   glGenVertexArrays(1, &bbVao);
363   glBindVertexArray(bbVao);
364   GLuint vbos[2];
365   glGenBuffers(2, vbos);
366
367   vector<vec3> poss = {{-1, -1, 0}, {-1, 1, 0}, {1, 1, 0}, {1, -1, 0}};
368   vector<GLuint> indices = {2, 1, 0, 3, 2, 0};
369
370   GLuint posLoc = glGetAttribLocation(bbProg, "vPosition");
371   glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
372   glBufferData(GL_ARRAY_BUFFER, poss.size() * sizeof(glm::vec3), &poss[0],
373                GL_STATIC_DRAW);
374   glEnableVertexAttribArray(posLoc);
375   glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
376
377   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]);
378   glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
379                &indices[0], GL_STATIC_DRAW);
380
381   prog.validate();
382
383   precalculateBillboardTextures();
384
385   initClouds(&cs);
386   calculateMetaballs();
387
388   glGenTextures(1, &attenuationTex);
389
390   glutKeyboardFunc(keyboard);
391   glutMotionFunc(motion);
392   glutPassiveMotionFunc(passiveMotion);
393   glutTimerFunc(16, timer, 0);
394
395   // set up billboard prog
396
397   glutMainLoop();
398
399   return 0;
400 }