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

@ -8,6 +8,18 @@
"vertex_path": "shaders/21-screen.vert.glsl",
"frag_path": "shaders/21-screen.frag.glsl"
},
"skybox_shader": {
"vertex_path": "shaders/24-skybox.vert.glsl",
"frag_path": "shaders/24-skybox.frag.glsl"
},
"skybox_faces": [
"../assets/skybox/right.jpg",
"../assets/skybox/left.jpg",
"../assets/skybox/top.jpg",
"../assets/skybox/bottom.jpg",
"../assets/skybox/front.jpg",
"../assets/skybox/back.jpg"
],
"materials": {
"default": {
"ambient": [0.5, 0.5, 0.5],

View file

@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, TexCoords);
}

View file

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
TexCoords = aPos;
vec4 pos = projection * view * vec4(aPos, 1.0);
gl_Position = pos.xyww;
}

View file

@ -103,6 +103,8 @@ namespace components {
struct Scene {
components::Shader shader;
components::Shader screen_shader;
components::Shader skybox_shader;
std::vector<std::string> skybox_faces;
std::unordered_map<std::string, Material> materials;
std::unordered_map<std::string, Model> models;
std::vector<Thing> things;
@ -110,5 +112,5 @@ namespace components {
Lighting light;
};
ENROLL_COMPONENT(Scene, shader, screen_shader, materials, models, things, camera, light);
ENROLL_COMPONENT(Scene, shader, screen_shader, skybox_shader, skybox_faces, materials, models, things, camera, light);
}

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);
}

View file

@ -27,6 +27,8 @@ struct Thing {
struct Scene {
Shader shader;
Shader skybox_shader;
std::vector<std::string> skybox_faces;
Screen screen;
FrameBuffer framebuffer;
@ -35,12 +37,16 @@ struct Scene {
components::Lighting light;
std::unordered_map<std::string, Model> models;
std::vector<Thing> things;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
unsigned int skyboxVAO = 0;
unsigned int skyboxVBO = 0;
unsigned int skybox_texture_id = 0;
Scene(components::Scene& config):
shader{config.shader.vertex_path, config.shader.frag_path},
skybox_shader{config.skybox_shader.vertex_path, config.skybox_shader.frag_path},
skybox_faces{config.skybox_faces},
screen{.shader={config.screen_shader.vertex_path, config.screen_shader.frag_path}},
materials{config.materials},
camera{
@ -70,8 +76,12 @@ struct Scene {
shader.setInt("texture1", 0);
shader.apply_lighting(light);
skybox_shader.use();
skybox_shader.setInt("skybox", 0);
framebuffer.init();
screen.init();
init_skybox();
}
void update();
@ -79,4 +89,7 @@ struct Scene {
void render(GLFWwindow* window);
void cleanup();
void spawn(const std::string& name, components::Position& position, components::Rotation& rotation);
void init_skybox();
void load_skybox_textures();
void render_skybox(const glm::mat4& projection);
};

View file

@ -0,0 +1,46 @@
#pragma once
float skyboxVertices[] = {
// positions
-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
};

BIN
assets/skybox/back.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 723 KiB

BIN
assets/skybox/bottom.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

BIN
assets/skybox/front.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

BIN
assets/skybox/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 KiB

BIN
assets/skybox/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 KiB

BIN
assets/skybox/top.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB