]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/convert_map.c
make it output nice looking origin brushes
[divverent/netradiant.git] / tools / quake3 / q3map2 / convert_map.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 CONVERT_MAP_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 ConvertBrush()
43 exports a map brush
44 */
45
46 #define SNAP_FLOAT_TO_INT       4
47 #define SNAP_INT_TO_FLOAT       (1.0 / SNAP_FLOAT_TO_INT)
48
49 typedef vec_t vec2_t[2];
50
51 static vec_t Det3x3(vec_t a00, vec_t a01, vec_t a02,
52                     vec_t a10, vec_t a11, vec_t a12,
53                     vec_t a20, vec_t a21, vec_t a22)
54 {
55         return
56                 a00 * (a11 * a22 - a12 * a21)
57         -       a01 * (a10 * a22 - a12 * a20)
58         +       a02 * (a10 * a21 - a11 * a20);
59 }
60
61 void GetBestSurfaceTriangleMatchForBrushside(side_t *buildSide, bspDrawVert_t *bestVert[3])
62 {
63         bspDrawSurface_t *s;
64         int i;
65         int t;
66         vec_t best = 0;
67         vec_t thisarea;
68         vec3_t normdiff;
69         vec3_t v1v0, v2v0, norm;
70         bspDrawVert_t *vert[3];
71         winding_t *polygon;
72         plane_t *buildPlane = &mapplanes[buildSide->planenum];
73         int matches = 0;
74
75         // first, start out with NULLs
76         bestVert[0] = bestVert[1] = bestVert[2] = NULL;
77
78         // brute force through all surfaces
79         for(s = bspDrawSurfaces; s != bspDrawSurfaces + numBSPDrawSurfaces; ++s)
80         {
81                 if(s->surfaceType != MST_PLANAR && s->surfaceType != MST_TRIANGLE_SOUP)
82                         continue;
83                 if(strcmp(buildSide->shaderInfo->shader, bspShaders[s->shaderNum].shader))
84                         continue;
85                 for(t = 0; t + 3 <= s->numIndexes; t += 3)
86                 {
87                         vert[0] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 0]];
88                         vert[1] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 1]];
89                         vert[2] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 2]];
90                         if(s->surfaceType == MST_PLANAR)
91                         {
92                                 VectorSubtract(vert[0]->normal, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
93                                 VectorSubtract(vert[1]->normal, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
94                                 VectorSubtract(vert[2]->normal, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
95                         }
96                         else
97                         {
98                                 // this is more prone to roundoff errors, but with embedded
99                                 // models, there is no better way
100                                 VectorSubtract(vert[1]->xyz, vert[0]->xyz, v1v0);
101                                 VectorSubtract(vert[2]->xyz, vert[0]->xyz, v2v0);
102                                 CrossProduct(v2v0, v1v0, norm);
103                                 VectorNormalize(norm, norm);
104                                 VectorSubtract(norm, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
105                         }
106                         if(abs(DotProduct(vert[0]->xyz, buildPlane->normal) - buildPlane->dist) >= distanceEpsilon) continue;
107                         if(abs(DotProduct(vert[1]->xyz, buildPlane->normal) - buildPlane->dist) >= distanceEpsilon) continue;
108                         if(abs(DotProduct(vert[2]->xyz, buildPlane->normal) - buildPlane->dist) >= distanceEpsilon) continue;
109                         // Okay. Correct surface type, correct shader, correct plane. Let's start with the business...
110                         polygon = CopyWinding(buildSide->winding);
111                         for(i = 0; i < 3; ++i)
112                         {
113                                 // 0: 1, 2
114                                 // 1: 2, 0
115                                 // 2; 0, 1
116                                 vec3_t *v1 = &vert[(i+1)%3]->xyz;
117                                 vec3_t *v2 = &vert[(i+2)%3]->xyz;
118                                 vec3_t triNormal;
119                                 vec_t triDist;
120                                 vec3_t sideDirection;
121                                 // we now need to generate triNormal and triDist so that they represent the plane spanned by normal and (v2 - v1).
122                                 VectorSubtract(*v2, *v1, sideDirection);
123                                 CrossProduct(sideDirection, buildPlane->normal, triNormal);
124                                 triDist = DotProduct(*v1, triNormal);
125                                 ChopWindingInPlace(&polygon, triNormal, triDist, distanceEpsilon);
126                                 if(!polygon)
127                                         goto exwinding;
128                         }
129                         thisarea = WindingArea(polygon);
130                         if(thisarea > 0)
131                                 ++matches;
132                         if(thisarea > best)
133                         {
134                                 best = thisarea;
135                                 bestVert[0] = vert[0];
136                                 bestVert[1] = vert[1];
137                                 bestVert[2] = vert[2];
138                         }
139                         FreeWinding(polygon);
140 exwinding:
141                         ;
142                 }
143         }
144         //if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
145         //      fprintf(stderr, "brushside with %s: %d matches (%f area)\n", buildSide->shaderInfo->shader, matches, best);
146 }
147
148 static void ConvertOriginBrush( FILE *f, int num, vec3_t origin )
149 {
150         char pattern[6][5][3] = {
151                 { "+++", "+-+", "-++", " - ", "-  " },
152                 { "+++", "-++", "++-", "+  ", "  +" },
153                 { "+++", "++-", "+-+", " - ", "  +" },
154                 { "---", "+--", "-+-", " - ", "+  " },
155                 { "---", "--+", "+--", "-  ", "  +" },
156                 { "---", "-+-", "--+", " + ", "  +" }
157         };
158         int i;
159 #define S(a,b,c) (pattern[a][b][c] == '+' ? +1 : pattern[a][b][c] == '-' ? -1 : 0)
160 #define FRAC(x) ((x) - floor(x))
161
162         /* start brush */
163         fprintf( f, "\t// brush %d\n", num );
164         fprintf( f, "\t{\n" );
165         fprintf( f, "\tbrushDef\n" );
166         fprintf( f, "\t{\n" );
167         /* print brush side */
168         /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
169
170         for(i = 0; i < 6; ++i)
171                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
172                                 origin[0] + 8 * S(i,0,0), origin[1] + 8 * S(i,0,1), origin[2] + 8 * S(i,0,2),
173                                 origin[0] + 8 * S(i,1,0), origin[1] + 8 * S(i,1,1), origin[2] + 8 * S(i,1,2),
174                                 origin[0] + 8 * S(i,2,0), origin[1] + 8 * S(i,2,1), origin[2] + 8 * S(i,2,2),
175                                 1/16.0, 0.0, FRAC((S(i,3,0) * origin[0] + S(i,3,1) * origin[1] + S(i,3,2) * origin[2]) / 16.0 + 0.5),
176                                 0.0, 1/16.0, FRAC((S(i,4,0) * origin[0] + S(i,4,1) * origin[1] + S(i,4,2) * origin[2]) / 16.0 + 0.5),
177                                 "common/origin",
178                                 0
179                            );
180 #undef FRAC
181 #undef S
182         
183         /* end brush */
184         fprintf( f, "\t}\n" );
185         fprintf( f, "\t}\n\n" );
186 }
187
188 static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin )
189 {
190         int                             i, j;
191         bspBrushSide_t  *side;
192         side_t                  *buildSide;
193         bspShader_t             *shader;
194         char                    *texture;
195         bspPlane_t              *plane;
196         plane_t         *buildPlane;
197         vec3_t                  pts[ 3 ];
198         bspDrawVert_t   *vert[3];
199         int valid;
200         
201         
202         /* start brush */
203         fprintf( f, "\t// brush %d\n", num );
204         fprintf( f, "\t{\n" );
205         fprintf( f, "\tbrushDef\n" );
206         fprintf( f, "\t{\n" );
207         
208         /* clear out build brush */
209         for( i = 0; i < buildBrush->numsides; i++ )
210         {
211                 buildSide = &buildBrush->sides[ i ];
212                 if( buildSide->winding != NULL )
213                 {
214                         FreeWinding( buildSide->winding );
215                         buildSide->winding = NULL;
216                 }
217         }
218         buildBrush->numsides = 0;
219         
220         /* iterate through bsp brush sides */
221         for( i = 0; i < brush->numSides; i++ )
222         {
223                 /* get side */
224                 side = &bspBrushSides[ brush->firstSide + i ];
225                 
226                 /* get shader */
227                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
228                         continue;
229                 shader = &bspShaders[ side->shaderNum ];
230                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
231                         continue;
232                 
233                 /* get plane */
234                 plane = &bspPlanes[ side->planeNum ];
235                 
236                 /* add build side */
237                 buildSide = &buildBrush->sides[ buildBrush->numsides ];
238                 buildBrush->numsides++;
239                 
240                 /* tag it */
241                 buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
242                 buildSide->planenum = side->planeNum;
243                 buildSide->winding = NULL;
244         }
245         
246         /* make brush windings */
247         if( !CreateBrushWindings( buildBrush ) )
248                 return;
249         
250         /* iterate through build brush sides */
251         for( i = 0; i < buildBrush->numsides; i++ )
252         {
253                 /* get build side */
254                 buildSide = &buildBrush->sides[ i ];
255                 
256                 /* get plane */
257                 buildPlane = &mapplanes[ buildSide->planenum ];
258
259                 /* dummy check */
260                 if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )
261                         continue;
262
263                 // st-texcoords -> texMat block
264                 // start out with dummy
265                 VectorSet(buildSide->texMat[0], 1/32.0, 0, 0);
266                 VectorSet(buildSide->texMat[1], 0, 1/32.0, 0);
267
268                 // find surface for this side (by brute force)
269                 // surface format:
270                 //   - meshverts point in pairs of three into verts
271                 //   - (triangles)
272                 //   - find the triangle that has most in common with our side
273                 GetBestSurfaceTriangleMatchForBrushside(buildSide, vert);
274                 valid = 0;
275
276                 if(vert[0] && vert[1] && vert[2])
277                 {
278                         int i;
279                         vec3_t texX, texY;
280                         vec3_t xy1I, xy1J, xy1K;
281                         vec2_t stI, stJ, stK;
282                         vec_t D, D0, D1, D2;
283
284                         ComputeAxisBase(buildPlane->normal, texX, texY);
285
286                         VectorSet(xy1I, DotProduct(vert[0]->xyz, texX), DotProduct(vert[0]->xyz, texY), 1);
287                         VectorSet(xy1J, DotProduct(vert[1]->xyz, texX), DotProduct(vert[1]->xyz, texY), 1);
288                         VectorSet(xy1K, DotProduct(vert[2]->xyz, texX), DotProduct(vert[2]->xyz, texY), 1);
289                         stI[0] = vert[0]->st[0]; stI[1] = vert[0]->st[1];
290                         stJ[0] = vert[1]->st[0]; stJ[1] = vert[1]->st[1];
291                         stK[0] = vert[2]->st[0]; stK[1] = vert[2]->st[1];
292
293                         //   - solve linear equations:
294                         //     - (x, y) := xyz . (texX, texY)
295                         //     - st[i] = texMat[i][0]*x + texMat[i][1]*y + texMat[i][2]
296                         //       (for three vertices)
297                         D = Det3x3(
298                                 xy1I[0], xy1I[1], 1,
299                                 xy1J[0], xy1J[1], 1,
300                                 xy1K[0], xy1K[1], 1
301                         );
302                         if(D != 0)
303                         {
304                                 for(i = 0; i < 2; ++i)
305                                 {
306                                         D0 = Det3x3(
307                                                 stI[i], xy1I[1], 1,
308                                                 stJ[i], xy1J[1], 1,
309                                                 stK[i], xy1K[1], 1
310                                         );
311                                         D1 = Det3x3(
312                                                 xy1I[0], stI[i], 1,
313                                                 xy1J[0], stJ[i], 1,
314                                                 xy1K[0], stK[i], 1
315                                         );
316                                         D2 = Det3x3(
317                                                 xy1I[0], xy1I[1], stI[i],
318                                                 xy1J[0], xy1J[1], stJ[i],
319                                                 xy1K[0], xy1K[1], stK[i]
320                                         );
321                                         VectorSet(buildSide->texMat[i], D0 / D, D1 / D, D2 / D);
322                                         valid = 1;
323                                 }
324                         }
325                         else
326                                 fprintf(stderr, "degenerate triangle found when solving texMat equations for\n(%f %f %f) (%f %f %f) (%f %f %f)\n( %f %f %f )\n( %f %f %f ) -> ( %f %f )\n( %f %f %f ) -> ( %f %f )\n( %f %f %f ) -> ( %f %f )\n",
327                                         buildPlane->normal[0], buildPlane->normal[1], buildPlane->normal[2],
328                                         vert[0]->normal[0], vert[0]->normal[1], vert[0]->normal[2],
329                                         texX[0], texX[1], texX[2], texY[0], texY[1], texY[2],
330                                         vert[0]->xyz[0], vert[0]->xyz[1], vert[0]->xyz[2], xy1I[0], xy1I[1],
331                                         vert[1]->xyz[0], vert[1]->xyz[1], vert[1]->xyz[2], xy1J[0], xy1J[1],
332                                         vert[2]->xyz[0], vert[2]->xyz[1], vert[2]->xyz[2], xy1K[0], xy1K[1]
333                                         );
334                 }
335                 else
336                         if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
337                                 fprintf(stderr, "no matching triangle for brushside using %s (hopefully nobody can see this side anyway)\n", buildSide->shaderInfo->shader);
338                 
339                 /* get texture name */
340                 if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )
341                         texture = buildSide->shaderInfo->shader + 9;
342                 else
343                         texture = buildSide->shaderInfo->shader;
344                 
345                 /* get plane points and offset by origin */
346                 for( j = 0; j < 3; j++ )
347                 {
348                         VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
349                         //%     pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
350                         //%     pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
351                         //%     pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
352                 }
353                 
354                 /* print brush side */
355                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
356                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
357                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
358                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
359                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
360                         buildSide->texMat[0][0], buildSide->texMat[0][1], buildSide->texMat[0][2],
361                         buildSide->texMat[1][0], buildSide->texMat[1][1], buildSide->texMat[1][2],
362                         texture,
363                         // DEBUG: valid ? 0 : C_DETAIL
364                         0
365                         );
366                 // TODO write brush primitives format here
367         }
368         
369         /* end brush */
370         fprintf( f, "\t}\n" );
371         fprintf( f, "\t}\n\n" );
372 }
373
374 #if 0
375         /* iterate through the brush sides (ignore the first 6 bevel planes) */
376         for( i = 0; i < brush->numSides; i++ )
377         {
378                 /* get side */
379                 side = &bspBrushSides[ brush->firstSide + i ];
380                 
381                 /* get shader */
382                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
383                         continue;
384                 shader = &bspShaders[ side->shaderNum ];
385                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
386                         continue;
387                 
388                 /* get texture name */
389                 if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
390                         texture = shader->shader + 9;
391                 else
392                         texture = shader->shader;
393                 
394                 /* get plane */
395                 plane = &bspPlanes[ side->planeNum ];
396
397                 /* make plane points */
398                 {
399                         vec3_t  vecs[ 2 ];
400
401                         
402                         MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
403                         VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
404                         VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
405                         VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
406                 }
407
408                 /* offset by origin */
409                 for( j = 0; j < 3; j++ )
410                         VectorAdd( pts[ j ], origin, pts[ j ] );
411                 
412                 /* print brush side */
413                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
414                 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",
415                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
416                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
417                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
418                         texture );
419         }
420 #endif
421
422
423
424 /*
425 ConvertPatch()
426 converts a bsp patch to a map patch
427
428         {
429                 patchDef2
430                 {
431                         base_wall/concrete
432                         ( 9 3 0 0 0 )
433                         (
434                                 ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
435                                 ...
436                         )
437                 }
438         }
439
440 */
441
442 static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )
443 {
444         int                             x, y;
445         bspShader_t             *shader;
446         char                    *texture;
447         bspDrawVert_t   *dv;
448         vec3_t                  xyz;
449         
450         
451         /* only patches */
452         if( ds->surfaceType != MST_PATCH )
453                 return;
454         
455         /* get shader */
456         if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )
457                 return;
458         shader = &bspShaders[ ds->shaderNum ];
459         
460         /* get texture name */
461         if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
462                 texture = shader->shader + 9;
463         else
464                 texture = shader->shader;
465         
466         /* start patch */
467         fprintf( f, "\t// patch %d\n", num );
468         fprintf( f, "\t{\n" );
469         fprintf( f, "\t\tpatchDef2\n" );
470         fprintf( f, "\t\t{\n" );
471         fprintf( f, "\t\t\t%s\n", texture );
472         fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
473         fprintf( f, "\t\t\t(\n" );
474         
475         /* iterate through the verts */
476         for( x = 0; x < ds->patchWidth; x++ )
477         {
478                 /* start row */
479                 fprintf( f, "\t\t\t\t(" );
480                 
481                 /* iterate through the row */
482                 for( y = 0; y < ds->patchHeight; y++ )
483                 {
484                         /* get vert */
485                         dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];
486                         
487                         /* offset it */
488                         VectorAdd( origin, dv->xyz, xyz );
489                         
490                         /* print vertex */
491                         fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
492                 }
493                 
494                 /* end row */
495                 fprintf( f, " )\n" );
496         }
497         
498         /* end patch */
499         fprintf( f, "\t\t\t)\n" );
500         fprintf( f, "\t\t}\n" );
501         fprintf( f, "\t}\n\n" );
502 }
503
504
505
506 /*
507 ConvertModel()
508 exports a bsp model to a map file
509 */
510
511 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
512 {
513         int                                     i, num;
514         bspBrush_t                      *brush;
515         bspDrawSurface_t        *ds;
516         
517         
518         /* convert bsp planes to map planes */
519         nummapplanes = numBSPPlanes;
520         AUTOEXPAND_BY_REALLOC(mapplanes, nummapplanes, allocatedmapplanes, 1024);
521         for( i = 0; i < numBSPPlanes; i++ )
522         {
523                 VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
524                 mapplanes[ i ].dist = bspPlanes[ i ].dist;
525                 mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
526                 mapplanes[ i ].hash_chain = 0;
527         }
528         
529         /* allocate a build brush */
530         buildBrush = AllocBrush( 512 );
531         buildBrush->entityNum = 0;
532         buildBrush->original = buildBrush;
533
534         if(origin[0] != 0 || origin[1] != 0 || origin[2] != 0)
535                 ConvertOriginBrush(f, -1, origin);
536         
537         /* go through each brush in the model */
538         for( i = 0; i < model->numBSPBrushes; i++ )
539         {
540                 num = i + model->firstBSPBrush;
541                 brush = &bspBrushes[ num ];
542                 ConvertBrush( f, num, brush, origin );
543         }
544         
545         /* free the build brush */
546         free( buildBrush );
547         
548         /* go through each drawsurf in the model */
549         for( i = 0; i < model->numBSPSurfaces; i++ )
550         {
551                 num = i + model->firstBSPSurface;
552                 ds = &bspDrawSurfaces[ num ];
553                 
554                 /* we only love patches */
555                 if( ds->surfaceType == MST_PATCH )
556                         ConvertPatch( f, num, ds, origin );
557         }
558 }
559
560
561
562 /*
563 ConvertEPairs()
564 exports entity key/value pairs to a map file
565 */
566
567 static void ConvertEPairs( FILE *f, entity_t *e, qboolean skip_origin )
568 {
569         epair_t *ep;
570         
571         
572         /* walk epairs */
573         for( ep = e->epairs; ep != NULL; ep = ep->next )
574         {
575                 /* ignore empty keys/values */
576                 if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )
577                         continue;
578
579                 /* ignore model keys with * prefixed values */
580                 if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )
581                         continue;
582                 
583                 /* ignore origin keys if skip_origin is set */
584                 if( skip_origin && !Q_stricmp( ep->key, "origin" ) )
585                         continue;
586                 
587                 /* emit the epair */
588                 fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
589         }
590 }
591
592
593
594 /*
595 ConvertBSPToMap()
596 exports an quake map file from the bsp
597 */
598
599 int ConvertBSPToMap( char *bspName )
600 {
601         int                             i, modelNum;
602         FILE                    *f;
603         bspModel_t              *model;
604         entity_t                *e;
605         vec3_t                  origin;
606         const char              *value;
607         char                    name[ 1024 ], base[ 1024 ];
608         
609         
610         /* note it */
611         Sys_Printf( "--- Convert BSP to MAP ---\n" );
612         
613         /* create the bsp filename from the bsp name */
614         strcpy( name, bspName );
615         StripExtension( name );
616         strcat( name, "_converted.map" );
617         Sys_Printf( "writing %s\n", name );
618         
619         ExtractFileBase( bspName, base );
620         strcat( base, ".bsp" );
621         
622         /* open it */
623         f = fopen( name, "wb" );
624         if( f == NULL )
625                 Error( "Open failed on %s\n", name );
626         
627         /* print header */
628         fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
629         
630         /* walk entity list */
631         for( i = 0; i < numEntities; i++ )
632         {
633                 /* get entity */
634                 e = &entities[ i ];
635                 
636                 /* start entity */
637                 fprintf( f, "// entity %d\n", i );
638                 fprintf( f, "{\n" );
639                 
640                 /* get model num */
641                 if( i == 0 )
642                         modelNum = 0;
643                 else
644                 {
645                         value = ValueForKey( e, "model" );
646                         if( value[ 0 ] == '*' )
647                                 modelNum = atoi( value + 1 );
648                         else
649                                 modelNum = -1;
650                 }
651                 
652                 /* export keys */
653                 ConvertEPairs( f, e, modelNum >= 0 );
654                 fprintf( f, "\n" );
655                 
656                 /* only handle bsp models */
657                 if( modelNum >= 0 )
658                 {
659                         /* get model */
660                         model = &bspModels[ modelNum ];
661                         
662                         /* get entity origin */
663                         value = ValueForKey( e, "origin" );
664                         if( value[ 0 ] == '\0' )
665                                 VectorClear( origin );
666                         else
667                                 GetVectorForKey( e, "origin", origin );
668                         
669                         /* convert model */
670                         ConvertModel( f, model, modelNum, origin );
671                 }
672                 
673                 /* end entity */
674                 fprintf( f, "}\n\n" );
675         }
676         
677         /* close the file and return */
678         fclose( f );
679         
680         /* return to sender */
681         return 0;
682 }