-
Notifications
You must be signed in to change notification settings - Fork 8
/
texture.cpp
267 lines (235 loc) · 7.57 KB
/
texture.cpp
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
#include "texture.h"
#include <cassert>
#include <filesystem>
#include <functional>
#include <map>
#include "global.h"
#include "chunk.h"
#include "gameobj.h"
#include "ByteWriter.h"
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <GL/GL.h>
#include <stb_image.h>
#include <stb_image_write.h>
#include "squish.h"
std::map<uint32_t, void*> texmap;
void GlifyTexture(Chunk* c) {
uint8_t* d = (uint8_t*)c->maindata.data();
uint32_t texid = *(uint32_t*)d;
int texh = *(uint16_t*)(d + 4);
int texw = *(uint16_t*)(d + 6);
int nmipmaps = *(uint16_t*)(d + 8);
uint8_t* firstbmp = d + 20;
while (*(firstbmp++));
uint32_t pal[256];
if (c->tag == 'PALN')
{
uint8_t* pnt = firstbmp;
for (int m = 0; m < nmipmaps; m++)
pnt += *(uint32_t*)pnt + 4;
uint32_t npalentries = *(uint32_t*)pnt; pnt += 4;
if (npalentries > 256) npalentries = 256;
memcpy(pal, pnt, 4 * npalentries);
}
GLuint gltex;
glGenTextures(1, &gltex);
glBindTexture(GL_TEXTURE_2D, gltex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (nmipmaps > 1) ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
texmap[texid] = (void*)(uintptr_t)gltex;
uint8_t* bmp = firstbmp;
for (int m = 0; m < nmipmaps; m++)
{
uint32_t mmsize = *(uint32_t*)bmp; bmp += 4;
int mmWidth = std::max(texw >> m, 1);
int mmHeight = std::max(texh >> m, 1);
if (c->tag == 'PALN')
{
uint32_t* pix32 = new uint32_t[mmsize];
for (uint32_t p = 0; p < mmsize; p++)
pix32[p] = pal[bmp[p]];
glTexImage2D(GL_TEXTURE_2D, m, 4, mmWidth, mmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix32);
delete[] pix32;
}
else if (c->tag == 'RGBA')
glTexImage2D(GL_TEXTURE_2D, m, 4, mmWidth, mmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, bmp);
else
ferr("Unknown texture format in Pack(Repeat).PAL.");
bmp += mmsize;
}
}
void GlifyAllTextures()
{
for (Chunk& c : g_scene.palPack.subchunks)
GlifyTexture(&c);
for (Chunk& c : g_scene.lgtPack.subchunks)
GlifyTexture(&c);
glBindTexture(GL_TEXTURE_2D, 0);
}
void InvalidateTexture(uint32_t texid)
{
GLuint gltex = (GLuint)(uintptr_t)texmap.at(texid);
glDeleteTextures(1, &gltex);
GlifyTexture(FindTextureChunk(g_scene, texid).first);
}
void UncacheAllTextures()
{
for (auto& [id, vtex] : texmap) {
GLuint gltex = (GLuint)(uintptr_t)vtex;
glDeleteTextures(1, &gltex);
}
texmap.clear();
}
std::tuple<uint32_t, Chunk*, Chunk*> AddUninitializedTexture(Scene& scene)
{
uint32_t texId = ++scene.numTextures;
Chunk& chk = scene.palPack.subchunks.emplace_back();
Chunk& dxtchk = scene.dxtPack.subchunks.emplace_back();
return { texId, &chk, &dxtchk };
}
uint32_t AddTexture(Scene& scene, uint8_t* pixels, int width, int height, std::string_view name)
{
auto& [id, chk, dxtchk] = AddUninitializedTexture(scene);
ImportTexture(pixels, width, height, name, *chk, *dxtchk, id);
return id;
}
uint32_t AddTexture(Scene& scene, const std::filesystem::path& filepath)
{
auto& [id, chk, dxtchk] = AddUninitializedTexture(scene);
ImportTexture(filepath, *chk, *dxtchk, id);
return id;
}
uint32_t AddTexture(Scene& scene, const void* mem, size_t memSize, std::string_view name)
{
auto& [id, chk, dxtchk] = AddUninitializedTexture(scene);
ImportTexture(mem, memSize, name, *chk, *dxtchk, id);
return id;
}
void ImportTexture(uint8_t* pixels, int width, int height, std::string_view name, Chunk& chk, Chunk& dxtchk, int texid)
{
int numMipmaps = 1;
int flags = 0x14;
int random = 0x12345678;
ByteWriter<std::vector<uint8_t>> chkdata, dxtdata;
chkdata.addU32(texid);
chkdata.addU16(height);
chkdata.addU16(width);
chkdata.addU32(numMipmaps);
chkdata.addU32(flags);
chkdata.addU32(random);
chkdata.addStringNT(name);
dxtdata = chkdata;
int size = width * height * 4;
chkdata.addS32(size);
chkdata.addData(pixels, size);
// 1 COPY >_<
chk.tag = 'RGBA';
auto str = chkdata.take();
chk.maindata.resize(str.size());
memcpy(chk.maindata.data(), str.data(), str.size());
// DXT
size = squish::GetStorageRequirements(width, height, squish::kDxt1);
dxtdata.addS32(size);
uint8_t* comp = dxtdata.addEmpty(size);
squish::CompressImage(pixels, width, height, comp, squish::kDxt1);
// 1 COPY >_<
dxtchk.tag = 'DXT1';
str = dxtdata.take();
dxtchk.maindata.resize(str.size());
memcpy(dxtchk.maindata.data(), str.data(), str.size());
}
void ImportTexture(const std::filesystem::path& filepath, Chunk& chk, Chunk& dxtchk, int texid)
{
int width, height, channels;
uint8_t* pixels = stbi_load(filepath.string().c_str(), &width, &height, &channels, 4);
std::string name = filepath.stem().string();
ImportTexture(pixels, width, height, name, chk, dxtchk, texid);
stbi_image_free(pixels);
}
void ImportTexture(const void* mem, size_t memSize, std::string_view name, Chunk& chk, Chunk& dxtchk, int texid)
{
int width, height, channels;
uint8_t* pixels = stbi_load_from_memory((const uint8_t*)mem, memSize, &width, &height, &channels, 4);
ImportTexture(pixels, width, height, name, chk, dxtchk, texid);
stbi_image_free(pixels);
}
DynArray<uint32_t> ConvertTextureToRGBA8(Chunk* texChunk) {
const TexInfo* ti = (const TexInfo*)texChunk->maindata.data();
uint8_t* firstbmp = texChunk->maindata.data() + 20;
while (*(firstbmp++)); // skip name
if (texChunk->tag == 'PALN')
{
uint32_t pal[256];
uint8_t* pnt = firstbmp;
for (int m = 0; m < (int)ti->numMipmaps; m++)
pnt += *(uint32_t*)pnt + 4;
uint32_t npalentries = *(uint32_t*)pnt; pnt += 4;
if (npalentries > 256) npalentries = 256;
memcpy(pal, pnt, 4 * npalentries);
pnt = firstbmp;
uint32_t mmsize = *(uint32_t*)pnt; pnt += 4;
auto pix32 = DynArray<uint32_t>(mmsize);
for (uint32_t p = 0; p < mmsize; p++)
pix32[p] = pal[pnt[p]];
return pix32;
}
else if (texChunk->tag == 'RGBA') {
uint8_t* pnt = firstbmp;
uint32_t mmsize = *(uint32_t*)pnt; pnt += 4;
auto pix32 = DynArray<uint32_t>(mmsize/4);
memcpy(pix32.data(), pnt, mmsize);
return pix32;
}
return {};
}
void ExportTexture(Chunk* texChunk, const std::filesystem::path& filepath)
{
const TexInfo* ti = (const TexInfo*)texChunk->maindata.data();
auto rgba = ConvertTextureToRGBA8(texChunk);
assert(rgba.size() > 0);
stbi_write_png(filepath.string().c_str(), ti->width, ti->height, 4, rgba.data(), 0);
}
std::vector<uint8_t> ExportTextureToPNGInMemory(Chunk* texChunk)
{
const TexInfo* ti = (const TexInfo*)texChunk->maindata.data();
auto rgba = ConvertTextureToRGBA8(texChunk);
assert(rgba.size() > 0);
ByteWriter<std::vector<uint8_t>> byteWriter;
auto writeFunc = [](void* context, void* data, int size) -> void {
auto* bytes = (decltype(byteWriter)*)context;
bytes->addData(data, size);
};
stbi_write_png_to_func(writeFunc, &byteWriter, ti->width, ti->height, 4, rgba.data(), 0);
return byteWriter.take();
}
std::pair<Chunk*, Chunk*> FindTextureChunk(Scene& scene, uint32_t id)
{
for (Chunk& chk : scene.palPack.subchunks) {
uint32_t chkid = *(uint32_t*)chk.maindata.data();
if (chkid == id) {
int nth = &chk - scene.palPack.subchunks.data();
assert(*(uint32_t*)scene.dxtPack.subchunks[nth].maindata.data() == id);
return { &chk, &scene.dxtPack.subchunks[nth] };
}
}
for (Chunk& chk : scene.lgtPack.subchunks) {
uint32_t chkid = *(uint32_t*)chk.maindata.data();
if (chkid == id) {
return { &chk, nullptr };
}
}
return { nullptr, nullptr };
}
std::pair<Chunk*, Chunk*> FindTextureChunkByName(Scene& scene, std::string_view name)
{
for (Chunk& chk : scene.palPack.subchunks) {
const TexInfo* ti = (const TexInfo*)chk.maindata.data();
if (name == ti->name) {
int nth = &chk - scene.palPack.subchunks.data();
return { &chk, &scene.dxtPack.subchunks[nth] };
}
}
return { nullptr, nullptr };
}