Have a working skybox now.

This commit is contained in:
Zed A. Shaw 2026-09-03 16:57:51 -04:00
parent 87c513b9f0
commit 4ee5058e82
13 changed files with 158 additions and 2 deletions

View file

@ -1,6 +1,7 @@
#include "scene.hpp"
#include "dbc.hpp"
#include <math.h>
#include "skybox.hpp"
void Scene::update() {
float currentFrame = glfwGetTime();
@ -51,6 +52,8 @@ void Scene::render(GLFWwindow* window) {
draw_thing(shader, thing);
}
render_skybox(projection);
framebuffer.commit();
framebuffer.draw(screen);
@ -73,3 +76,58 @@ void Scene::spawn(const std::string& name, components::Position& position, compo
dbc::log($F("No model named: {}", name));
}
}
void Scene::init_skybox() {
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
load_skybox_textures();
}
void Scene::load_skybox_textures() {
int width = 0;
int height = 0;
int nrChannels = 0;
size_t face_i = 0;
glGenTextures(1, &skybox_texture_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture_id);
for(const auto& face : skybox_faces) {
unsigned char *data = stbi_load(face.c_str(), &width, &height, &nrChannels, 0);
dbc::check(data != nullptr, $F("Failed to load skybox face {}", face));
dbc::check(nrChannels == 3, $F("Skybox face {} must be RGB but you have {} channels", face, nrChannels));
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face_i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
face_i++;
stbi_image_free(data);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
void Scene::render_skybox(const glm::mat4& projection) {
glDepthFunc(GL_LEQUAL);
skybox_shader.use();
glm::mat4 view = glm::mat4(glm::mat3(camera.look_at()));
skybox_shader.setMat4("view", view);
skybox_shader.setMat4("projection", projection);
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture_id);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS);
}