4#define STB_IMAGE_IMPLEMENTATION 
    6#include "../../OGLDef.h" 
    9#include "../Loader/STB/stb_image.h" 
   10#include "../Loader/XML/XML.h" 
   11#include "../../MacroDef.h" 
   12#include "../AssetsDef.h" 
   16unsigned int STexture::m_emptyTextureId = 0;
 
   19    SetUndestroyable(
true);
 
   20    if(m_emptyTextureId == 0) {
 
   25CSE::STexture::STexture(STexture::Type type) : 
SResource(
"STexture") {
 
   26    SetUndestroyable(
true);
 
   30STexture::~STexture() = 
default;
 
   32bool STexture::LoadFile(
const char* path) {
 
   34    if (m_texId != 0) 
return false;
 
   37    unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, 0);
 
   42bool STexture::LoadFromMemory(
const unsigned char* rawData, 
int length) {
 
   44    if(m_type == TEX_3D) {
 
   46        data = stbi_load_from_memory(rawData, length, &w, &h, &m_channels, 0);
 
   49        data = stbi_load_from_memory(rawData, length, &m_width, &m_height, &m_channels, 0);
 
   53bool STexture::Load(
unsigned char* data) {
 
   56        stbi_image_free(data);
 
   60    glGenTextures(1, &m_texId);
 
   61    glBindTexture(m_targetGL, m_texId);
 
   63    m_internalFormat = GL_RGB;
 
   66            m_internalFormat = GL_R8;
 
   69            m_internalFormat = GL_RG;
 
   72            m_internalFormat = GL_RGBA;
 
   78            glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_internalFormat, m_glType, data);
 
   81            glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_internalFormat, m_glType, data);
 
   84            glTexImage3D(m_targetGL, 0, m_internalFormat, m_width, m_height, m_depth, 0, m_internalFormat, m_glType, data);
 
   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);
 
   94    stbi_image_free(data);
 
   98void STexture::LoadEmpty() {
 
   99    if (m_emptyTextureId != 0) 
return;
 
  101    glGenTextures(1, &m_emptyTextureId);
 
  102    glBindTexture(GL_TEXTURE_2D, m_emptyTextureId);
 
  104    GLubyte data[] = { 255, 0, 255 };
 
  106    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
 
  109bool STexture::ReloadFile(
const char* path) {
 
  111    return LoadFile(path);
 
  114bool STexture::Reload(
unsigned char* data) {
 
  119void STexture::Release() {
 
  120    glDeleteTextures(1, &m_texId);
 
  126void STexture::Exterminate() {
 
  130void STexture::Bind(GLint location, 
int layout) {
 
  132        BindEmpty(location, layout, m_type);
 
  135    glUniform1i(location, layout);
 
  137    glActiveTexture(GL_TEXTURE0 + layout);
 
  138    glBindTexture(m_targetGL, m_texId);
 
  141bool STexture::InitTexture(
int width, 
int height, 
int channel, 
int internalFormat, 
int glType) {
 
  148    m_channels = channel;
 
  149    m_internalFormat = internalFormat;
 
  152    glGenTextures(1, &m_texId);
 
  153    glBindTexture(m_targetGL, m_texId);
 
  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,
 
  164            glTexImage2D(m_targetGL, 0, m_internalFormat, m_width, m_height, 0, m_channels, m_glType, 
nullptr);
 
  168            glTexImage3D(m_targetGL, 0, m_internalFormat, m_width, m_height, m_depth, 0, m_channels, m_glType, 
nullptr);
 
  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);
 
  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);
 
  188void STexture::SetParameteri(
int targetName, 
int value)
 const {
 
  189    glBindTexture(m_targetGL, m_texId);
 
  190    glTexParameteri(m_targetGL, targetName, value);
 
  193void STexture::SetParameterfv(
int targetName, 
float* value)
 const {
 
  194    glBindTexture(m_targetGL, m_texId);
 
  195    glTexParameterfv(m_targetGL, targetName, value);
 
  199        const std::string img_str = CSE::AssetMgr::LoadAssetFile(asset->name_path);
 
  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;
 
  207        const auto& hashChildren = hashData.children;
 
  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);
 
  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);
 
  236    LoadFromMemory(
reinterpret_cast<const unsigned char*
>(img_str.c_str()), img_str.length());
 
  239void STexture::GenerateMipmap()
 const {
 
  240    glBindTexture(m_targetGL, m_texId);
 
  241    glGenerateMipmap(m_targetGL);
 
  244STexture::Type STexture::GetType()
 const {
 
  248void STexture::SetType(STexture::Type type) {
 
  250    m_targetGL = GetTypeToTargetGL(type);
 
  253void STexture::BindEmpty(GLint location, 
int layout, STexture::Type type) {
 
  254    glUniform1i(location, layout);
 
  256    glActiveTexture(GL_TEXTURE0 + layout);
 
  257    glBindTexture(GetTypeToTargetGL(type), m_emptyTextureId);
 
  260int STexture::GetTypeToTargetGL(STexture::Type type) {
 
  263            return GL_TEXTURE_2D;
 
  265            return GL_TEXTURE_CUBE_MAP;
 
  267            return GL_TEXTURE_3D;
 
  269            return GL_TEXTURE_2D;
 
  276std::string STexture::PrintValue()
 const {
 
void SetValue(std::string name_str, Arguments value) override