Norbert's Game Engine v200
Loading...
Searching...
No Matches
eTexture Class Reference

#include <Texture.hpp>

Inheritance diagram for eTexture:
eBase

Public Member Functions

 eTexture (bool autoDestroy=true)
 
void Release ()
 
void Load (eString filename, const bool flipUV=true, uint64 flags=0)
 
void LoadMem (strg &mem, eString name, uint64 flags=0)
 
void LoadDXT (strg &mem, eString name, uint64 flags=0)
 
void SetHandle (eTextureHandle handle)
 
eTextureHandle GetHandle ()
 
vec2i GetSize ()
 
- Public Member Functions inherited from eBase
template<typename T >
T * Cast (eBase *obj)
 Cast class to base.
 

Static Public Member Functions

static eString CompileDXT (eString inputFile, eString outputFile, uint8 bc=1, const bool mips=false, const bool isNormalMap=false)
 

Detailed Description

Texture flags: xyzw x: 0 -> Mirror 1 -> Border 2 -> Clamp y: 0 -> border color shift 1 -> border color mask z: 0 -> compare greater 1 -> compare not equal 2 -> compare never 3 -> compare always 4 -> compare shift w: 0 -> min/mag point 1 -> min/mag mask 2 -> min/mag shift 3 -> min/mag anisotropic Texture class

Definition at line 70 of file Texture.hpp.

Constructor & Destructor Documentation

◆ eTexture()

eTexture::eTexture ( bool autoDestroy = true)

Definition at line 24 of file Texture.cpp.

25{
26 bAutoDestroy = autoDestroy;
27}

◆ ~eTexture()

eTexture::~eTexture ( )

Definition at line 29 of file Texture.cpp.

30{
31 if (bAutoDestroy)
32 Release();
33}

Member Function Documentation

◆ CompileDXT()

eString eTexture::CompileDXT ( eString inputFile,
eString outputFile,
uint8 bc = 1,
const bool mips = false,
const bool isNormalMap = false )
static

Definition at line 361 of file Texture.cpp.

362{
363 if (!EDR::FileExists(inputFile.Get()))
364 {
365 eConsole::PrintLog("ERROR::eTexture::CompileDXT", "Input file could not be found!");
366 return "ERROR";
367 }
368
369 if (bc == 0 || bc > 5)
370 {
371 eConsole::PrintLog("ERROR::eTexture::CompileDXT", "Invalid BC format!");
372 return "ERROR";
373 }
374 eString outcmd = TEXT("texturec.exe -f \"") + inputFile + TEXT("\" -o \"") + outputFile + TEXT("\" -t bc") + eString::ToString(bc);
375
376 if (isNormalMap) outcmd += " -n";
377 if(mips) outcmd += " -m";
378
379 if (EDR::FileExists(outputFile.Get()))
380 EDR::RemoveFile(outputFile.Get());
381
382 std::ostringstream command;
383 command << outcmd.GetStrg();
384
385 FILE* pipe = _popen(command.str().c_str(), "r");
386 if (!pipe)
387 {
388 eConsole::PrintLog("ERROR::eTexture::CompileDXT", "Error creating process!");
389 return "ERROR";
390 }
391
392 std::ostringstream output;
393 char buffer[128];
394 while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
395 output << buffer;
396
397 if (_pclose(pipe) == -1)
398 {
399 eConsole::PrintLog("ERROR::eTexture::CompileDXT", "Error closing process!");
400 return "ERROR";
401 }
402
403 return output.str();
404}
static void PrintLog(eString head, eString msg)
Print a log.
Definition Console.cpp:22
const strg & GetStrg() const
Return text as std::string.
Definition String.cpp:70
const char * Get()
Return text as c-string.
Definition String.cpp:64
void RemoveFile(const char *filePath)
Removes a file from the drive.
Definition EDR.cpp:371

◆ GetHandle()

eTextureHandle eTexture::GetHandle ( )

Definition at line 351 of file Texture.cpp.

352{
353 return mHandle;
354}

◆ GetSize()

vec2i eTexture::GetSize ( )

Definition at line 356 of file Texture.cpp.

357{
358 return mSize;
359}

◆ Load()

void eTexture::Load ( eString filename,
const bool flipUV = true,
uint64 flags = 0 )

Definition at line 44 of file Texture.cpp.

