-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbspfile_rbsp.c
364 lines (279 loc) · 11.9 KB
/
bspfile_rbsp.c
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
----------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define BSPFILE_RBSP_C
/* dependencies */
#include "kmap2.h"
/* -------------------------------------------------------------------------------
this file handles translating the bsp file format used by quake 3, rtcw, and ef
into the abstracted bsp file used by xmap2.
------------------------------------------------------------------------------- */
/* constants */
#define LUMP_ENTITIES 0
#define LUMP_SHADERS 1
#define LUMP_PLANES 2
#define LUMP_NODES 3
#define LUMP_LEAFS 4
#define LUMP_LEAFSURFACES 5
#define LUMP_LEAFBRUSHES 6
#define LUMP_MODELS 7
#define LUMP_BRUSHES 8
#define LUMP_BRUSHSIDES 9
#define LUMP_DRAWVERTS 10
#define LUMP_DRAWINDEXES 11
#define LUMP_FOGS 12
#define LUMP_SURFACES 13
#define LUMP_LIGHTMAPS 14
#define LUMP_LIGHTGRID 15
#define LUMP_VISIBILITY 16
#define LUMP_LIGHTARRAY 17
#define HEADER_LUMPS 18
/* types */
typedef struct
{
char ident[4];
int version;
bspLump_t lumps[HEADER_LUMPS];
}
rbspHeader_t;
/* light grid */
#define MAX_MAP_GRID 0xffff
#define MAX_MAP_GRIDARRAY 0x100000
#define LG_EPSILON 4
static void CopyLightGridLumps(rbspHeader_t * header)
{
int i;
unsigned short *inArray;
bspGridPoint_t *in, *out;
/* get count */
numBSPGridPoints = GetLumpElements((bspHeader_t *) header, LUMP_LIGHTARRAY, sizeof(*inArray));
/* allocate buffer */
bspGridPoints = safe_malloc(numBSPGridPoints * sizeof(*bspGridPoints));
memset(bspGridPoints, 0, numBSPGridPoints * sizeof(*bspGridPoints));
/* copy */
inArray = GetLump((bspHeader_t *) header, LUMP_LIGHTARRAY);
in = GetLump((bspHeader_t *) header, LUMP_LIGHTGRID);
out = bspGridPoints;
for(i = 0; i < numBSPGridPoints; i++)
{
memcpy(out, &in[*inArray], sizeof(*in));
inArray++;
out++;
}
}
static void AddLightGridLumps(FILE * file, rbspHeader_t * header)
{
int i, j, k, c, d;
int numGridPoints, maxGridPoints;
bspGridPoint_t *gridPoints, *in, *out;
int numGridArray;
unsigned short *gridArray;
qboolean bad;
/* allocate temporary buffers */
maxGridPoints = (numBSPGridPoints < MAX_MAP_GRID) ? numBSPGridPoints : MAX_MAP_GRID;
gridPoints = safe_malloc(maxGridPoints * sizeof(*gridPoints));
gridArray = safe_malloc(numBSPGridPoints * sizeof(*gridArray));
/* zero out */
numGridPoints = 0;
numGridArray = numBSPGridPoints;
/* for each bsp grid point, find an approximate twin */
Sys_Printf("Storing lightgrid: %d points\n", numBSPGridPoints);
for(i = 0; i < numGridArray; i++)
{
/* get points */
in = &bspGridPoints[i];
/* walk existing list */
for(j = 0; j < numGridPoints; j++)
{
/* get point */
out = &gridPoints[j];
#ifdef KMAP2_NO_TYPE_PUNNING
//FIXME (0xA5EA): depends on MAX_LIGHTMAPS define which is currently 4
if (!Vec4_Comp(in->styles, out->styles))
continue;
#else
/* compare styles */
if(*((unsigned int *)in->styles) != *((unsigned int *)out->styles))
continue;
#endif
/* compare direction */
d = abs(in->latLong[0] - out->latLong[0]);
if(d < (255 - LG_EPSILON) && d > LG_EPSILON)
continue;
d = abs(in->latLong[1] - out->latLong[1]);
if(d < 255 - LG_EPSILON && d > LG_EPSILON)
continue;
/* compare light */
bad = qfalse;
for(k = 0; (k < MAX_LIGHTMAPS && bad == qfalse); k++)
{
for(c = 0; c < 3; c++)
{
if(abs((int)in->ambient[k][c] - (int)out->ambient[k][c]) > LG_EPSILON ||
abs((int)in->directed[k][c] - (int)out->directed[k][c]) > LG_EPSILON)
{
bad = qtrue;
break;
}
}
}
/* failure */
if(bad)
continue;
/* this sample is ok */
break;
}
/* set sample index */
gridArray[i] = (unsigned short)j;
/* if no sample found, add a new one */
if(j >= numGridPoints && numGridPoints < maxGridPoints)
{
out = &gridPoints[numGridPoints++];
memcpy(out, in, sizeof(*in));
}
}
/* swap array */
for(i = 0; i < numGridArray; i++)
gridArray[i] = LittleShort(gridArray[i]);
/* write lumps */
AddLump(file, (bspHeader_t *) header, LUMP_LIGHTGRID, gridPoints, (numGridPoints * sizeof(*gridPoints)));
AddLump(file, (bspHeader_t *) header, LUMP_LIGHTARRAY, gridArray, (numGridArray * sizeof(*gridArray)));
/* free buffers */
free(gridPoints);
free(gridArray);
}
/*
LoadRBSPFile()
loads a raven bsp file into memory
*/
void LoadRBSPFile(const char *filename)
{
rbspHeader_t *header;
/* load the file header */
LoadFile(filename, (void **)&header);
/* swap the header (except the first 4 bytes) */
SwapBlock((int *)((byte *) header + sizeof(int)), sizeof(*header) - sizeof(int));
#ifdef KMAP2_NO_TYPE_PUNNING
if(force == qfalse && !Vec4_Comp(header->ident, game->bspIdent))
Error("%s is not a %s file", filename, game->bspIdent);
#else
/* make sure it matches the format we're trying to load */
if(force == qfalse && *((int *)header->ident) != *((int *)game->bspIdent))
Error("%s is not a %s file", filename, game->bspIdent);
#endif
if(force == qfalse && header->version != game->bspVersion)
Error("%s is version %d, not %d", filename, header->version, game->bspVersion);
/* load/convert lumps */
numBSPShaders =
CopyLump_Allocate((bspHeader_t *) header, LUMP_SHADERS, (void **)&bspShaders, sizeof(bspShader_t), &allocatedBSPShaders);
numBSPModels =
CopyLump_Allocate((bspHeader_t *) header, LUMP_MODELS, (void **)&bspModels, sizeof(bspModel_t), &allocatedBSPModels);
numBSPPlanes =
CopyLump_Allocate((bspHeader_t *) header, LUMP_PLANES, (void **)&bspPlanes, sizeof(bspPlane_t), &allocatedBSPPlanes);
numBSPLeafs = CopyLump((bspHeader_t *) header, LUMP_LEAFS, bspLeafs, sizeof(bspLeaf_t));
numBSPNodes =
CopyLump_Allocate((bspHeader_t *) header, LUMP_NODES, (void **)&bspNodes, sizeof(bspNode_t), &allocatedBSPNodes);
numBSPLeafSurfaces =
CopyLump_Allocate((bspHeader_t *) header, LUMP_LEAFSURFACES, (void **)&bspLeafSurfaces, sizeof(bspLeafSurfaces[0]),
&allocatedBSPLeafSurfaces);
numBSPLeafBrushes =
CopyLump_Allocate((bspHeader_t *) header, LUMP_LEAFBRUSHES, (void **)&bspLeafBrushes, sizeof(bspLeafBrushes[0]),
&allocatedBSPLeafBrushes);
numBSPBrushes =
CopyLump_Allocate((bspHeader_t *) header, LUMP_BRUSHES, (void **)&bspBrushes, sizeof(bspBrush_t),
&allocatedBSPLeafBrushes);
numBSPBrushSides =
CopyLump_Allocate((bspHeader_t *) header, LUMP_BRUSHSIDES, (void **)&bspBrushSides, sizeof(bspBrushSide_t),
&allocatedBSPBrushSides);
numBSPDrawVerts = GetLumpElements((bspHeader_t *) header, LUMP_DRAWVERTS, sizeof(bspDrawVerts[0]));
SetDrawVerts(numBSPDrawVerts);
CopyLump((bspHeader_t *) header, LUMP_DRAWVERTS, bspDrawVerts, sizeof(bspDrawVerts[0]));
numBSPDrawSurfaces = GetLumpElements((bspHeader_t *) header, LUMP_SURFACES, sizeof(bspDrawSurfaces[0]));
SetDrawSurfaces(numBSPDrawSurfaces);
CopyLump((bspHeader_t *) header, LUMP_SURFACES, bspDrawSurfaces, sizeof(bspDrawSurfaces[0]));
numBSPFogs = CopyLump((bspHeader_t *) header, LUMP_FOGS, bspFogs, sizeof(bspFogs[0]));
numBSPDrawIndexes = CopyLump((bspHeader_t *) header, LUMP_DRAWINDEXES, bspDrawIndexes, sizeof(bspDrawIndexes[0]));
numBSPVisBytes = CopyLump((bspHeader_t *) header, LUMP_VISIBILITY, bspVisBytes, 1);
numBSPLightBytes = GetLumpElements((bspHeader_t *) header, LUMP_LIGHTMAPS, 1);
bspLightBytes = safe_malloc(numBSPLightBytes);
CopyLump((bspHeader_t *) header, LUMP_LIGHTMAPS, bspLightBytes, 1);
bspEntDataSize = CopyLump_Allocate((bspHeader_t *) header, LUMP_ENTITIES, (void **)&bspEntData, 1, &allocatedBSPEntData);
CopyLightGridLumps(header);
/* free the file buffer */
free(header);
}
/*
WriteRBSPFile()
writes a raven bsp file
*/
void WriteRBSPFile(const char *filename)
{
rbspHeader_t outheader, *header;
FILE *file;
time_t t;
char marker[1024];
int size;
/* set header */
header = &outheader;
memset(header, 0, sizeof(*header));
//% Swapfile();
#ifdef KMAP2_NO_TYPE_PUNNING
/* set up header */
Vec4_Copy(game->bspIdent, header->ident);
header->version = LittleLong(game->bspVersion);
#else
/* set up header */
*((int *)(bspHeader_t *) header->ident) = *((int *)game->bspIdent);
header->version = LittleLong(game->bspVersion);
#endif
/* write initial header */
file = SafeOpenWrite(filename);
SafeWrite(file, (bspHeader_t *) header, sizeof(*header)); /* overwritten later */
/* add marker lump */
time(&t);
sprintf(marker, "I LOVE MY KMAP2 %s on %s)", KMAP_VERSION, asctime(localtime(&t)));
AddLump(file, (bspHeader_t *) header, 0, marker, strlen(marker) + 1);
/* add lumps */
AddLump(file, (bspHeader_t *) header, LUMP_SHADERS, bspShaders, numBSPShaders * sizeof(bspShader_t));
AddLump(file, (bspHeader_t *) header, LUMP_PLANES, bspPlanes, numBSPPlanes * sizeof(bspPlane_t));
AddLump(file, (bspHeader_t *) header, LUMP_LEAFS, bspLeafs, numBSPLeafs * sizeof(bspLeaf_t));
AddLump(file, (bspHeader_t *) header, LUMP_NODES, bspNodes, numBSPNodes * sizeof(bspNode_t));
AddLump(file, (bspHeader_t *) header, LUMP_BRUSHES, bspBrushes, numBSPBrushes * sizeof(bspBrush_t));
AddLump(file, (bspHeader_t *) header, LUMP_BRUSHSIDES, bspBrushSides, numBSPBrushSides * sizeof(bspBrushSides[0]));
AddLump(file, (bspHeader_t *) header, LUMP_LEAFSURFACES, bspLeafSurfaces, numBSPLeafSurfaces * sizeof(bspLeafSurfaces[0]));
AddLump(file, (bspHeader_t *) header, LUMP_LEAFBRUSHES, bspLeafBrushes, numBSPLeafBrushes * sizeof(bspLeafBrushes[0]));
AddLump(file, (bspHeader_t *) header, LUMP_MODELS, bspModels, numBSPModels * sizeof(bspModel_t));
AddLump(file, (bspHeader_t *) header, LUMP_DRAWVERTS, bspDrawVerts, numBSPDrawVerts * sizeof(bspDrawVerts[0]));
AddLump(file, (bspHeader_t *) header, LUMP_SURFACES, bspDrawSurfaces, numBSPDrawSurfaces * sizeof(bspDrawSurfaces[0]));
AddLump(file, (bspHeader_t *) header, LUMP_VISIBILITY, bspVisBytes, numBSPVisBytes);
AddLump(file, (bspHeader_t *) header, LUMP_LIGHTMAPS, bspLightBytes, numBSPLightBytes);
AddLightGridLumps(file, header);
AddLump(file, (bspHeader_t *) header, LUMP_ENTITIES, bspEntData, bspEntDataSize);
AddLump(file, (bspHeader_t *) header, LUMP_FOGS, bspFogs, numBSPFogs * sizeof(bspFog_t));
AddLump(file, (bspHeader_t *) header, LUMP_DRAWINDEXES, bspDrawIndexes, numBSPDrawIndexes * sizeof(bspDrawIndexes[0]));
/* emit bsp size */
size = ftell(file);
Sys_Printf("Wrote %.1f MB (%d bytes)\n", (float)size / (1024 * 1024), size);
/* write the completed header */
fseek(file, 0, SEEK_SET);
SafeWrite(file, header, sizeof(*header));
/* close the file */
fclose(file);
}