Draw the sun, debug the colour and variable size
[clouds.git] / clouds.cpp
index baabcba703d4aca0e44e80f142e7eb97a82ed1c7..5d188233fe66c9eb16bdbbfcd5a08fffd7a377cb 100644 (file)
@@ -17,6 +17,7 @@
 enum Mode {
   render,
   debugContDist,
+  debugColor,
   debugProbExt,
   debugProbAct
 };
@@ -25,7 +26,7 @@ Mode curMode = render;
 using namespace std;
 using namespace glm;
 
-const float metaballR = 1.5f;
+const float metaballR = 2.f * 1.f / 16.f;
 inline float metaballField(float r) {
   if (r > metaballR)
     return 0;
@@ -43,7 +44,7 @@ void checkError() {
   }
 }
 
-GLuint bbProg;
+GLuint bbProg, sunProg;
 GLuint bbVao;
 
 // Here we need to generate n_q textures for different densities of metaballs
@@ -64,10 +65,10 @@ void precalculateBillboardTextures() {
     for (int j = 0; j < 32; j++) {
       for (int i = 0; i < 32; i++) {
         // TODO: properly calculate this instead of whatever this is
-        float r = distance(vec2(i, j), vec2(16, 16)) / 16;
+        float r = distance(vec2(i, j), vec2(16, 16)) / 32;
         float density = (float)d / NQ;
         data[i + j * 32] =
-            1 - (density * metaballField(r) / normalizationFactor);
+            1 - (density * 0.01 * metaballField(r * metaballR) / normalizationFactor);
       }
     }
 
@@ -92,12 +93,12 @@ void precalculateBillboardTextures() {
 struct Metaball {
   vec3 pos;
   ivec3 coords;
-  /** Radius, density */
-  float r, d;
+  /** Density */
+  float d;
   vec4 col;
 };
 
-array<Metaball, CLOUD_DIM * CLOUD_DIM * CLOUD_DIM> metaballs;
+array<Metaball, CLOUD_DIM_X * CLOUD_DIM_Y * CLOUD_DIM_Z> metaballs;
 
 Clouds cs;
 
@@ -111,22 +112,25 @@ void calculateMetaballs() {
   /*   Metaball m = {{x,y,z}, r}; */
   /*   metaballs.push_back(m); */
   /* } */
-  for (int i = 0; i < CLOUD_DIM; i++) {
-    for (int j = 0; j < CLOUD_DIM; j++) {
-      for (int k = 0; k < CLOUD_DIM; k++) {
+  for (int i = 0; i < CLOUD_DIM_X; i++) {
+    for (int j = 0; j < CLOUD_DIM_Y; j++) {
+      for (int k = 0; k < CLOUD_DIM_Z; k++) {
+        const float cloudScale = 1.f / 16;
         Metaball m = {
-            {i / (float)CLOUD_DIM, j / (float)CLOUD_DIM, k / (float)CLOUD_DIM},
-            {i, j, k},
-            1.f / (float)CLOUD_DIM};
-        m.pos = (m.pos * vec3(2)) - vec3(1);
+            vec3(i, j, k) * vec3(cloudScale),
+            {i, j, k} };
+        /* m.pos = (m.pos * vec3(2)) - (cloudScale / 2); */
+        m.pos -= vec3(CLOUD_DIM_X, CLOUD_DIM_Y, CLOUD_DIM_Z) * cloudScale / 2.f;
         m.d = cs.q[i][j][k];
-        metaballs[i * CLOUD_DIM * CLOUD_DIM + j * CLOUD_DIM + k] = m;
+        metaballs[i * CLOUD_DIM_Y * CLOUD_DIM_Z + j * CLOUD_DIM_Z + k] = m;
       }
     }
   }
 }
 
-vec3 sunPos = {0, 2, 2}, sunDir = {0, -1, -1};
+vec3 sunPos = {0, 5, 5}, sunDir = {0, -1, -1};
+/* vec4 sunColor = {1,0,0.429,1}; */
+vec4 sunColor = {1,1,1,1};
 vec3 camPos = {0, 0, -5}, viewPos = {0, 0, 0};
 mat4 proj; // projection matrix
 mat4 view; // view matrix
@@ -159,6 +163,8 @@ mat4 faceView(mat4 m) {
 
 GLuint attenuationTex;
 
+const float metaballScale = metaballR * 1.4f;
+
 void shadeClouds() {
   glDisable(GL_DEPTH_TEST);
   // shaderOutput * 0 + buffer * shader alpha
@@ -174,14 +180,17 @@ void shadeClouds() {
   glUniform1i(glGetUniformLocation(bbProg, "tex"), 0);
 
   GLuint modelLoc = glGetUniformLocation(bbProg, "model");
+  glUniform1i(glGetUniformLocation(bbProg, "debug"), 0);
 
   for (auto &k : metaballs) {
     // place the billboard at the center of k
-    mat4 model = scale(translate(mat4(1), k.pos), vec3(k.r) * 2.f);
+    mat4 model = translate(mat4(1), k.pos);
 
     // rotate the billboard so that its normal is oriented to the sun
     model = faceView(model);
 
+    model = scale(model, vec3(metaballScale));
+
     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
 
     // Set the billboard color as RGBA = (1.0, 1.0, 1.0, 1.0).
@@ -213,7 +222,6 @@ void shadeClouds() {
                  value_ptr(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.
@@ -224,9 +232,20 @@ void shadeClouds() {
   checkError();
 }
 
-void renderObject() {}
+void renderObject() {
+  glDisable(GL_BLEND);
+  // render the sun
+  glUseProgram(sunProg);
+  mat4 model = translate(mat4(1), sunPos);
+  /* model = lookAt(sunPos, sunPos + sunDir, {0, 1, 0}) * model; */
+  model = translate(scale(translate(model, -sunPos), vec3(0.3)), sunPos);
+  glUniformMatrix4fv(glGetUniformLocation(sunProg, "model"), 1, GL_FALSE, glm::value_ptr(model));
+  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 
+}
 
 void renderClouds() {
+  glUseProgram(bbProg);
+
   // Sort metaballs in descending order from the viewpoint
   sort(metaballs.begin(), metaballs.end(), [](Metaball &a, Metaball &b) {
     return distance(camPos, a.pos) > distance(camPos, b.pos);
@@ -248,10 +267,12 @@ void renderClouds() {
     GLuint modelLoc = glGetUniformLocation(bbProg, "model");
 
     // Place the billboard at the center of the corresponding metaball n.
-    mat4 model = scale(translate(mat4(1), k.pos), vec3(k.r) * 2.f);
+    mat4 model = translate(mat4(1), k.pos);
     // Rotate the billboard so that its normal is oriented to the viewpoint.
     model = faceView(model);
 
+    model = scale(model, vec3(metaballScale));
+
     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
 
     // Set the billboard color as C[n].
@@ -266,6 +287,7 @@ void renderClouds() {
     // Don't modulate it -- blend it
     glUniform1i(glGetUniformLocation(bbProg, "modulate"), 0);
 
+    glUniform1f(glGetUniformLocation(bbProg, "debugColor"), curMode == debugColor);
     if (curMode != render) {
       float debugVal = 0;
       if (curMode == debugContDist) debugVal = k.d;
@@ -273,7 +295,7 @@ void renderClouds() {
       else if (curMode == debugProbExt) debugVal = cs.p_ext[k.coords.x][k.coords.y][k.coords.z];
       glUniform1f(glGetUniformLocation(bbProg, "debugVal"), debugVal);
       glDisable(GL_BLEND);
-      model = scale(model, vec3(0.02));
+      model = scale(model, vec3(0.2));
       glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
     }
 
@@ -298,6 +320,9 @@ void display() {
 
   view = glm::lookAt(camPos, viewPos, {0, 1, 0});
   proj = glm::perspective(45.f, aspect, znear, zfar);
+  glUseProgram(sunProg);
+  setProjectionAndViewUniforms(sunProg);
+  glUseProgram(bbProg);
   setProjectionAndViewUniforms(bbProg);
 
   glClearColor(0.83, 1, 1, 1); // background color
@@ -322,7 +347,7 @@ void keyboard(unsigned char key, int x, int y) {
   if (key == ' ') {
     calculateMetaballs();
     needsRedisplay = true;
-    needsReshading = true;
+    needsReshading = curMode == render;
   }
   if (key == '0') {
     curMode = render;
@@ -333,10 +358,14 @@ void keyboard(unsigned char key, int x, int y) {
     needsRedisplay = true;
   }
   if (key == '2') {
-    curMode = debugProbAct;
+    curMode = debugColor;
     needsRedisplay = true;
   }
   if (key == '3') {
+    curMode = debugProbAct;
+    needsRedisplay = true;
+  }
+  if (key == '4') {
     curMode = debugProbExt;
     needsRedisplay = true;
   }
@@ -353,7 +382,7 @@ void motion(int x, int y) {
   float dx = x - prevMouseX, dy = y - prevMouseY;
   prevMouseX = x;
   prevMouseY = y;
-  const vec3 origin(0, 18, 0);
+  const vec3 origin(0, 0, 0);
   const float sensitivity = 0.003f;
   auto camMat = translate(mat4(1), origin + camPos);
   auto rotation = rotate(rotate(mat4(1), -dx * sensitivity, {0, 1, 0}),
@@ -379,11 +408,14 @@ int main(int argc, char **argv) {
   glewInit();
 
   Program prog("billboardvert.glsl", "billboardfrag.glsl");
-
   bbProg = prog.progId;
-  glUseProgram(bbProg);
+  Program sProg("sunvert.glsl", "sunfrag.glsl");
+  sunProg = sProg.progId;
 
   glGenVertexArrays(1, &bbVao);
+  glUseProgram(sunProg);
+  glBindVertexArray(bbVao);
+  glUseProgram(bbProg);
   glBindVertexArray(bbVao);
   GLuint vbos[2];
   glGenBuffers(2, vbos);