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
11using namespace CSE;
12
13unsigned int STexture::m_emptyTextureId = 0;
14
15STexture::STexture() {
16 SetUndestroyable(true);
17 if(m_emptyTextureId == 0) {
18 LoadEmpty();
19 }
20}
21
22STexture::STexture(STexture::Type type) {
23 SetUndestroyable(true);
24 SetType(type);
25}
26
27STexture::~STexture() = default;
28
29bool STexture::LoadFile(const char* path) {
30
31 if (m_texId != 0) return false;
32
33// m_name = path;
34 unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, 0);
35
36 return Load(data);
37}
38
39bool STexture::LoadFromMemory(const unsigned char* rawData, int length) {
40 auto data = stbi_load_from_memory(rawData, length, &m_width, &m_height, &m_channels, 0);
41 return Load(data);
42}
43
44bool STexture::Load(unsigned char* data) {
45
46 if (m_texId != 0) {
47 stbi_image_free(data);
48 return false;
49 }
50
51 glGenTextures(1, &m_texId);
52 glBindTexture(m_targetGL, m_texId);
53
54 m_internalFormat = GL_RGB;
55 switch (m_channels) {
56 case 1:
57 m_internalFormat = GL_R8;
58 break;
59 case 2:
60 m_internalFormat = GL_RG;
61 break;
62 case 4:
63 m_internalFormat = GL_RGBA;
64 break;
65 }
66
67 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_internalFormat, m_glType, data);
68
69 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_S, GL_REPEAT);
70 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_T, GL_REPEAT);
71 glTexParameteri(m_targetGL, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
72 glTexParameteri(m_targetGL, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
73
74 stbi_image_free(data);
75 return true;
76}
77
78void STexture::LoadEmpty() {
79 if (m_emptyTextureId != 0) return;
80
81 glGenTextures(1, &m_emptyTextureId);
82 glBindTexture(GL_TEXTURE_2D, m_emptyTextureId);
83
84 GLubyte data[] = { 255, 0, 255 };
85
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
87}
88
89bool STexture::ReloadFile(const char* path) {
90 Release();
91 return LoadFile(path);
92}
93
94bool STexture::Reload(unsigned char* data) {
95 Release();
96 return Load(data);
97}
98
99void STexture::Release() {
100 glDeleteTextures(1, &m_texId);
101 m_texId = 0;
102 m_height = 0;
103 m_width = 0;
104}
105
106void STexture::Exterminate() {
107 Release();
108}
109
110void STexture::Bind(GLint location, int layout) {
111 if (m_texId == 0) {
112 BindEmpty(location, layout, m_type);
113 return;
114 }
115 glUniform1i(location, layout);
116
117 glActiveTexture(GL_TEXTURE0 + layout);
118 glBindTexture(m_targetGL, m_texId);
119}
120
121bool STexture::InitTexture(int width, int height, int channel, int internalFormat, int glType) {
122 if (m_texId != 0) {
123 return false;
124 }
125
126 m_width = width;
127 m_height = height;
128 m_channels = channel;
129 m_internalFormat = internalFormat;
130 m_glType = glType;
131
132 glGenTextures(1, &m_texId);
133 glBindTexture(m_targetGL, m_texId);
134
135 switch (m_type) {
136 case TEX_CUBE:
137 for (GLuint i = 0; i < 6; ++i) {
138 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, m_internalFormat, m_width, m_height, 0, m_channels,
139 m_glType, nullptr);
140 }
141 break;
142
143 case TEX_2D:
144 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_channels, m_glType, nullptr);
145 }
146
147 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
148 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
149 glTexParameteri(m_targetGL, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
150 glTexParameteri(m_targetGL, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
151 glTexParameteri(m_targetGL, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
152
153 return true;
154}
155
156bool STexture::InitTextureMipmap(int width, int height, int channel, int internalFormat, int glType) {
157 auto result = InitTexture(width, height, channel, internalFormat, glType);
158 if(!result) return false;
159 glTexParameteri(m_targetGL, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
160 return result;
161}
162
163void STexture::SetParameteri(int targetName, int value) const {
164 glBindTexture(GL_TEXTURE_2D, m_texId);
165 glTexParameteri(GL_TEXTURE_2D, targetName, value);
166}
167
168void STexture::SetParameterfv(int targetName, float* value) const {
169 glBindTexture(GL_TEXTURE_2D, m_texId);
170 glTexParameterfv(GL_TEXTURE_2D, targetName, value);
171}
172
173void STexture::Init(const AssetMgr::AssetReference* asset) {
174 const std::string img_str = CSE::AssetMgr::LoadAssetFile(asset->name_path);
175
176 LoadFromMemory(reinterpret_cast<const unsigned char*>(img_str.c_str()), img_str.length());
177}
178
179void STexture::GenerateMipmap() const {
180 glBindTexture(m_targetGL, m_texId);
181 glGenerateMipmap(m_targetGL);
182}
183
184STexture::Type STexture::GetType() const {
185 return m_type;
186}
187
188void STexture::SetType(STexture::Type type) {
189 m_type = type;
190 m_targetGL = GetTypeToTargetGL(type);
191}
192
193void STexture::BindEmpty(GLint location, int layout, STexture::Type type) {
194 glUniform1i(location, layout);
195
196 glActiveTexture(GL_TEXTURE0 + layout);
197 glBindTexture(GetTypeToTargetGL(type), m_emptyTextureId);
198}
199
200int STexture::GetTypeToTargetGL(STexture::Type type) {
201 switch (type) {
202 case TEX_2D:
203 return GL_TEXTURE_2D;
204 case TEX_CUBE:
205 return GL_TEXTURE_CUBE_MAP;
206 default:
207 return GL_TEXTURE_2D;
208 }
209}
210
211
212
213