45{
46 const bool isDXT = filename.Find(".dds");
47
48 if (isDXT)
49 {
50 std::ifstream file(filename.GetStrg(), std::ios::binary);
51 if (!file)
52 {
53 eConsole::PrintLog("WARNING::eTexture::Load", "Failed to load texture '" + filename.GetStrg() + "'!");
55 return;
56 }
57
58 file.seekg(0, std::ios::end);
59 uint64 size = file.tellg();
60 file.seekg(0, std::ios::beg);
61
62 std::vector<uint8> buffer(size);
63 file.read(reinterpret_cast<char*>(buffer.data()), size);
64
65 uint64 texFlags = 0;
66 uint64 uvwFlag = flags / 1000;
67 uint64 borderFlag = (flags % 1000) / 100;
68 uint64 compFlag = (flags % 100) / 10;
69 uint64 minMaxFlag = flags % 10;
70
71 switch (uvwFlag)
72 {
73 case 0: { texFlags |= BGFX_SAMPLER_UVW_MIRROR; }break;
74 case 1: { texFlags |= BGFX_SAMPLER_UVW_BORDER; }break;
75 case 2: { texFlags |= BGFX_SAMPLER_UVW_CLAMP; }break;
76 };
77
78 switch (borderFlag)
79 {
80 case 1: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_SHIFT; }break;
81 case 2: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_MASK; }break;
82 };
83
84 switch (compFlag)
85 {
86 case 0: { texFlags |= BGFX_SAMPLER_COMPARE_GREATER; }break;
87 case 1: { texFlags |= BGFX_SAMPLER_COMPARE_NOTEQUAL; }break;
88 case 2: { texFlags |= BGFX_SAMPLER_COMPARE_NEVER; }break;
89 case 3: { texFlags |= BGFX_SAMPLER_COMPARE_ALWAYS; }break;
90 case 4: { texFlags |= BGFX_SAMPLER_COMPARE_SHIFT; }break;
91 };
92
93 switch (minMaxFlag)
94 {
95 case 0: { texFlags |= (BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT); }break;
96 case 1: { texFlags |= (BGFX_SAMPLER_MIN_MASK | BGFX_SAMPLER_MAG_MASK); }break;
97 case 2: { texFlags |= (BGFX_SAMPLER_MIN_SHIFT | BGFX_SAMPLER_MAG_SHIFT); }break;
98 case 3: { texFlags |= (BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC); }break;
99 };
100
101 mHandle = bgfx::createTexture(bgfx::copy(buffer.data(), buffer.size()), texFlags);
102
103 if (bgfx::isValid(mHandle))
104 bgfx::setName(mHandle, filename.Get());
105
106 file.seekg(12);
107 int width, height;
108 file.read(reinterpret_cast<char*>(&height), sizeof(uint32));
109 file.read(reinterpret_cast<char*>(&width), sizeof(uint32));
110 file.close();
111 mSize = vec2i(width, height);
112 }
113 else
114 {
115 stbi_set_flip_vertically_on_load(flipUV);
116 int width, height, nrComponents;
117 unsigned char* data = stbi_load(filename.Get(), &width, &height, &nrComponents, 0);
118
119 if (data)
120 {
121 unsigned char* out = data;
122 /*
123 if (mQuality != nullptr && !bSolidQuality)
124 {
125 switch (*mQuality)
126 {
127 case eQuality::VeryLow:
128 {
129 out = nullptr;
130 out = new unsigned char[(width / 8) * (height / 8) * nrComponents];
131 stbir_resize_uint8(data, width, height, 0, out, width / 8, height / 8, 0, nrComponents);
132 width /= 8;
133 height /= 8;
134 stbi_image_free(data);
135 }break;
136 case eQuality::Low:
137 {
138 out = nullptr;
139 out = new unsigned char[(width / 4) * (height / 4) * nrComponents];
140 stbir_resize_uint8(data, width, height, 0, out, width / 4, height / 4, 0, nrComponents);
141 width /= 4;
142 height /= 4;
143 stbi_image_free(data);
144 }break;
145 case eQuality::Medium:
146 {
147 out = nullptr;
148 out = new unsigned char[(width / 2) * (height / 2) * nrComponents];
149 stbir_resize_uint8(data, width, height, 0, out, width / 2, height / 2, 0, nrComponents);
150 width /= 2;
151 height /= 2;
152 stbi_image_free(data);
153 }break;
154 }
155 }
156 */
157
158 bgfx::TextureFormat::Enum format = bgfx::TextureFormat::R8;
159 if (nrComponents == 3)
160 format = bgfx::TextureFormat::RGB8;
161 else if (nrComponents == 4)
162 format = bgfx::TextureFormat::RGBA8;
163
164 uint64 texFlags = 0;
165 uint64 uvwFlag = flags / 1000;
166 uint64 borderFlag = (flags % 1000) / 100;
167 uint64 compFlag = (flags % 100) / 10;
168 uint64 minMaxFlag = flags % 10;
169
170 switch (uvwFlag)
171 {
172 case 0: { texFlags |= BGFX_SAMPLER_UVW_MIRROR; }break;
173 case 1: { texFlags |= BGFX_SAMPLER_UVW_BORDER; }break;
174 case 2: { texFlags |= BGFX_SAMPLER_UVW_CLAMP; }break;
175 };
176
177 switch (borderFlag)
178 {
179 case 1: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_SHIFT; }break;
180 case 2: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_MASK; }break;
181 };
182
183 switch (compFlag)
184 {
185 case 0: { texFlags |= BGFX_SAMPLER_COMPARE_GREATER; }break;
186 case 1: { texFlags |= BGFX_SAMPLER_COMPARE_NOTEQUAL; }break;
187 case 2: { texFlags |= BGFX_SAMPLER_COMPARE_NEVER; }break;
188 case 3: { texFlags |= BGFX_SAMPLER_COMPARE_ALWAYS; }break;
189 case 4: { texFlags |= BGFX_SAMPLER_COMPARE_SHIFT; }break;
190 };
191
192 switch (minMaxFlag)
193 {
194 case 0: { texFlags |= (BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT); }break;
195 case 1: { texFlags |= (BGFX_SAMPLER_MIN_MASK | BGFX_SAMPLER_MAG_MASK); }break;
196 case 2: { texFlags |= (BGFX_SAMPLER_MIN_SHIFT | BGFX_SAMPLER_MAG_SHIFT); }break;
197 case 3: { texFlags |= (BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC); }break;
198 };
199
200 mHandle = bgfx::createTexture2D(
201 static_cast<uint16>(width),
202 static_cast<uint16>(height),
203 false,
204 0,
205 format,
206 texFlags,
207 bgfx::copy(out, width * height * nrComponents));
208
209 if (bgfx::isValid(mHandle))
210 bgfx::setName(mHandle, filename.Get());
211
212 mSize = vec2i(width, height);
213
214 stbi_image_free(out);
215 }
216 else
217 {
218 stbi_image_free(data);
219 eConsole::PrintLog("WARNING::eTexture::Load", "Failed to load texture '" + filename.GetStrg() + "'!");
221 }
222 stbi_set_flip_vertically_on_load(false);
223 }
224}
static void WriteToDisk()
Write the console text to a crash file called 'CRASH.log'.
Definition Console.cpp:40
const bool Find(eString vl)
Returns true if token was found.
Definition String.cpp:126

