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

Public Types

enum  Type { TEX_2D = 0 , TEX_CUBE = 1 }
 

Public Member Functions

 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)
 
int getMWidth () const
 
int getMHeight () const
 
- Public Member Functions inherited from CSE::SResource
 SResource (bool isRegister)
 
 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 ()
 
virtual void __FORCE_DESTROY__ ()
 
virtual std::string GenerateMeta ()
 
std::string GetHash () 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)
 
template<class T >
static T * Create (const AssetMgr::AssetReference *asset)
 
template<class T >
static T * Get (std::string name)
 

Protected Member Functions

void Init (const AssetMgr::AssetReference *asset) override
 

Protected Attributes

Type m_type = TEX_2D
 
int m_targetGL = GL_TEXTURE_2D
 
int m_width = 0
 
int m_height = 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
 

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
18 };

Constructor & Destructor Documentation

◆ STexture() [1/2]

STexture::STexture ( )

Definition at line 15 of file STexture.cpp.

15 {
16 SetUndestroyable(true);
17 if(m_emptyTextureId == 0) {
18 LoadEmpty();
19 }
20}

◆ STexture() [2/2]

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

Definition at line 22 of file STexture.cpp.

22 {
23 SetUndestroyable(true);
24 SetType(type);
25}

Member Function Documentation

◆ Bind()

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

Definition at line 110 of file STexture.cpp.

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

◆ BindEmpty()

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

Definition at line 193 of file STexture.cpp.

193 {
194 glUniform1i(location, layout);
195
196 glActiveTexture(GL_TEXTURE0 + layout);
197 glBindTexture(GetTypeToTargetGL(type), m_emptyTextureId);
198}

◆ Exterminate()

void STexture::Exterminate ( )
overridevirtual

Implements CSE::SObject.

Definition at line 106 of file STexture.cpp.

106 {
107 Release();
108}

◆ GenerateMipmap()

void STexture::GenerateMipmap ( ) const

Definition at line 179 of file STexture.cpp.

179 {
180 glBindTexture(m_targetGL, m_texId);
181 glGenerateMipmap(m_targetGL);
182}

◆ 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 184 of file STexture.cpp.

184 {
185 return m_type;
186}

◆ Init()

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

Implements CSE::SResource.

Definition at line 173 of file STexture.cpp.

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

◆ 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 121 of file STexture.cpp.

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

◆ InitTextureMipmap()

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

Definition at line 156 of file STexture.cpp.

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

◆ Load()

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

Definition at line 44 of file STexture.cpp.

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

◆ LoadFile()

bool STexture::LoadFile ( const char *  path)

Definition at line 29 of file STexture.cpp.

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

◆ LoadFromMemory()

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

Definition at line 39 of file STexture.cpp.

39 {
40 auto data = stbi_load_from_memory(rawData, length, &m_width, &m_height, &m_channels, 0);
41 return Load(data);
42}

◆ Release()

void STexture::Release ( )

Definition at line 99 of file STexture.cpp.

99 {
100 glDeleteTextures(1, &m_texId);
101 m_texId = 0;
102 m_height = 0;
103 m_width = 0;
104}

◆ Reload()

bool STexture::Reload ( unsigned char *  data)

Definition at line 94 of file STexture.cpp.

94 {
95 Release();
96 return Load(data);
97}

◆ ReloadFile()

bool STexture::ReloadFile ( const char *  path)

Definition at line 89 of file STexture.cpp.

89 {
90 Release();
91 return LoadFile(path);
92}

◆ SetParameterfv()

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

Definition at line 168 of file STexture.cpp.

168 {
169 glBindTexture(GL_TEXTURE_2D, m_texId);
170 glTexParameterfv(GL_TEXTURE_2D, targetName, value);
171}

◆ SetParameteri()

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

Definition at line 163 of file STexture.cpp.

163 {
164 glBindTexture(GL_TEXTURE_2D, m_texId);
165 glTexParameteri(GL_TEXTURE_2D, targetName, value);
166}

◆ SetType()

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

Definition at line 188 of file STexture.cpp.

188 {
189 m_type = type;
190 m_targetGL = GetTypeToTargetGL(type);
191}

Member Data Documentation

◆ m_channels

int CSE::STexture::m_channels = 0
protected

Definition at line 80 of file STexture.h.

◆ m_glType

int CSE::STexture::m_glType = GL_UNSIGNED_BYTE
protected

Definition at line 82 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 81 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 84 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: