Nimbin2git.christianimmanuel.de / Games / strichmaennchen / skybox.c

strichmaennchen git · main

git clone https://git.christianimmanuel.de/games/strichmaennchen.gitwget https://git.christianimmanuel.de/games/strichmaennchen/archive/strichmaennchen.tar.gz
skybox.c 3.7 KB · 128 lines raw
#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);
}