Source

qutils / qbsp / writebsp.c

Full commit
  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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <stdarg.h>
#include <unistd.h>
#include "../common/cmdlib.h"
#include "../common/mathlib.h"
#include "../common/bspfile.h"
#include "bsp5.h"
#include "map.h"

int headclipnode;
int firstface;

//===========================================================================

/*
==================
FindFinalPlane

Used to find plane index numbers for clip nodes read from child processes
==================
*/
int
FindFinalPlane(dplane_t * p)
{
	int i;
	dplane_t *dplane;

	for(i = 0, dplane = dplanes; i < numplanes; i++, dplane++) {
		if(p->type != dplane->type)
			continue;
		if(p->dist != dplane->dist)
			continue;
		if(p->normal[0] != dplane->normal[0])
			continue;
		if(p->normal[1] != dplane->normal[1])
			continue;
		if(p->normal[2] != dplane->normal[2])
			continue;
		return i;
	}

//
// new plane
//
	if(numplanes == MAX_MAP_PLANES)
		Error("numplanes == MAX_MAP_PLANES");
	dplane = &dplanes[numplanes];
	*dplane = *p;
	numplanes++;

	return numplanes - 1;
}



int planemapping[MAX_MAP_PLANES];

void
WriteNodePlanes_r(node_t * node)
{
	plane_t *plane;
	dplane_t *dplane;

	if(node->planenum == -1)
		return;
	if(planemapping[node->planenum] == -1) {	// a new plane
		planemapping[node->planenum] = numplanes;

		if(numplanes == MAX_MAP_PLANES)
			Error("numplanes == MAX_MAP_PLANES");
		plane = &planes[node->planenum];
		dplane = &dplanes[numplanes];
		dplane->normal[0] = plane->normal[0];
		dplane->normal[1] = plane->normal[1];
		dplane->normal[2] = plane->normal[2];
		dplane->dist = plane->dist;
		dplane->type = plane->type;

		numplanes++;
	}

	node->outputplanenum = planemapping[node->planenum];

	WriteNodePlanes_r(node->children[0]);
	WriteNodePlanes_r(node->children[1]);
}

/*
==================
WriteNodePlanes

==================
*/
void
WriteNodePlanes(node_t * nodes)
{
	memset(planemapping, -1, sizeof(planemapping));
	WriteNodePlanes_r(nodes);
}

//===========================================================================

/*
==================
WriteClipNodes_r

==================
*/
int
WriteClipNodes_r(node_t * node)
{
	int i, c;
	dclipnode_t *cn;
	int num;

// FIXME: free more stuff?  
	if(node->planenum == -1) {
		num = node->contents;
		free(node);
		return num;
	}
// emit a clipnode
	c = numclipnodes;
	cn = &dclipnodes[numclipnodes];
	numclipnodes++;
	cn->planenum = node->outputplanenum;
	for(i = 0; i < 2; i++)
		cn->children[i] = WriteClipNodes_r(node->children[i]);

	free(node);
	return c;
}

/*
==================
WriteClipNodes

Called after the clipping hull is completed.  Generates a disk format
representation and frees the original memory.
==================
*/
void
WriteClipNodes(node_t * nodes)
{
	headclipnode = numclipnodes;
	WriteClipNodes_r(nodes);
}

//===========================================================================

/*
==================
WriteLeaf
==================
*/
void
WriteLeaf(node_t * node)
{
	face_t **fp, *f;
	dleaf_t *leaf_p;

// emit a leaf
	leaf_p = &dleafs[numleafs];
	numleafs++;

	leaf_p->contents = node->contents;

//
// write bounding box info
//  
	VectorCopy(node->mins, leaf_p->mins);
	VectorCopy(node->maxs, leaf_p->maxs);

	leaf_p->visofs = -1;		// no vis info yet

//
// write the marksurfaces
//
	leaf_p->firstmarksurface = nummarksurfaces;

	for(fp = node->markfaces; *fp; fp++) {
		// emit a marksurface
		if(nummarksurfaces == MAX_MAP_MARKSURFACES)
			Error("nummarksurfaces == MAX_MAP_MARKSURFACES");
		f = *fp;
		do {
			dmarksurfaces[nummarksurfaces] = f->outputnumber;
			nummarksurfaces++;
			f = f->original;	// grab tjunction split faces
		} while(f);
	}

	leaf_p->nummarksurfaces = nummarksurfaces - leaf_p->firstmarksurface;
}


/*
==================
WriteDrawNodes_r
==================
*/
void
WriteDrawNodes_r(node_t * node)
{
	dnode_t *n;
	int i;

// emit a node  
	if(numnodes == MAX_MAP_NODES)
		Error("numnodes == MAX_MAP_NODES");
	n = &dnodes[numnodes];
	numnodes++;

	VectorCopy(node->mins, n->mins);
	VectorCopy(node->maxs, n->maxs);

	n->planenum = node->outputplanenum;
	n->firstface = node->firstface;
	n->numfaces = node->numfaces;

//
// recursively output the other nodes
//  

	for(i = 0; i < 2; i++) {
		if(node->children[i]->planenum == -1) {
			if(node->children[i]->contents == CONTENTS_SOLID)
				n->children[i] = -1;
			else {
				n->children[i] = -(numleafs + 1);
				WriteLeaf(node->children[i]);
			}
		} else {
			n->children[i] = numnodes;
			WriteDrawNodes_r(node->children[i]);
		}
	}
}

/*
==================
WriteDrawNodes
==================
*/
void
WriteDrawNodes(node_t * headnode)
{
	int i;
	int start;
	dmodel_t *bm;

#if 0
	if(headnode->contents < 0)
		Error("FinishBSPModel: empty model");
#endif

// emit a model
	if(nummodels == MAX_MAP_MODELS)
		Error("nummodels == MAX_MAP_MODELS");
	bm = &dmodels[nummodels];
	nummodels++;

	bm->headnode[0] = numnodes;
	bm->firstface = firstface;
	bm->numfaces = numfaces - firstface;
	firstface = numfaces;

	start = numleafs;

	if(headnode->contents < 0)
		WriteLeaf(headnode);
	else
		WriteDrawNodes_r(headnode);
	bm->visleafs = numleafs - start;

	for(i = 0; i < 3; i++) {
		bm->mins[i] = headnode->mins[i] + SIDESPACE + 1;	// remove the padding
		bm->maxs[i] = headnode->maxs[i] - SIDESPACE - 1;
	}
// FIXME: are all the children decendant of padded nodes?
}


/*
==================
BumpModel

Used by the clipping hull processes that only need to store headclipnode
==================
*/
void
BumpModel(int hullnum)
{
	dmodel_t *bm;

// emit a model
	if(nummodels == MAX_MAP_MODELS)
		Error("nummodels == MAX_MAP_MODELS");
	bm = &dmodels[nummodels];
	nummodels++;

	bm->headnode[hullnum] = headclipnode;
}

//=============================================================================

typedef struct {
	char identification[4];		// should be WAD2
	int numlumps;
	int infotableofs;
} wadinfo_t;


typedef struct {
	int filepos;
	int disksize;
	int size;					// uncompressed
	char type;
	char compression;
	char pad1, pad2;
	char name[16];				// must be null terminated
} lumpinfo_t;

FILE *texfile;
wadinfo_t wadinfo;
lumpinfo_t *lumpinfo;

void
CleanupName(char *in, char *out)
{
	int i;

	for(i = 0; i < 16; i++) {
		if(!in[i])
			break;

		out[i] = toupper(in[i]);
	}

	for(; i < 16; i++)
		out[i] = 0;
}


/*
=================
TEX_InitFromWad
=================
*/
void
TEX_InitFromWad(char *path)
{
	int i;

	texfile = SafeOpenRead(path);
	SafeRead(texfile, &wadinfo, sizeof(wadinfo));
	if(strncmp(wadinfo.identification, "WAD2", 4))
		Error("TEX_InitFromWad: %s isn't a wadfile", path);
	wadinfo.numlumps = LittleLong(wadinfo.numlumps);
	wadinfo.infotableofs = LittleLong(wadinfo.infotableofs);
	fseek(texfile, wadinfo.infotableofs, SEEK_SET);
	lumpinfo = malloc(wadinfo.numlumps * sizeof(lumpinfo_t));
	SafeRead(texfile, lumpinfo, wadinfo.numlumps * sizeof(lumpinfo_t));

	for(i = 0; i < wadinfo.numlumps; i++) {
		CleanupName(lumpinfo[i].name, lumpinfo[i].name);
		lumpinfo[i].filepos = LittleLong(lumpinfo[i].filepos);
		lumpinfo[i].disksize = LittleLong(lumpinfo[i].disksize);
	}
}

/*
==================
LoadLump
==================
*/
int
LoadLump(char *name, byte * dest)
{
	int i;
	char cname[16];

	CleanupName(name, cname);

	for(i = 0; i < wadinfo.numlumps; i++) {
		if(!strcmp(cname, lumpinfo[i].name)) {
			fseek(texfile, lumpinfo[i].filepos, SEEK_SET);
			SafeRead(texfile, dest, lumpinfo[i].disksize);
			return lumpinfo[i].disksize;
		}
	}

	printf("WARNING: texture %s not found\n", name);
	return 0;
}


/*
==================
AddAnimatingTextures
==================
*/
void
AddAnimatingTextures(void)
{
	int base;
	int i, j, k;
	char name[32];

	base = nummiptex;

	for(i = 0; i < base; i++) {
		if(miptex[i][0] != '+')
			continue;
		strcpy(name, miptex[i]);

		for(j = 0; j < 20; j++) {
			if(j < 10)
				name[1] = '0' + j;
			else
				name[1] = 'A' + j - 10;	// alternate animation


			// see if this name exists in the wadfile
			for(k = 0; k < wadinfo.numlumps; k++)
				if(!strcmp(name, lumpinfo[k].name)) {
					FindMiptex(name);	// add to the miptex list
					break;
				}
		}
	}

	printf("added %i texture frames\n", nummiptex - base);
}

/*
==================
WriteMiptex
==================
*/
void
WriteMiptex(void)
{
	int i, len;
	byte *data;
	dmiptexlump_t *l;
	char *path;
	char fullpath[1024];

	path = ValueForKey(&entities[0], "_wad");
	if(!path || !path[0]) {
		path = ValueForKey(&entities[0], "wad");
		if(!path || !path[0]) {
			printf("WARNING: no wadfile specified\n");
			texdatasize = 0;
			return;
		}
	}

	sprintf(fullpath, "%s/%s", gamedir, path);

	TEX_InitFromWad(fullpath);

	AddAnimatingTextures();

	l = (dmiptexlump_t *) dtexdata;
	data = (byte *) & l->dataofs[nummiptex];
	l->nummiptex = nummiptex;
	for(i = 0; i < nummiptex; i++) {
		l->dataofs[i] = data - (byte *) l;
		len = LoadLump(miptex[i], data);
		if(data + len - dtexdata >= MAX_MAP_MIPTEX)
			Error("Textures exceeded MAX_MAP_MIPTEX");
		if(!len)
			l->dataofs[i] = -1;	// didn't find the texture
		data += len;
	}

	texdatasize = data - dtexdata;
}

//===========================================================================


/*
==================
BeginBSPFile
==================
*/
void
BeginBSPFile(void)
{
// edge 0 is not used, because 0 can't be negated
	numedges = 1;

// leaf 0 is common solid with no faces
	numleafs = 1;
	dleafs[0].contents = CONTENTS_SOLID;

	firstface = 0;
}


/*
==================
FinishBSPFile
==================
*/
void
FinishBSPFile(void)
{
	printf("--- FinishBSPFile ---\n");
	printf("WriteBSPFile: %s\n", bspfilename);

	WriteMiptex();

	PrintBSPFileSizes();
	WriteBSPFile(bspfilename);
}