twn_fog.c: fog added!

This commit is contained in:
veclav talica 2024-10-01 16:10:36 +03:00
parent edc6fb1e5c
commit ad0438849e
7 changed files with 76 additions and 0 deletions

View File

@ -105,6 +105,7 @@ set(TWN_SOURCE_FILES
src/rendering/twn_triangles.c src/rendering/twn_triangles.c
src/rendering/twn_circles.c src/rendering/twn_circles.c
src/rendering/twn_skybox.c src/rendering/twn_skybox.c
src/rendering/twn_fog.c
# for dynamic load based solution main is compiled in a separate target # for dynamic load based solution main is compiled in a separate target
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c $<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c

View File

@ -85,6 +85,7 @@ static void ingame_tick(State *state) {
} }
push_skybox("/assets/miramar/miramar_*.tga"); push_skybox("/assets/miramar/miramar_*.tga");
push_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 });
} }

View File

@ -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' */ /* 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_skybox(const char *paths);
TWN_API void push_fog(float start, float end, float density, Color color);
#endif #endif

34
src/rendering/twn_fog.c Normal file
View File

@ -0,0 +1,34 @@
#include "twn_rendering.h"
#include "twn_rendering_c.h"
#include "twn_util.h"
#include <stdbool.h>
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();
}

View File

@ -666,3 +666,30 @@ void finally_render_skybox(char *paths) {
glCallList(list); 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);
}

View File

@ -93,11 +93,14 @@ static void render_space(void) {
return; return;
use_space_pipeline(); use_space_pipeline();
apply_fog();
for (size_t i = 0; i < hmlenu(ctx.uncolored_mesh_batches); ++i) { for (size_t i = 0; i < hmlenu(ctx.uncolored_mesh_batches); ++i) {
draw_uncolored_space_traingle_batch(&ctx.uncolored_mesh_batches[i].value, draw_uncolored_space_traingle_batch(&ctx.uncolored_mesh_batches[i].value,
ctx.uncolored_mesh_batches[i].key); ctx.uncolored_mesh_batches[i].key);
} }
pop_fog();
} }

View File

@ -225,4 +225,12 @@ void render_skybox(void);
void finally_render_skybox(char *paths_in_use); 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 #endif