Loading...
Searching...
No Matches
Skybox.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 14/06/2025.
3//
4
5#include "Skybox.h"
6#include <GL/glew.h>
7#include <iostream>
8
9#include <stb_image.h>
10
11namespace hellfire {
13 if (cubemap_texture_ != 0) glDeleteTextures(1, &cubemap_texture_);
14 }
15
16 void Skybox::set_cubemap_faces(const std::array<std::string, 6> &faces) {
17 if (cubemap_texture_ != 0) {
18 glDeleteTextures(1, &cubemap_texture_);
19 }
20 cubemap_texture_ = load_cubemap(faces);
21 }
22
23 uint32_t Skybox::load_cubemap(const std::array<std::string, 6> &faces) {
24 uint32_t textureID;
25 glGenTextures(1, &textureID);
26 glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
27
28 stbi_set_flip_vertically_on_load(false);
29 int width, height, nrChannels;
30 for (uint32_t i = 0; i < faces.size(); i++) {
31 unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
32 if (data) {
33 GLenum format = (nrChannels == 3) ? GL_RGB : GL_RGBA;
34 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
35 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
36 stbi_image_free(data);
37 } else {
38 std::cout << "cubemaps texture failed to load at path: " << faces[i] << std::endl;
39 stbi_image_free(data);
40 }
41 }
42
43 stbi_set_flip_vertically_on_load(false);
44
45 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
46 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
47 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
48 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
49 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
50
51 return textureID;
52 }
53}
uint32_t load_cubemap(const std::array< std::string, 6 > &faces)
Definition Skybox.cpp:23
void set_cubemap_faces(const std::array< std::string, 6 > &faces)
Definition Skybox.cpp:16