1#include "TextureCubemap.h"
6#include <stb/stb_image.h>
9#include "Base/BaseMath.h"
13static void Log(
const char *str)
15 std::cout << str << std::endl;
19TextureCubemap::TextureCubemap()
27 memset(facesData, 0,
sizeof(facesData));
28 memset(facesSizes, 0,
sizeof(facesSizes));
29 memset(textures, 0,
sizeof(textures));
33TextureCubemap::~TextureCubemap()
35 for (
int i = 0; i < 6; i++)
38 delete[] facesData[i];
41 for (
int i = 0; i < 6; i++)
47 glDeleteTextures(1, &rendereID);
50void TextureCubemap::SetUpOnGPU()
52 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
53 glGenTextures(1, &rendereID);
54 glBindTexture(GL_TEXTURE_CUBE_MAP, rendereID);
55 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
56 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
57 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
58 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
59 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
62bool TextureCubemap::LoadFaces(std::vector<std::string> paths)
67 for (std::string path : paths)
69 res = LoadFace(path, face++);
75bool TextureCubemap::LoadFace(std::string path,
int face)
78 int width, height, nrChannels;
79 unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 3);
80 facesSizes[face] =
IVec2(width, height);
84 std::cout <<
"Loaded : " << path <<
"\n";
88 stbi_image_free(facesData[face]);
91 facesData[face] = data;
98 std::cout <<
"Failed to load : " << path << std::endl;
99 stbi_image_free(data);
106void TextureCubemap::DeleteData()
108 for (
int i = 0; i < 6; i++)
112 facesData[i] =
nullptr;
116bool TextureCubemap::UploadFaceToGPU(
int face)
118 glBindTexture(GL_TEXTURE_CUBE_MAP, rendereID);
124 delete textures[face];
125 textures[face] =
nullptr;
128 textures[face] =
new Texture2D(facesSizes[face].x, facesSizes[face].y);
129 textures[face]->Bind();
130 textures[face]->SetData(facesData[face], facesSizes[face].x * facesSizes[face].y * 3);
131 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGB, facesSizes[face].x, facesSizes[face].y, 0, GL_RGB, GL_UNSIGNED_BYTE, facesData[face]);
138void TextureCubemap::UploadDataToGPU()
140 for (
int i = 0; i < 6; i++)
146void TextureCubemap::Bind(
int slot)
148 glActiveTexture(GL_TEXTURE0 + slot);
149 glBindTexture(GL_TEXTURE_CUBE_MAP, rendereID);