Nimbin[12]?Games / strichmaennchen / commits / 7b0505d

strichmaennchen git · main

Entropie festival game; first steps, second try

opengl glsl c 3d game entropie · first commit 2025-09-16 · last commit 2025-10-05 (1 year ago) · synced 1 hour ago

C 95% GLSL 4.7%
git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gz

Starting light reflection

Christian Immanuel · 2025-09-18 19:48 · 7b0505d732d693f9ccec44aa1f4e5bcf0281bea1

 Makefile                 |   2 +-
 camera.c                 |   8 +++
 camera.h                 |   1 +
 game.c                   |  20 ++++----
 main.c                   |   9 +++-
 mesh.c                   |  11 ++--
 renderer.h               |   2 +
 shaders/.basic.frag.swp  | Bin 0 -> 12288 bytes
 shaders/.skybox.frag.swp | Bin 0 -> 12288 bytes
 shaders/.skybox.vert.swp | Bin 0 -> 12288 bytes
 shaders/basic.frag       |  28 ++++++++---
 shaders/skybox.frag      |  29 +++++++++++
 shaders/skybox.vert      |  15 ++++++
 skybox.c                 | 128 +++++++++++++++++++++++++++++++++++++++++++++++
 skybox.h                 |  18 +++++++
 15 files changed, 245 insertions(+), 26 deletions(-)

diff --git a/Makefile b/Makefile
index dc7f594..0a5f6e1 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CFLAGS = -Wall -Wextra -O2
 LDFLAGS = -lSDL3 -lSDL3_image -lm -lGLEW -lGL
 
 TARGET = game
-SRC = main.c object.c arm.c camera.c entity.c game.c input.c linalg.c math.c renderer.c mesh.c platform.c human.c world.c glfuncs.c shader.c time.c
+SRC = main.c object.c arm.c camera.c entity.c game.c input.c linalg.c math.c renderer.c mesh.c platform.c human.c world.c glfuncs.c shader.c time.c skybox.c
 
 all: $(TARGET)
 
diff --git a/camera.c b/camera.c
index acafd1a..10630db 100644
--- a/camera.c
+++ b/camera.c
@@ -84,3 +84,11 @@ void Camera_followPlayer(Camera *cam, Human *player) {
     cam->position = vec3_add(player->object->position, (Vec3){0, 1.6f, 0}); // eye height
     Camera_update(cam);
 }
+
+
+
+Mat4 Camera_getViewMatrix(Camera* cam) {
+    // Simple LookAt: pos, target=pos+front, up
+    Vec3 target = vec3_add(cam->position, cam->front);
+    return mat4_lookAt(cam->position, target, cam->up);
+}
diff --git a/camera.h b/camera.h
index 3f0d5ab..933f585 100644
--- a/camera.h
+++ b/camera.h
@@ -26,5 +26,6 @@ void Camera_update(Camera* cam);
 void Camera_rotate(Camera* cam, float yaw_delta, float pitch_delta);
 void Camera_move(Camera* cam, Userinput* input, float dt);
 void Camera_followPlayer(Camera *cam, Human *player);
+Mat4 Camera_getViewMatrix(Camera* cam);
 
 #endif
diff --git a/game.c b/game.c
index a7604e4..0179274 100644
--- a/game.c
+++ b/game.c
@@ -9,24 +9,21 @@ bool Game_init(Game* game, Renderer* renderer, Camera* camera) {
 
     Human  *player     = Human_create(vec3(1,0,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), true, 60);
     player->isPlayer   = true;
-    //Human  *human      = Human_create(vec3(1,2,0), CUBE_VERTICES_STANDARD, false);
     Human  *human      = Human_create(vec3(0,4,0), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60);
     Human  *human_1    = Human_create(vec3(-1,2,1), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60);
 
+    Object* ground     = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(10, 1, 10), 0);
+    Object* tree       = Object_create(OBJ_TREE,   CUBE_VERTICES_WARM,   vec3(3,0,2), vec3(1, 1, 1), 0);
+    Object* cube       = Object_create(OBJ_CUBE,   CUBE_VERTICES_WARM,   vec3(-0.2f,0,1.5f), vec3(1,1,1), 20);
+
     // for (int j = 0; j<10; j++) for (int i = 0; i<10; i++) World_addHuman(&game->world, Human_create(vec3(i,5,j), CUBE_VERTICES_MODERN, vec3(0.5f,1.6f,0.25f), false, 60));
 
     /*
     Object* triangle   = Object_create(OBJ_TRIANGLE, vec3(2.0f, 4.0f, 1.0f), vec3(0.5f,0.5f,0.0f), vec3(0,0,0));
     Object* axes       = Object_create(OBJ_AXES,     vec3(0,0,0), vec3(0,0,0), vec3(0,0,0));
     Object* pyramid    = Object_create(OBJ_PYRAMID,  vec3(0,0,0), vec3(2.0f,2.0f,2.0f), vec3(1,0,1));
+    Object* ground_1   = Object_create(OBJ_GROUND, CUBE_VERTICES_WARM, vec3(0,4,0), vec3(10, 1, 10), 0);
     */
