3 #include "simulation.h"
10 #include <glm/ext.hpp>
11 #include <glm/glm.hpp>
15 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
23 Mode curMode = render;
28 const float metaballR = 1.5f;
29 inline float metaballField(float r) {
32 const float a = r / metaballR;
33 return (-4.f / 9.f * powf(a, 6)) + (17.f / 9.f * powf(a, 4)) -
34 (22.f / 9.f * powf(a, 2)) + 1;
37 const float normalizationFactor = 748.f / 405.f * M_PI * metaballR;
40 if (GLenum e = glGetError()) {
41 fprintf(stderr, "%s\n", gluErrorString(e));
49 // Here we need to generate n_q textures for different densities of metaballs
50 // These textures then go on the billboards
51 // The texture stores attenuation ratio?
56 // Stores attenuation ratio inside r channel
57 // Should be highest value at center
58 void precalculateBillboardTextures() {
59 fprintf(stderr, "Calculating billboard textures...\n");
60 glGenTextures(NQ, bbTexIds);
62 for (int d = 0; d < NQ; d++) {
64 for (int j = 0; j < 32; j++) {
65 for (int i = 0; i < 32; i++) {
66 // TODO: properly calculate this instead of whatever this is
67 float r = distance(vec2(i, j), vec2(16, 16)) / 16;
68 float density = (float)d / NQ;
70 1 - (density * metaballField(r) / normalizationFactor);
76 snprintf(path, 32, "bbtex/%i.tga", d);
77 saveGrayscale(data, 32, 32, path);
79 glBindTexture(GL_TEXTURE_2D, bbTexIds[d]);
82 glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 32, 32, 0, GL_RED, GL_FLOAT, data);
83 glGenerateMipmap(GL_TEXTURE_2D); // required, otherwise texture is blank
87 fprintf(stderr, "\r%i out of %i densities calculated%s", d + 1, NQ,
88 d == NQ - 1 ? "\n" : "");
95 /** Radius, density */
100 array<Metaball, CLOUD_DIM * CLOUD_DIM * CLOUD_DIM> metaballs;
104 void calculateMetaballs() {
106 /* for (int i = 0; i < 256; i++) { */
107 /* float x = ((float)rand()/(float)(RAND_MAX) - 0.5) * 2; */
108 /* float y = ((float)rand()/(float)(RAND_MAX) - 0.5) * 2; */
109 /* float z = ((float)rand()/(float)(RAND_MAX) - 0.5) * 2; */
110 /* float r = (float)rand()/(float)(RAND_MAX) * 1; */
111 /* Metaball m = {{x,y,z}, r}; */
112 /* metaballs.push_back(m); */
114 for (int i = 0; i < CLOUD_DIM; i++) {
115 for (int j = 0; j < CLOUD_DIM; j++) {
116 for (int k = 0; k < CLOUD_DIM; k++) {
118 {i / (float)CLOUD_DIM, j / (float)CLOUD_DIM, k / (float)CLOUD_DIM},
120 1.f / (float)CLOUD_DIM};
121 m.pos = (m.pos * vec3(2)) - vec3(1);
123 metaballs[i * CLOUD_DIM * CLOUD_DIM + j * CLOUD_DIM + k] = m;
129 vec3 sunPos = {0, 2, 2}, sunDir = {0, -1, -1};
130 vec3 camPos = {0, 0, -5}, viewPos = {0, 0, 0};
131 mat4 proj; // projection matrix
132 mat4 view; // view matrix
133 float znear = 0.001, zfar = 1000;
134 float width = 600, height = 400;
135 float aspect = width / height;
137 void setProjectionAndViewUniforms(GLuint progId) {
138 GLuint projLoc = glGetUniformLocation(progId, "projection");
139 glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
141 GLuint viewLoc = glGetUniformLocation(progId, "view");
142 glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
145 /** Orientates the transformation matrix to face the camera in the view matrix
147 mat4 faceView(mat4 m) {
148 m[0][0] = view[0][0];
149 m[0][1] = view[1][0];
150 m[0][2] = view[2][0];
151 m[1][0] = view[0][1];
152 m[1][1] = view[1][1];
153 m[1][2] = view[2][1];
154 m[2][0] = view[0][2];
155 m[2][1] = view[1][2];
156 m[2][2] = view[2][2];
160 GLuint attenuationTex;
163 glDisable(GL_DEPTH_TEST);
164 // shaderOutput * 0 + buffer * shader alpha
165 glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
168 // sort by ascending distance from the sun
169 sort(metaballs.begin(), metaballs.end(), [](Metaball &a, Metaball &b) {
170 return distance(sunPos, a.pos) < distance(sunPos, b.pos);
173 glActiveTexture(GL_TEXTURE0);
174 glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
176 GLuint modelLoc = glGetUniformLocation(bbProg, "model");
178 for (auto &k : metaballs) {
179 // place the billboard at the center of k
180 mat4 model = scale(translate(mat4(1), k.pos), vec3(k.r) * 2.f);
182 // rotate the billboard so that its normal is oriented to the sun
183 model = faceView(model);
185 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
187 // Set the billboard color as RGBA = (1.0, 1.0, 1.0, 1.0).
188 vec4 color = {1, 1, 1, 1};
189 glUniform4fv(glGetUniformLocation(bbProg, "color"), 1,
190 glm::value_ptr(color));
192 // Map the billboard texture with GL_MODULATE.
193 // i.e. multiply rather than add
194 // but glTexEnv is for the old fixed function pipeline --
195 // need to just tell our fragment shader then to modulate
197 glBindTexture(GL_TEXTURE_2D, bbTexIds[dIdx]);
198 glUniform1i(glGetUniformLocation(bbProg, "modulate"), 1);
200 // Render the billboard.
201 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
203 // Read the pixel value corresponding to the center of metaball k.
204 // 1. First get position in opengl screen space: from [-1,1]
205 // 2. Normalize to [0,1]
206 // 3. Multiply by (width * height)
208 ((vec2(proj * view * model * vec4(0, 0, 0, 1)) + vec2(1)) / vec2(2)) *
211 // TODO: This is a huge bottleneck
212 glReadPixels(screenPos.x, screenPos.y, 1, 1, GL_RGBA, GL_FLOAT,
215 // Multiply the pixel value by the sunlight color.
216 vec4 sunColor = {1, 1, 0.9, 1};
219 // Store the color into an array C[k] as the color of the billboard.
227 void renderObject() {}
229 void renderClouds() {
230 // Sort metaballs in descending order from the viewpoint
231 sort(metaballs.begin(), metaballs.end(), [](Metaball &a, Metaball &b) {
232 return distance(camPos, a.pos) > distance(camPos, b.pos);
235 glUniform1i(glGetUniformLocation(bbProg, "debug"), curMode != render);
237 glDisable(GL_DEPTH_TEST);
239 // shaderOutput * 1 + buffer * shader alpha
240 glBlendFunc(GL_ONE, GL_SRC_ALPHA);
242 glActiveTexture(GL_TEXTURE0);
243 glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
245 for (int i = 0; i < metaballs.size(); i++) {
246 Metaball k = metaballs[i];
248 GLuint modelLoc = glGetUniformLocation(bbProg, "model");
250 // Place the billboard at the center of the corresponding metaball n.
251 mat4 model = scale(translate(mat4(1), k.pos), vec3(k.r) * 2.f);
252 // Rotate the billboard so that its normal is oriented to the viewpoint.
253 model = faceView(model);
255 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
257 // Set the billboard color as C[n].
259 glUniform4fv(glGetUniformLocation(bbProg, "color"), 1,
260 glm::value_ptr(k.col));
262 // Map the billboard texture.
264 glBindTexture(GL_TEXTURE_2D, bbTexIds[dIdx]);
266 // Don't modulate it -- blend it
267 glUniform1i(glGetUniformLocation(bbProg, "modulate"), 0);
269 if (curMode != render) {
271 if (curMode == debugContDist) debugVal = k.d;
272 else if (curMode == debugProbAct) debugVal = cs.p_act[k.coords.x][k.coords.y][k.coords.z];
273 else if (curMode == debugProbExt) debugVal = cs.p_ext[k.coords.x][k.coords.y][k.coords.z];
274 glUniform1f(glGetUniformLocation(bbProg, "debugVal"), debugVal);
276 model = scale(model, vec3(0.02));
277 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
280 // Render the billboard with the blending function.
281 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
285 bool needsReshading = true;
287 if (needsReshading) {
288 // TODO: find a way to make sure there's no clipping
289 view = glm::lookAt(sunPos + sunDir * vec3(20), sunPos, {0, 1, 0});
290 proj = glm::ortho(1.f * aspect, -1.f * aspect, -1.f, 1.f, znear, zfar);
291 setProjectionAndViewUniforms(bbProg);
293 glClearColor(1, 1, 1, 1);
294 glClear(GL_COLOR_BUFFER_BIT);
296 needsReshading = false;
299 view = glm::lookAt(camPos, viewPos, {0, 1, 0});
300 proj = glm::perspective(45.f, aspect, znear, zfar);
301 setProjectionAndViewUniforms(bbProg);
303 glClearColor(0.83, 1, 1, 1); // background color
304 glClear(GL_COLOR_BUFFER_BIT);
305 renderObject(); // render things that aren't clouds
311 bool needsRedisplay = false;
313 /* calculateMetaballs(); */
314 if (needsRedisplay) {
317 needsRedisplay = false;
318 glutTimerFunc(16, timer, 0);
321 void keyboard(unsigned char key, int x, int y) {
323 calculateMetaballs();
324 needsRedisplay = true;
325 needsReshading = true;
329 needsRedisplay = true;
332 curMode = debugContDist;
333 needsRedisplay = true;
336 curMode = debugProbAct;
337 needsRedisplay = true;
340 curMode = debugProbExt;
341 needsRedisplay = true;
345 int prevMouseX, prevMouseY;
346 bool firstMouse = true;
347 void motion(int x, int y) {
353 float dx = x - prevMouseX, dy = y - prevMouseY;
356 const vec3 origin(0, 18, 0);
357 const float sensitivity = 0.003f;
358 auto camMat = translate(mat4(1), origin + camPos);
359 auto rotation = rotate(rotate(mat4(1), -dx * sensitivity, {0, 1, 0}),
360 -dy * sensitivity, {1, 0, 0});
361 auto rotAroundOrig = camMat * rotation * translate(mat4(1), origin - camPos);
362 camPos = rotAroundOrig * glm::vec4(camPos, 0);
363 needsRedisplay = true;
366 void passiveMotion(int x, int y) {
371 int main(int argc, char **argv) {
372 glutInit(&argc, argv);
373 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB |
374 GLUT_3_2_CORE_PROFILE);
375 glutInitWindowSize(width, height);
376 glutCreateWindow("Clouds");
377 glutDisplayFunc(display);
381 Program prog("billboardvert.glsl", "billboardfrag.glsl");
383 bbProg = prog.progId;
384 glUseProgram(bbProg);
386 glGenVertexArrays(1, &bbVao);
387 glBindVertexArray(bbVao);
389 glGenBuffers(2, vbos);
391 vector<vec3> poss = {{-1, -1, 0}, {-1, 1, 0}, {1, 1, 0}, {1, -1, 0}};
392 vector<GLuint> indices = {2, 1, 0, 3, 2, 0};
394 GLuint posLoc = glGetAttribLocation(bbProg, "vPosition");
395 glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
396 glBufferData(GL_ARRAY_BUFFER, poss.size() * sizeof(glm::vec3), &poss[0],
398 glEnableVertexAttribArray(posLoc);
399 glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
401 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]);
402 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
403 &indices[0], GL_STATIC_DRAW);
407 precalculateBillboardTextures();
410 calculateMetaballs();
412 glGenTextures(1, &attenuationTex);
414 glutKeyboardFunc(keyboard);
415 glutMotionFunc(motion);
416 glutPassiveMotionFunc(passiveMotion);
417 glutTimerFunc(16, timer, 0);
419 // set up billboard prog