CSEngine
Loading...
Searching...
No Matches
CSE::STexture Class Reference
Inheritance diagram for CSE::STexture:
CSE::SResource CSE::SObject CSE::VariableBinder CSE::ReflectionObject

Public Types

enum  Type { TEX_2D = 0 , TEX_CUBE = 1 , TEX_3D = 2 }
 

Public Member Functions

 RESOURCE_DEFINE_CONSTRUCTOR (STexture)
 
 STexture (Type type)
 
bool LoadFile (const char *path)
 
bool LoadFromMemory (const unsigned char *rawData, int length)
 
virtual bool Load (unsigned char *data)
 
bool ReloadFile (const char *path)
 
bool Reload (unsigned char *data)
 
unsigned int GetTextureID () const
 
virtual bool InitTexture (int width, int height, int channel=GL_RGB, int internalFormat=GL_RGB8, int glType=GL_UNSIGNED_BYTE)
 
bool InitTextureMipmap (int width, int height, int channel=GL_RGB, int internalFormat=GL_RGB8, int glType=GL_UNSIGNED_BYTE)
 
virtual void SetParameteri (int targetName, int value) const
 
virtual void SetParameterfv (int targetName, float *value) const
 
void Release ()
 
void Exterminate () override
 
virtual void Bind (GLint location, int layout)
 
void GenerateMipmap () const
 
Type GetType () const
 
void SetType (Type type)
 
void SetValue (std::string name_str, Arguments value) override
 
std::string PrintValue () const override
 
- Public Member Functions inherited from CSE::SResource
 SResource (std::string classType)
 
 SResource (const SResource *resource, bool isRegister)
 
void SetName (std::string name)
 
void SetAbsoluteID (std::string id)
 
std::string GetName () const
 
std::string GetAbsoluteID () const
 
AssetMgr::AssetReferenceGetAssetReference (std::string hash="") const
 
void LinkResource (AssetMgr::AssetReference *asset)
 
void LinkResource (std::string name)
 
void SetHash (std::string &hash) override
 
- Public Member Functions inherited from CSE::SObject
 SObject (bool isRegister)
 
virtual void SetUndestroyable (bool enable)
 
virtual void Destroy ()
 
std::string GetHash () const
 
- Public Member Functions inherited from CSE::VariableBinder
- Public Member Functions inherited from CSE::ReflectionObject
 ReflectionObject (std::string type)
 
void SetClassType (std::string type)
 
const char * GetClassType () const
 
bool IsSameClass (const char *classType) const
 

Static Public Member Functions

static void BindEmpty (GLint location, int layout, STexture::Type type=TEX_2D)
 
- Static Public Member Functions inherited from CSE::SResource
template<class T >
static T * Create (const std::string &name)
 
static SResourceCreate (const std::string &name, const std::string &classType)
 
template<class T >
static T * Create (const AssetMgr::AssetReference *asset)
 
static SResourceCreate (const AssetMgr::AssetReference *asset, const std::string &classType)
 
template<class T >
static T * Get (std::string name)
 
static SResourceGet (std::string &name)
 
- Static Public Member Functions inherited from CSE::ReflectionObject
static ReflectionObjectNewObject (const std::string &name)
 

Protected Member Functions

void Init (const AssetMgr::AssetReference *asset) override
 
- Protected Member Functions inherited from CSE::SResource
- Protected Member Functions inherited from CSE::VariableBinder

Protected Attributes

Type m_type = TEX_2D
 
int m_targetGL = GL_TEXTURE_2D
 
int m_width = 0
 
int m_height = 0
 
int m_depth = 0
 
int m_channels = 0
 
int m_internalFormat = 0
 
int m_glType = GL_UNSIGNED_BYTE
 
unsigned int m_texId = 0
 
- Protected Attributes inherited from CSE::SObject
std::string m_hash
 
- Protected Attributes inherited from CSE::ReflectionObject
std::string m_class
 

Additional Inherited Members

- Protected Types inherited from CSE::VariableBinder
typedef std::vector< std::string > Arguments
 

Detailed Description

Definition at line 14 of file STexture.h.

Member Enumeration Documentation

◆ Type

enum CSE::STexture::Type

Definition at line 16 of file STexture.h.

16 {
17 TEX_2D = 0, TEX_CUBE = 1, TEX_3D = 2
18 };

Constructor & Destructor Documentation

◆ STexture()

CSE::STexture::STexture ( STexture::Type type)
explicit

Definition at line 25 of file STexture.cpp.

25 : SResource("STexture") {
26 SetUndestroyable(true);
27 SetType(type);
28}

Member Function Documentation

◆ Bind()

void STexture::Bind ( GLint location,
int layout )
virtual

Definition at line 130 of file STexture.cpp.

130 {
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}

◆ BindEmpty()

void STexture::BindEmpty ( GLint location,
int layout,
STexture::Type type = TEX_2D )
static

Definition at line 253 of file STexture.cpp.

253 {
254 glUniform1i(location, layout);
255
256 glActiveTexture(GL_TEXTURE0 + layout);
257 glBindTexture(GetTypeToTargetGL(type), m_emptyTextureId);
258}

◆ Exterminate()

