From a9a7422a1936ed360bf06339bfaa1fb5a0e24ad6 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Tue, 15 Oct 2024 18:42:55 +0200 Subject: [PATCH] skybox loading code --- assets/shaders/skybox.fs | 2 +- assets/shaders/skybox.vs | 2 +- src/rendering/skybox.cpp | 47 ++++++++++++++++++++++++++++++++++++++++ src/rendering/skybox.h | 15 +++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/rendering/skybox.cpp create mode 100644 src/rendering/skybox.h diff --git a/assets/shaders/skybox.fs b/assets/shaders/skybox.fs index f2a6016..d6f55b1 100644 --- a/assets/shaders/skybox.fs +++ b/assets/shaders/skybox.fs @@ -11,5 +11,5 @@ out vec4 fColor; // Main void main() { - fColor = vec4(vTexCoords, 1.0, 1.0); + fColor = texture(uTexture, vTexCoords); } diff --git a/assets/shaders/skybox.vs b/assets/shaders/skybox.vs index b140237..0cc797e 100644 --- a/assets/shaders/skybox.vs +++ b/assets/shaders/skybox.vs @@ -12,7 +12,7 @@ uniform mat4 projection; void main() { - gl_Position = projection * vec4(vec3(view * vec4(aPos, 0.0)), 1.0); + gl_Position = projection * view * vec4(aPos, 1.0); vPos = vec3(vec4(aPos, 1.0)); vNormal = aNormal; vTexCoords = aTexCoords; diff --git a/src/rendering/skybox.cpp b/src/rendering/skybox.cpp new file mode 100644 index 0000000..f7d3caa --- /dev/null +++ b/src/rendering/skybox.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "skybox.h" + +Skybox::Skybox(std::vector faces, unsigned int format) { + glGenTextures(1, &this->ID); + glBindTexture(GL_TEXTURE_CUBE_MAP, this->ID); + + // Wrapping + 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); + + // Interpolation + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // stbi_set_flip_vertically_on_load(true); + for (unsigned int i = 0; i< faces.size(); i++) { + int width, height, nrChannels; + unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, + &nrChannels, 0); + + if (!data) { + printf("Failed to load texture '%s'\n", faces[i].c_str()); + abort(); + } + + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width, height, 0, format, + GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + + + stbi_image_free(data); + } +} + +Skybox::~Skybox() { + glDeleteTextures(1, &this->ID); +} + +void Skybox::activate(unsigned int textureIdx) { + glActiveTexture(GL_TEXTURE0 + textureIdx); + glBindTexture(GL_TEXTURE_2D, this->ID); +} diff --git a/src/rendering/skybox.h b/src/rendering/skybox.h new file mode 100644 index 0000000..5934cb7 --- /dev/null +++ b/src/rendering/skybox.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#define GL_RGB 0x1907 + +class Skybox { +private: + unsigned int ID; +public: + Skybox(std::vector, unsigned format = GL_RGB); + ~Skybox(); + + void activate(unsigned int textureIdx); +};