| 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 |
#define BSPFILE_RBSP_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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_LIGHTARRAY 17 |
| 68 |
#define HEADER_LUMPS 18 |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
typedef struct |
| 73 |
{ |
| 74 |
char ident[ 4 ]; |
| 75 |
int version; |
| 76 |
|
| 77 |
bspLump_t lumps[ HEADER_LUMPS ]; |
| 78 |
} |
| 79 |
rbspHeader_t; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define MAX_MAP_GRID 0xffff |
| 85 |
#define MAX_MAP_GRIDARRAY 0x100000 |
| 86 |
#define LG_EPSILON 4 |
| 87 |
|
| 88 |
|
| 89 |
static void CopyLightGridLumps( rbspHeader_t *header ){ |
| 90 |
int i; |
| 91 |
unsigned short *inArray; |
| 92 |
bspGridPoint_t *in, *out; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
numBSPGridPoints = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTARRAY, sizeof( *inArray ) ); |
| 99 |
|
| 100 |
|
| 101 |
bspGridPoints = safe_malloc0( numBSPGridPoints * sizeof( *bspGridPoints ) ); |
| 102 |
|
| 103 |
|
| 104 |
inArray = GetLump( (bspHeader_t*) header, LUMP_LIGHTARRAY ); |
| 105 |
in = GetLump( (bspHeader_t*) header, LUMP_LIGHTGRID ); |
| 106 |
out = bspGridPoints; |
| 107 |
for ( i = 0; i < numBSPGridPoints; i++ ) |
| 108 |
{ |
| 109 |
memcpy( out, &in[ *inArray ], sizeof( *in ) ); |
| 110 |
inArray++; |
| 111 |
out++; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
static void AddLightGridLumps( FILE *file, rbspHeader_t *header ){ |
| 117 |
int i, j, k, c, d; |
| 118 |
int numGridPoints, maxGridPoints; |
| 119 |
bspGridPoint_t *gridPoints, *in, *out; |
| 120 |
int numGridArray; |
| 121 |
unsigned short *gridArray; |
| 122 |
qboolean bad; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
maxGridPoints = ( numBSPGridPoints < MAX_MAP_GRID ) ? numBSPGridPoints : MAX_MAP_GRID; |
| 127 |
gridPoints = safe_malloc( maxGridPoints * sizeof( *gridPoints ) ); |
| 128 |
gridArray = safe_malloc( numBSPGridPoints * sizeof( *gridArray ) ); |
| 129 |
|
| 130 |
|
| 131 |
numGridPoints = 0; |
| 132 |
numGridArray = numBSPGridPoints; |
| 133 |
|
| 134 |
|
| 135 |
Sys_Printf( "Storing lightgrid: %d points\n", numBSPGridPoints ); |
| 136 |
for ( i = 0; i < numGridArray; i++ ) |
| 137 |
{ |
| 138 |
|
| 139 |
in = &bspGridPoints[ i ]; |
| 140 |
|
| 141 |
|
| 142 |
for ( j = 0; j < numGridPoints; j++ ) |
| 143 |
{ |
| 144 |
|
| 145 |
out = &gridPoints[ j ]; |
| 146 |
|
| 147 |
|
| 148 |
if ( memcmp( in->styles, out->styles, MAX_LIGHTMAPS ) ) { |
| 149 |
continue; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
d = abs( in->latLong[ 0 ] - out->latLong[ 0 ] ); |
| 154 |
if ( d < ( 255 - LG_EPSILON ) && d > LG_EPSILON ) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
d = abs( in->latLong[ 1 ] - out->latLong[ 1 ] ); |
| 158 |
if ( d < 255 - LG_EPSILON && d > LG_EPSILON ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
bad = qfalse; |
| 164 |
for ( k = 0; ( k < MAX_LIGHTMAPS && bad == qfalse ); k++ ) |
| 165 |
{ |
| 166 |
for ( c = 0; c < 3; c++ ) |
| 167 |
{ |
| 168 |
if ( abs( (int) in->ambient[ k ][ c ] - (int) out->ambient[ k ][ c ] ) > LG_EPSILON || |
| 169 |
abs( (int) in->directed[ k ][ c ] - (int) out->directed[ k ][ c ] ) > LG_EPSILON ) { |
| 170 |
bad = qtrue; |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
if ( bad ) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
gridArray[ i ] = (unsigned short) j; |
| 187 |
|
| 188 |
|
| 189 |
if ( j >= numGridPoints && numGridPoints < maxGridPoints ) { |
| 190 |
out = &gridPoints[ numGridPoints++ ]; |
| 191 |
memcpy( out, in, sizeof( *in ) ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
for ( i = 0; i < numGridArray; i++ ) |
| 197 |
gridArray[ i ] = LittleShort( gridArray[ i ] ); |
| 198 |
|
| 199 |
|
| 200 |
AddLump( file, (bspHeader_t*) header, LUMP_LIGHTGRID, gridPoints, ( numGridPoints * sizeof( *gridPoints ) ) ); |
| 201 |
AddLump( file, (bspHeader_t*) header, LUMP_LIGHTARRAY, gridArray, ( numGridArray * sizeof( *gridArray ) ) ); |
| 202 |
|
| 203 |
|
| 204 |
free( gridPoints ); |
| 205 |
free( gridArray ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
void LoadRBSPFile( const char *filename ){ |
| 216 |
rbspHeader_t *header; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
LoadFile( filename, (void**) &header ); |
| 221 |
|
| 222 |
|
| 223 |
SwapBlock( (int*) ( (byte*) header + sizeof( int ) ), sizeof( *header ) - sizeof( int ) ); |
| 224 |
|
| 225 |
|
| 226 |
if ( force == qfalse && *( (int*) header->ident ) != *( (int*) game->bspIdent ) ) { |
| 227 |
Error( "%s is not a %s file", filename, game->bspIdent ); |
| 228 |
} |
| 229 |
if ( force == qfalse && header->version != game->bspVersion ) { |
| 230 |
Error( "%s is version %d, not %d", filename, header->version, game->bspVersion ); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
numBSPShaders = CopyLump_Allocate( (bspHeader_t*) header, LUMP_SHADERS, (void **) &bspShaders, sizeof( bspShader_t ), &allocatedBSPShaders ); |
| 235 |
|
| 236 |
numBSPModels = CopyLump_Allocate( (bspHeader_t*) header, LUMP_MODELS, (void **) &bspModels, sizeof( bspModel_t ), &allocatedBSPModels ); |
| 237 |
|
| 238 |
numBSPPlanes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_PLANES, (void **) &bspPlanes, sizeof( bspPlane_t ), &allocatedBSPPlanes ); |
| 239 |
|
| 240 |
numBSPLeafs = CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, sizeof( bspLeaf_t ) ); |
| 241 |
|
| 242 |
numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes ); |
| 243 |
|
| 244 |
numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces ); |
| 245 |
|
| 246 |
numBSPLeafBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFBRUSHES, (void **) &bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ), &allocatedBSPLeafBrushes ); |
| 247 |
|
| 248 |
numBSPBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHES, (void **) &bspBrushes, sizeof( bspBrush_t ), &allocatedBSPLeafBrushes ); |
| 249 |
|
| 250 |
numBSPBrushSides = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHSIDES, (void **) &bspBrushSides, sizeof( bspBrushSide_t ), &allocatedBSPBrushSides ); |
| 251 |
|
| 252 |
numBSPDrawVerts = GetLumpElements( (bspHeader_t*) header, LUMP_DRAWVERTS, sizeof( bspDrawVerts[ 0 ] ) ); |
| 253 |
SetDrawVerts( numBSPDrawVerts ); |
| 254 |
CopyLump( (bspHeader_t*) header, LUMP_DRAWVERTS, bspDrawVerts, sizeof( bspDrawVerts[ 0 ] ) ); |
| 255 |
|
| 256 |
numBSPDrawSurfaces = GetLumpElements( (bspHeader_t*) header, LUMP_SURFACES, sizeof( bspDrawSurfaces[ 0 ] ) ); |
| 257 |
SetDrawSurfaces( numBSPDrawSurfaces ); |
| 258 |
CopyLump( (bspHeader_t*) header, LUMP_SURFACES, bspDrawSurfaces, sizeof( bspDrawSurfaces[ 0 ] ) ); |
| 259 |
|
| 260 |
numBSPFogs = CopyLump( (bspHeader_t*) header, LUMP_FOGS, bspFogs, sizeof( bspFogs[ 0 ] ) ); |
| 261 |
|
| 262 |
numBSPDrawIndexes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_DRAWINDEXES, (void **) &bspDrawIndexes, sizeof( bspDrawIndexes[ 0 ] ), &allocatedBSPDrawIndexes ); |
| 263 |
|
| 264 |
numBSPVisBytes = CopyLump( (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes, 1 ); |
| 265 |
|
| 266 |
numBSPLightBytes = GetLumpElements( (bspHeader_t*) header, LUMP_LIGHTMAPS, 1 ); |
| 267 |
bspLightBytes = safe_malloc( numBSPLightBytes ); |
| 268 |
CopyLump( (bspHeader_t*) header, LUMP_LIGHTMAPS, bspLightBytes, 1 ); |
| 269 |
|
| 270 |
bspEntDataSize = CopyLump_Allocate( (bspHeader_t*) header, LUMP_ENTITIES, (void **) &bspEntData, 1, &allocatedBSPEntData ); |
| 271 |
|
| 272 |
CopyLightGridLumps( header ); |
| 273 |
|
| 274 |
|
| 275 |
free( header ); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
void WriteRBSPFile( const char *filename ){ |
| 286 |
rbspHeader_t outheader, *header; |
| 287 |
FILE *file; |
| 288 |
time_t t; |
| 289 |
int size; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
header = &outheader; |
| 294 |
memset( header, 0, sizeof( *header ) ); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
*( (int*) (bspHeader_t*) header->ident ) = *( (int*) game->bspIdent ); |
| 300 |
header->version = LittleLong( game->bspVersion ); |
| 301 |
|
| 302 |
|
| 303 |
file = SafeOpenWrite( filename ); |
| 304 |
SafeWrite( file, (bspHeader_t*) header, sizeof( *header ) ); |
| 305 |
|
| 306 |
|
| 307 |
const char marker[] = "I LOVE MY Q3MAP2"; |
| 308 |
AddLump( file, (bspHeader_t*) header, 0, marker, strlen( marker ) + 1 ); |
| 309 |
|
| 310 |
|
| 311 |
AddLump( file, (bspHeader_t*) header, LUMP_SHADERS, bspShaders, numBSPShaders * sizeof( bspShader_t ) ); |
| 312 |
AddLump( file, (bspHeader_t*) header, LUMP_PLANES, bspPlanes, numBSPPlanes * sizeof( bspPlane_t ) ); |
| 313 |
AddLump( file, (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, numBSPLeafs * sizeof( bspLeaf_t ) ); |
| 314 |
AddLump( file, (bspHeader_t*) header, LUMP_NODES, bspNodes, numBSPNodes * sizeof( bspNode_t ) ); |
| 315 |
AddLump( file, (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, numBSPBrushes * sizeof( bspBrush_t ) ); |
| 316 |
AddLump( file, (bspHeader_t*) header, LUMP_BRUSHSIDES, bspBrushSides, numBSPBrushSides * sizeof( bspBrushSides[ 0 ] ) ); |
| 317 |
AddLump( file, (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, numBSPLeafSurfaces * sizeof( bspLeafSurfaces[ 0 ] ) ); |
| 318 |
AddLump( file, (bspHeader_t*) header, LUMP_LEAFBRUSHES, bspLeafBrushes, numBSPLeafBrushes * sizeof( bspLeafBrushes[ 0 ] ) ); |
| 319 |
AddLump( file, (bspHeader_t*) header, LUMP_MODELS, bspModels, numBSPModels * sizeof( bspModel_t ) ); |
| 320 |
AddLump( file, (bspHeader_t*) header, LUMP_DRAWVERTS, bspDrawVerts, numBSPDrawVerts * sizeof( bspDrawVerts[ 0 ] ) ); |
| 321 |
AddLump( file, (bspHeader_t*) header, LUMP_SURFACES, bspDrawSurfaces, numBSPDrawSurfaces * sizeof( bspDrawSurfaces[ 0 ] ) ); |
| 322 |
AddLump( file, (bspHeader_t*) header, LUMP_VISIBILITY, bspVisBytes, numBSPVisBytes ); |
| 323 |
AddLump( file, (bspHeader_t*) header, LUMP_LIGHTMAPS, bspLightBytes, numBSPLightBytes ); |
| 324 |
AddLightGridLumps( file, header ); |
| 325 |
AddLump( file, (bspHeader_t*) header, LUMP_ENTITIES, bspEntData, bspEntDataSize ); |
| 326 |
AddLump( file, (bspHeader_t*) header, LUMP_FOGS, bspFogs, numBSPFogs * sizeof( bspFog_t ) ); |
| 327 |
AddLump( file, (bspHeader_t*) header, LUMP_DRAWINDEXES, bspDrawIndexes, numBSPDrawIndexes * sizeof( bspDrawIndexes[ 0 ] ) ); |
| 328 |
|
| 329 |
|
| 330 |
size = ftell( file ); |
| 331 |
Sys_Printf( "Wrote %.1f MB (%d bytes)\n", (float) size / ( 1024 * 1024 ), size ); |
| 332 |
|
| 333 |
|
| 334 |
fseek( file, 0, SEEK_SET ); |
| 335 |
SafeWrite( file, header, sizeof( *header ) ); |
| 336 |
|
| 337 |
|
| 338 |
fclose( file ); |
| 339 |
} |