]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add diffuse lighting, improve skybox and add timelapse
authorElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 28 Sep 2021 18:10:26 +0000 (20:10 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 28 Sep 2021 18:10:26 +0000 (20:10 +0200)
19 files changed:
README.md
shaders/3d/fragment.glsl
shaders/3d/vertex.glsl
shaders/sky/clouds/fragment.glsl [new file with mode: 0755]
shaders/sky/clouds/vertex.glsl [new file with mode: 0755]
shaders/sky/skybox/fragment.glsl
src/client/blockmesh.c
src/client/cube.c
src/client/debug_menu.c
src/client/debug_menu.h
src/client/game.c
src/client/input.c
src/client/object.c
src/client/object.h
src/client/scene.c
src/client/scene.h
src/client/sky.c
src/day.c
src/day.h

index df4183fe6a02d05fb274c6f6a0b8dec739a5a0a0..3b58989917980776a4896a8618136e9e2c2a78c9 100644 (file)
--- a/README.md
+++ b/README.md
@@ -29,6 +29,7 @@ or alternatively:
 | Left Shift | When flying: Move Down |
 | F | Toggle flight |
 | C | Toggle collision |
+| T | Toggle timelapse |
 | F11 | Toggle fullscreen |
 | F3 | Toggle debug info |
 | ESC | Pause / unpause game |
index a3c9a9f509af5b3a0e60ccbef287c6cff540deb7..be0eb58fd0c0aa846025892ae606c34cddd3c8f5 100755 (executable)
@@ -1,9 +1,12 @@
+in vec3 fragmentNormal;
 in float fragmentTextureIndex;
 in vec2 fragmentTextureCoords;
 in vec3 fragmentColor;
 
 out vec4 outColor;
 
+uniform float daylight;
+uniform vec3 lightDir;
 uniform sampler2D textures[MAX_TEXTURE_UNITS];
 
 vec3 hsv2rgb(vec3 c)
@@ -15,5 +18,12 @@ vec3 hsv2rgb(vec3 c)
 
 void main()
 {
-       outColor = texture(textures[int(fragmentTextureIndex + 0.5)], fragmentTextureCoords) * vec4(hsv2rgb(vec3(fragmentColor)), 1.0);
+       vec3 lightColor = vec3(1.0);
+
+       vec3 ambient = mix(0.2, 0.8, daylight) * lightColor;
+       vec3 diffuse = 0.2 * daylight * clamp(dot(normalize(fragmentNormal), normalize(lightDir)), 0.0, 1.0) * lightColor;
+
+       vec3 light = ambient + diffuse;
+
+       outColor = texture(textures[int(fragmentTextureIndex + 0.5)], fragmentTextureCoords) * vec4(hsv2rgb(vec3(fragmentColor)), 1.0) * vec4(light, 1.0);
 }
index 7226cb7a2385c27ba0214204ea71c8fc5e8b3adf..4417addba682b314c55c7972c582f68d0bb187b0 100755 (executable)
@@ -1,8 +1,10 @@
 layout(location = 0) in vec3 vertexPosition;
-layout(location = 1) in float vertexTextureIndex;
-layout(location = 2) in vec2 vertexTextureCoords;
-layout(location = 3) in vec3 vertexColor;
+layout(location = 1) in vec3 vertexNormal;
+layout(location = 2) in float vertexTextureIndex;
+layout(location = 3) in vec2 vertexTextureCoords;
+layout(location = 4) in vec3 vertexColor;
 
+out vec3 fragmentNormal;
 out float fragmentTextureIndex;
 out vec2 fragmentTextureCoords;
 out vec3 fragmentColor;
@@ -13,6 +15,7 @@ void main()
 {
        gl_Position = MVP * vec4(vertexPosition, 1.0);
 
+       fragmentNormal = vertexNormal;
        fragmentTextureIndex = vertexTextureIndex;
        fragmentTextureCoords = vertexTextureCoords;
        fragmentColor = vertexColor;
diff --git a/shaders/sky/clouds/fragment.glsl b/shaders/sky/clouds/fragment.glsl
new file mode 100755 (executable)
index 0000000..f22dc67
--- /dev/null
@@ -0,0 +1,31 @@
+in vec3 fragmentTextureCoords;
+
+out vec4 outColor;
+
+uniform float daylight;
+uniform samplerCube texture0;
+
+float reverseMix(float value, float min, float max)
+{
+       return clamp((value - min) / (max - min), 0.0, 1.0);
+}
+
+float strengthen(float value, float exponent, float max)
+{
+       return min((1.0 - pow(1.0 - value, exponent)) / (1.0 - pow(1.0 - max, exponent)), 1.0);
+}
+
+void main()
+{
+       float height = normalize(fragmentTextureCoords).y;
+
+       vec4 topColor = texture(texture0, vec3(0.0, 1.0, 0.0));
+       vec4 bottomColor = texture(texture0, vec3(1.0, 0.11, 0.5));
+       vec4 expectedColor = mix(bottomColor, topColor, height);
+
+       vec4 dayColor = texture(texture0, fragmentTextureCoords);
+
+       float cloudFactor = reverseMix(length(dayColor.rg - expectedColor.rg), 0.15, length(vec2(1.0)));
+
+       outColor = vec4(dayColor.rgb, mix(cloudFactor, strengthen(cloudFactor, 8.0, 0.1), daylight));
+}
diff --git a/shaders/sky/clouds/vertex.glsl b/shaders/sky/clouds/vertex.glsl
new file mode 100755 (executable)
index 0000000..6aab041
--- /dev/null
@@ -0,0 +1,12 @@
+layout(location = 0) in vec3 vertexPosition;
+
+out vec3 fragmentTextureCoords;
+
+uniform mat4 VP;
+
+void main()
+{
+       gl_Position = VP * vec4(vertexPosition, 1.0);
+       gl_Position.z = gl_Position.w;
+       fragmentTextureCoords = vertexPosition;
+}
index e8c42306a4b7eeb52982cd7c726de06b846f1176..ce1bac4fa2168ca15e3c34494e7f8de166e49c12 100755 (executable)
@@ -2,18 +2,16 @@ in vec3 fragmentTextureCoords;
 
 out vec4 outColor;
 
-uniform bool transparency;
 uniform float daylight;
 uniform samplerCube textures[2];
 
 void main()
 {
-       outColor = mix(texture(textures[1], fragmentTextureCoords), texture(textures[0], fragmentTextureCoords), daylight);
+       vec4 topColor = texture(textures[0], vec3(0.0, 1.0, 0.0));
+       vec4 bottomColor = texture(textures[0], vec3(1.0, 0.11, 0.5));
 
-       if (transparency) {
-               float f = 0.2;
-               float e = 2.0;
+       vec4 dayColor = mix(bottomColor, topColor, normalize(fragmentTextureCoords).y);
+       vec4 nightColor = texture(textures[1], fragmentTextureCoords);
 
-               outColor.a = pow((outColor.r + outColor.g) / 2.0 + f, e) / pow(1.0 + f, e);
-       }
+       outColor = mix(nightColor, dayColor, daylight);
 }
index 4b1e99db4185b5116e189a1baee9fdcc522590e0..26ca2bacea878275bc7d2a41debd408afcbb445b 100644 (file)
@@ -66,7 +66,7 @@ void blockmesh_make(MapBlock *block)
        Object *obj = object_create();
 
        obj->pos = (v3f32) {block->pos.x * MAPBLOCK_SIZE - half_block_size, block->pos.y * MAPBLOCK_SIZE - half_block_size, block->pos.z * MAPBLOCK_SIZE - half_block_size};
-       obj->frustum_culling = true;
+       obj->frustum_culling = false;
        obj->box = (aabb3f32) {{-half_block_size, -half_block_size, -half_block_size}, {half_block_size, half_block_size, half_block_size}};
 
        make_vertices(obj, block);
index 23fb817ef9a7b78c0b4dc2803e442226f48eac91..b089bf60bcdb221e1c634b29708f08c6a1037ef5 100644 (file)
@@ -2,52 +2,52 @@
 
 Vertex3D cube_vertices[6][6] = {
        {
-               {{-0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, -0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, -0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, -0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, +0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {+0.0, +0.0, -1.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, -0.5}, {+0.0, +0.0, -1.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+0.0, +0.0, -1.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+0.0, +0.0, -1.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, -0.5}, {+0.0, +0.0, -1.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {+0.0, +0.0, -1.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
        },
        {
-               {{-0.5, -0.5, +0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, +0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, +0.5, +0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, +0.5}, {+0.0, +0.0, +1.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+0.0, +0.0, +1.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, +0.5}, {+0.0, +0.0, +1.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+0.0, +0.0, +1.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, +0.5}, {+0.0, +0.0, +1.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, +0.5}, {+0.0, +0.0, +1.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
        },
        {
-               {{-0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, +0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, +0.5}, {-1.0, +0.0, +0.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {-1.0, +0.0, +0.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, -0.5}, {-1.0, +0.0, +0.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {-1.0, +0.0, +0.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, +0.5}, {-1.0, +0.0, +0.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, +0.5}, {-1.0, +0.0, +0.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
        },
        {
-               {{+0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+1.0, +0.0, +0.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+1.0, +0.0, +0.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, -0.5}, {+1.0, +0.0, +0.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, -0.5}, {+1.0, +0.0, +0.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, +0.5}, {+1.0, +0.0, +0.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+1.0, +0.0, +0.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
        },
        {
-               {{-0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, -0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, -0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, +0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, -0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {+0.0, -1.0, +0.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, -0.5}, {+0.0, -1.0, +0.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, +0.5}, {+0.0, -1.0, +0.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, -0.5, +0.5}, {+0.0, -1.0, +0.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, +0.5}, {+0.0, -1.0, +0.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {+0.0, -1.0, +0.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
        },
        {
-               {{-0.5, +0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, -0.5}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{+0.5, +0.5, +0.5}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, +0.5, +0.5}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
-               {{-0.5, +0.5, -0.5}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, -0.5}, {+0.0, +1.0, +0.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+0.0, +1.0, +0.0}, +0.0, {+1.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+0.0, +1.0, +0.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+0.0, +1.0, +0.0}, +0.0, {+1.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, +0.5}, {+0.0, +1.0, +0.0}, +0.0, {+0.+0.0, +0.0}, {+1.+0.0, +0.+0.0, +1.0}},
+               {{-0.5, +0.5, -0.5}, {+0.0, +1.0, +0.0}, +0.0, {+0.+0.0, +1.0}, {+1.+0.0, +0.+0.0, +1.0}},
        },
 };
 
index af00972ebae407ca171871185d1789482936bf26..59002563acebd797f10a0059d5be4379e0dd162a 100644 (file)
@@ -25,6 +25,7 @@ typedef enum
        DME_SEED,
        DME_FLIGHT,
        DME_COLLISION,
+       DME_TIMELAPSE,
        DME_FULLSCREEN,
        DME_OPENGL,
        DME_GPU,
@@ -162,6 +163,13 @@ void debug_menu_update_collision()
        gui_set_text(gui_elements[DME_COLLISION], text);
 }
 
+void debug_menu_update_timelapse()
+{
+       char text[BUFSIZ];
+       sprintf(text, "timelapse: %s", timelapse ? "enabled" : "disabled");
+       gui_set_text(gui_elements[DME_TIMELAPSE], text);
+}
+
 void debug_menu_update_fullscreen()
 {
        char text[BUFSIZ];
index c32ecfb0a21092dac45baaa4df319ec850b3b9a2..96700fa53570062007560058d32961e70ba7b358 100644 (file)
@@ -19,6 +19,7 @@ void debug_menu_update_temperature();
 void debug_menu_update_seed();
 void debug_menu_update_flight();
 void debug_menu_update_collision();
+void debug_menu_update_timelapse();
 void debug_menu_update_fullscreen();
 void debug_menu_update_opengl();
 void debug_menu_update_gpu();
index 8374788601c6a85a49d34b92fc46099880bf9a52..21a31a11bbb32a5c30d5bd8367e60f8967966bd2 100644 (file)
@@ -52,14 +52,14 @@ static void game_loop(Client *client)
                        debug_menu_update_fps(frames);
                        fps_update_timer += 1.0;
                        frames = 0;
-
-                       debug_menu_update_time();
-                       debug_menu_update_daylight();
-                       debug_menu_update_sun_angle();
                }
 
                frames++;
 
+               debug_menu_update_time();
+               debug_menu_update_daylight();
+               debug_menu_update_sun_angle();
+
                glEnable(GL_DEPTH_TEST);
                glEnable(GL_ALPHA_TEST);
                glEnable(GL_BLEND);
@@ -118,6 +118,7 @@ bool game(Client *client)
        debug_menu_update_seed();
        debug_menu_update_flight();
        debug_menu_update_collision();
+       debug_menu_update_timelapse();
        debug_menu_update_fullscreen();
        debug_menu_update_opengl();
        debug_menu_update_gpu();
index c2964c27e22937941c5e891aea73d11404de054e..063e34b6dc554478933466c32f7036b2805fc6a3 100644 (file)
@@ -6,6 +6,7 @@
 #include "client/gui.h"
 #include "client/input.h"
 #include "client/window.h"
+#include "day.h"
 
 typedef struct
 {
@@ -22,6 +23,7 @@ static struct
        KeyListener fullscreen_listener;
        KeyListener fly_listener;
        KeyListener collision_listener;
+       KeyListener timelapse_listener;
        KeyListener debug_menu_listener;
 } input;
 
@@ -116,6 +118,7 @@ void input_tick()
        if (! input.paused) {
                do_key_listener(&input.fly_listener);
                do_key_listener(&input.collision_listener);
+               do_key_listener(&input.timelapse_listener);
                do_key_listener(&input.debug_menu_listener);
 
                if (input.fly_listener.fired) {
@@ -128,6 +131,13 @@ void input_tick()
                        debug_menu_update_collision();
                }
 
+               if (input.timelapse_listener.fired) {
+                       f64 current_time = get_time_of_day();
+                       timelapse = ! timelapse;
+                       set_time_of_day(current_time);
+                       debug_menu_update_timelapse();
+               }
+
                if (input.debug_menu_listener.fired)
                        debug_menu_toggle();
        }
@@ -157,6 +167,7 @@ void input_init()
        input.fullscreen_listener = create_key_listener(GLFW_KEY_F11);
        input.fly_listener = create_key_listener(GLFW_KEY_F);
        input.collision_listener = create_key_listener(GLFW_KEY_C);
+       input.timelapse_listener = create_key_listener(GLFW_KEY_T);
        input.debug_menu_listener = create_key_listener(GLFW_KEY_F3);
 
        input.pause_menu = gui_add(&gui_root, (GUIElementDefinition) {
index 8d40495fd869b518a3ae863d4c4b81b3290e79d2..a3b80186132f95bbbff61e42d3dc8c399bae236d 100644 (file)
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include "client/object.h"
 #include "client/scene.h"
-#define OBJECT_VERTEX_ATTRIBUTES 4
+#define OBJECT_VERTEX_ATTRIBUTES 5
 
 static VertexAttribute vertex_attributes[OBJECT_VERTEX_ATTRIBUTES] = {
        // position
@@ -11,6 +11,12 @@ static VertexAttribute vertex_attributes[OBJECT_VERTEX_ATTRIBUTES] = {
                .length = 3,
                .size = sizeof(Vertex3DPosition),
        },
+       // normal
+       {
+               .type = GL_FLOAT,
+               .length = 3,
+               .size = sizeof(Vertex3DNormal),
+       },
        // textureIndex
        {
                .type = GL_FLOAT,
index 3e06b3d2bc56ee582f11435ff9410cc2c045f0fc..9f78c330929ca76892a331a9319f52c8422e9f2f 100644 (file)
@@ -16,6 +16,10 @@ typedef struct {
        GLfloat x, y, z;
 } __attribute__((packed)) Vertex3DPosition;
 
+typedef struct {
+       GLfloat x, y, z;
+} __attribute__((packed)) Vertex3DNormal;
+
 typedef GLfloat Vertex3DTextureIndex;
 
 typedef struct {
@@ -29,6 +33,7 @@ typedef struct {
 typedef struct
 {
        Vertex3DPosition position;
+       Vertex3DNormal normal;
        Vertex3DTextureIndex textureIndex;
        Vertex3DTextureCoordinates textureCoordinates;
        Vertex3DColor color;
index 7fac5e23ad4ada36faa8e1751a9313738fa44bd2..d8beb97a3d7f49068f80141f5ac0645a38e3985c 100644 (file)
@@ -4,6 +4,7 @@
 #include "client/client.h"
 #include "client/scene.h"
 #include "client/shader.h"
+#include "day.h"
 #include "util.h"
 
 struct Scene scene;
@@ -24,6 +25,8 @@ bool scene_init()
        }
 
        scene.loc_MVP = glGetUniformLocation(scene.prog, "MVP");
+       scene.loc_daylight = glGetUniformLocation(scene.prog, "daylight");
+       scene.loc_lightDir = glGetUniformLocation(scene.prog, "lightDir");
 
        GLint texture_indices[scene.max_texture_units];
        for (GLint i = 0; i < scene.max_texture_units; i++)
@@ -58,15 +61,23 @@ void scene_add_object(Object *obj)
 
 void scene_render()
 {
-       glUseProgram(scene.prog);
-
-       mat4x4 view_proj;
-
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wpedantic"
+       mat4x4 view_proj;
        mat4x4_mul(view_proj, scene.projection, camera.view);
+
+       vec4 base_sunlight_dir = {0.0f, 0.0f, -1.0f, 1.0f};
+       vec4 sunlight_dir;
+       mat4x4 sunlight_mat;
+       mat4x4_identity(sunlight_mat);
+       mat4x4_rotate(sunlight_mat, sunlight_mat, 1.0f, 0.0f, 0.0f, get_sun_angle() + M_PI / 2.0f);
+       mat4x4_mul_vec4(sunlight_dir, sunlight_mat, base_sunlight_dir);
 #pragma GCC diagnostic pop
 
+       glUseProgram(scene.prog);
+       glProgramUniform3f(scene.prog, scene.loc_lightDir, sunlight_dir[0], sunlight_dir[1], sunlight_dir[2]);
+       glProgramUniform1f(scene.prog, scene.loc_daylight, get_daylight());
+
        for (ListPair **pairptr = &scene.objects.first; *pairptr != NULL; ) {
                ListPair *pair = *pairptr;
                Object *obj = pair->key;
index 4e26d252ff3905001394616c64d1d63ed319083c..5f2739a1b545456fe45dddeac084bd6c704dbadb 100644 (file)
@@ -14,6 +14,8 @@ extern struct Scene
        pthread_mutex_t mtx;
        GLuint prog;
        GLint loc_MVP;
+       GLint loc_daylight;
+       GLint loc_lightDir;
        GLint max_texture_units;
        mat4x4 projection;
        f32 fov;
index 0ea8c795d1dae8ce436bf170fee6edc88d065fec..e3bf6202af55596878d9e2ed420c07df5a900a99 100644 (file)
@@ -15,7 +15,6 @@ static struct
 {
        GLuint skybox_prog;
        GLint skybox_loc_VP;
-       GLint skybox_loc_transparency;
        GLint skybox_loc_daylight;
        GLuint skybox_textures[2];
        Mesh *skybox_mesh;
@@ -23,6 +22,10 @@ static struct
        GLint sun_loc_MVP;
        Texture *sun_texture;
        Mesh *sun_mesh;
+       GLuint clouds_prog;
+       GLint clouds_loc_VP;
+       GLint clouds_loc_daylight;
+       Mesh *clouds_mesh;
 } sky;
 
 typedef struct
@@ -97,16 +100,18 @@ static VertexLayout skybox_vertex_layout = {
 };
 
 static VertexSkybox skybox_vertices[6][6];
+static VertexSkybox clouds_vertices[6][6];
 
 bool sky_init()
 {
+       // skybox
+
        if (! shader_program_create(RESSOURCEPATH "shaders/sky/skybox", &sky.skybox_prog, NULL)) {
                fprintf(stderr, "Failed to create skybox shader program\n");
                return false;
        }
 
        sky.skybox_loc_VP = glGetUniformLocation(sky.skybox_prog, "VP");
-       sky.skybox_loc_transparency = glGetUniformLocation(sky.skybox_prog, "transparency");
        sky.skybox_loc_daylight = glGetUniformLocation(sky.skybox_prog, "daylight");
 
        sky.skybox_textures[0] = texture_create_cubemap(RESSOURCEPATH "textures/skybox/day");
@@ -135,6 +140,8 @@ bool sky_init()
        sky.skybox_mesh->free_vertices = false;
        sky.skybox_mesh->layout = &skybox_vertex_layout;
 
+       // sun
+
        if (! shader_program_create(RESSOURCEPATH "shaders/sky/sun", &sky.sun_prog, NULL)) {
                fprintf(stderr, "Failed to create sun shader program\n");
                return false;
@@ -153,6 +160,33 @@ bool sky_init()
        sky.sun_mesh->free_vertices = false;
        sky.sun_mesh->layout = &sun_vertex_layout;
 
+       // clouds
+
+       if (! shader_program_create(RESSOURCEPATH "shaders/sky/clouds", &sky.clouds_prog, NULL)) {
+               fprintf(stderr, "Failed to create clouds shader program\n");
+               return false;
+       }
+
+       sky.clouds_loc_VP = glGetUniformLocation(sky.clouds_prog, "VP");
+       sky.clouds_loc_daylight = glGetUniformLocation(sky.clouds_prog, "daylight");
+
+       for (int f = 0; f < 6; f++) {
+               for (int v = 0; v < 6; v++) {
+                       clouds_vertices[f][v].position.x = cube_vertices[f][v].position.x;
+                       clouds_vertices[f][v].position.y = fmax(cube_vertices[f][v].position.y, 0.0);
+                       clouds_vertices[f][v].position.z = cube_vertices[f][v].position.z;
+               }
+       }
+
+       sky.clouds_mesh = mesh_create();
+       sky.clouds_mesh->textures = sky.skybox_textures;
+       sky.clouds_mesh->textures_count = 1;
+       sky.clouds_mesh->free_textures = false;
+       sky.clouds_mesh->vertices = clouds_vertices;
+       sky.clouds_mesh->vertices_count = 36;
+       sky.clouds_mesh->free_vertices = false;
+       sky.clouds_mesh->layout = &skybox_vertex_layout;
+
        return true;
 }
 
@@ -171,6 +205,7 @@ void sky_deinit()
 #pragma GCC diagnostic ignored "-Wpedantic"
 void sky_render()
 {
+       f64 daylight = get_daylight();
        f64 sun_angle = get_sun_angle();
 
        vec3 sun_pos = {0.0f, cos(sun_angle), sin(sun_angle)};
@@ -179,8 +214,7 @@ void sky_render()
 
        mat4x4 model;
        mat4x4_translate(model, sun_pos[0], sun_pos[1], sun_pos[2]);
-       mat4x4_rotate(model, model, 1.0f, 0.0f, 0.0f, sun_angle + 90);
-       mat4x4_rotate(model, model, 0.0f, 0.0f, 1.0f, M_PI / 4.0f);
+       mat4x4_rotate(model, model, 1.0f, 0.0f, 0.0f, sun_angle + M_PI / 2.0f);
 
        mat4x4 view;
        mat4x4_dup(view, camera.view);
@@ -198,16 +232,16 @@ void sky_render()
 
        glUseProgram(sky.skybox_prog);
        glUniformMatrix4fv(sky.skybox_loc_VP, 1, GL_FALSE, VP[0]);
-       glUniform1i(sky.skybox_loc_transparency, 0);
-       glUniform1f(sky.skybox_loc_daylight, get_daylight());
+       glUniform1f(sky.skybox_loc_daylight, daylight);
        mesh_render(sky.skybox_mesh);
 
        glUseProgram(sky.sun_prog);
        glUniformMatrix4fv(sky.sun_loc_MVP, 1, GL_FALSE, MVP[0]);
        mesh_render(sky.sun_mesh);
 
-       glUseProgram(sky.skybox_prog);
-       glUniform1i(sky.skybox_loc_transparency, 1);
-       mesh_render(sky.skybox_mesh);
+       glUseProgram(sky.clouds_prog);
+       glUniformMatrix4fv(sky.clouds_loc_VP, 1, GL_FALSE, VP[0]);
+       glUniform1f(sky.clouds_loc_daylight, daylight);
+       mesh_render(sky.clouds_mesh);
 }
 #pragma GCC diagnostic pop
index e3d79797916859e2ea4da65faad0d443529db3cf..0cc55000b4ed5ecb0a4cbaf03831392015f1f621 100644 (file)
--- a/src/day.c
+++ b/src/day.c
@@ -1,19 +1,26 @@
 #include <math.h>
+#include <time.h>
 #include "day.h"
 #include "util.h"
 
-static time_t time_of_day_offset;
+bool timelapse = false;
+static f64 time_of_day_offset;
 
-f64 get_time_of_day()
+f64 get_unix_time()
 {
        struct timespec ts;
        clock_gettime(CLOCK_REALTIME, &ts);
+       return ((f64) ts.tv_sec + ts.tv_nsec / 1.0e9) * (timelapse ? 100.0 : 1.0);
+}
 
-       return fmod((f64) ts.tv_sec - time_of_day_offset + ts.tv_nsec  / 1.0e9, MINUTES_PER_DAY);
+f64 get_time_of_day()
+{
+       return fmod(get_unix_time() - time_of_day_offset, MINUTES_PER_DAY);
 }
-void set_time_of_day(time_t new_time)
+
+void set_time_of_day(f64 new_time)
 {
-       time_of_day_offset = time(NULL) - new_time;
+       time_of_day_offset = get_unix_time() - new_time;
 }
 
 f64 get_sun_angle()
@@ -28,7 +35,7 @@ f64 get_daylight()
 
 void split_time_of_day(int *hours, int *minutes)
 {
-       time_t time_of_day = get_time_of_day();
+       int time_of_day = get_time_of_day();
 
        *minutes = time_of_day % MINUTES_PER_HOUR;
        *hours = time_of_day / MINUTES_PER_HOUR;
index 0aa66c7fc3aa23f8db30679ff7dffade302b0da0..c2bc6530460425d6ab27e9b87b46251177605b3f 100644 (file)
--- a/src/day.h
+++ b/src/day.h
@@ -1,7 +1,7 @@
 #ifndef _DAY_H_
 #define _DAY_H_
 
-#include <time.h>
+#include <stdbool.h>
 #include <dragontype/number.h>
 #define MINUTES_PER_HOUR 60
 #define HOURS_PER_DAY 24
 // 1 second in real life = 1 minute ingame
 
 f64 get_time_of_day();
-void set_time_of_day(time_t new_time);
+void set_time_of_day(f64 new_time);
 f64 get_sun_angle();
 f64 get_daylight();
 void split_time_of_day(int *hours, int *minutes);
 
+extern bool timelapse;
+
 #endif