1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
#include <al/lib.h>
AL_IGNORE_WARNING("-Wunused-function")
AL_IGNORE_WARNING("-Wmissing-field-initializers")
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_STATIC
#ifdef MAURI_LOCAL_STB
#include <stb_image_write.h>
#else
#include <stb/stb_image_write.h>
#endif
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#define STBI_NO_STDIO
#define STBI_NO_FAILURE_STRINGS
#ifdef MAURI_LOCAL_STB
#include <stb_image.h>
#else
#include <stb/stb_image.h>
#endif
AL_IGNORE_WARNING_END
#include <lz4.h>
#include <s3tc.h>
#include "texture.h"
#include "log.h"
using namespace Mauri;
Texture::Texture(Asset *asset)
{
this->wrapped = false;
//std::string_view type = asset->reads(9);
//std::string_view itype = asset->reads(9);
asset->seek(18);
TextureFormat format = (TextureFormat)asset->read<u32>();
this->flags.i = asset->read<u32>();
//u32 texture_width = asset->read<u32>();
//u32 texture_height = asset->read<u32>();
asset->seek(8);
this->width = asset->read<u32>();
this->height = asset->read<u32>();
asset->seek(4);
std::string_view container = asset->reads(9);
TextureVersion version;
bool has_container = false;
u32 image_count = asset->read<u32>();
if (container.compare(0, 8, "TEXB0003") == 0)
{
has_container = asset->read<s32>() != -1;
version = TEXB0003;
}
else if (container.compare(0, 8, "TEXB0002") == 0)
{
version = TEXB0002;
}
//else if (container.compare(0, 8, "TEXB0001") == 0)
else
{
version = TEXB0001;
}
this->textures.resize(image_count);
this->texture_resolutions.resize(image_count);
for (u32 s = 0; s < image_count; s++)
{
u32 mm_count = asset->read<u32>();
bool clamp = this->flags.m.CLAMP_UV == 1;
bool no_interp = this->flags.m.NO_INTERPOLATION == 1;
RenderTexture *texture = new RenderTexture(no_interp, clamp, true, mm_count);
this->textures[s] = texture;
this->texture_resolutions[s] = vec4{0.f, 0.f, this->width, this->height};
for (u32 i = 0; i < mm_count; i++)
{
u32 mm_width = asset->read<u32>();
u32 mm_height = asset->read<u32>();
bool lz4_compressed = false;
u32 decompressed_byte_count = 0;
if (version == TEXB0002 || version == TEXB0003)
{
lz4_compressed = asset->read<u32>() == 1;
decompressed_byte_count = asset->read<u32>();
}
u32 byte_count = asset->read<u32>();
if (!lz4_compressed) decompressed_byte_count = byte_count;
byte *buffer = asset->ptr();
if (lz4_compressed)
{
char *decompressed = new char[decompressed_byte_count];
LZ4_decompress_safe((char *)buffer, decompressed,
byte_count, decompressed_byte_count);
buffer = (byte *)decompressed;
}
if (!has_container)
{
assert(buffer);
switch (format)
{
case RGBA8888:
texture->format = GL_RGBA;
texture->upload(buffer, mm_width, mm_height, i);
break;
case DXT5:
{
warn("DXT5 texture");
u32 *decompressed = new u32[(mm_width + 3) * (mm_height + 3)];
BlockDecompressImageDXT5(mm_width, mm_height, buffer, decompressed);
texture->format = GL_RGBA;
texture->upload((byte *)decompressed, mm_width, mm_height, i, GL_UNSIGNED_INT_8_8_8_8);
delete[] decompressed;
break;
}
case DXT1:
{
warn("DXT1 texture");
u32 *decompressed = new u32[(mm_width + 3) * (mm_height + 3)];
BlockDecompressImageDXT1(mm_width, mm_height, buffer, decompressed);
texture->format = GL_RGBA;
texture->upload((byte *)decompressed, mm_width, mm_height, i, GL_UNSIGNED_INT_8_8_8_8);
delete[] decompressed;
break;
}
case DXT3:
warn("DXT3 texture");
break;
case RG88:
texture->format = GL_RG;
texture->upload(buffer, mm_width, mm_height, i);
break;
case R8:
texture->format = GL_RED;
texture->upload(buffer, mm_width, mm_height, i);
break;
default:
warn("uknown texture format");
break;
}
if (i == 0)
{
this->textures[s]->width = mm_width;
this->textures[s]->height = mm_height;
}
}
else
{
s32 w, h, channels;
byte *data = stbi_load_from_memory(buffer, byte_count, &w, &h, &channels, 0);
assert(data);
switch (channels)
{
case 1:
texture->format = GL_RED;
break;
case 2:
texture->format = GL_RG;
break;
case 3:
texture->format = GL_RGB;
break;
case 4:
default:
texture->format = GL_RGBA;
break;
}
texture->upload(data, w, h, i);
stbi_image_free(data);
if (i == 0)
{
this->textures[s]->width = w;
this->textures[s]->height = h;
}
}
if (lz4_compressed) delete[] buffer;
asset->seek(byte_count);
}
this->texture_resolutions[s][0] = this->textures[s]->width;
this->texture_resolutions[s][1] = this->textures[s]->height;
}
this->texture = this->textures[0];
this->texture_resolution = &this->texture_resolutions[0];
if (this->is_gif())
{
std::string_view gif_container = asset->reads(9);
u32 gif_frame_count = asset->read<u32>();
if (gif_container.compare(0, 8, "TEXS0002") == 0) {}
else if (gif_container.compare(0, 8, "TEXS0003") == 0)
{
this->gif_width = asset->read<u32>();
this->gif_height = asset->read<u32>();
}
this->frames.resize(gif_frame_count);
for (u32 i = 0; i < gif_frame_count; i++)
{
this->frames[i] = TextureFrame{
asset->read<u32>(),
asset->read<f32>(),
asset->read<f32>(),
asset->read<f32>(),
asset->read<f32>(),
asset->read<f32>(),
asset->read<f32>(),
asset->read<f32>(),
};
}
}
asset->reset();
}
Texture::Texture(RenderFramebuffer *buffer)
{
this->wrapped = true;
this->texture = buffer->texture;
this->width = buffer->width;
this->height = buffer->height;
this->texture->width = buffer->width;
this->texture->height = buffer->height;
this->texture_resolutions.resize(1);
this->texture_resolutions[0][0] = buffer->width;
this->texture_resolutions[0][1] = buffer->height;
this->texture_resolutions[0][2] = buffer->width;
this->texture_resolutions[0][3] = buffer->height;
this->texture_resolution = &this->texture_resolutions[0];
}
auto Texture::update(f32 time) -> void
{
if (!this->is_gif() || time < this->gif_update) return;
TextureFrame *frame = &this->frames[this->gif_index];
this->texture = this->textures[frame->id];
this->texture_resolution = &this->texture_resolutions[frame->id];
// Scale texture cordinates to size of one frame.
this->texture_rotation[0] = frame->width / (f32)this->texture->width;
//this->texture_rotation[1] = 0.f;
//this->texture_rotation[2] = 0.f;
this->texture_rotation[1] = frame->unknown0 / (f32)this->texture->width;
this->texture_rotation[2] = frame->unknown1 / (f32)this->texture->height;
this->texture_rotation[3] = frame->height / (f32)this->texture->height;
// Offset texture cordinates to the current frame
// in the spritesheet.
this->texture_translation[0] = frame->x / (f32)this->texture->width;
this->texture_translation[1] = frame->y / (f32)this->texture->height;
this->gif_index = (this->gif_index + 1) % this->frames.size();
this->gif_update = time + frame->frame_time;
}
auto Texture::bind(s32 index) -> void
{
this->texture->bind(index);
}
auto Texture::save_to_file(const std::string &filename) -> void
{
for (u32 i = 0; i < this->textures.size(); i++)
{
u32 width = this->textures[i]->width;
u32 height = this->textures[i]->height;
u32 *data = this->textures[i]->pixels.data();
stbi_write_png((filename + std::to_string(i)).c_str(), width, height, 4, data, 4 * width);
}
}
Texture::~Texture()
{
if (!this->wrapped) delete this->texture;
}
Framebuffer::Framebuffer(std::string name, s32 width, s32 height, f32 scale)
: name(name)
{
this->buffer = new RenderFramebuffer(width, height, scale);
this->texture = new Texture(this->buffer);
}
Framebuffer::~Framebuffer()
{
if (this->buffer) delete this->buffer;
if (this->texture) delete this->texture;
}
|