From: Elias Fleckenstein Date: Wed, 29 Sep 2021 09:51:03 +0000 (+0200) Subject: Add fog and face culling X-Git-Tag: 0.2~21 X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=e93a262033218d8705ed01fbcb03db813db14904;p=dragonblocks_alpha.git Add fog and face culling --- diff --git a/shaders/3d/fragment.glsl b/shaders/3d/fragment.glsl index be0eb58..f69fb49 100755 --- a/shaders/3d/fragment.glsl +++ b/shaders/3d/fragment.glsl @@ -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)); } diff --git a/shaders/3d/vertex.glsl b/shaders/3d/vertex.glsl index 4417add..bb991ae 100755 --- a/shaders/3d/vertex.glsl +++ b/shaders/3d/vertex.glsl @@ -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; diff --git a/src/client/cube.c b/src/client/cube.c index b089bf6..4241379 100644 --- a/src/client/cube.c +++ b/src/client/cube.c @@ -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}}, }, }; diff --git a/src/client/game.c b/src/client/game.c index 21a31a1..e9dbebc 100644 --- a/src/client/game.c +++ b/src/client/game.c @@ -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); diff --git a/src/client/gui.c b/src/client/gui.c index 8af0e00..76ca68f 100644 --- a/src/client/gui.c +++ b/src/client/gui.c @@ -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) diff --git a/src/client/object.c b/src/client/object.c index a3b8018..def0e4f 100644 --- a/src/client/object.c +++ b/src/client/object.c @@ -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); diff --git a/src/client/object.h b/src/client/object.h index 9f78c33..9d06302 100644 --- a/src/client/object.h +++ b/src/client/object.h @@ -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 diff --git a/src/client/scene.c b/src/client/scene.c index d8beb97..9d076ab 100644 --- a/src/client/scene.c +++ b/src/client/scene.c @@ -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; } } diff --git a/src/client/scene.h b/src/client/scene.h index 5f2739a..639dc9a 100644 --- a/src/client/scene.h +++ b/src/client/scene.h @@ -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; diff --git a/src/client/sky.c b/src/client/sky.c index e3bf620..5a4092f 100644 --- a/src/client/sky.c +++ b/src/client/sky.c @@ -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