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