-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcPortal.cpp
321 lines (281 loc) · 8.63 KB
/
cPortal.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
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
311
312
313
314
315
316
317
318
319
320
321
#include "stdafx.h"
#include "cPortal.h"
#include "cByteStream.h"
/* Portal Types:
01 - Type 1 models
02 - Type 2 models (Modelgroups)
03 - Animations
04 - Palettes
05 - Texture lookups
06 - Textures
08 - Texture Lookups
0A - Sounds
0D - Dungeon Parts
0E - UI text
0F - palette lookups?
13 - Worldinfo
22 - String lists
25 - More string lists
27 - Magic Strings
31 - Character creation strings
*/
PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2DARB;
cPortal::cPortal()
{
m_tfPortal = new cTurbineFile();
m_tfPortal->LoadFile("client_portal.dat");
TCHAR szEXEPathname[_MAX_PATH];
GetModuleFileName(NULL, szEXEPathname, _MAX_PATH);
*(strrchr(szEXEPathname, '\\')+1) = 0;
strcat(szEXEPathname, "client_highres.dat");
FILE *ftest = fopen(szEXEPathname,"rb");
if (ftest)
{
fclose(ftest);
m_tfPortalHighRes = new cTurbineFile();
m_tfPortalHighRes->LoadFile("client_highres.dat");
}
else
m_tfPortalHighRes = NULL;
}
cPortal::~cPortal()
{
if (m_tfPortal)
delete m_tfPortal;
if (m_tfPortalHighRes)
delete m_tfPortalHighRes;
}
cPortalFile * cPortal::OpenEntry( DWORD dwID )
{
cPortalFile *tp = NULL;
if (m_tfPortalHighRes)
tp = m_tfPortalHighRes->OpenEntry(dwID);
if (tp)
return tp;
else return m_tfPortal->OpenEntry(dwID);
}
int cPortal::HighestPowerOfTwo(DWORD In)
{
int tp = -1;
for (int i=0;i<32;i++)
{
if (In & 1) tp = i;
In >>= 1;
}
return tp;
}
DWORD cPortal::FindTexturePalette(DWORD Texture, std::vector<stPaletteSwap> *vPaletteSwaps, float fTransTex, float fTransPix)
{
//TODO: Implement the transparency!
cPortalFile *pfTex = OpenEntry(Texture);
if (!pfTex)
return 0;
//redirect if it can find it
if ((Texture & 0xFF000000) == 0x05000000)
{
DWORD NumIDs = *((DWORD *) (pfTex->data + 9));
for (DWORD i=13; i<=13 + NumIDs*4; i+=4)
{
DWORD tp = FindGraphic(*((DWORD *) (pfTex->data + i)), vPaletteSwaps, fTransTex, fTransPix);
if (tp)
{
m_mGraphicEdgeCache[Texture] = m_mGraphicEdgeCache[*((DWORD *) (pfTex->data + i))];
return tp;
}
}
}
return 0;
}
DWORD cPortal::FindGraphic(DWORD ID, std::vector<stPaletteSwap> *vPaletteSwaps, float fTransTex, float fTransPix)
{
//Pull it out of the cache, if possible...
QWORD Hash = ID | ((QWORD) ID << 32);
int num = 0;
if (vPaletteSwaps)
{
for (std::vector<stPaletteSwap>::iterator i = vPaletteSwaps->begin(); i != vPaletteSwaps->end(); i++)
{
Hash += num*((QWORD) i->newPalette << 40);
Hash += num*((QWORD) i->length << 32);
Hash += num*((QWORD) i->offset << 24);
Hash += num++;
}
}
Hash += (QWORD) (*((DWORD *) &fTransTex))*72;
Hash += (QWORD) (*((DWORD *) &fTransPix))*9727;
if (m_mGraphicCache.find(Hash) != m_mGraphicCache.end())
return m_mGraphicCache[Hash];
//Open object and make sure it exists...
cPortalFile *pfUI = OpenEntry(ID);
if (!pfUI)
return 0;
cByteStream pBS(pfUI->data, pfUI->length);
pBS.ReadBegin();
//Load and parse image
DWORD dwPicID = pBS.ReadDWORD();
DWORD unk = pBS.ReadDWORD();
DWORD sizeX = pBS.ReadDWORD();
DWORD sizeY = pBS.ReadDWORD();
DWORD format = pBS.ReadDWORD();
DWORD length = pBS.ReadDWORD();
if (format == 0x1F4) //JPEG
return 0;
//If it's a palettized image, form the palette and transparencies
DWORD palette = 0;
DWORD *newPalette = 0;
cPortalFile *pfPal = 0;
bool newPalUsed = false;
if ((format == 0x65) || (format == 0x29))
{
//Form the palette we're going to use
memcpy(&palette, pfUI->data+24+length, sizeof(DWORD));
pfPal = OpenEntry(palette);
DWORD palentries = *((DWORD *) (pfPal->data+4));
//Copy a new palette if we have to, otherwise just pull from memory
newPalUsed = true;
newPalette = new DWORD[palentries];
memcpy(newPalette, pfPal->data+8, palentries*sizeof(DWORD));
//Perform paletteswaps
if (vPaletteSwaps)
{
for (std::vector<stPaletteSwap>::iterator i = vPaletteSwaps->begin(); i != vPaletteSwaps->end(); i++)
{
cPortalFile *pfPalNew = m_Portal->OpenEntry(0x04000000 | i->newPalette);
if (!pfPalNew)
return false;
if (format == 0x29) //256 entries
memcpy(&newPalette[i->offset], pfPalNew->data+8+(i->offset*sizeof(DWORD)), (i->length*sizeof(DWORD)));
else //2048 entries
memcpy(&newPalette[i->offset*8], pfPalNew->data+8+(i->offset*sizeof(DWORD))*8, (i->length*sizeof(DWORD))*8);
}
}
//Overall Transparency
DWORD tpf = ((DWORD) (0xFF * (1.0f-fTransTex))) << 24;
for (DWORD i=0;i<palentries;i++)
newPalette[i] = (newPalette[i] & 0x00FFFFFF) | tpf;
//Color key 0 (0-7 for 2048-entry palettes, cuz turbine is gay)
tpf = ((DWORD) (0xFF * (1.0f-fTransPix))) << 24;
if (format == 0x29)
{
newPalette[0] = (newPalette[0] & 0x00FFFFFF) | tpf;
}
else
{
for (int l=0;l<8;l++)
newPalette[l] = (newPalette[l] & 0x00FFFFFF) | tpf;
}
}
//Figure out size of actual texture to store (must be powers of 2)
int powx = HighestPowerOfTwo(sizeX), powy = HighestPowerOfTwo(sizeY);
DWORD sizeX2 = 1 << powx;
while (sizeX2 < sizeX)
sizeX2 <<= 1;
DWORD sizeY2 = 1 << powy;
while (sizeY2 < sizeY)
sizeY2 <<= 1;
//Disable DXT textures for termserv
//if (format >= 0x31545844) return 0;
//Generate the OpenGL texture
GLuint dwid = 0;
glGenTextures( 1, &dwid );
glBindTexture(GL_TEXTURE_2D, dwid);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
if ((sizeX2 == sizeX) && (sizeY2 == sizeY) && (format != 0x65) && (format != 0x29))
{
//If it's just immediately loadable without any processing, do it!
if (format == 0x31545844)
glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, sizeX2,sizeY2, 0, length, pfUI->data+24);
else if (format == 0x35545844)
glCompressedTexImage2DARB(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, sizeX2,sizeY2, 0, length, pfUI->data+24);
else if (format == 0x15)
glTexImage2D(GL_TEXTURE_2D, 0, 4, sizeX2,sizeY2, 0, GL_BGRA, GL_UNSIGNED_BYTE, pfUI->data+24);
else if (format == 0xF3)
glTexImage2D(GL_TEXTURE_2D, 0, 3, sizeX2,sizeY2, 0, GL_RGB, GL_UNSIGNED_BYTE, pfUI->data+24);
else if (format == 0x14)
glTexImage2D(GL_TEXTURE_2D, 0, 3, sizeX2,sizeY2, 0, GL_BGR, GL_UNSIGNED_BYTE, pfUI->data+24);
else if ((format == 0x1C) || (format == 0xF4))
glTexImage2D(GL_TEXTURE_2D, 0, 1, sizeX2,sizeY2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pfUI->data+24);
else if ((format == 0x17) || (format == 0x1A)) //16-bit grayscale?
glTexImage2D(GL_TEXTURE_2D, 0, 1, sizeX2,sizeY2, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, pfUI->data+24);
else
return NULL;
}
else
{
//Padding will be required, unfortunately...
BYTE *tex = new BYTE[sizeX2*sizeY2*4];
ZeroMemory(tex, sizeX2*sizeY2*4);
for (DWORD y=0;y<sizeY2;y++)
{
for (DWORD x=0;x<sizeX2;x++)
{
if ((x < sizeX) && (y < sizeY))
{
//Image formats...
switch (format)
{
case 0x14:
case 0xF3: //RGB (24-bit color)
*((DWORD *) &tex[4*sizeX2*y + x*4]) = *((DWORD *) (pfUI->data+24+(3*(sizeX*y+x)))) | 0xFF000000;
break;
case 0x15: //RGBA (32-bit color)
*((DWORD *) &tex[4*sizeX2*y + x*4]) = *((DWORD *) (pfUI->data+24+(4*(sizeX*y+x))));
break;
case 0x65: //2-byte indexed, palette ID follows encoded data
{
WORD index = *((WORD *)(pfUI->data+24+(2*(sizeX*y+x))));
*((DWORD *) &tex[4*sizeX2*y + x*4]) = newPalette[index]/* & 0x7FFFFFFF*/;
}
break;
case 0x29: //1-byte indexed, palette ID follows encoded data
{
BYTE index = *((BYTE *)(pfUI->data+24+(sizeX*y+x)));
*((DWORD *) &tex[4*sizeX2*y + x*4]) = newPalette[index]/* & 0x7FFFFFFF*/;
}
break;
case 0x1C: //1-byte grayscale
case 0xF4:
{
DWORD index = *((BYTE *)(pfUI->data+24+(sizeX*y+x)));
*((DWORD *) &tex[4*sizeX2*y + x*4]) = index | (index << 8) | (index << 16) | 0xFF000000;
}
break;
default:
{
return NULL;
}
}
}
else
*((DWORD *) &tex[4*sizeX2*y + x*4]) = 0x00000000;
}
}
glTexImage2D(GL_TEXTURE_2D, 0, 4, sizeX2,sizeY2, 0, GL_BGRA, GL_UNSIGNED_BYTE, tex);
delete []tex;
}
//Store info to our caches
POINTf tpoint( (float) sizeX/sizeX2, (float) sizeY/sizeY2 );
m_mGraphicEdgeCache[ID] = tpoint;
m_mGraphicCache[Hash] = dwid;
//Clean up
if (newPalUsed)
delete []newPalette;
return dwid;
}
POINTf cPortal::GetGraphicEdges(DWORD ID)
{
if (m_mGraphicEdgeCache.find(ID) == m_mGraphicEdgeCache.end())
{
POINTf tpzero( 0, 0 );
return tpzero;
}
return m_mGraphicEdgeCache[ID];
}
DWORD cPortal::GetPoolSize()
{
DWORD size = 0;
if (m_tfPortalHighRes) size += m_tfPortalHighRes->GetPoolSize();
size += m_tfPortal->GetPoolSize();
return size;
}