strichmaennchen git · main
Entropie festival game; first steps, second try
C 95% GLSL 4.7%git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gzjeh shadow is close,...
game.c | 34 +++++++------------------------ game.h | 3 ++- main.c | 12 ++++------- shaders/.basic.frag.swp | Bin 0 -> 12288 bytes shaders/basic.frag | 53 ++++++++++++++++++++++++++++++++---------------- shaders/basic.vert | 22 +++++++++++++++----- time.c | 20 ++++++++++++++---- time.h | 4 +++- ui.c | 2 +- world.c | 36 ++++++++++++++++++++++++++++++-- world.h | 2 +- 11 files changed, 121 insertions(+), 67 deletions(-) diff --git a/game.c b/game.c index e568c59..c21ab8f 100644 --- a/game.c +++ b/game.c @@ -7,6 +7,9 @@ #include <float.h> bool Game_init(Game* game) { + //game->time = Time_init(3600.0f); + game->time = Time_init(3600.0f); + World_init(&game->world); Human *player = Human_create(vec3(1,0,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), true, 60); @@ -71,42 +74,19 @@ void Game_update(Game* game, Userinput* input, float dt) { World_update(&game->world, dt); } -void Game_render(Game* game, Renderer* renderer, Camera* camera, Time* time) { +void Game_render(Game* game, Renderer* renderer, Camera* camera) { Renderer_clear(0.0f, 0.0f, 0.0f, 1.0f); - // Compute light matrices - Vec3 lightPos = vec3_scale(time->sunDirection, 50.0f); - Vec3 lightTarget = {0, 0, 0}; - Vec3 lightUp = {0, 1, 0}; - renderer->lightView = mat4_lookAt(lightPos, lightTarget, lightUp); - float left = -30.0f, right = 30.0f, bottom = -30.0f, top = 30.0f, near = -100.0f, far = 100.0f; - renderer->lightProjection = mat4_orthographic(left, right, bottom, top, near, far); - - // Shadow pass - glViewport(0, 0, renderer->shadowMapWidth, renderer->shadowMapHeight); - glBindFramebuffer(GL_FRAMEBUFFER, renderer->shadowFramebuffer); - glClear(GL_DEPTH_BUFFER_BIT); - - glEnable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(1.0f, 1.0f); // Reduced offset to minimize peter panning - - glEnable(GL_CULL_FACE); - glCullFace(GL_FRONT); - - World_renderShadows(&game->world, renderer, renderer->lightView, renderer->lightProjection); - - glDisable(GL_CULL_FACE); - glDisable(GL_POLYGON_OFFSET_FILL); - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, renderer->width, renderer->height); + World_renderShadows(&game->world, renderer, game->time); - World_render(&game->world, renderer, camera, time); + World_render(&game->world, renderer, camera, game->time); } void Game_shutdown(Game* game) { printf("asd%p", game); + Time_destroy(game->time); //if (game->triangle) Object_destroy(game->triangle); //if (game->axes) Object_destroy(game->axes); } diff --git a/game.h b/game.h index 9380587..a446b32 100644 --- a/game.h +++ b/game.h @@ -12,13 +12,14 @@ typedef struct { World world; Human* player; + Time* time; // later: score, level state, player refs, etc. } Game; bool Game_init(Game* game); void Game_update(Game* game, Userinput* input, float dt); -void Game_render(Game* game, Renderer* renderer, Camera* camera, Time* time); +void Game_render(Game* game, Renderer* renderer, Camera* camera); void Game_shutdown(Game* game); #endif diff --git a/main.c b/main.c index 7ff2669..e2b94d0 100644 --- a/main.c +++ b/main.c @@ -38,9 +38,6 @@ int main(void) { Input_reset(&userinput); Uint64 prev_ticks = SDL_GetTicks(); - Time worldTime; - Time_init(&worldTime, 3600.0f); - Skybox skybox; Skybox_init(&skybox, "shaders/skybox.vert", "shaders/skybox.frag"); @@ -52,12 +49,11 @@ int main(void) { return 1; } - while (userinput.running) { float dt = Time_delta(&prev_ticks); if (dt > 0.05f) dt = 0.05f; - Time_update(&worldTime, dt); + Time_update(game.time, dt); prev_ticks = SDL_GetTicks(); @@ -80,9 +76,9 @@ int main(void) { Camera_followPlayer(&camera, game.player); Camera_update(&camera, renderer); - Game_render(&game, renderer, &camera, &worldTime); - Skybox_draw(&skybox, renderer, &camera, &worldTime); - Ui_render(&ui, renderer, &worldTime); + Game_render(&game, renderer, &camera); + Skybox_draw(&skybox, renderer, &camera, game.time); + Ui_render(&ui, renderer, game.time); SDL_GL_SwapWindow(renderer->window); } diff --git a/shaders/.basic.frag.swp b/shaders/.basic.frag.swp new file mode 100644 index 0000000..88af8a7 Binary files /dev/null and b/shaders/.basic.frag.swp differ diff --git a/shaders/basic.frag b/shaders/basic.frag index 32d8f5d..ba87490 100644 --- a/shaders/basic.frag +++ b/shaders/basic.frag @@ -1,31 +1,50 @@ #version 330 core -in vec3 FragPos; in vec3 Normal; in vec3 VertexColor; in vec4 FragPosLightSpace; +in vec3 FragPos; +in vec3 Normal; +in vec3 VertexColor; +in vec4 FragPosLightSpace; out vec4 FragColor; -uniform vec3 viewPos; uniform vec3 lightDirection; uniform vec3 lightColor; uniform vec3 ambientColor; uniform sampler2D shadowMap; uniform float dayFactor; +uniform vec3 viewPos; +uniform vec3 lightDirection; +uniform vec3 lightColor; +uniform vec3 ambientColor; +uniform sampler2D shadowMap; +uniform float dayFactor; vec3 rotateY(vec3 v, float angle) { float c = cos(angle); float s = sin(angle); return vec3(c*v.x + s*v.z, v.y, -s*v.x + c*v.z); } vec3 rotateX(vec3 v, float angle) { float c = cos(angle); float s = sin(angle); return vec3(v.x, c*v.y - s*v.z, s*v.y + c*v.z); } -float ShadowCalculation(vec4 fragPosLightSpace) { vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; projCoords = projCoords * 0.5 + 0.5; if (projCoords.z > 1.0) return 1.0; +float ShadowCalculation(vec4 fragPosLightSpace) { + vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; + projCoords = projCoords * 0.5 + 0.5; + + // If outside shadow map, treat as fully lit + if (projCoords.x < 0.0 || projCoords.x > 1.0 || + projCoords.y < 0.0 || projCoords.y > 1.0) + return 1.0; + + if (projCoords.z > 1.0) + return 1.0; + float currentDepth = projCoords.z; -vec3 norm = normalize(Normal); -vec3 lightDir = normalize(lightDirection); -float bias = max(0.002 * (1.0 - dot(norm, lightDir)), 0.0001); // Further reduced bias to avoid peter panning - -// Larger PCF kernel for smoother shadows (5x5) -float shadow = 0.0; -vec2 texelSize = 1.0 / textureSize(shadowMap, 0); -for(int x = -2; x <= 2; ++x) { - for(int y = -2; y <= 2; ++y) { - float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; - shadow += currentDepth - bias > pcfDepth ? 0.5 : 1.0; + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(lightDirection); + float bias = max(0.002 * (1.0 - dot(norm, lightDir)), 0.0001); + + // PCF (5x5) for smoother shadows + float shadow = 0.0; + vec2 texelSize = 1.0 / textureSize(shadowMap, 0); + for(int x = -2; x <= 2; ++x) { + for(int y = -2; y <= 2; ++y) { + float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; + shadow += currentDepth - bias > pcfDepth ? 0.5 : 1.0; + } } -} -return shadow / 25.0; + return shadow / 25.0; } void main() { vec3 norm = normalize(Normal); @@ -54,7 +73,7 @@ for(int i = 0; i < 4; ++i) { // Moon vec3 moonDir = normalize(-lightDirection); vec3 moonColor = vec3(0.6, 0.65, 0.75); -float moonIntensity = 0.02 * (1.0 - dayFactor); +float moonIntensity = 0.05 * (1.0 - dayFactor); float moonDiffuseFactor = max(dot(norm, moonDir), 0.0); vec3 moonDiffuse = moonDiffuseFactor * moonColor * VertexColor * moonIntensity; totalDiffuse += moonDiffuse; diff --git a/shaders/basic.vert b/shaders/basic.vert index 555b43a..3512dfb 100644 --- a/shaders/basic.vert +++ b/shaders/basic.vert @@ -1,11 +1,23 @@ #version 330 core -layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; layout (location = 2) in vec3 aColor; +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec3 aColor; -out vec3 FragPos; out vec3 Normal; out vec3 VertexColor; out vec4 FragPosLightSpace; +out vec3 FragPos; +out vec3 Normal; +out vec3 VertexColor; +out vec4 FragPosLightSpace; -uniform mat4 model; uniform mat4 view; uniform mat4 projection; uniform mat4 lightSpaceMatrix; +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; +uniform mat4 lightSpaceMatrix; -void main() { FragPos = vec3(model * vec4(aPos, 1.0)); Normal = mat3(transpose(inverse(model))) * aNormal; // Removed flip -VertexColor = aColor; FragPosLightSpace = lightSpaceMatrix * vec4(aPos, 1.0); gl_Position = projection * view * vec4(FragPos, 1.0); +void main() { + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = mat3(transpose(inverse(model))) * aNormal; // Removed flip + VertexColor = aColor; + FragPosLightSpace = lightSpaceMatrix * vec4(aPos, 1.0); + gl_Position = projection * view * vec4(FragPos, 1.0); } diff --git a/time.c b/time.c index 2878b8e..66b254e 100644 --- a/time.c +++ b/time.c @@ -1,5 +1,6 @@ #include "time.h" #include <math.h> +#include <stdlib.h> float fmodf(float x, float y); float cosf(float x); @@ -19,8 +20,11 @@ float Time_delta(Uint64* prev_ticks) { return dt; } -void Time_init(Time* t, float timeScale) { - t->hours = 12.0f; // start at noon +Time* Time_init(float timeScale) { + Time* t = malloc(sizeof(Time)); + if (!t) return NULL; + //t->hours = 12.0f; + t->hours = 13.0f; t->minutes = 0.0f; t->seconds = 0.0f; t->timeScale = timeScale; @@ -29,6 +33,8 @@ void Time_init(Time* t, float timeScale) { t->dayEnd = 21.0f; // 21:00 Time_computeSunPosition(t); + + return t; } void Time_update(Time* t, float deltaSeconds) { @@ -77,7 +83,13 @@ void Time_computeSunPosition(Time* t) { t->sunColor.z = 0.8f * intensity; // ambient color - t->ambientColor.x = 0.1f + 0.2f * intensity; - t->ambientColor.y = 0.1f + 0.2f * intensity; + t->ambientColor.x = 0.1f + 0.2f * intensity; + t->ambientColor.y = 0.1f + 0.2f * intensity; t->ambientColor.z = 0.15f + 0.2f * intensity; } + + + +void Time_destroy(Time* t) { + free(t); +} diff --git a/time.h b/time.h index 2df790f..da8f740 100644 --- a/time.h +++ b/time.h @@ -22,11 +22,13 @@ typedef struct { Vec3 ambientColor; // global ambient light } Time; -void Time_init(Time* t, float timeScale); +Time* Time_init(float timeScale); void Time_update(Time* t, float deltaSeconds); // optional helpers float Time_getFractionOfDay(const Time* t); // 0.0..1.0 void Time_computeSunPosition(Time* t); // sets sunDirection & colors +void Time_destroy(Time* t); + #endif diff --git a/ui.c b/ui.c index 980f2a5..ef1216a 100644 --- a/ui.c +++ b/ui.c @@ -104,7 +104,7 @@ void Ui_render(Ui *ui, Renderer *renderer, Time *time) { // Compose text char buf[64]; - snprintf(buf, sizeof(buf), "%02d:%02d", (int)time->hours, (int)time->minutes); + snprintf(buf, sizeof(buf), "%02d:%02d:%02d", (int)time->hours, (int)time->minutes, (int)time->seconds); if (strcmp(buf, ui->last_text) != 0) { strncpy(ui->last_text, buf, sizeof(ui->last_text)-1); diff --git a/world.c b/world.c index efb54dc..6d4c096 100644 --- a/world.c +++ b/world.c @@ -2,6 +2,7 @@ #include "glfuncs.h" #include <stdlib.h> #include <stdio.h> +#include <math.h> void World_init(World *world) { world->countObjects = 0; @@ -59,7 +60,32 @@ void World_render(World *world, Renderer *renderer, Camera *camera, Time* time) } } -void World_renderShadows(World* world, Renderer* renderer, Mat4 lightView, Mat4 lightProjection) { +void World_renderShadows(World* world, Renderer* renderer, Time* time) { + // Compute light matrices + Vec3 forward = vec3_normalize(time->sunDirection); + Vec3 arbitrary = fabsf(forward.y) > 0.99f ? (Vec3){1,0,0} : (Vec3){0,1,0}; + Vec3 right = vec3_normalize(vec3_cross(arbitrary, forward)); + Vec3 up = vec3_cross(forward, right); + + Vec3 lightPos = vec3_scale(forward, 50.0f); + Vec3 lightTarget = (Vec3){0, 0, 0}; + + renderer->lightView = mat4_lookAt(lightPos, lightTarget, up); + + float left = -30.0f, rightv = 30.0f, bottom = -30.0f, top = 30.0f, near = -100.0f, far = 100.0f; + renderer->lightProjection = mat4_orthographic(left, rightv, bottom, top, near, far); + + // Shadow pass + glViewport(0, 0, renderer->shadowMapWidth, renderer->shadowMapHeight); + glBindFramebuffer(GL_FRAMEBUFFER, renderer->shadowFramebuffer); + glClear(GL_DEPTH_BUFFER_BIT); + + glEnable(GL_POLYGON_OFFSET_FILL); + glPolygonOffset(1.0f, 1.0f); + + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT); + glUseProgram(renderer->shadowShader); for (int i = 0; i < world->countObjects; i++) { @@ -70,7 +96,7 @@ void World_renderShadows(World* world, Renderer* renderer, Mat4 lightView, Mat4 model = mat4_mul(model, mat4_rotate_z(obj->rotation.z)); model = mat4_mul(model, mat4_scale(obj->scale)); - Mat4 lightSpace = mat4_mul(lightProjection, mat4_mul(lightView, model)); + Mat4 lightSpace = mat4_mul(renderer->lightProjection, mat4_mul(renderer->lightView, model)); GLint loc = glGetUniformLocation(renderer->shadowShader, "lightSpaceMatrix"); glUniformMatrix4fv(loc, 1, GL_FALSE, (float*)&lightSpace); @@ -81,6 +107,12 @@ void World_renderShadows(World* world, Renderer* renderer, Mat4 lightView, Mat4 glDrawArrays(obj->mesh.drawMode, 0, obj->mesh.vertexCount); } } + + glDisable(GL_CULL_FACE); + glDisable(GL_POLYGON_OFFSET_FILL); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, renderer->width, renderer->height); } void World_destroy(World *world) { diff --git a/world.h b/world.h index e930245..66ebd98 100644 --- a/world.h +++ b/world.h @@ -22,7 +22,7 @@ void World_addHuman(World *world, Human *hum); void World_removeHuman(World *world, Human *hum); void World_update(World *world, float dt); void World_render(World *world, Renderer *renderer, Camera *camera, Time* time); -void World_renderShadows(World* world, Renderer* renderer, Mat4 lightView, Mat4 lightProjection); +void World_renderShadows(World* world, Renderer* renderer, Time* time); void World_destroy(World *world);