]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/convert_map.c
fix compile errors
[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, qboolean brushPrimitives )
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         if(brushPrimitives)
206         {
207                 fprintf( f, "\tbrushDef\n" );
208                 fprintf( f, "\t{\n" );
209         }
210         
211         /* clear out build brush */
212         for( i = 0; i < buildBrush->numsides; i++ )
213         {
214                 buildSide = &buildBrush->sides[ i ];
215                 if( buildSide->winding != NULL )
216                 {
217                         FreeWinding( buildSide->winding );
218                         buildSide->winding = NULL;
219                 }
220         }
221         buildBrush->numsides = 0;
222         
223         /* iterate through bsp brush sides */
224         for( i = 0; i < brush->numSides; i++ )
225         {
226                 /* get side */
227                 side = &bspBrushSides[ brush->firstSide + i ];
228                 
229                 /* get shader */
230                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
231                         continue;
232                 shader = &bspShaders[ side->shaderNum ];
233                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
234                         continue;
235                 
236                 /* get plane */
237                 plane = &bspPlanes[ side->planeNum ];
238                 
239                 /* add build side */
240                 buildSide = &buildBrush->sides[ buildBrush->numsides ];
241                 buildBrush->numsides++;
242                 
243                 /* tag it */
244                 buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
245                 buildSide->planenum = side->planeNum;
246                 buildSide->winding = NULL;
247         }
248         
249         /* make brush windings */
250         if( !CreateBrushWindings( buildBrush ) )
251                 return;
252         
253         /* iterate through build brush sides */
254         for( i = 0; i < buildBrush->numsides; i++ )
255         {
256                 /* get build side */
257                 buildSide = &buildBrush->sides[ i ];
258                 
259                 /* get plane */
260                 buildPlane = &mapplanes[ buildSide->planenum ];
261
262                 /* dummy check */
263                 if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )
264                         continue;
265
266                 // st-texcoords -> texMat block
267                 // start out with dummy
268                 VectorSet(buildSide->texMat[0], 1/32.0, 0, 0);
269                 VectorSet(buildSide->texMat[1], 0, 1/32.0, 0);
270
271                 // find surface for this side (by brute force)
272                 // surface format:
273                 //   - meshverts point in pairs of three into verts
274                 //   - (triangles)
275                 //   - find the triangle that has most in common with our side
276                 GetBestSurfaceTriangleMatchForBrushside(buildSide, vert);
277                 valid = 0;
278
279                 /* get texture name */
280                 if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )
281                         texture = buildSide->shaderInfo->shader + 9;
282                 else
283                         texture = buildSide->shaderInfo->shader;
284                 
285                 /* get plane points and offset by origin */
286                 for( j = 0; j < 3; j++ )
287                 {
288                         VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
289                         //%     pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
290                         //%     pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
291                         //%     pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
292                 }
293
294                 if(vert[0] && vert[1] && vert[2])
295                 {
296                         if(brushPrimitives)
297                         {
298                                 int i;
299                                 vec3_t texX, texY;
300                                 vec2_t xyI, xyJ, xyK;
301                                 vec2_t stI, stJ, stK;
302                                 vec_t D, D0, D1, D2;
303
304                                 ComputeAxisBase(buildPlane->normal, texX, texY);
305
306                                 xyI[0] = DotProduct(vert[0]->xyz, texX);
307                                 xyI[1] = DotProduct(vert[0]->xyz, texY);
308                                 xyJ[0] = DotProduct(vert[1]->xyz, texX);
309                                 xyJ[1] = DotProduct(vert[1]->xyz, texY);
310                                 xyK[0] = DotProduct(vert[2]->xyz, texX);
311                                 xyK[1] = DotProduct(vert[2]->xyz, texY);
312                                 stI[0] = vert[0]->st[0]; stI[1] = vert[0]->st[1];
313                                 stJ[0] = vert[1]->st[0]; stJ[1] = vert[1]->st[1];
314                                 stK[0] = vert[2]->st[0]; stK[1] = vert[2]->st[1];
315
316                                 //   - solve linear equations:
317                                 //     - (x, y) := xyz . (texX, texY)
318                                 //     - st[i] = texMat[i][0]*x + texMat[i][1]*y + texMat[i][2]
319                                 //       (for three vertices)
320                                 D = Det3x3(
321                                         xyI[0], xyI[1], 1,
322                                         xyJ[0], xyJ[1], 1,
323                                         xyK[0], xyK[1], 1
324                                 );
325                                 if(D != 0)
326                                 {
327                                         for(i = 0; i < 2; ++i)
328                                         {
329                                                 D0 = Det3x3(
330                                                         stI[i], xyI[1], 1,
331                                                         stJ[i], xyJ[1], 1,
332                                                         stK[i], xyK[1], 1
333                                                 );
334                                                 D1 = Det3x3(
335                                                         xyI[0], stI[i], 1,
336                                                         xyJ[0], stJ[i], 1,
337                                                         xyK[0], stK[i], 1
338                                                 );
339                                                 D2 = Det3x3(
340                                                         xyI[0], xyI[1], stI[i],
341                                                         xyJ[0], xyJ[1], stJ[i],
342                                                         xyK[0], xyK[1], stK[i]
343                                                 );
344                                                 VectorSet(buildSide->texMat[i], D0 / D, D1 / D, D2 / D);
345                                         }
346                                         valid = 1;
347                                 }
348                                 else
349                                         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",
350                                                 buildPlane->normal[0], buildPlane->normal[1], buildPlane->normal[2],
351                                                 vert[0]->normal[0], vert[0]->normal[1], vert[0]->normal[2],
352                                                 texX[0], texX[1], texX[2], texY[0], texY[1], texY[2],
353                                                 vert[0]->xyz[0], vert[0]->xyz[1], vert[0]->xyz[2], xyI[0], xyI[1],
354                                                 vert[1]->xyz[0], vert[1]->xyz[1], vert[1]->xyz[2], xyJ[0], xyJ[1],
355                                                 vert[2]->xyz[0], vert[2]->xyz[1], vert[2]->xyz[2], xyK[0], xyK[1]
356                                                 );
357
358                                 /* print brush side */
359                                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
360                                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
361                                                 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
362                                                 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
363                                                 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
364                                                 buildSide->texMat[0][0], buildSide->texMat[0][1], buildSide->texMat[0][2],
365                                                 buildSide->texMat[1][0], buildSide->texMat[1][1], buildSide->texMat[1][2],
366                                                 texture,
367                                                 // DEBUG: valid ? 0 : C_DETAIL
368                                                 0
369                                            );
370                         }
371                         else
372                         {
373                                 // invert QuakeTextureVecs
374                                 vec3_t vecs[2];
375                                 int sv, tv;
376                                 vec2_t stI, stJ, stK;
377                                 vec3_t sts[2];
378                                 vec2_t shift, scale;
379                                 vec_t rotate;
380                                 vec_t D, D0, D1, D2;
381
382                                 TextureAxisFromPlane(buildPlane, vecs[0], vecs[1]);
383                                 if (vecs[0][0])
384                                         sv = 0;
385                                 else if (vecs[0][1])
386                                         sv = 1;
387                                 else
388                                         sv = 2;
389                                 if (vecs[1][0])
390                                         tv = 0;
391                                 else if (vecs[1][1])
392                                         tv = 1;
393                                 else
394                                         tv = 2;
395
396                                 stI[0] = vert[0]->st[0] * buildSide->shaderInfo->shaderWidth; stI[1] = vert[0]->st[1] * buildSide->shaderInfo->shaderHeight;
397                                 stJ[0] = vert[1]->st[0] * buildSide->shaderInfo->shaderWidth; stJ[1] = vert[1]->st[1] * buildSide->shaderInfo->shaderHeight;
398                                 stK[0] = vert[2]->st[0] * buildSide->shaderInfo->shaderWidth; stK[1] = vert[2]->st[1] * buildSide->shaderInfo->shaderHeight;
399
400                                 D = Det3x3(
401                                         vert[0]->xyz[sv], vert[0]->xyz[tv], 1,
402                                         vert[1]->xyz[sv], vert[1]->xyz[tv], 1,
403                                         vert[2]->xyz[sv], vert[2]->xyz[tv], 1
404                                 );
405                                 if(D != 0)
406                                 {
407                                         for(i = 0; i < 2; ++i)
408                                         {
409                                                 D0 = Det3x3(
410                                                         stI[i], vert[0]->xyz[tv], 1,
411                                                         stJ[i], vert[1]->xyz[tv], 1,
412                                                         stK[i], vert[2]->xyz[tv], 1
413                                                 );
414                                                 D1 = Det3x3(
415                                                         vert[0]->xyz[sv], stI[i], 1,
416                                                         vert[1]->xyz[sv], stJ[i], 1,
417                                                         vert[2]->xyz[sv], stK[i], 1
418                                                 );
419                                                 D2 = Det3x3(
420                                                         vert[0]->xyz[sv], vert[0]->xyz[tv], stI[i],
421                                                         vert[1]->xyz[sv], vert[1]->xyz[tv], stJ[i],
422                                                         vert[2]->xyz[sv], vert[2]->xyz[tv], stK[i]
423                                                 );
424                                                 VectorSet(sts[i], D0 / D, D1 / D, D2 / D);
425                                         }
426                                         valid = 1;
427                                 }
428                                 else
429                                         fprintf(stderr, "degenerate triangle found when solving texDef equations\n"); // FIXME add stuff here
430
431                                 // now we must solve:
432                                         //      // now we must invert:
433                                         //      ang = rotate / 180 * Q_PI;
434                                         //      sinv = sin(ang);
435                                         //      cosv = cos(ang);
436                                         //      ns = cosv * vecs[0][sv];
437                                         //      nt = sinv * vecs[0][sv];
438                                         //      vecsrotscaled[0][sv] = ns / scale[0];
439                                         //      vecsrotscaled[0][tv] = nt / scale[0];
440                                         //      ns = -sinv * vecs[1][tv];
441                                         //      nt =  cosv * vecs[1][tv];
442                                         //      vecsrotscaled[1][sv] = ns / scale[1];
443                                         //      vecsrotscaled[1][tv] = nt / scale[1];
444                                 scale[0] = sqrt(sts[0][0] * sts[0][0] + sts[0][1] * sts[0][1]);
445                                 scale[1] = sqrt(sts[1][0] * sts[1][0] + sts[1][1] * sts[1][1]);
446                                 rotate = atan2(sts[0][1] - sts[1][0], sts[0][0] + sts[1][1]);
447                                 shift[0] = sts[0][2];
448                                 shift[1] = sts[1][2];
449
450                                 /* print brush side */
451                                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
452                                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %.8f %.8f %.8f %.8f %.8f %s %d 0 0\n",
453                                                 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
454                                                 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
455                                                 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
456                                                 shift[0], shift[1], rotate, scale[0], scale[1],
457                                                 texture,
458                                                 // DEBUG: valid ? 0 : C_DETAIL
459                                                 0
460                                            );
461                         }
462                 }
463                 else
464                         if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
465                                 fprintf(stderr, "no matching triangle for brushside using %s (hopefully nobody can see this side anyway)\n", buildSide->shaderInfo->shader);
466         }
467         
468         /* end brush */
469         if(brushPrimitives)
470         {
471                 fprintf( f, "\t}\n" );
472         }
473         fprintf( f, "\t}\n\n" );
474 }
475
476 #if 0
477         /* iterate through the brush sides (ignore the first 6 bevel planes) */
478         for( i = 0; i < brush->numSides; i++ )
479         {
480                 /* get side */
481                 side = &bspBrushSides[ brush->firstSide + i ];
482                 
483                 /* get shader */
484                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
485                         continue;
486                 shader = &bspShaders[ side->shaderNum ];
487                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
488                         continue;
489                 
490                 /* get texture name */
491                 if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
492                         texture = shader->shader + 9;
493                 else
494                         texture = shader->shader;
495                 
496                 /* get plane */
497                 plane = &bspPlanes[ side->planeNum ];
498
499                 /* make plane points */
500                 {
501                         vec3_t  vecs[ 2 ];
502
503                         
504                         MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
505                         VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
506                         VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
507                         VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
508                 }
509
510                 /* offset by origin */
511                 for( j = 0; j < 3; j++ )
512                         VectorAdd( pts[ j ], origin, pts[ j ] );
513                 
514                 /* print brush side */
515                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
516                 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",
517                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
518                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
519                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
520                         texture );
521         }
522 #endif
523
524
525
526 /*
527 ConvertPatch()
528 converts a bsp patch to a map patch
529
530         {
531                 patchDef2
532                 {
533                         base_wall/concrete
534                         ( 9 3 0 0 0 )
535                         (
536                                 ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
537                                 ...
538                         )
539                 }
540         }
541
542 */
543
544 static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )
545 {
546         int                             x, y;
547         bspShader_t             *shader;
548         char                    *texture;
549         bspDrawVert_t   *dv;
550         vec3_t                  xyz;
551         
552         
553         /* only patches */
554         if( ds->surfaceType != MST_PATCH )
555                 return;
556         
557         /* get shader */
558         if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )
559                 return;
560         shader = &bspShaders[ ds->shaderNum ];
561         
562         /* get texture name */
563         if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
564                 texture = shader->shader + 9;
565         else
566                 texture = shader->shader;
567         
568         /* start patch */
569         fprintf( f, "\t// patch %d\n", num );
570         fprintf( f, "\t{\n" );
571         fprintf( f, "\t\tpatchDef2\n" );
572         fprintf( f, "\t\t{\n" );
573         fprintf( f, "\t\t\t%s\n", texture );
574         fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
575         fprintf( f, "\t\t\t(\n" );
576         
577         /* iterate through the verts */
578         for( x = 0; x < ds->patchWidth; x++ )
579         {
580                 /* start row */
581                 fprintf( f, "\t\t\t\t(" );
582                 
583                 /* iterate through the row */
584                 for( y = 0; y < ds->patchHeight; y++ )
585                 {
586                         /* get vert */
587                         dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];
588                         
589                         /* offset it */
590                         VectorAdd( origin, dv->xyz, xyz );
591                         
592                         /* print vertex */
593                         fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
594                 }
595                 
596                 /* end row */
597                 fprintf( f, " )\n" );
598         }
599         
600         /* end patch */
601         fprintf( f, "\t\t\t)\n" );
602         fprintf( f, "\t\t}\n" );
603         fprintf( f, "\t}\n\n" );
604 }
605
606
607
608 /*
609 ConvertModel()
610 exports a bsp model to a map file
611 */
612
613 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin, qboolean brushPrimitives )
614 {
615         int                                     i, num;
616         bspBrush_t                      *brush;
617         bspDrawSurface_t        *ds;
618         
619         
620         /* convert bsp planes to map planes */
621         nummapplanes = numBSPPlanes;
622         AUTOEXPAND_BY_REALLOC(mapplanes, nummapplanes, allocatedmapplanes, 1024);
623         for( i = 0; i < numBSPPlanes; i++ )
624         {
625                 VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
626                 mapplanes[ i ].dist = bspPlanes[ i ].dist;
627                 mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
628                 mapplanes[ i ].hash_chain = 0;
629         }
630         
631         /* allocate a build brush */
632         buildBrush = AllocBrush( 512 );
633         buildBrush->entityNum = 0;
634         buildBrush->original = buildBrush;
635
636         if(origin[0] != 0 || origin[1] != 0 || origin[2] != 0)
637                 ConvertOriginBrush(f, -1, origin);
638         
639         /* go through each brush in the model */
640         for( i = 0; i < model->numBSPBrushes; i++ )
641         {
642                 num = i + model->firstBSPBrush;
643                 brush = &bspBrushes[ num ];
644                 ConvertBrush( f, num, brush, origin, brushPrimitives );
645         }
646         
647         /* free the build brush */
648         free( buildBrush );
649         
650         /* go through each drawsurf in the model */
651         for( i = 0; i < model->numBSPSurfaces; i++ )
652         {
653                 num = i + model->firstBSPSurface;
654                 ds = &bspDrawSurfaces[ num ];
655                 
656                 /* we only love patches */
657                 if( ds->surfaceType == MST_PATCH )
658                         ConvertPatch( f, num, ds, origin );
659         }
660 }
661
662
663
664 /*
665 ConvertEPairs()
666 exports entity key/value pairs to a map file
667 */
668
669 static void ConvertEPairs( FILE *f, entity_t *e, qboolean skip_origin )
670 {
671         epair_t *ep;
672         
673         
674         /* walk epairs */
675         for( ep = e->epairs; ep != NULL; ep = ep->next )
676         {
677                 /* ignore empty keys/values */
678                 if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )
679                         continue;
680
681                 /* ignore model keys with * prefixed values */
682                 if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )
683                         continue;
684                 
685                 /* ignore origin keys if skip_origin is set */
686                 if( skip_origin && !Q_stricmp( ep->key, "origin" ) )
687                         continue;
688                 
689                 /* emit the epair */
690                 fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
691         }
692 }
693
694
695
696 /*
697 ConvertBSPToMap()
698 exports an quake map file from the bsp
699 */
700
701 int ConvertBSPToMap_Ext( char *bspName, qboolean brushPrimitives )
702 {
703         int                             i, modelNum;
704         FILE                    *f;
705         bspModel_t              *model;
706         entity_t                *e;
707         vec3_t                  origin;
708         const char              *value;
709         char                    name[ 1024 ], base[ 1024 ];
710         
711         
712         /* note it */
713         Sys_Printf( "--- Convert BSP to MAP ---\n" );
714         
715         /* create the bsp filename from the bsp name */
716         strcpy( name, bspName );
717         StripExtension( name );
718         strcat( name, "_converted.map" );
719         Sys_Printf( "writing %s\n", name );
720         
721         ExtractFileBase( bspName, base );
722         strcat( base, ".bsp" );
723         
724         /* open it */
725         f = fopen( name, "wb" );
726         if( f == NULL )
727                 Error( "Open failed on %s\n", name );
728         
729         /* print header */
730         fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
731         
732         /* walk entity list */
733         for( i = 0; i < numEntities; i++ )
734         {
735                 /* get entity */
736                 e = &entities[ i ];
737                 
738                 /* start entity */
739                 fprintf( f, "// entity %d\n", i );
740                 fprintf( f, "{\n" );
741                 
742                 /* get model num */
743                 if( i == 0 )
744                         modelNum = 0;
745                 else
746                 {
747                         value = ValueForKey( e, "model" );
748                         if( value[ 0 ] == '*' )
749                                 modelNum = atoi( value + 1 );
750                         else
751                                 modelNum = -1;
752                 }
753                 
754                 /* export keys */
755                 ConvertEPairs( f, e, modelNum >= 0 );
756                 fprintf( f, "\n" );
757                 
758                 /* only handle bsp models */
759                 if( modelNum >= 0 )
760                 {
761                         /* get model */
762                         model = &bspModels[ modelNum ];
763                         
764                         /* get entity origin */
765                         value = ValueForKey( e, "origin" );
766                         if( value[ 0 ] == '\0' )
767                                 VectorClear( origin );
768                         else
769                                 GetVectorForKey( e, "origin", origin );
770                         
771                         /* convert model */
772                         ConvertModel( f, model, modelNum, origin, brushPrimitives );
773                 }
774                 
775                 /* end entity */
776                 fprintf( f, "}\n\n" );
777         }
778         
779         /* close the file and return */
780         fclose( f );
781         
782         /* return to sender */
783         return 0;
784 }
785
786 int ConvertBSPToMap( char *bspName )
787 {
788         return ConvertBSPToMap_Ext(bspName, qfalse);
789 }
790
791 int ConvertBSPToMap_BP( char *bspName )
792 {
793         return ConvertBSPToMap_Ext(bspName, qtrue);
794 }