]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add fog and face culling
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 29 Sep 2021 09:51:03 +0000 (11:51 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 29 Sep 2021 09:51:03 +0000 (11:51 +0200)
shaders/3d/fragment.glsl
shaders/3d/vertex.glsl
src/client/cube.c
src/client/game.c
src/client/gui.c
src/client/object.c
src/client/object.h
src/client/scene.c
src/client/scene.h
src/client/sky.c

index be0eb58fd0c0aa846025892ae606c34cddd3c8f5..f69fb49790dcb49b28d3401d059e28c98379ee0a 100755 (executable)
@@ -1,3 +1,4 @@
+in vec3 fragmentPosition;
 in vec3 fragmentNormal;
 in float fragmentTextureIndex;
 in vec2 fragmentTextureCoords;
@@ -7,6 +8,7 @@ out vec4 outColor;
 
 uniform float daylight;
 uniform vec3 lightDir;
+uniform vec3 cameraPos;
 uniform sampler2D textures[MAX_TEXTURE_UNITS];
 
 vec3 hsv2rgb(vec3 c)
@@ -19,11 +21,15 @@ vec3 hsv2rgb(vec3 c)
 void main()
 {
        vec3 lightColor = vec3(1.0);
+       vec3 fogColor = vec3(0x87, 0xCE, 0xEB) / vec3(0xFF);
 
-       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;
+       float ambientStrength = mix(0.3, 0.7, daylight);
+
+       vec3 ambient = ambientStrength * lightColor;
+       vec3 diffuse = 0.3 * 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);
+       outColor.rgb = mix(outColor.rgb, ambientStrength * vec3(fogColor), clamp(length(fragmentPosition - cameraPos) / 255.0, 0.0, 1.0));
 }
index 4417addba682b314c55c7972c582f68d0bb187b0..bb991ae628cccdbd53c344d5a08f5228043cfade 100755 (executable)
@@ -4,17 +4,21 @@ layout(location = 2) in float vertexTextureIndex;
 layout(location = 3) in vec2 vertexTextureCoords;
 layout(location = 4) in vec3 vertexColor;
 
+out vec3 fragmentPosition;
 out vec3 fragmentNormal;
 out float fragmentTextureIndex;
 out vec2 fragmentTextureCoords;
 out vec3 fragmentColor;
 
