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