◆ LoadDXT()

void eTexture::LoadDXT ( strg & mem,
eString name,
uint64 flags = 0 )

Definition at line 286 of file Texture.cpp.

287{
288 if (!mem.empty())
289 {
290 uint64 texFlags = 0;
291 uint64 uvwFlag = flags / 1000;
292 uint64 borderFlag = (flags % 1000) / 100;
293 uint64 compFlag = (flags % 100) / 10;
294 uint64 minMaxFlag = flags % 10;
295
296 switch (uvwFlag)
297 {
298 case 0: { texFlags |= BGFX_SAMPLER_UVW_MIRROR; }break;
299 case 1: { texFlags |= BGFX_SAMPLER_UVW_BORDER; }break;
300 case 2: { texFlags |= BGFX_SAMPLER_UVW_CLAMP; }break;
301 };
302
303 switch (borderFlag)
304 {
305 case 1: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_SHIFT; }break;
306 case 2: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_MASK; }break;
307 };
308
309 switch (compFlag)
310 {
311 case 0: { texFlags |= BGFX_SAMPLER_COMPARE_GREATER; }break;
312 case 1: { texFlags |= BGFX_SAMPLER_COMPARE_NOTEQUAL; }break;
313 case 2: { texFlags |= BGFX_SAMPLER_COMPARE_NEVER; }break;
314 case 3: { texFlags |= BGFX_SAMPLER_COMPARE_ALWAYS; }break;
315 case 4: { texFlags |= BGFX_SAMPLER_COMPARE_SHIFT; }break;
316 };
317
318 switch (minMaxFlag)
319 {
320 case 0: { texFlags |= (BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT); }break;
321 case 1: { texFlags |= (BGFX_SAMPLER_MIN_MASK | BGFX_SAMPLER_MAG_MASK); }break;
322 case 2: { texFlags |= (BGFX_SAMPLER_MIN_SHIFT | BGFX_SAMPLER_MAG_SHIFT); }break;
323 case 3: { texFlags |= (BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC); }break;
324 };
325
326 std::vector<uint8_t> buffer(mem.size());
327 std::memcpy(buffer.data(), mem.data(), buffer.size());
328
329 mHandle = bgfx::createTexture(bgfx::copy(buffer.data(), buffer.size()), texFlags);
330
331 if (bgfx::isValid(mHandle))
332 bgfx::setName(mHandle, name.Get());
333
334 const char* header = mem.data();
335 int height = *reinterpret_cast<const uint32_t*>(header + 12);
336 int width = *reinterpret_cast<const uint32_t*>(header + 16);
337 mSize = vec2i(width, height);
338 }
339 else
340 {
341 eConsole::PrintLog("WARNING::eTexture::LoadDXT", "data was invalid!");
343 }
344}