-    Object* cube       = Object_create(OBJ_CUBE,   CUBE_VERTICES_WARM,   vec3(-0.2f,0,1.5f), vec3(1,1,1), 20);
-
-    Object* ground     = Object_create(OBJ_GROUND, CUBE_VERTICES_GROUND, vec3(0,0,0), vec3(10, 1, 10), 0);
-    Object* ground_1   = Object_create(OBJ_GROUND, CUBE_VERTICES_WARM,   vec3(1,0,0), vec3(1, 1, 1), 0);
-    Object* ground_2   = Object_create(OBJ_GROUND, CUBE_VERTICES_MODERN, vec3(2,1,0), vec3(1, 1, 1), 0);
-    Object* ground_3   = Object_create(OBJ_GROUND, CUBE_VERTICES_WARM,   vec3(3,2,0), vec3(1, 1, 1), 0);
-    Object* tree       = Object_create(OBJ_TREE,   CUBE_VERTICES_WARM,   vec3(3,0,2), vec3(1, 1, 1), 0);
 
     game->player = player;
     if ( !human || !ground)
@@ -40,19 +37,22 @@ bool Game_init(Game* game, Renderer* renderer, Camera* camera) {
     */
 
 
-    World_addObject(&game->world, ground);
     World_addHuman(&game->world, player);
     World_addHuman(&game->world, human);
     World_addHuman(&game->world, human_1);
+
+    World_addObject(&game->world, ground);
+    World_addObject(&game->world, tree);
     World_addObject(&game->world, cube);
+
     //World_addObject(&game->world, axes);
     /*
+    World_addObject(&game->world, ground_1);
     World_addObject(&game->world, cube);
     World_addObject(&game->world, ground_1);
     World_addObject(&game->world, ground_2);
     World_addObject(&game->world, ground_3);
     */
-    World_addObject(&game->world, tree);
 
     return true;
 }
diff --git a/main.c b/main.c
index 4031318..77443c8 100644
--- a/main.c
+++ b/main.c
@@ -11,6 +11,7 @@
 #include "game.h"
 #include "platform.h"
 #include "time.h"
+#include "skybox.h"
 
 #include "glfuncs.h"
 
