]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/bspfile_ibsp.c
1c4c994a894fb5fad23ea32e5b6cc472b1aa503f
[xonotic/netradiant.git] / tools / quake3 / q3map2 / bspfile_ibsp.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
8    GtkRadiant is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GtkRadiant is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GtkRadiant; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22    ----------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define BSPFILE_IBSP_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41
42 /* -------------------------------------------------------------------------------
43
44    this file handles translating the bsp file format used by quake 3, rtcw, and ef
45    into the abstracted bsp file used by q3map2.
46
47    ------------------------------------------------------------------------------- */
48
49 /* constants */
50 #define LUMP_ENTITIES       0
51 #define LUMP_SHADERS        1
52 #define LUMP_PLANES         2
53 #define LUMP_NODES          3
54 #define LUMP_LEAFS          4
55 #define LUMP_LEAFSURFACES   5
56 #define LUMP_LEAFBRUSHES    6
57 #define LUMP_MODELS         7
58 #define LUMP_BRUSHES        8
59 #define LUMP_BRUSHSIDES     9
60 #define LUMP_DRAWVERTS      10
61 #define LUMP_DRAWINDEXES    11
62 #define LUMP_FOGS           12
63 #define LUMP_SURFACES       13
64 #define LUMP_LIGHTMAPS      14
65 #define LUMP_LIGHTGRID      15
66 #define LUMP_VISIBILITY     16
67 #define LUMP_ADVERTISEMENTS 17
68 #define HEADER_LUMPS        18
69
70
71 /* types */
72 typedef struct
73 {
74         char ident[ 4 ];
75         int version;
76
77         bspLump_t lumps[ HEADER_LUMPS ];
78 }
79 ibspHeader_t;
80
81
82
83 /* brush sides */
84 typedef struct
85 {
86         int planeNum;
87         int shaderNum;
88 }
89 ibspBrushSide_t;
90
91
92 static void CopyBrushSidesLump( ibspHeader_t *header ){
93         int i;
94         ibspBrushSide_t *in;
95         bspBrushSide_t  *out;
96
97
98         /* get count */
99         numBSPBrushSides = GetLumpElements( (bspHeader_t*) header, LUMP_BRUSHSIDES, sizeof( *in ) );
100
101         /* copy */
102         in = GetLump( (bspHeader_t*) header, LUMP_BRUSHSIDES );
103         for ( i = 0; i < numBSPBrushSides; i++ )
104         {
105                 AUTOEXPAND_BY_REALLOC( bspBrushSides, i, allocatedBSPBrushSides, 1024 );
106                 out = &bspBrushSides[i];
107                 out->planeNum = in->planeNum;
108                 out->shaderNum = in->shaderNum;
109                 out->surfaceNum = -1;
110                 in++;
111         }
112 }
113
114
115 static void AddBrushSidesLump( FILE *file, ibspHeader_t *header ){
116         int i, size;
117         bspBrushSide_t  *in;
118         ibspBrushSide_t *buffer, *out;
119
120
121         /* allocate output buffer */
122         size = numBSPBrushSides * sizeof( *buffer );
123         buffer = safe_malloc0( size );
124
125         /* convert */
126         in = bspBrushSides;
127         out = buffer;
128         for ( i = 0; i < numBSPBrushSides; i++ )
129         {
130                 out->planeNum = in->planeNum;
131                 out->shaderNum = in->shaderNum;
132                 in++;
133                 out++;
134         }
135
136         /* write lump */
137         AddLump( file, (bspHeader_t*) header, LUMP_BRUSHSIDES, buffer, size );
138
139         /* free buffer */
140         free( buffer );
141 }
142
143
144
145 /* drawsurfaces */
146 typedef struct ibspDrawSurface_s
147 {
148         int shaderNum;
149         int fogNum;
150         int surfaceType;
151
152         int firstVert;
153         int numVerts;
154
155         int firstIndex;
156         int numIndexes;
157
158         int lightmapNum;
159         int lightmapX, lightmapY;
160         int lightmapWidth, lightmapHeight;
161
162         vec3_t lightmapOrigin;
163         vec3_t lightmapVecs[ 3 ];
164
165         int patchWidth;
166         int patchHeight;
167 }
168 ibspDrawSurface_t;
169
170
171 static void CopyDrawSurfacesLump( ibspHeader_t *header ){
172         int i, j;
173         ibspDrawSurface_t   *in;
174         bspDrawSurface_t    *out;
175
176
177         /* get count */
178         numBSPDrawSurfaces = GetLumpElements( (bspHeader_t*) header, LUMP_SURFACES, sizeof( *in ) );
179         SetDrawSurfaces( numBSPDrawSurfaces );
180
181         /* copy */
182         in = GetLump( (bspHeader_t*) header, LUMP_SURFACES );
183         out = bspDrawSurfaces;
184         for ( i = 0; i < numBSPDrawSurfaces; i++ )
185         {
186                 out->shaderNum = in->shaderNum;
187                 out->fogNum = in->fogNum;
188                 out->surfaceType = in->surfaceType;
189                 out->firstVert = in->firstVert;
190                 out->numVerts = in->numVerts;
191                 out->firstIndex = in->firstIndex;
192                 out->numIndexes = in->numIndexes;
193
194                 out->lightmapStyles[ 0 ] = LS_NORMAL;
195                 out->vertexStyles[ 0 ] = LS_NORMAL;
196                 out->lightmapNum[ 0 ] = in->lightmapNum;
197                 out->lightmapX[ 0 ] = in->lightmapX;
198                 out->lightmapY[ 0 ] = in->lightmapY;
199
200                 for ( j = 1; j < MAX_LIGHTMAPS; j++ )
201                 {
202                         out->lightmapStyles[ j ] = LS_NONE;
203                         out->vertexStyles[ j ] = LS_NONE;
204                         out->lightmapNum[ j ] = -3;
205                         out->lightmapX[ j ] = 0;
206                         out->lightmapY[ j ] = 0;
207                 }
208
209                 out->lightmapWidth = in->lightmapWidth;
210                 out->lightmapHeight = in->lightmapHeight;
211
212                 VectorCopy( in->lightmapOrigin, out->lightmapOrigin );
213                 VectorCopy( in->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );
214                 VectorCopy( in->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
215                 VectorCopy( in->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
216
217                 out->patchWidth = in->patchWidth;
218                 out->patchHeight = in->patchHeight;
219
220                 in++;
221                 out++;
222         }
223 }
224
225
226 static void AddDrawSurfacesLump( FILE *file, ibspHeader_t *header ){
227         int i, size;
228         bspDrawSurface_t    *in;
229         ibspDrawSurface_t   *buffer, *out;
230
231
232         /* allocate output buffer */
233         size = numBSPDrawSurfaces * sizeof( *buffer );
234         buffer = safe_malloc0( size );
235
236         /* convert */
237         in = bspDrawSurfaces;
238         out = buffer;
239         for ( i = 0; i < numBSPDrawSurfaces; i++ )
240         {
241                 out->shaderNum = in->shaderNum;
242                 out->fogNum = in->fogNum;
243                 out->surfaceType = in->surfaceType;
244                 out->firstVert = in->firstVert;
245                 out->numVerts = in->numVerts;
246                 out->firstIndex = in->firstIndex;
247                 out->numIndexes = in->numIndexes;
248
249                 out->lightmapNum = in->lightmapNum[ 0 ];
250                 out->lightmapX = in->lightmapX[ 0 ];
251                 out->lightmapY = in->lightmapY[ 0 ];
252                 out->lightmapWidth = in->lightmapWidth;
253                 out->lightmapHeight = in->lightmapHeight;
254
255                 VectorCopy( in->lightmapOrigin, out->lightmapOrigin );
256                 VectorCopy( in->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] );
257                 VectorCopy( in->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] );
258                 VectorCopy( in->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] );
259
260                 out->patchWidth = in->patchWidth;
261                 out->patchHeight = in->patchHeight;
262
263                 in++;
264                 out++;
265         }
266
267         /* write lump */
268         AddLump( file, (bspHeader_t*) header, LUMP_SURFACES, buffer, size );
269
270         /* free buffer */
271         free( buffer );
272 }
273
274
275
276 /* drawverts */
277 typedef struct
278 {
279         vec3_t xyz;
280         float st[ 2 ];
281         float lightmap[ 2 ];
282         vec3_t normal;
283         byte color[ 4 ];
284 }
285 ibspDrawVert_t;
286
287
288 static void CopyDrawVertsLump( ibspHeader_t *header ){
289         int i;
290         ibspDrawVert_t  *in;
291         bspDrawVert_t   *out;
292
293
294         /* get count */
295         numBSPDrawVerts = GetLumpElements( (bspHeader_t*) header, LUMP_DRAWVERTS, sizeof( *in ) );
296         SetDrawVerts( numBSPDrawVerts );
297
298         /* copy */
299         in = GetLump( (bspHeader_t*) header, LUMP_DRAWVERTS );
300         out = bspDrawVerts;
301         for ( i = 0; i < numBSPDrawVerts; i++ )
302         {
303                 VectorCopy( in->xyz, out->xyz );
304                 out->st[ 0 ] = in->st[ 0 ];
305                 out->st[ 1 ] = in->st[ 1 ];
306
307                 out->lightmap[ 0 ][ 0 ] = in->lightmap[ 0 ];
308                 out->lightmap[ 0 ][ 1 ] = in->lightmap[ 1 ];
309
310                 VectorCopy( in->normal, out->normal );
311
312                 out->color[ 0 ][ 0 ] = in->color[ 0 ];
313                 out->color[ 0 ][ 1 ] = in->color[ 1 ];
314                 out->color[ 0 ][ 2 ] = in->color[ 2 ];
315                 out->color[ 0 ][ 3 ] = in->color[ 3 ];
316
317                 in++;
318                 out++;
319         }
320 }
321
322
323 static void AddDrawVertsLump( FILE *file, ibspHeader_t *header ){
324         int i, size;
325         bspDrawVert_t   *in;
326         ibspDrawVert_t  *buffer, *out;
327
328
329         /* allocate output buffer */
330         size = numBSPDrawVerts * sizeof( *buffer );
331         buffer = safe_malloc0( size );
332
333         /* convert */
334         in = bspDrawVerts;
335         out = buffer;
336         for ( i = 0; i < numBSPDrawVerts; i++ )
337         {
338                 VectorCopy( in->xyz, out->xyz );
339                 out->st[ 0 ] = in->st[ 0 ];
340                 out->st[ 1 ] = in->st[ 1 ];
341
342                 out->lightmap[ 0 ] = in->lightmap[ 0 ][ 0 ];
343                 out->lightmap[ 1 ] = in->lightmap[ 0 ][ 1 ];
344
345                 VectorCopy( in->normal, out->normal );
346
347                 out->color[ 0 ] = in->color[ 0 ][ 0 ];
348                 out->color[ 1 ] = in->color[ 0 ][ 1 ];
349                 out->color[ 2 ] = in->color[ 0 ][ 2 ];
350                 out->color[ 3 ] = in->color[ 0 ][ 3 ];
351
352                 in++;
353                 out++;
354         }
355
356         /* write lump */
357         AddLump( file, (bspHeader_t*) header, LUMP_DRAWVERTS, buffer, size );
358
359         /* free buffer */
360         free( buffer );
361 }
362
363
364
365 /* light grid */
366 typedef struct
367 {
368         byte ambient[ 3 ];
369         byte directed[ 3 ];
370         byte latLong[ 2 ];
371 }
372 ibspGridPoint_t;
373
374
375 static void CopyLightGridLumps( ibspHeader_t *header ){
376         int i, j;
377         ibspGridPoint_t *in;
378         bspGridPoint_t  *out;
379
380
381         /* get count */
382         numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTGRID, sizeof( *in ) );
383
384         /* allocate buffer */
385         bspGridPoints = safe_malloc0( numBSPGridPoints * sizeof( *bspGridPoints ) );
386
387         /* copy */
388         in = GetLump( (bspHeader_t*) header, LUMP_LIGHTGRID );
389         out = bspGridPoints;
390         for ( i = 0; i < numBSPGridPoints; i++ )
391         {
392                 for ( j = 0; j < MAX_LIGHTMAPS; j++ )
393                 {
394                         VectorCopy( in->ambient, out->ambient[ j ] );
395                         VectorCopy( in->directed, out->directed[ j ] );
396                         out->styles[ j ] = LS_NONE;
397                 }
398
399                 out->styles[ 0 ] = LS_NORMAL;
400
401                 out->latLong[ 0 ] = in->latLong[ 0 ];
402                 out->latLong[ 1 ] = in->latLong[ 1 ];
403
404                 in++;
405                 out++;
406         }
407 }
408
409
410 static void AddLightGridLumps( FILE *file, ibspHeader_t *header ){
411         int i;
412         bspGridPoint_t  *in;
413         ibspGridPoint_t *buffer, *out;
414
415
416         /* dummy check */
417         if ( bspGridPoints == NULL ) {
418                 return;
419         }
420
421         /* allocate temporary buffer */
422         buffer = safe_malloc( numBSPGridPoints * sizeof( *out ) );
423
424         /* convert */
425         in = bspGridPoints;
426         out = buffer;
427         for ( i = 0; i < numBSPGridPoints; i++ )
428         {
429                 VectorCopy( in->ambient[ 0 ], out->ambient );
430                 VectorCopy( in->directed[ 0 ], out->directed );
431
432                 out->latLong[ 0 ] = in->latLong[ 0 ];
433                 out->latLong[ 1 ] = in->latLong[ 1 ];
434
435                 in++;
436                 out++;
437         }
438
439         /* write lumps */
440         AddLump( file, (bspHeader_t*) header, LUMP_LIGHTGRID, buffer, ( numBSPGridPoints * sizeof( *out ) ) );
441
442         /* free buffer (ydnar 2002-10-22: [bug 641] thanks Rap70r! */
443         free( buffer );
444 }
445
446 /*
447    LoadIBSPFile()
448    loads a quake 3 bsp file into memory
449  */
450
451 void LoadIBSPFile( const char *filename ){
452         ibspHeader_t    *header;
453
454
455         /* load the file header */
456         LoadFile( filename, (void**) &header );
457
458         /* swap the header (except the first 4 bytes) */
459         SwapBlock( (int*) ( (byte*) header + sizeof( int ) ), sizeof( *header ) - sizeof( int ) );
460
461         /* make sure it matches the format we're trying to load */
462         if ( force == qfalse && *( (int*) header->ident ) != *( (int*) game->bspIdent ) ) {
463                 Error( "%s is not a %s file", filename, game->bspIdent );
464         }
465         if ( force == qfalse && header->version != game->bspVersion ) {
466                 Error( "%s is version %d, not %d", filename, header->version, game->bspVersion );
467         }
468
469         /* load/convert lumps */
470         numBSPShaders = CopyLump_Allocate( (bspHeader_t*) header, LUMP_SHADERS, (void **) &bspShaders, sizeof( bspShader_t ), &allocatedBSPShaders );
471
472         numBSPModels = CopyLump_Allocate( (bspHeader_t*) header, LUMP_MODELS, (void **) &bspModels, sizeof( bspModel_t ), &allocatedBSPModels );
473
474         numBSPPlanes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_PLANES, (void **) &bspPlanes, sizeof( bspPlane_t ), &allocatedBSPPlanes );
475
476         numBSPLeafs = CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, sizeof( bspLeaf_t ) ); // TODO fix overflow
477
478         numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes );
479
480         numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces );
481
482         numBSPLeafBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFBRUSHES, (void **) &bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ), &allocatedBSPLeafBrushes );
483
484         numBSPBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHES, (void **) &bspBrushes, sizeof( bspBrush_t ), &allocatedBSPLeafBrushes );
485
486         CopyBrushSidesLump( header );
487
488         CopyDrawVertsLump( header );
489
490         CopyDrawSurfacesLump( header );
491
492         numBSPFogs = CopyLump( (bspHeader_t*) header, LUMP_FOGS, bspFogs, sizeof( bspFog_t ) ); // TODO fix overflow
493
494         numBSPDrawIndexes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_DRAWINDEXES, (void **) &bspDrawIndexes, sizeof( bspDrawIndexes[ 0 ] ), &allocatedBSPDrawIndexes );
495
496         numBSPVisBytes = CopyLump( (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes, 1 ); // TODO fix overflow
497
498         numBSPLightBytes = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTMAPS, 1 ); // TODO change to CopyLump_Allocate
499         bspLightBytes = safe_malloc( numBSPLightBytes );
500         CopyLump( (bspHeader_t*) header, LUMP_LIGHTMAPS, bspLightBytes, 1 );
501
502         bspEntDataSize = CopyLump_Allocate( (bspHeader_t*) header, LUMP_ENTITIES, (void **) &bspEntData, 1, &allocatedBSPEntData );
503
504         CopyLightGridLumps( header );
505
506         /* advertisements */
507         if ( header->version == 47 ) { // quake live's bsp version
508                 numBSPAds = CopyLump( (bspHeader_t*) header, LUMP_ADVERTISEMENTS, bspAds, sizeof( bspAdvertisement_t ) );
509         }
510         else{
511                 numBSPAds = 0;
512         }
513
514         /* free the file buffer */
515         free( header );
516 }
517
518
519
520 /*
521    WriteIBSPFile()
522    writes an id bsp file
523  */
524
525 void WriteIBSPFile( const char *filename ){
526         ibspHeader_t outheader, *header;
527         FILE            *file;
528         time_t t;
529         char marker[ 1024 ];
530         int size;
531
532
533         /* set header */
534         header = &outheader;
535         memset( header, 0, sizeof( *header ) );
536
537         //%     Swapfile();
538
539         /* set up header */
540         *( (int*) (bspHeader_t*) header->ident ) = *( (int*) game->bspIdent );
541         header->version = LittleLong( game->bspVersion );
542
543         /* write initial header */
544         file = SafeOpenWrite( filename );
545         SafeWrite( file, (bspHeader_t*) header, sizeof( *header ) );    /* overwritten later */
546
547         /* add marker lump */
548         time( &t );
549
550         /* asctime adds an implicit trailing \n */
551         sprintf( marker, "I LOVE MY Q3MAP2 %s on %s", Q3MAP_VERSION, asctime( localtime( &t ) ) );
552         AddLump( file, (bspHeader_t*) header, 0, marker, strlen( marker ) + 1 );
553
554         /* add lumps */
555         AddLump( file, (bspHeader_t*) header, LUMP_SHADERS, bspShaders, numBSPShaders * sizeof( bspShader_t ) );
556         AddLump( file, (bspHeader_t*) header, LUMP_PLANES, bspPlanes, numBSPPlanes * sizeof( bspPlane_t ) );
557         AddLump( file, (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, numBSPLeafs * sizeof( bspLeaf_t ) );
558         AddLump( file, (bspHeader_t*) header, LUMP_NODES, bspNodes, numBSPNodes * sizeof( bspNode_t ) );
559         AddLump( file, (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, numBSPBrushes * sizeof( bspBrush_t ) );
560         AddBrushSidesLump( file, header );
561         AddLump( file, (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, numBSPLeafSurfaces * sizeof( bspLeafSurfaces[ 0 ] ) );
562         AddLump( file, (bspHeader_t*) header, LUMP_LEAFBRUSHES, bspLeafBrushes, numBSPLeafBrushes * sizeof( bspLeafBrushes[ 0 ] ) );
563         AddLump( file, (bspHeader_t*) header, LUMP_MODELS, bspModels, numBSPModels * sizeof( bspModel_t ) );
564         AddDrawVertsLump( file, header );
565         AddDrawSurfacesLump( file, header );
566         AddLump( file, (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes, numBSPVisBytes );
567         AddLump( file, (bspHeader_t*) header, LUMP_LIGHTMAPS, bspLightBytes, numBSPLightBytes );
568         AddLightGridLumps( file, header );
569         AddLump( file, (bspHeader_t*) header, LUMP_ENTITIES, bspEntData, bspEntDataSize );
570         AddLump( file, (bspHeader_t*) header, LUMP_FOGS, bspFogs, numBSPFogs * sizeof( bspFog_t ) );
571         AddLump( file, (bspHeader_t*) header, LUMP_DRAWINDEXES, bspDrawIndexes, numBSPDrawIndexes * sizeof( bspDrawIndexes[ 0 ] ) );
572
573         /* advertisements */
574         AddLump( file, (bspHeader_t*) header, LUMP_ADVERTISEMENTS, bspAds, numBSPAds * sizeof( bspAdvertisement_t ) );
575
576         /* emit bsp size */
577         size = ftell( file );
578         Sys_Printf( "Wrote %.1f MB (%d bytes)\n", (float) size / ( 1024 * 1024 ), size );
579
580         /* write the completed header */
581         fseek( file, 0, SEEK_SET );
582         SafeWrite( file, header, sizeof( *header ) );
583
584         /* close the file */
585         fclose( file );
586 }