4#define STB_IMAGE_IMPLEMENTATION
6#include "../../OGLDef.h"
9#include "../Loader/STB/stb_image.h"
13unsigned int STexture::m_emptyTextureId = 0;
16 SetUndestroyable(
true);
17 if(m_emptyTextureId == 0) {
22STexture::STexture(STexture::Type type) {
23 SetUndestroyable(
true);
27STexture::~STexture() =
default;
29bool STexture::LoadFile(
const char* path) {
31 if (m_texId != 0)
return false;
34 unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, 0);
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);
44bool STexture::Load(
unsigned char* data) {
47 stbi_image_free(data);
51 glGenTextures(1, &m_texId);
52 glBindTexture(m_targetGL, m_texId);
54 m_internalFormat = GL_RGB;
57 m_internalFormat = GL_R8;
60 m_internalFormat = GL_RG;
63 m_internalFormat = GL_RGBA;
67 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_internalFormat, m_glType, data);
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);
74 stbi_image_free(data);
78void STexture::LoadEmpty() {
79 if (m_emptyTextureId != 0)
return;
81 glGenTextures(1, &m_emptyTextureId);
82 glBindTexture(GL_TEXTURE_2D, m_emptyTextureId);
84 GLubyte data[] = { 255, 0, 255 };
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
89bool STexture::ReloadFile(
const char* path) {
91 return LoadFile(path);
94bool STexture::Reload(
unsigned char* data) {
99void STexture::Release() {
100 glDeleteTextures(1, &m_texId);
106void STexture::Exterminate() {
110void STexture::Bind(GLint location,
int layout) {
112 BindEmpty(location, layout, m_type);
115 glUniform1i(location, layout);
117 glActiveTexture(GL_TEXTURE0 + layout);
118 glBindTexture(m_targetGL, m_texId);
121bool STexture::InitTexture(
int width,
int height,
int channel,
int internalFormat,
int glType) {
128 m_channels = channel;
129 m_internalFormat = internalFormat;
132 glGenTextures(1, &m_texId);
133 glBindTexture(m_targetGL, m_texId);
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,
144 glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_channels, m_glType,
nullptr);
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);
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);
163void STexture::SetParameteri(
int targetName,
int value)
const {
164 glBindTexture(GL_TEXTURE_2D, m_texId);
165 glTexParameteri(GL_TEXTURE_2D, targetName, value);
168void STexture::SetParameterfv(
int targetName,
float* value)
const {
169 glBindTexture(GL_TEXTURE_2D, m_texId);
170 glTexParameterfv(GL_TEXTURE_2D, targetName, value);
174 const std::string img_str = CSE::AssetMgr::LoadAssetFile(asset->name_path);
176 LoadFromMemory(
reinterpret_cast<const unsigned char*
>(img_str.c_str()), img_str.length());
179void STexture::GenerateMipmap()
const {
180 glBindTexture(m_targetGL, m_texId);
181 glGenerateMipmap(m_targetGL);
184STexture::Type STexture::GetType()
const {
188void STexture::SetType(STexture::Type type) {
190 m_targetGL = GetTypeToTargetGL(type);
193void STexture::BindEmpty(GLint location,
int layout, STexture::Type type) {
194 glUniform1i(location, layout);
196 glActiveTexture(GL_TEXTURE0 + layout);
197 glBindTexture(GetTypeToTargetGL(type), m_emptyTextureId);
200int STexture::GetTypeToTargetGL(STexture::Type type) {
203 return GL_TEXTURE_2D;
205 return GL_TEXTURE_CUBE_MAP;
207 return GL_TEXTURE_2D;