From 376c26aae6d27643280e0561147ef6babbd1dc46 Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Wed, 4 Jun 2025 20:58:30 +0200 Subject: [PATCH] Cloud shader fix, entity waving effect --- game/res/shaders/clouds.frag | 2 +- game/res/shaders/clouds.vert | 4 +++- game/res/shaders/default.vert | 19 ++++++++++++++++++- game/src/chunk/chunkman.h | 2 +- game/src/entity/entity.h | 1 + game/src/entity/entman.c | 17 +++++++++++++---- game/src/entity/templates.c | 2 ++ game/src/state.c | 4 +++- 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/game/res/shaders/clouds.frag b/game/res/shaders/clouds.frag index 18447de..bbf83b7 100644 --- a/game/res/shaders/clouds.frag +++ b/game/res/shaders/clouds.frag @@ -13,5 +13,5 @@ void main () discard; } - gl_FragColor = texture2D (texture, f_uv); + gl_FragColor = vec4 (0.7, 0.7, 0.7, 1.0); } diff --git a/game/res/shaders/clouds.vert b/game/res/shaders/clouds.vert index de17b21..bf6b552 100644 --- a/game/res/shaders/clouds.vert +++ b/game/res/shaders/clouds.vert @@ -12,6 +12,8 @@ varying vec2 f_uv; void main () { - f_uv = (uv * 20.0) + (time * 20.0); + /* Because the UVs go from 0 to 1, and I CBA to wrap textures, we + gotta mod it. */ + f_uv = (uv * 20.0) + vec2 (mod(time * 0.000003, 1.0), mod(time * 0.000002, 1.0)); gl_Position = projection * view * model * position; } diff --git a/game/res/shaders/default.vert b/game/res/shaders/default.vert index 21ce9f3..b539105 100644 --- a/game/res/shaders/default.vert +++ b/game/res/shaders/default.vert @@ -7,10 +7,27 @@ uniform mat4 view; uniform mat4 projection; uniform mat4 model; +uniform float time; +uniform bool wavy; + varying vec2 f_uv; void main () { f_uv = uv; - gl_Position = projection * view * model * position; + + vec4 pos = position; + + if (wavy) + { + vec2 center = vec2 (0.5, 0.5); + vec2 local = pos.xz - center; + + local.x += sin(local.x * time * 0.001) / 64.0; + local.y += cos(local.y * time * 0.001) / 64.0; + + pos.xz = local + center; + } + + gl_Position = projection * view * model * pos; } diff --git a/game/src/chunk/chunkman.h b/game/src/chunk/chunkman.h index 316b413..ac26702 100644 --- a/game/src/chunk/chunkman.h +++ b/game/src/chunk/chunkman.h @@ -9,7 +9,7 @@ #include "../entity/entman.h" #include "../entity/templates.h" -#define RENDER_DISTANCE 6 +#define RENDER_DISTANCE 4 void chunkman_init (); void chunkman_update (vec3 camera_position); diff --git a/game/src/entity/entity.h b/game/src/entity/entity.h index dbfe4cd..f77e5b9 100644 --- a/game/src/entity/entity.h +++ b/game/src/entity/entity.h @@ -12,6 +12,7 @@ typedef struct Entity enum { RENDER_NORMAL, RENDER_BILLBOARD_Y, RENDER_BILLBOARD } render_type; bool visible; bool dont_cull; + bool wavy; void *userdata; diff --git a/game/src/entity/entman.c b/game/src/entity/entman.c index bbc03e8..c1b0bf4 100644 --- a/game/src/entity/entman.c +++ b/game/src/entity/entman.c @@ -7,7 +7,7 @@ extern vec3 camera_position; extern int last_time; -Entity *player_entity; +Entity *player_entity, *sky_entity; static GSequence *entities; static EntityMesh mesh_quad1, mesh_quad2, mesh_quad_x; @@ -40,8 +40,8 @@ entman_init () ENTADD (player_entity); - Entity *sky = malloc (sizeof (Entity)); - *sky = (Entity) { + sky_entity = malloc (sizeof (Entity)); + *sky_entity = (Entity) { .mesh_type = MESH_QUAD1, .origin = ORIGIN_CENTER, .visible = true, @@ -53,7 +53,7 @@ entman_init () .program = RESPRG ("clouds") }; - ENTADD (sky); + ENTADD (sky_entity); } void @@ -184,6 +184,15 @@ entman_render_internal (gpointer data, gpointer userdata) program_set_mat4 (entity->program, "model", model); program_set_float (entity->program, "time", last_time); + if (entity->wavy) + { + program_set_float (entity->program, "wavy", true); + } + else + { + program_set_float (entity->program, "wavy", false); + } + if (entity->texture != last_texture) { glActiveTexture (GL_TEXTURE0); diff --git a/game/src/entity/templates.c b/game/src/entity/templates.c index afa7f6b..ceec908 100644 --- a/game/src/entity/templates.c +++ b/game/src/entity/templates.c @@ -23,6 +23,7 @@ ent_templates_init () *tree = (Entity) { .mesh_type = MESH_QUAD_X, .visible = true, + .wavy = true, .origin = ORIGIN_BOTTOM, .position = { 0.0 }, .scale = { 3.0, 8.0, 3.0 }, @@ -38,6 +39,7 @@ ent_templates_init () .mesh_type = MESH_QUAD1, .render_type = RENDER_BILLBOARD_Y, .visible = true, + .wavy = true, .origin = ORIGIN_BOTTOM, .position = { 0.0 }, .scale = { 1.0, 0.5, 1.0 }, diff --git a/game/src/state.c b/game/src/state.c index 5b8b491..c77a7e6 100644 --- a/game/src/state.c +++ b/game/src/state.c @@ -1,7 +1,7 @@ #include "state.h" extern vec3 camera_position; -extern Entity *player_entity; +extern Entity *player_entity, *sky_entity; float delta_time; int last_time; @@ -21,6 +21,8 @@ game_state_tick (int ticks) glm_vec3_copy (player_entity->position, camera_position); camera_position[1] += player_entity->scale[1] - 0.2; + sky_entity->position[0] = camera_position[0]; + sky_entity->position[2] = camera_position[2]; if (flags_get ("moving_forward")) {