CSEngine
Loading...
Searching...
No Matches
STexture.cpp
1//
2// Created by ounols on 19. 4. 18.
3//
4#define STB_IMAGE_IMPLEMENTATION
5
6#include "../../OGLDef.h"
7#include "STexture.h"
8
9#include "../Loader/STB/stb_image.h"
10#include "../Loader/XML/XML.h"
11#include "../../MacroDef.h"
12#include "../AssetsDef.h"
13
14using namespace CSE;
15
16unsigned int STexture::m_emptyTextureId = 0;
17
18RESOURCE_CONSTRUCTOR(STexture) {
19 SetUndestroyable(true);
20 if(m_emptyTextureId == 0) {
21 LoadEmpty();
22 }
23}
24
25CSE::STexture::STexture(STexture::Type type) : SResource("STexture") {
26 SetUndestroyable(true);
27 SetType(type);
28}
29
30STexture::~STexture() = default;
31
32bool STexture::LoadFile(const char* path) {
33
34 if (m_texId != 0) return false;
35
36// m_name = path;
37 unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, 0);
38
39 return Load(data);
40}
41
42bool STexture::LoadFromMemory(const unsigned char* rawData, int length) {
43 unsigned char* data;
44 if(m_type == TEX_3D) {
45 int w, h;
46 data = stbi_load_from_memory(rawData, length, &w, &h, &m_channels, 0);
47 }
48 else
49 data = stbi_load_from_memory(rawData, length, &m_width, &m_height, &m_channels, 0);
50 return Load(data);
51}
52
53bool STexture::Load(unsigned char* data) {
54
55 if (m_texId != 0) {
56 stbi_image_free(data);
57 return false;
58 }
59
60 glGenTextures(1, &m_texId);
61 glBindTexture(m_targetGL, m_texId);
62
63 m_internalFormat = GL_RGB;
64 switch (m_channels) {
65 case 1:
66 m_internalFormat = GL_R8;
67 break;
68 case 2:
69 m_internalFormat = GL_RG;
70 break;
71 case 4:
72 m_internalFormat = GL_RGBA;
73 break;
74 }
75
76 switch (m_type) {
77 case TEX_2D:
78 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_internalFormat, m_glType, data);
79 break;
80 case TEX_CUBE:
81 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_internalFormat, m_glType, data);
82 break;
83 case TEX_3D:
84 glTexImage3D(m_targetGL, 0, m_internalFormat, m_width, m_height, m_depth, 0, m_internalFormat, m_glType, data);
85 break;
86 }
87
88 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_S, GL_REPEAT);
89 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_T, GL_REPEAT);
90 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_R, GL_REPEAT);
91 glTexParameteri(m_targetGL, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
92 glTexParameteri(m_targetGL, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
93
94 stbi_image_free(data);
95 return true;
96}
97
98void STexture::LoadEmpty() {
99 if (m_emptyTextureId != 0) return;
100
101 glGenTextures(1, &m_emptyTextureId);
102 glBindTexture(GL_TEXTURE_2D, m_emptyTextureId);
103
104 GLubyte data[] = { 255, 0, 255 };
105
106 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
107}
108
109bool STexture::ReloadFile(const char* path) {
110 Release();
111 return LoadFile(path);
112}
113
114bool STexture::Reload(unsigned char* data) {
115 Release();
116 return Load(data);
117}
118
119void STexture::Release() {
120 glDeleteTextures(1, &m_texId);
121 m_texId = 0;
122 m_height = 0;
123 m_width = 0;
124}
125
126void STexture::Exterminate() {
127 Release();
128}
129
130void STexture::Bind(GLint location, int layout) {
131 if (m_texId == 0) {
132 BindEmpty(location, layout, m_type);
133 return;
134 }
135 glUniform1i(location, layout);
136
137 glActiveTexture(GL_TEXTURE0 + layout);
138 glBindTexture(m_targetGL, m_texId);
139}
140
141bool STexture::InitTexture(int width, int height, int channel, int internalFormat, int glType) {
142 if (m_texId != 0) {
143 return false;
144 }
145
146 m_width = width;
147 m_height = height;
148 m_channels = channel;
149 m_internalFormat = internalFormat;
150 m_glType = glType;
151
152 glGenTextures(1, &m_texId);
153 glBindTexture(m_targetGL, m_texId);
154
155 switch (m_type) {
156 case TEX_CUBE:
157 for (GLuint i = 0; i < 6; ++i) {
158 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, m_internalFormat, m_width, m_height, 0, m_channels,
159 m_glType, nullptr);
160 }
161 break;
162
163 case TEX_2D:
164 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_channels, m_glType, nullptr);
165 break;
166
167 case TEX_3D:
168 glTexImage3D(m_targetGL, 0, m_internalFormat, m_width, m_height, m_depth, 0, m_channels, m_glType, nullptr);
169 break;
170 }
171
172 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
173 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
174 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
175 glTexParameteri(m_targetGL, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176 glTexParameteri(m_targetGL, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177
178 return true;
179}
180
181bool STexture::InitTextureMipmap(int width, int height, int channel, int internalFormat, int glType) {
182 auto result = InitTexture(width, height, channel, internalFormat, glType);
183 if(!result) return false;
184 glTexParameteri(m_targetGL, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
185 return result;
186}
187
188void STexture::SetParameteri(int targetName, int value) const {
189 glBindTexture(m_targetGL, m_texId);
190 glTexParameteri(m_targetGL, targetName, value);
191}
192
193void STexture::SetParameterfv(int targetName, float* value) const {
194 glBindTexture(m_targetGL, m_texId);
195 glTexParameterfv(m_targetGL, targetName, value);
196}
197
198void STexture::Init(const AssetMgr::AssetReference* asset) {
199 const std::string img_str = CSE::AssetMgr::LoadAssetFile(asset->name_path);
200
201 std::string hashRaw = AssetMgr::LoadAssetFile(asset->name_path + ".meta");
202 if(!hashRaw.empty()) {
203 const XNode* root = XFILE().loadBuffer(hashRaw);
204 const auto& hashData = root->getNode("hash-data");
205 std::string hash = hashData.getAttribute("hash").value;
206 SetHash(hash);
207 const auto& hashChildren = hashData.children;
208
209 if(hashData.children.size() <= 0) {
210 hashRaw = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
211 "<CSEMETA version=\"1.0.0\">\n"
212 "<hash-data hash=\"" + m_hash + "\">\n"
213 + "<tex type=\"" + std::to_string(TEX_2D) + "\" depth=\"" + std::to_string(m_depth) + "\"></tex>" +
214 "\n</hash-data>\n</CSEMETA>";
215 if (!Settings::IsAssetsPacked())
216 SaveTxtFile(asset->name_path + ".meta", hashRaw);
217 }
218 else {
219 for(const auto& child : hashChildren) {
220 const auto& type_str = child.getAttribute("type").value;
221 if(!type_str.empty())
222 SetType(static_cast<Type>(std::stoi(type_str)));
223 if(m_type == TEX_3D) {
224 const auto& depth_str = child.getAttribute("depth").value;
225 const auto& width_str = child.getAttribute("width").value;
226 const auto& height_str = child.getAttribute("height").value;
227 m_depth = std::stoi(depth_str);
228 m_width = std::stoi(width_str);
229 m_height = std::stoi(height_str);
230 }
231 }
232 }
233 SAFE_DELETE(root);
234 }
235
236 LoadFromMemory(reinterpret_cast<const unsigned char*>(img_str.c_str()), img_str.length());
237}
238
239void STexture::GenerateMipmap() const {
240 glBindTexture(m_targetGL, m_texId);
241 glGenerateMipmap(m_targetGL);
242}
243
244STexture::Type STexture::GetType() const {
245 return m_type;
246}
247
248void STexture::SetType(STexture::Type type) {
249 m_type = type;
250 m_targetGL = GetTypeToTargetGL(type);
251}
252
253void STexture::BindEmpty(GLint location, int layout, STexture::Type type) {
254 glUniform1i(location, layout);
255
256 glActiveTexture(GL_TEXTURE0 + layout);
257 glBindTexture(GetTypeToTargetGL(type), m_emptyTextureId);
258}
259
260int STexture::GetTypeToTargetGL(STexture::Type type) {
261 switch (type) {
262 case TEX_2D:
263 return GL_TEXTURE_2D;
264 case TEX_CUBE:
265 return GL_TEXTURE_CUBE_MAP;
266 case TEX_3D:
267 return GL_TEXTURE_3D;
268 default:
269 return GL_TEXTURE_2D;
270 }
271}
272
273void STexture::SetValue(std::string name_str, VariableBinder::Arguments value) {
274}
275
276std::string STexture::PrintValue() const {
277 return {};
278}
279
280
281
282
void SetValue(std::string name_str, Arguments value) override
Definition STexture.cpp:273
Definition XML.h:77
Definition XML.h:43