1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 ----------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
46 #define SNAP_FLOAT_TO_INT 4
47 #define SNAP_INT_TO_FLOAT ( 1.0 / SNAP_FLOAT_TO_INT )
49 static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin ){
60 fprintf( f, "\t// brush %d\n", num );
61 fprintf( f, "\t{\n" );
63 /* clear out build brush */
64 for ( i = 0; i < buildBrush->numsides; i++ )
66 buildSide = &buildBrush->sides[ i ];
67 if ( buildSide->winding != NULL ) {
68 FreeWinding( buildSide->winding );
69 buildSide->winding = NULL;
72 buildBrush->numsides = 0;
74 /* iterate through bsp brush sides */
75 for ( i = 0; i < brush->numSides; i++ )
78 side = &bspBrushSides[ brush->firstSide + i ];
81 if ( side->shaderNum < 0 || side->shaderNum >= numBSPShaders ) {
84 shader = &bspShaders[ side->shaderNum ];
85 if ( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) ) {
90 plane = &bspPlanes[ side->planeNum ];
93 buildSide = &buildBrush->sides[ buildBrush->numsides ];
94 buildBrush->numsides++;
97 buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
98 buildSide->planenum = side->planeNum;
99 buildSide->winding = NULL;
102 /* make brush windings */
103 if ( !CreateBrushWindings( buildBrush ) ) {
107 /* iterate through build brush sides */
108 for ( i = 0; i < buildBrush->numsides; i++ )
111 buildSide = &buildBrush->sides[ i ];
114 if ( buildSide->shaderInfo == NULL || buildSide->winding == NULL ) {
118 /* get texture name */
119 if ( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) ) {
120 texture = buildSide->shaderInfo->shader + 9;
123 texture = buildSide->shaderInfo->shader;
126 /* get plane points and offset by origin */
127 for ( j = 0; j < 3; j++ )
129 VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
130 //% pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
131 //% pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
132 //% pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
135 /* print brush side */
136 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
137 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s 0 0 0 0.5 0.5 0 0 0\n",
138 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
139 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
140 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
145 fprintf( f, "\t}\n\n" );
149 /* iterate through the brush sides (ignore the first 6 bevel planes) */
150 for ( i = 0; i < brush->numSides; i++ )
153 side = &bspBrushSides[ brush->firstSide + i ];
156 if ( side->shaderNum < 0 || side->shaderNum >= numBSPShaders ) {
159 shader = &bspShaders[ side->shaderNum ];
160 if ( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) ) {
164 /* get texture name */
165 if ( !Q_strncasecmp( shader->shader, "textures/", 9 ) ) {
166 texture = shader->shader + 9;
169 texture = shader->shader;
173 plane = &bspPlanes[ side->planeNum ];
175 /* make plane points */
180 MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
181 VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
182 VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
183 VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
186 /* offset by origin */
187 for ( j = 0; j < 3; j++ )
188 VectorAdd( pts[ j ], origin, pts[ j ] );
190 /* print brush side */
191 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
192 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s 0 0 0 0.5 0.5 0 0 0\n",
193 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
194 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
195 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
204 converts a bsp patch to a map patch
212 ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
220 static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin ){
229 if ( ds->surfaceType != MST_PATCH ) {
234 if ( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders ) {
237 shader = &bspShaders[ ds->shaderNum ];
239 /* get texture name */
240 if ( !Q_strncasecmp( shader->shader, "textures/", 9 ) ) {
241 texture = shader->shader + 9;
244 texture = shader->shader;
248 fprintf( f, "\t// patch %d\n", num );
249 fprintf( f, "\t{\n" );
250 fprintf( f, "\t\tpatchDef2\n" );
251 fprintf( f, "\t\t{\n" );
252 fprintf( f, "\t\t\t%s\n", texture );
253 fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
254 fprintf( f, "\t\t\t(\n" );
256 /* iterate through the verts */
257 for ( x = 0; x < ds->patchWidth; x++ )
260 fprintf( f, "\t\t\t\t(" );
262 /* iterate through the row */
263 for ( y = 0; y < ds->patchHeight; y++ )
266 dv = &bspDrawVerts[ ds->firstVert + ( y * ds->patchWidth ) + x ];
269 VectorAdd( origin, dv->xyz, xyz );
272 fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
276 fprintf( f, " )\n" );
280 fprintf( f, "\t\t\t)\n" );
281 fprintf( f, "\t\t}\n" );
282 fprintf( f, "\t}\n\n" );
289 exports a bsp model to a map file
292 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin ){
295 bspDrawSurface_t *ds;
298 /* convert bsp planes to map planes */
299 nummapplanes = numBSPPlanes;
300 for ( i = 0; i < numBSPPlanes; i++ )
302 VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
303 mapplanes[ i ].dist = bspPlanes[ i ].dist;
304 mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
305 mapplanes[ i ].hash_chain = NULL;
308 /* allocate a build brush */
309 buildBrush = AllocBrush( 512 );
310 buildBrush->entityNum = 0;
311 buildBrush->original = buildBrush;
313 /* go through each brush in the model */
314 for ( i = 0; i < model->numBSPBrushes; i++ )
316 num = i + model->firstBSPBrush;
317 brush = &bspBrushes[ num ];
318 ConvertBrush( f, num, brush, origin );
321 /* free the build brush */
324 /* go through each drawsurf in the model */
325 for ( i = 0; i < model->numBSPSurfaces; i++ )
327 num = i + model->firstBSPSurface;
328 ds = &bspDrawSurfaces[ num ];
330 /* we only love patches */
331 if ( ds->surfaceType == MST_PATCH ) {
332 ConvertPatch( f, num, ds, origin );
341 exports entity key/value pairs to a map file
344 static void ConvertEPairs( FILE *f, entity_t *e ){
349 for ( ep = e->epairs; ep != NULL; ep = ep->next )
351 /* ignore empty keys/values */
352 if ( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' ) {
356 /* ignore model keys with * prefixed values */
357 if ( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' ) {
362 fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
370 exports an quake map file from the bsp
373 int ConvertBSPToMap( char *bspName ){
380 char name[ 1024 ], base[ 1024 ];
384 Sys_Printf( "--- Convert BSP to MAP ---\n" );
386 /* create the bsp filename from the bsp name */
387 strcpy( name, bspName );
388 StripExtension( name );
389 strcat( name, "_converted.map" );
390 Sys_Printf( "writing %s\n", name );
392 ExtractFileBase( bspName, base );
393 strcat( base, ".bsp" );
396 f = fopen( name, "wb" );
398 Error( "Open failed on %s\n", name );
402 fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
404 /* walk entity list */
405 for ( i = 0; i < numEntities; i++ )
411 fprintf( f, "// entity %d\n", i );
415 ConvertEPairs( f, e );
424 value = ValueForKey( e, "model" );
425 if ( value[ 0 ] == '*' ) {
426 modelNum = atoi( value + 1 );
433 /* only handle bsp models */
434 if ( modelNum >= 0 ) {
436 model = &bspModels[ modelNum ];
438 /* get entity origin */
439 value = ValueForKey( e, "origin" );
440 if ( value[ 0 ] == '\0' ) {
441 VectorClear( origin );
444 GetVectorForKey( e, "origin", origin );
448 ConvertModel( f, model, modelNum, origin );
452 fprintf( f, "}\n\n" );
455 /* close the file and return */
458 /* return to sender */