skybox loading code

This commit is contained in:
maelstrom 2024-10-15 18:42:55 +02:00
parent c0e6669045
commit a9a7422a19
4 changed files with 64 additions and 2 deletions

View file

@ -11,5 +11,5 @@ out vec4 fColor;
// Main // Main
void main() { void main() {
fColor = vec4(vTexCoords, 1.0, 1.0); fColor = texture(uTexture, vTexCoords);
} }

View file

@ -12,7 +12,7 @@ uniform mat4 projection;
void main() 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)); vPos = vec3(vec4(aPos, 1.0));
vNormal = aNormal; vNormal = aNormal;
vTexCoords = aTexCoords; vTexCoords = aTexCoords;

47
src/rendering/skybox.cpp Normal file
View file

@ -0,0 +1,47 @@
#include <GL/glew.h>
#include <GL/gl.h>
#include <stb/stb_image.h>
#include "skybox.h"
Skybox::Skybox(std::vector<std::string> 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);
}

15
src/rendering/skybox.h Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <string>
#include <vector>
#define GL_RGB 0x1907
class Skybox {
private:
unsigned int ID;
public:
Skybox(std::vector<std::string>, unsigned format = GL_RGB);
~Skybox();
void activate(unsigned int textureIdx);
};