@@ -45,10 +46,11 @@ int main(void) {
     Uint64 prev_ticks = SDL_GetTicks();
 
     Time worldTime;
-    Time_init(&worldTime, 60.0f);
+    Time_init(&worldTime, 3600.0f);
 
+    Skybox skybox;
+    Skybox_init(&skybox, "shaders/skybox.vert", "shaders/skybox.frag");
 
-    //Object* ground = Object_create(OBJ_CUBE,   vec3(0,-0.5f,0), vec3(0,0,0));
 
     while (userinput.running) {
         float dt = Time_delta(&prev_ticks);
@@ -64,6 +66,7 @@ int main(void) {
 
         Input_reset(&userinput);
         Input_process(&userinput);
+        printf("%.2f:%.2f\n", worldTime.hours, worldTime.minutes);
 
         Camera_rotate(&camera, userinput.yaw_delta, userinput.pitch_delta);
         //Camera_move(&camera, &userinput, dt);
@@ -73,6 +76,7 @@ int main(void) {
 
         Camera_followPlayer(&camera, game.player);
         Game_render(&game, &renderer, &camera, &worldTime);
+        Skybox_draw(&skybox, &renderer, &camera, &worldTime);
 
         //Renderer_testCube(&renderer, &camera);
         //Object_render(ground, &renderer, &camera);
@@ -83,6 +87,7 @@ int main(void) {
 
 
 
+    Skybox_destroy(&skybox);
     Renderer_shutdown(&renderer);
     Platform_shutdown(&renderer);
     return 0;
diff --git a/mesh.c b/mesh.c
index 4c0e4a4..b2c195b 100644
--- a/mesh.c
+++ b/mesh.c
@@ -36,14 +36,15 @@ void Mesh_draw(Mesh mesh, struct Renderer *renderer, TransformMatrices* trans_ma
     glUniformMatrix4fv(locProj,  1, GL_FALSE, (float*)&(trans_matrices->projection));
 
     // Set light & camera positions using Time
-    GLint locLight = glGetUniformLocation(renderer->shaderProgram, "lightPos");
-    GLint locLightColor = glGetUniformLocation(renderer->shaderProgram, "lightColor"); // if you add this uniform
-    GLint locViewPos = glGetUniformLocation(renderer->shaderProgram, "viewPos");
+    GLint locLightDir   = glGetUniformLocation(renderer->shaderProgram, "lightDirection");
+    GLint locLightColor = glGetUniformLocation(renderer->shaderProgram, "lightColor");
+    GLint locViewPos    = glGetUniformLocation(renderer->shaderProgram, "viewPos");
 
-    glUniform3f(locLight, time->sunDirection.x, time->sunDirection.y, time->sunDirection.z);
-    glUniform3f(locLightColor, time->sunColor.x, time->sunColor.y, time->sunColor.z); // optional
+    glUniform3f(locLightDir, time->sunDirection.x, time->sunDirection.y, time->sunDirection.z);
+    glUniform3f(locLightColor, time->sunColor.x, time->sunColor.y, time->sunColor.z);
     glUniform3f(locViewPos, viewPos.x, viewPos.y, viewPos.z);
 
+
     // Draw
     glBindVertexArray(mesh.vao);
     if (mesh.hasIndices)
diff --git a/renderer.h b/renderer.h
index ff63c77..2380528 100644
--- a/renderer.h
+++ b/renderer.h
@@ -19,6 +19,8 @@ typedef struct Renderer {
 
     const char *vertexShaderSrc;
     const char *fragmentShaderSrc;
+
+    Mat4 projection;
 } Renderer;
 
 bool Renderer_create(Renderer* renderer, int width, int height);
diff --git a/shaders/.basic.frag.swp b/shaders/.basic.frag.swp
new file mode 100644
index 0000000..3922f09
Binary files /dev/null and b/shaders/.basic.frag.swp differ
diff --git a/shaders/.skybox.frag.swp b/shaders/.skybox.frag.swp
new file mode 100644
index 0000000..37bb6c9
Binary files /dev/null and b/shaders/.skybox.frag.swp differ
diff --git a/shaders/.skybox.vert.swp b/shaders/.skybox.vert.swp
new file mode 100644
index 0000000..c2604c1
Binary files /dev/null and b/shaders/.skybox.vert.swp differ
diff --git a/shaders/basic.frag b/shaders/basic.frag
index 4de7026..86431b7 100644
--- a/shaders/basic.frag
+++ b/shaders/basic.frag
@@ -6,23 +6,35 @@ in vec3 VertexColor;
 
 out vec4 FragColor;
 
-uniform vec3 lightPos;
 uniform vec3 viewPos;
+uniform vec3 lightDirection;   // <-- must be vec3
+uniform vec3 lightColor;
 uniform vec3 ambientColor;
 
 void main()
-{
-    vec3 ambient = VertexColor * ambientColor;
+{ 
+
 
     vec3 norm = normalize(Normal);
-    vec3 lightDir = normalize(lightPos - FragPos);
+    vec3 lightDir = normalize(lightDirection);
+
+    // Hemispheric ambient light
+    vec3 skyColor = vec3(0.4, 0.5, 0.7);    // bluish sky light
+    vec3 groundColor = vec3(0.2, 0.15, 0.1); // brownish ground bounce
+    float hemiFactor = norm.y * 0.5 + 0.5;   // blend by surface "upness"
+    vec3 hemiAmbient = mix(groundColor, skyColor, hemiFactor);
+    // Final ambient term, scaled by time->ambientColor
+    vec3 ambient = hemiAmbient * ambientColor + vec3(0.05, 0.05, 0.05);
+
+    // diffuse
     float diff = max(dot(norm, lightDir), 0.0);
-    vec3 diffuse = VertexColor * diff;
+    vec3 diffuse = diff * lightColor * VertexColor;
 
+    // specular
     vec3 viewDir = normalize(viewPos - FragPos);
-    vec3 halfwayDir = normalize(lightDir + viewDir);
-    float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);
-    vec3 specular = vec3(1.0) * spec * 0.5;
+    vec3 reflectDir = reflect(-lightDir, norm);
+    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
+    vec3 specular = spec * lightColor;
 
     vec3 result = ambient + diffuse + specular;
     FragColor = vec4(result, 1.0);
diff --git a/shaders/skybox.frag b/shaders/skybox.frag
new file mode 100644
index 0000000..78ce7c2
--- /dev/null
+++ b/shaders/skybox.frag
@@ -0,0 +1,29 @@
+#version 330 core
+
+in vec3 TexCoords;   // from skybox.vert (normalized direction)
+out vec4 FragColor;
+
+uniform vec3 sunDirection;  // from Time
+uniform vec3 sunColor;      // from Time
+uniform vec3 ambientColor;  // from Time
+
+void main()
+{
+    vec3 dir = normalize(TexCoords);
+
+    // Cosine of angle between current dir and sunDirection
+    float sunDot = dot(dir, normalize(sunDirection));
+
+    // Adjust sharpness of sun disc
+    float sunSize = 0.999;       // closer to 1.0 = smaller sun
+    float sunIntensity = smoothstep(sunSize, 1.0, sunDot);
+
+    // Sky base gradient (simple: horizon darker, zenith lighter)
+    float t = max(dir.y * 0.5 + 0.5, 0.0);
+    vec3 skyColor = mix(ambientColor * 0.5, ambientColor, t);
+
+    // Add sun glow
+    skyColor += sunColor * sunIntensity;
+
+    FragColor = vec4(skyColor, 1.0);
+}
diff --git a/shaders/skybox.vert b/shaders/skybox.vert
new file mode 100644
index 0000000..af29643
--- /dev/null
+++ b/shaders/skybox.vert
@@ -0,0 +1,15 @@
+#version 330 core
+
+layout (location = 0) in vec3 aPos;
+
+out vec3 TexCoords;
+
+uniform mat4 view;
+uniform mat4 projection;
+
+void main()
+{
+    TexCoords = aPos; // direction vector from cube
+    vec4 pos = projection * view * vec4(aPos, 1.0);
+    gl_Position = pos.xyww; // trick to keep cube depth = 1.0
+}
diff --git a/skybox.c b/skybox.c
new file mode 100644
index 0000000..54d2700
--- /dev/null
+++ b/skybox.c
@@ -0,0 +1,128 @@
+#include "skybox.h"
+#include "glfuncs.h"
+#include "shader.h"
+#include "camera.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+// Cube vertices for a unit cube
+static float cubeVertices[] = {
+    -1.0f,  1.0f, -1.0f,
+    -1.0f, -1.0f, -1.0f,
+     1.0f, -1.0f, -1.0f,
+     1.0f, -1.0f, -1.0f,
+     1.0f,  1.0f, -1.0f,
+    -1.0f,  1.0f, -1.0f,
+
+    -1.0f, -1.0f,  1.0f,
+    -1.0f, -1.0f, -1.0f,
+    -1.0f,  1.0f, -1.0f,
+    -1.0f,  1.0f, -1.0f,
+    -1.0f,  1.0f,  1.0f,
+    -1.0f, -1.0f,  1.0f,
+
+     1.0f, -1.0f, -1.0f,
+     1.0f, -1.0f,  1.0f,
+     1.0f,  1.0f,  1.0f,
+     1.0f,  1.0f,  1.0f,
+     1.0f,  1.0f, -1.0f,
+     1.0f, -1.0f, -1.0f,
+
+    -1.0f, -1.0f,  1.0f,
+    -1.0f,  1.0f,  1.0f,
+     1.0f,  1.0f,  1.0f,
+     1.0f,  1.0f,  1.0f,
+     1.0f, -1.0f,  1.0f,
+    -1.0f, -1.0f,  1.0f,
+
+    -1.0f,  1.0f, -1.0f,
+     1.0f,  1.0f, -1.0f,
+     1.0f,  1.0f,  1.0f,
+     1.0f,  1.0f,  1.0f,
+    -1.0f,  1.0f,  1.0f,
+    -1.0f,  1.0f, -1.0f,
+
+    -1.0f, -1.0f, -1.0f,
+    -1.0f, -1.0f,  1.0f,
+     1.0f, -1.0f, -1.0f,
+     1.0f, -1.0f, -1.0f,
+    -1.0f, -1.0f,  1.0f,
+     1.0f, -1.0f,  1.0f
+};
+
+extern GLuint createShaderProgram(const char* vertSrc, const char* fragSrc);
+
+bool Skybox_init(Skybox* skybox, const char* vertPath, const char* fragPath) {
+    // Load shaders
+    char* vertSrc = readFile(vertPath);
+    char* fragSrc = readFile(fragPath);
+    if (!vertSrc || !fragSrc) {
+        fprintf(stderr, "Failed to load skybox shaders\n");
+        return false;
+    }
+    skybox->shaderProgram = createShaderProgram(vertSrc, fragSrc);
+    free(vertSrc);
+    free(fragSrc);
+
+    // Setup cube VAO/VBO
+    glGenVertexArrays(1, &skybox->vao);
+    glGenBuffers(1, &skybox->vbo);
+
+    glBindVertexArray(skybox->vao);
+    glBindBuffer(GL_ARRAY_BUFFER, skybox->vbo);
+    glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
+
+    glEnableVertexAttribArray(0);
+    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
+
+    glBindVertexArray(0);
+    return true;
+}
+
+void Skybox_draw(Skybox* skybox, Renderer* renderer, Camera* camera, Time* time) {
+    glDepthFunc(GL_LEQUAL);
+    glUseProgram(skybox->shaderProgram);
+    // Send sun/ambient info to the skybox shader
+    GLint locSunDir = glGetUniformLocation(skybox->shaderProgram, "sunDirection");
+    GLint locSunCol = glGetUniformLocation(skybox->shaderProgram, "sunColor");
+    GLint locAmbCol = glGetUniformLocation(skybox->shaderProgram, "ambientColor");
+
+    glUniform3f(locSunDir, time->sunDirection.x, time->sunDirection.y, time->sunDirection.z);
+    glUniform3f(locSunCol, time->sunColor.x, time->sunColor.y, time->sunColor.z);
+    glUniform3f(locAmbCol, time->ambientColor.x, time->ambientColor.y, time->ambientColor.z);
+
+
+
+    TransformMatrices m;
+    m.projection = mat4_perspective(camera->fov,
+                                    (float)renderer->width / renderer->height,
+                                    0.1f, 100.0f);
+
+    m.view = mat4_lookAt(camera->position,
+                         vec3_add(camera->position, camera->front),
+                         camera->up);
+    // remove translation
+    m.view.m[12] = m.view.m[13] = m.view.m[14] = 0.0f;
+
+    m.model = mat4_identity();
+
+    GLint locView = glGetUniformLocation(skybox->shaderProgram, "view");
+    GLint locProj = glGetUniformLocation(skybox->shaderProgram, "projection");
+
+    glUniformMatrix4fv(locView, 1, GL_FALSE, (float*)&m.view);
+    glUniformMatrix4fv(locProj, 1, GL_FALSE, (float*)&m.projection);
+
+    glBindVertexArray(skybox->vao);
+    glDrawArrays(GL_TRIANGLES, 0, 36);
+    glBindVertexArray(0);
+
+    glDepthFunc(GL_LESS);
+}
+
+void Skybox_destroy(Skybox* skybox) {
+    glDeleteVertexArrays(1, &skybox->vao);
+    glDeleteBuffers(1, &skybox->vbo);
+    glDeleteProgram(skybox->shaderProgram);
+}
diff --git a/skybox.h b/skybox.h
new file mode 100644
index 0000000..13799aa
--- /dev/null
+++ b/skybox.h
@@ -0,0 +1,18 @@
+#ifndef SKYBOX_H
+#define SKYBOX_H
+
+#include "renderer.h"
+#include "camera.h"
+#include "time.h"
+
+typedef struct {
+    GLuint vao;
+    GLuint vbo;
+    GLuint shaderProgram;
+} Skybox;
+
+bool Skybox_init(Skybox* skybox, const char* vertPath, const char* fragPath);
+void Skybox_draw(Skybox* skybox, Renderer* renderer, Camera* camera, Time* time);
+void Skybox_destroy(Skybox* skybox);
+
+#endif