-uniform mat4 MVP;
+uniform mat4 model;
+uniform mat4 VP;
 
 void main()
 {
-       gl_Position = MVP * vec4(vertexPosition, 1.0);
+       vec4 worldSpace = model * vec4(vertexPosition, 1.0);
+       gl_Position = VP * worldSpace;
 
+       fragmentPosition = worldSpace.xyz;
        fragmentNormal = vertexNormal;
        fragmentTextureIndex = vertexTextureIndex;
        fragmentTextureCoords = vertexTextureCoords;
index b089bf60bcdb221e1c634b29708f08c6a1037ef5..42413796b34a1e33313c116e24d44c76022b99d0 100644 (file)
@@ -2,52 +2,52 @@
 
 Vertex3D cube_vertices[6][6] = {
        {
-               {{-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, -1.0}, +0.0, {+0.0, +0.0}, {+0.0, +0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+0.0, +0.0, -1.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, {+1.0, +0.0}, {+0.0, +0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+0.0, +0.0, -1.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.0, +0.0, +1.0}},
+               {{-0.5, +0.5, -0.5}, {+0.0, +0.0, -1.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, +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, +0.0, +1.0}, +0.0, {+0.0, +0.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.0, +0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+0.0, +0.0, +1.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, {+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, +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.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}, {-1.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, +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.0, +0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {-1.0, +0.0, +0.0}, +0.0, {+0.0, +0.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.0, +0.0, +1.0}},
+               {{-0.5, +0.5, +0.5}, {-1.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, {+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}, {+1.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.0, +0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+1.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.0, +0.0, +1.0}},
+               {{+0.5, +0.5, +0.5}, {+1.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, {+1.0, +0.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, -1.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, {+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.0, +0.0, +1.0}},
+               {{+0.5, -0.5, +0.5}, {+0.0, -1.0, +0.0}, +0.0, {+1.0, +0.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.0, +0.0, +1.0}},
+               {{-0.5, -0.5, -0.5}, {+0.0, -1.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, +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, +1.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, {+1.0, +0.0}, {+0.0, +0.0, +1.0}},
+               {{+0.5, +0.5, -0.5}, {+0.0, +1.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.0}, {+0.0, +0.0, +1.0}},
+               {{-0.5, +0.5, -0.5}, {+0.0, +1.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.0}, {+0.0, +0.0, +1.0}},
        },
 };
 
index 21a31a11bbb32a5c30d5bd8367e60f8967966bd2..e9dbebc9f82281d021a77db5ffe759c36baf63a7 100644 (file)
@@ -64,15 +64,22 @@ static void game_loop(Client *client)
                glEnable(GL_ALPHA_TEST);
                glEnable(GL_BLEND);
                glEnable(GL_MULTISAMPLE);
+               glEnable(GL_CULL_FACE);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                glAlphaFunc(GL_GREATER, 0.0f);
+               glCullFace(GL_BACK);
+               glFrontFace(GL_CCW);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
                input_tick();
                client_player_tick(dtime);
 
                scene_render();
+
+               glDisable(GL_CULL_FACE);
                sky_render();
+
+               glDisable(GL_DEPTH_TEST);
                gui_render();
 
                glfwSwapBuffers(window.handle);
index 8af0e006f9cd259194e3a5188675fc63c06adbf2..76ca68fbc541decb8e1cf17289dde8976983b754 100644 (file)
@@ -273,9 +273,7 @@ static void render_element(BintreeNode *node, unused void *arg)
 
 void gui_render()
 {
-       glDisable(GL_DEPTH_TEST);
        bintree_traverse(&gui_root.children, BTT_INORDER, &render_element, NULL);
-       glEnable(GL_DEPTH_TEST);
 }
 
 GUIElement *gui_add(GUIElement *parent, GUIElementDefinition def)
index a3b80186132f95bbbff61e42d3dc8c399bae236d..def0e4f5fab28bdf7ec5c7d77af5c9e9165173d0 100644 (file)
@@ -204,21 +204,24 @@ static bool inside_frustum(aabb3f32 box, mat4x4 MVP)
 #pragma GCC diagnostic pop
 
 
-void object_render(Object *obj, mat4x4 view_proj, GLint loc_MVP)
+void object_render(Object *obj)
 {
        if (! obj->visible)
                return;
 
-       mat4x4 MVP;
+       if (obj->frustum_culling) {
+               mat4x4 MVP;
+
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wpedantic"
-       mat4x4_mul(MVP, view_proj, obj->transform);
+               mat4x4_mul(MVP, scene.VP, obj->transform);
 #pragma GCC diagnostic pop
 
-       if (obj->frustum_culling && ! inside_frustum(obj->box, MVP))
-               return;
+                if (! inside_frustum(obj->box, MVP))
+                       return;
+       }
 
-       glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, MVP[0]);
+       glUniformMatrix4fv(scene.loc_model, 1, GL_FALSE, obj->transform[0]);
 
        if (obj->wireframe)
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
index 9f78c330929ca76892a331a9319f52c8422e9f2f..9d0630237f68489ddbe8c4828fe451981abdee1a 100644 (file)
@@ -67,6 +67,6 @@ void object_set_texture(Object *obj, Texture *texture);
 void object_add_vertex(Object *obj, Vertex3D *vertex);
 bool object_add_to_scene(Object *obj);
 void object_transform(Object *obj);
-void object_render(Object *obj, mat4x4 view_proj, GLint loc_MVP);
+void object_render(Object *obj);
 
 #endif
index d8beb97a3d7f49068f80141f5ac0645a38e3985c..9d076ab65cb11a772b40be7434f6693ca81add7e 100644 (file)
@@ -24,9 +24,11 @@ bool scene_init()
                return false;
        }
 
-       scene.loc_MVP = glGetUniformLocation(scene.prog, "MVP");
+       scene.loc_model = glGetUniformLocation(scene.prog, "model");
+       scene.loc_VP = glGetUniformLocation(scene.prog, "VP");
        scene.loc_daylight = glGetUniformLocation(scene.prog, "daylight");
        scene.loc_lightDir = glGetUniformLocation(scene.prog, "lightDir");
+       scene.loc_cameraPos = glGetUniformLocation(scene.prog, "cameraPos");
 
        GLint texture_indices[scene.max_texture_units];
        for (GLint i = 0; i < scene.max_texture_units; i++)
@@ -35,7 +37,7 @@ bool scene_init()
        glProgramUniform1iv(scene.prog, glGetUniformLocation(scene.prog, "textures"), scene.max_texture_units, texture_indices);
 
        scene.fov = 86.1f;
-       scene.render_distance = 255.0f;
+       scene.render_distance = 255.0f + 32.0f;
 
        return true;
 }
@@ -63,8 +65,7 @@ void scene_render()
 {
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wpedantic"
-       mat4x4 view_proj;
-       mat4x4_mul(view_proj, scene.projection, camera.view);
+       mat4x4_mul(scene.VP, scene.projection, camera.view);
 
        vec4 base_sunlight_dir = {0.0f, 0.0f, -1.0f, 1.0f};
        vec4 sunlight_dir;
@@ -75,8 +76,10 @@ void scene_render()
 #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());
+       glUniformMatrix4fv(scene.loc_VP, 1, GL_FALSE, scene.VP[0]);
+       glUniform3f(scene.loc_lightDir, sunlight_dir[0], sunlight_dir[1], sunlight_dir[2]);
+       glUniform3f(scene.loc_cameraPos, camera.eye[0], camera.eye[1], camera.eye[2]);
+       glUniform1f(scene.loc_daylight, get_daylight());
 
        for (ListPair **pairptr = &scene.objects.first; *pairptr != NULL; ) {
                ListPair *pair = *pairptr;
@@ -88,7 +91,7 @@ void scene_render()
                        free(pair);
                        object_delete(obj);
                } else {
-                       object_render(obj, view_proj, scene.loc_MVP);
+                       object_render(obj);
                        pairptr = &pair->next;
                }
        }
index 5f2739a1b545456fe45dddeac084bd6c704dbadb..639dc9a241f23c78427adbcc15e0c9e3a1e26222 100644 (file)
@@ -13,10 +13,13 @@ extern struct Scene
        List objects;
        pthread_mutex_t mtx;
        GLuint prog;
-       GLint loc_MVP;
+       GLint loc_model;
+       GLint loc_VP;
        GLint loc_daylight;
        GLint loc_lightDir;
+       GLint loc_cameraPos;
        GLint max_texture_units;
+       mat4x4 VP;
        mat4x4 projection;
        f32 fov;
        f32 render_distance;
index e3bf6202af55596878d9e2ed420c07df5a900a99..5a4092f654a0eeb90f6278602798e663f6cc1492 100644 (file)
@@ -243,5 +243,7 @@ void sky_render()
        glUniformMatrix4fv(sky.clouds_loc_VP, 1, GL_FALSE, VP[0]);
        glUniform1f(sky.clouds_loc_daylight, daylight);
        mesh_render(sky.clouds_mesh);
+
+       glDepthFunc(GL_LESS);
 }
 #pragma GCC diagnostic pop