TerraForge3D  2.3.1
3D Terrain And Landscape Generator
TextureCubemap.cpp
1#include "TextureCubemap.h"
2
3#include <iostream>
4#include <cstdlib>
5#include <cstring>
6#include <stb/stb_image.h>
7#include <glad/glad.h>
8
9#include "Base/BaseMath.h"
10
11#include <iostream>
12
13static void Log(const char *str)
14{
15 std::cout << str << std::endl;
16}
17
18
19TextureCubemap::TextureCubemap()
20{
21 faces.push_back("");
22 faces.push_back("");
23 faces.push_back("");
24 faces.push_back("");
25 faces.push_back("");
26 faces.push_back("");
27 memset(facesData, 0, sizeof(facesData));
28 memset(facesSizes, 0, sizeof(facesSizes));
29 memset(textures, 0, sizeof(textures));
30}
31
32
33TextureCubemap::~TextureCubemap()
34{
35 for (int i = 0; i < 6; i++)
36 if (facesData[i])
37 {
38 delete[] facesData[i];
39 }
40
41 for (int i = 0; i < 6; i++)
42 if (textures[i])
43 {
44 delete textures[i];
45 }
46
47 glDeleteTextures(1, &rendereID);
48}
49
50void TextureCubemap::SetUpOnGPU()
51{
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);
60}
61
62bool TextureCubemap::LoadFaces(std::vector<std::string> paths)
63{
64 bool res = true;
65 int face = 0;
66
67 for (std::string path : paths)
68 {
69 res = LoadFace(path, face++);
70 }
71
72 return res;
73}
74
75bool TextureCubemap::LoadFace(std::string path, int face)
76{
77 bool res = true;
78 int width, height, nrChannels;
79 unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 3);
80 facesSizes[face] = IVec2(width, height);
81
82 if (data)
83 {
84 std::cout << "Loaded : " << path << "\n";
85
86 if(facesData[face])
87 {
88 stbi_image_free(facesData[face]);
89 }
90
91 facesData[face] = data;
92 res = true;
93 }
94
95 else
96 {
97 res = false;
98 std::cout << "Failed to load : " << path << std::endl;
99 stbi_image_free(data);
100 }
101
102 faces[face] = path;
103 return res;
104}
105
106void TextureCubemap::DeleteData()
107{
108 for (int i = 0; i < 6; i++)
109 if (facesData[i])
110 {
111 delete facesData[i];
112 facesData[i] = nullptr;
113 }
114}
115
116bool TextureCubemap::UploadFaceToGPU(int face)
117{
118 glBindTexture(GL_TEXTURE_CUBE_MAP, rendereID);
119
120 if (facesData[face])
121 {
122 if (textures[face])
123 {
124 delete textures[face];
125 textures[face] = nullptr;
126 }
127
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]);
132 return true;
133 }
134
135 return false;
136}
137
138void TextureCubemap::UploadDataToGPU()
139{
140 for (int i = 0; i < 6; i++)
141 {
142 UploadFaceToGPU(i);
143 }
144}
145
146void TextureCubemap::Bind(int slot)
147{
148 glActiveTexture(GL_TEXTURE0 + slot);
149 glBindTexture(GL_TEXTURE_CUBE_MAP, rendereID);
150}
Definition: BaseMath.h:4