void STexture::Exterminate ( )
overridevirtual

Implements CSE::SObject.

Definition at line 126 of file STexture.cpp.

126 {
127 Release();
128}

◆ GenerateMipmap()

void STexture::GenerateMipmap ( ) const

Definition at line 239 of file STexture.cpp.

239 {
240 glBindTexture(m_targetGL, m_texId);
241 glGenerateMipmap(m_targetGL);
242}

◆ GetTextureID()

unsigned int CSE::STexture::GetTextureID ( ) const
inline

Definition at line 35 of file STexture.h.

35 {
36 return m_texId;
37 }

◆ GetType()

STexture::Type STexture::GetType ( ) const

Definition at line 244 of file STexture.cpp.

244 {
245 return m_type;
246}

◆ Init()

void STexture::Init ( const AssetMgr::AssetReference * asset)
overrideprotectedvirtual

Implements CSE::SResource.

Definition at line 198 of file STexture.cpp.

198 {
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}
Definition XML.h:77
Definition XML.h:43

◆ InitTexture()

bool STexture::InitTexture ( int width,
int height,
int channel = GL_RGB,
int internalFormat = GL_RGB8,
int glType = GL_UNSIGNED_BYTE )
virtual

Definition at line 141 of file STexture.cpp.

141 {
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}

◆ InitTextureMipmap()

bool STexture::InitTextureMipmap ( int width,
int height,
int channel = GL_RGB,
int internalFormat = GL_RGB8,
int glType = GL_UNSIGNED_BYTE )

Definition at line 181 of file STexture.cpp.

181 {
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}

◆ Load()

bool STexture::Load ( unsigned char * data)
virtual

Definition at line 53 of file STexture.cpp.

53 {
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}

◆ LoadFile()

bool STexture::LoadFile ( const char * path)

Definition at line 32 of file STexture.cpp.

32 {
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}

◆ LoadFromMemory()

bool STexture::LoadFromMemory ( const unsigned char * rawData,
int length )

Definition at line 42 of file STexture.cpp.

42 {
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}

◆ PrintValue()

std::string STexture::PrintValue ( ) const
overridevirtual

Implements CSE::VariableBinder.

Definition at line 276 of file STexture.cpp.

276 {
277 return {};
278}

◆ Release()

void STexture::Release ( )

Definition at line 119 of file STexture.cpp.

119 {
120 glDeleteTextures(1, &m_texId);
121 m_texId = 0;
122 m_height = 0;
123 m_width = 0;
124}

◆ Reload()

bool STexture::Reload ( unsigned char * data)

Definition at line 114 of file STexture.cpp.

114 {
115 Release();
116 return Load(data);
117}

◆ ReloadFile()

bool STexture::ReloadFile ( const char * path)

Definition at line 109 of file STexture.cpp.

109 {
110 Release();
111 return LoadFile(path);
112}

◆ SetParameterfv()

void STexture::SetParameterfv ( int targetName,
float * value ) const
virtual

Definition at line 193 of file STexture.cpp.

193 {
194 glBindTexture(m_targetGL, m_texId);
195 glTexParameterfv(m_targetGL, targetName, value);
196}

◆ SetParameteri()

void STexture::SetParameteri ( int targetName,
int value ) const
virtual

Definition at line 188 of file STexture.cpp.

188 {
189 glBindTexture(m_targetGL, m_texId);
190 glTexParameteri(m_targetGL, targetName, value);
191}

◆ SetType()

void STexture::SetType ( STexture::Type type)

Definition at line 248 of file STexture.cpp.

248 {
249 m_type = type;
250 m_targetGL = GetTypeToTargetGL(type);
251}

◆ SetValue()

void STexture::SetValue ( std::string name_str,
Arguments value )
overridevirtual

Used to get serialized values. All classes that inherit from VariableBinder will unconditionally call this function first during initialization.

Parameters
name_strName of values
valueSerialized values

Implements CSE::VariableBinder.

Definition at line 273 of file STexture.cpp.

273 {
274}

Member Data Documentation

◆ m_channels

int CSE::STexture::m_channels = 0
protected

Definition at line 81 of file STexture.h.

◆ m_depth

int CSE::STexture::m_depth = 0
protected

Definition at line 80 of file STexture.h.

◆ m_glType

int CSE::STexture::m_glType = GL_UNSIGNED_BYTE
protected

Definition at line 83 of file STexture.h.

◆ m_height

int CSE::STexture::m_height = 0
protected

Definition at line 79 of file STexture.h.

◆ m_internalFormat

int CSE::STexture::m_internalFormat = 0
protected

Definition at line 82 of file STexture.h.

◆ m_targetGL

int CSE::STexture::m_targetGL = GL_TEXTURE_2D
protected

Definition at line 77 of file STexture.h.

◆ m_texId

unsigned int CSE::STexture::m_texId = 0
protected

Definition at line 85 of file STexture.h.

◆ m_type

Type CSE::STexture::m_type = TEX_2D
protected

Definition at line 76 of file STexture.h.

◆ m_width

int CSE::STexture::m_width = 0
protected

Definition at line 78 of file STexture.h.


The documentation for this class was generated from the following files: