Start simulation
[clouds.git] / clouds.cpp
index 7bb51e854e61c39ba8adaf1be51d15525758d70f..e406f5c6b7500aa06d1c055a819834a43c677ae4 100644 (file)
@@ -1,4 +1,5 @@
 #include "debug.hpp"
+#include "simulation.h"
 #include "program.hpp"
 #include <GL/glew.h>
 #include <GLUT/glut.h>
 using namespace std;
 using namespace glm;
 
-static const int CLOUD_DIM = 16;
-struct Clouds {
-  char cld[CLOUD_DIM][CLOUD_DIM][CLOUD_DIM];
-  float contDist[CLOUD_DIM][CLOUD_DIM][CLOUD_DIM];
-};
-
 // calculate continuous distribution
 void calcContDist(Clouds *clds);
 
@@ -75,8 +70,6 @@ vector<vec4> bbColors;
 GLuint bbProg;
 GLuint bbVao;
 
-void calculateMetaballs() {}
-
 // Here we need to generate n_q textures for different densities of metaballs
 // These textures then go on the billboards
 // The texture stores attenuation ratio?
@@ -84,21 +77,21 @@ void calculateMetaballs() {}
 #define NQ 1
 GLuint bbTexIds[NQ];
 
+// Stores attenuation ratio inside r channel
+// Should be highest value at center
 void precalculateBillboardTextures() {
   float data[32 * 32];
   // TODO: properly calculate this instead of whatever this is
   for (int j = 0; j < 32; j++)
     for (int i = 0; i < 32; i++)
-      data[i + j * 32] = fmin(1.f, 0.1f + 2.f * (distance(vec2(i, j), vec2(16, 16)) / 16));
+      data[i + j * 32] = fmin(1.f, 0.5f + 1.f * (distance(vec2(i, j), vec2(16, 16)) / 16));
 
   glGenTextures(NQ, bbTexIds);
 
-  /* GLuint captureFBO; */
-  /* glGenFramebuffers(1, &captureFBO); */
   for (int i = 0; i < NQ; i++) {
     glBindTexture(GL_TEXTURE_2D, bbTexIds[i]);
-    /* glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 32, 32, 0, GL_RED, */
-    /*              GL_FLOAT, data); */
+    checkError();
+
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 32, 32, 0, GL_RED, GL_FLOAT, data);
     glGenerateMipmap(GL_TEXTURE_2D); // required, otherwise texture is blank
 
@@ -116,6 +109,32 @@ struct Metaball {
 vector<Metaball> metaballs = {{{0, 0, 0.5}, 1.f},
                               {{0, 0.3, 0.3}, 0.7f}};
 
+Clouds cs;
+
+void calculateMetaballs() {
+  stepClouds(&cs);
+  metaballs.clear();
+  for (int i = 0; i < CLOUD_DIM; i++) {
+    for (int j = 0; j < CLOUD_DIM; j++) {
+      for (int k = 0; k < CLOUD_DIM; k++) {
+        if (cs.cld[i][j][k]) {
+          /* float x = (float)rand()/(float)(RAND_MAX); */
+          /* float y = (float)rand()/(float)(RAND_MAX); */
+          /* float z = (float)rand()/(float)(RAND_MAX); */
+          /* float r = (float)rand()/(float)(RAND_MAX); */
+          /* Metaball m = {{x,y, 0.3 + z * 0.5}, r}; */
+          /* metaballs.push_back(m); */
+          Metaball m = {{i / (float)CLOUD_DIM, j / (float)CLOUD_DIM, k / (float)CLOUD_DIM},
+                        1.f / (float)CLOUD_DIM };
+          m.pos = (m.pos * vec3(2)) - vec3(1);
+          metaballs.push_back(m);
+        }
+      }
+    }
+  }
+  fprintf(stderr, "num metaballs: %lu\n", metaballs.size());
+}
+
 vec3 sunPos = {0, 2, 2}, viewPos = {0, 0, 0}, lookPos = {0, 0, 1};
 mat4 proj; // projection matrix
 mat4 view; // view matrix
@@ -152,6 +171,7 @@ void shadeClouds() {
   bbColors.clear();
 
   glDisable(GL_DEPTH_TEST);
+  // shaderOutput * 0 + buffer * shader alpha
   glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
   glEnable(GL_BLEND);
 
@@ -159,24 +179,11 @@ void shadeClouds() {
   sort(metaballs.begin(), metaballs.end(), [](Metaball &a, Metaball &b) {
     return distance(sunPos, a.pos) < distance(sunPos, b.pos);
   });
-  /* GLuint fbo; */
-  /* glGenFramebuffers(1, &fbo); */
-  /* glBindFramebuffer(GL_FRAMEBUFFER, fbo); */
-  /* glBindTexture(GL_TEXTURE_2D, attenuationTex); */
-  /* glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, */
-  /*              GL_UNSIGNED_BYTE, NULL); */
-  /* glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, */
-  /*                        attenuationTex, 0); */
-  /* assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); */
 
   glActiveTexture(GL_TEXTURE0);
   glBindTexture(GL_TEXTURE_2D, bbTexIds[0]);
   glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
 
-/*   glClearColor(1, 1, 1, 1); */
-/*   glClear(GL_COLOR_BUFFER_BIT); */
-
-
   GLuint modelLoc = glGetUniformLocation(bbProg, "model");
 
   for (auto k : metaballs) {
@@ -195,8 +202,9 @@ void shadeClouds() {
 
     // Map the billboard texture with GL_MODULATE.
     // i.e. multiply rather than add
-    // TODO: FIX- this crashes with invalid operation
-    /* glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); */
+    // but glTexEnv is for the old fixed function pipeline --
+    // need to just tell our fragment shader then to modulate
+    glUniform1i(glGetUniformLocation(bbProg, "modulate"), 1);
 
     // Render the billboard.
     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
@@ -209,22 +217,19 @@ void shadeClouds() {
                       * vec2(width, height);
     vec4 pixel;
     glReadPixels(screenPos.x, screenPos.y, 1, 1, GL_RGBA, GL_FLOAT, value_ptr(pixel));
+    /* fprintf(stderr, "pixel:"); */
+    /* dump(pixel); */
 
     // Multiply the pixel value by the sunlight color.
     vec4 sunColor = {1, 1, 0.9, 1};
     pixel *= sunColor;
 
     // Store the color into an array C[k] as the color of the billboard.
-    fprintf(stderr, "pixel: ");
-    dump(pixel);
     bbColors.push_back(pixel);
   }
 
-  /* saveFBO(); */
+  saveFBO();
   checkError();
-
-  /* glDeleteFramebuffers(1, &fbo); */
-  /* glBindFramebuffer(GL_FRAMEBUFFER, 0); // render to screen */
 }
 
 void renderObject() {}
@@ -237,6 +242,7 @@ void renderClouds() {
 
   glDisable(GL_DEPTH_TEST);
   glEnable(GL_BLEND);
+  // shaderOutput * 1 + buffer * shader alpha
   glBlendFunc(GL_ONE, GL_SRC_ALPHA);
   for (int i = 0; i < metaballs.size(); i++) {
     Metaball k = metaballs[i];
@@ -251,9 +257,12 @@ void renderClouds() {
     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
 
     // Set the billboard color as C[n].
-    fprintf(stderr, "bbColors[i]: ");
-    dump(bbColors[i]);
-    /* bbColors[i] = {0,0,0,0.5}; */
+    /* fprintf(stderr, "bbColors[i]: "); */
+    /* dump(bbColors[i]); */
+    /* bbColors[i].x = 1 - bbColors[i].x; */
+    /* bbColors[i].y = 1 - bbColors[i].y; */
+    /* bbColors[i].z = 1 - bbColors[i].z; */
+    bbColors[i].w = 1;
     glUniform4fv(glGetUniformLocation(bbProg, "color"), 1,
                  glm::value_ptr(bbColors[i]));
 
@@ -262,6 +271,9 @@ void renderClouds() {
     glBindTexture(GL_TEXTURE_2D, bbTexIds[0]);
     glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
 
+    // Don't modulate it -- blend it
+    glUniform1i(glGetUniformLocation(bbProg, "modulate"), 0);
+
     // Render the billboard with the blending function.
     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
   }
@@ -288,6 +300,19 @@ void display() {
   glutSwapBuffers();
 }
 
+void timer(int _) {
+  /* calculateMetaballs(); */
+  /* glutPostRedisplay(); */
+  /* glutTimerFunc(16, timer, 0); */
+}
+
+void keyboard(unsigned char key, int x, int y) {
+       if (key == ' ') {
+               calculateMetaballs();
+               glutPostRedisplay();
+       }
+}
+
 int main(int argc, char **argv) {
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB |
@@ -326,11 +351,13 @@ int main(int argc, char **argv) {
 
   precalculateBillboardTextures();
 
+  initClouds(&cs);
   calculateMetaballs();
 
   glGenTextures(1, &attenuationTex);
 
-  /* glutTimerFunc(16, timer, 0); */
+  glutKeyboardFunc(keyboard);
+  glutTimerFunc(16, timer, 0);
 
   // set up billboard prog