◆ LoadMem()

void eTexture::LoadMem ( strg & mem,
eString name,
uint64 flags = 0 )

Definition at line 226 of file Texture.cpp.

227{
228 if (!mem.empty())
229 {
230 uint64 texFlags = 0;
231 uint64 uvwFlag = flags / 1000;
232 uint64 borderFlag = (flags % 1000) / 100;
233 uint64 compFlag = (flags % 100) / 10;
234 uint64 minMaxFlag = flags % 10;
235
236 switch (uvwFlag)
237 {
238 case 0: { texFlags |= BGFX_SAMPLER_UVW_MIRROR; }break;
239 case 1: { texFlags |= BGFX_SAMPLER_UVW_BORDER; }break;
240 case 2: { texFlags |= BGFX_SAMPLER_UVW_CLAMP; }break;
241 };
242
243 switch (borderFlag)
244 {
245 case 1: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_SHIFT; }break;
246 case 2: { texFlags |= BGFX_SAMPLER_BORDER_COLOR_MASK; }break;
247 };
248
249 switch (compFlag)
250 {
251 case 0: { texFlags |= BGFX_SAMPLER_COMPARE_GREATER; }break;
252 case 1: { texFlags |= BGFX_SAMPLER_COMPARE_NOTEQUAL; }break;
253 case 2: { texFlags |= BGFX_SAMPLER_COMPARE_NEVER; }break;
254 case 3: { texFlags |= BGFX_SAMPLER_COMPARE_ALWAYS; }break;
255 case 4: { texFlags |= BGFX_SAMPLER_COMPARE_SHIFT; }break;
256 };
257
258 switch (minMaxFlag)
259 {
260 case 0: { texFlags |= (BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT); }break;
261 case 1: { texFlags |= (BGFX_SAMPLER_MIN_MASK | BGFX_SAMPLER_MAG_MASK); }break;
262 case 2: { texFlags |= (BGFX_SAMPLER_MIN_SHIFT | BGFX_SAMPLER_MAG_SHIFT); }break;
263 case 3: { texFlags |= (BGFX_SAMPLER_MIN_ANISOTROPIC | BGFX_SAMPLER_MAG_ANISOTROPIC); }break;
264 };
265
266 std::vector<uint8_t> buffer(mem.size());
267 std::memcpy(buffer.data(), mem.data(), buffer.size());
268
269 mHandle = bgfx::createTexture(bgfx::copy(buffer.data(), buffer.size()), texFlags);
270
271 if (bgfx::isValid(mHandle))
272 bgfx::setName(mHandle, name.Get());
273
274 const char* header = mem.data();
275 int height = *reinterpret_cast<const uint32_t*>(header + 12);
276 int width = *reinterpret_cast<const uint32_t*>(header + 16);
277 mSize = vec2i(width, height);
278 }
279 else
280 {
281 eConsole::PrintLog("WARNING::eTexture::LoadMem", "data was invalid!");
283 }
284}

◆ Release()

void eTexture::Release ( )

Definition at line 35 of file Texture.cpp.

36{
37 if (bgfx::isValid(mHandle))
38 {
39 bgfx::destroy(mHandle);
40 mHandle.idx = bgfx::kInvalidHandle;
41 }
42}

◆ SetHandle()

void eTexture::SetHandle ( eTextureHandle handle)

Definition at line 346 of file Texture.cpp.

347{
348 mHandle = handle;
349}

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