From ad0438849eb1dd3d08a8e2cbc476c84958eff3e7 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 1 Oct 2024 16:10:36 +0300 Subject: [PATCH] twn_fog.c: fog added! --- CMakeLists.txt | 1 + apps/scenery/scenes/ingame.c | 1 + include/twn_rendering.h | 2 ++ src/rendering/twn_fog.c | 34 +++++++++++++++++++++++++++++ src/rendering/twn_gl_15_rendering.c | 27 +++++++++++++++++++++++ src/rendering/twn_rendering.c | 3 +++ src/rendering/twn_rendering_c.h | 8 +++++++ 7 files changed, 76 insertions(+) create mode 100644 src/rendering/twn_fog.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5786993..6a3c579 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,6 +105,7 @@ set(TWN_SOURCE_FILES src/rendering/twn_triangles.c src/rendering/twn_circles.c src/rendering/twn_skybox.c + src/rendering/twn_fog.c # for dynamic load based solution main is compiled in a separate target $<$>:src/twn_main.c diff --git a/apps/scenery/scenes/ingame.c b/apps/scenery/scenes/ingame.c index 2e49be6..5d449a1 100644 --- a/apps/scenery/scenes/ingame.c +++ b/apps/scenery/scenes/ingame.c @@ -85,6 +85,7 @@ static void ingame_tick(State *state) { } push_skybox("/assets/miramar/miramar_*.tga"); + push_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 }); } diff --git a/include/twn_rendering.h b/include/twn_rendering.h index 5cf1740..2028527 100644 --- a/include/twn_rendering.h +++ b/include/twn_rendering.h @@ -76,4 +76,6 @@ TWN_API void set_camera(const Camera *camera); /* expects '*' masks that will be expanded to 6 names: 'up', 'down', 'east', 'west', 'north' and 'south' */ TWN_API void push_skybox(const char *paths); +TWN_API void push_fog(float start, float end, float density, Color color); + #endif diff --git a/src/rendering/twn_fog.c b/src/rendering/twn_fog.c new file mode 100644 index 0000000..4beec7c --- /dev/null +++ b/src/rendering/twn_fog.c @@ -0,0 +1,34 @@ +#include "twn_rendering.h" +#include "twn_rendering_c.h" +#include "twn_util.h" + +#include + +static float start_cache, end_cache, density_cache; +static Color color_cache; +static bool fog_used = false; + + +void push_fog(float start, float end, float density, Color color) { + start_cache = start; + end_cache = end; + density_cache = density; + color_cache = color; + fog_used = true; +} + + +void apply_fog(void) { + if (!fog_used) + return; + + finally_apply_fog(start_cache, end_cache, density_cache, color_cache); +} + + +void pop_fog(void) { + if (!fog_used) + return; + + finally_pop_fog(); +} diff --git a/src/rendering/twn_gl_15_rendering.c b/src/rendering/twn_gl_15_rendering.c index 2fed12b..23fc0f9 100644 --- a/src/rendering/twn_gl_15_rendering.c +++ b/src/rendering/twn_gl_15_rendering.c @@ -666,3 +666,30 @@ void finally_render_skybox(char *paths) { glCallList(list); } + + +void finally_apply_fog(float start, float end, float density, Color color) { + if (density < 0.0f || density > 1.0f) + log_warn("Invalid fog density given, should be in range [0..1]"); + + /* TODO: cache it for constant parameters, which is a common case */ + + glEnable(GL_FOG); + + glFogf(GL_FOG_DENSITY, density); + glFogf(GL_FOG_START, start); + glFogf(GL_FOG_END, end); + + float color_conv[4]; + color_conv[0] = (float)color.r / UINT8_MAX; + color_conv[1] = (float)color.g / UINT8_MAX; + color_conv[2] = (float)color.b / UINT8_MAX; + color_conv[3] = (float)color.a / UINT8_MAX; + + glFogfv(GL_FOG_COLOR, color_conv); +} + + +void finally_pop_fog(void) { + glDisable(GL_FOG); +} diff --git a/src/rendering/twn_rendering.c b/src/rendering/twn_rendering.c index d8443ec..9031df5 100644 --- a/src/rendering/twn_rendering.c +++ b/src/rendering/twn_rendering.c @@ -93,11 +93,14 @@ static void render_space(void) { return; use_space_pipeline(); + apply_fog(); for (size_t i = 0; i < hmlenu(ctx.uncolored_mesh_batches); ++i) { draw_uncolored_space_traingle_batch(&ctx.uncolored_mesh_batches[i].value, ctx.uncolored_mesh_batches[i].key); } + + pop_fog(); } diff --git a/src/rendering/twn_rendering_c.h b/src/rendering/twn_rendering_c.h index 8cbb389..96f072d 100644 --- a/src/rendering/twn_rendering_c.h +++ b/src/rendering/twn_rendering_c.h @@ -225,4 +225,12 @@ void render_skybox(void); void finally_render_skybox(char *paths_in_use); +void apply_fog(void); + +void finally_apply_fog(float start, float end, float density, Color color); + +void pop_fog(void); + +void finally_pop_fog(void); + #endif