1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
46 #define SNAP_FLOAT_TO_INT 4
47 #define SNAP_INT_TO_FLOAT (1.0 / SNAP_FLOAT_TO_INT)
49 typedef vec_t vec2_t[2];
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)
56 a00 * (a11 * a22 - a12 * a21)
57 - a01 * (a10 * a22 - a12 * a20)
58 + a02 * (a10 * a21 - a11 * a20);
61 void GetBestSurfaceTriangleMatchForBrushside(side_t *buildSide, bspDrawVert_t *bestVert[3])
69 vec3_t v1v0, v2v0, norm;
70 bspDrawVert_t *vert[3];
72 plane_t *buildPlane = &mapplanes[buildSide->planenum];
75 // first, start out with NULLs
76 bestVert[0] = bestVert[1] = bestVert[2] = NULL;
78 // brute force through all surfaces
79 for(s = bspDrawSurfaces; s != bspDrawSurfaces + numBSPDrawSurfaces; ++s)
81 if(s->surfaceType != MST_PLANAR && s->surfaceType != MST_TRIANGLE_SOUP)
83 if(strcmp(buildSide->shaderInfo->shader, bspShaders[s->shaderNum].shader))
85 for(t = 0; t + 3 <= s->numIndexes; t += 3)
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))
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;
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;
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)
116 vec3_t *v1 = &vert[(i+1)%3]->xyz;
117 vec3_t *v2 = &vert[(i+2)%3]->xyz;
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);
129 thisarea = WindingArea(polygon);
135 bestVert[0] = vert[0];
136 bestVert[1] = vert[1];
137 bestVert[2] = vert[2];
139 FreeWinding(polygon);
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);
148 #define FRAC(x) ((x) - floor(x))
149 static void ConvertOriginBrush( FILE *f, int num, vec3_t origin, qboolean brushPrimitives )
151 int originSize = 256;
153 char pattern[6][7][3] = {
154 { "+++", "+-+", "-++", "- ", " + ", " - ", "- " },
155 { "+++", "-++", "++-", "- ", " +", "+ ", " +" },
156 { "+++", "++-", "+-+", " - ", " +", " - ", " +" },
157 { "---", "+--", "-+-", "- ", " + ", " - ", "+ " },
158 { "---", "--+", "+--", "- ", " +", "- ", " +" },
159 { "---", "-+-", "--+", " - ", " +", " + ", " +" }
162 #define S(a,b,c) (pattern[a][b][c] == '+' ? +1 : pattern[a][b][c] == '-' ? -1 : 0)
165 fprintf( f, "\t// brush %d\n", num );
166 fprintf( f, "\t{\n" );
169 fprintf( f, "\tbrushDef\n" );
170 fprintf( f, "\t{\n" );
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 */
175 for(i = 0; i < 6; ++i)
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),
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),
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,
207 fprintf( f, "\t}\n" );
208 fprintf( f, "\t}\n\n" );
211 static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin, qboolean brushPrimitives )
214 bspBrushSide_t *side;
221 bspDrawVert_t *vert[3];
226 fprintf( f, "\t// brush %d\n", num );
227 fprintf( f, "\t{\n" );
230 fprintf( f, "\tbrushDef\n" );
231 fprintf( f, "\t{\n" );
234 /* clear out build brush */
235 for( i = 0; i < buildBrush->numsides; i++ )
237 buildSide = &buildBrush->sides[ i ];
238 if( buildSide->winding != NULL )
240 FreeWinding( buildSide->winding );
241 buildSide->winding = NULL;
244 buildBrush->numsides = 0;
246 /* iterate through bsp brush sides */
247 for( i = 0; i < brush->numSides; i++ )
250 side = &bspBrushSides[ brush->firstSide + i ];
253 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
255 shader = &bspShaders[ side->shaderNum ];
256 //if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
260 plane = &bspPlanes[ side->planeNum ];
263 buildSide = &buildBrush->sides[ buildBrush->numsides ];
264 buildBrush->numsides++;
267 buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
268 buildSide->planenum = side->planeNum;
269 buildSide->winding = NULL;
272 /* make brush windings */
273 if( !CreateBrushWindings( buildBrush ) )
275 Sys_Printf( "CreateBrushWindings failed\n" );
279 /* iterate through build brush sides */
280 for( i = 0; i < buildBrush->numsides; i++ )
283 buildSide = &buildBrush->sides[ i ];
286 buildPlane = &mapplanes[ buildSide->planenum ];
289 if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )
292 // st-texcoords -> texMat block
293 // start out with dummy
294 VectorSet(buildSide->texMat[0], 1/32.0, 0, 0);
295 VectorSet(buildSide->texMat[1], 0, 1/32.0, 0);
297 // find surface for this side (by brute force)
299 // - meshverts point in pairs of three into verts
301 // - find the triangle that has most in common with our side
302 GetBestSurfaceTriangleMatchForBrushside(buildSide, vert);
305 /* get texture name */
306 if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )
307 texture = buildSide->shaderInfo->shader + 9;
309 texture = buildSide->shaderInfo->shader;
311 /* get plane points and offset by origin */
312 for( j = 0; j < 3; j++ )
314 VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
315 //% pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
316 //% pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
317 //% pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
320 if(vert[0] && vert[1] && vert[2])
326 vec2_t xyI, xyJ, xyK;
327 vec2_t stI, stJ, stK;
330 ComputeAxisBase(buildPlane->normal, texX, texY);
332 xyI[0] = DotProduct(vert[0]->xyz, texX);
333 xyI[1] = DotProduct(vert[0]->xyz, texY);
334 xyJ[0] = DotProduct(vert[1]->xyz, texX);
335 xyJ[1] = DotProduct(vert[1]->xyz, texY);
336 xyK[0] = DotProduct(vert[2]->xyz, texX);
337 xyK[1] = DotProduct(vert[2]->xyz, texY);
338 stI[0] = vert[0]->st[0]; stI[1] = vert[0]->st[1];
339 stJ[0] = vert[1]->st[0]; stJ[1] = vert[1]->st[1];
340 stK[0] = vert[2]->st[0]; stK[1] = vert[2]->st[1];
342 // - solve linear equations:
343 // - (x, y) := xyz . (texX, texY)
344 // - st[i] = texMat[i][0]*x + texMat[i][1]*y + texMat[i][2]
345 // (for three vertices)
353 for(i = 0; i < 2; ++i)
366 xyI[0], xyI[1], stI[i],
367 xyJ[0], xyJ[1], stJ[i],
368 xyK[0], xyK[1], stK[i]
370 VectorSet(buildSide->texMat[i], D0 / D, D1 / D, D2 / D);
375 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",
376 buildPlane->normal[0], buildPlane->normal[1], buildPlane->normal[2],
377 vert[0]->normal[0], vert[0]->normal[1], vert[0]->normal[2],
378 texX[0], texX[1], texX[2], texY[0], texY[1], texY[2],
379 vert[0]->xyz[0], vert[0]->xyz[1], vert[0]->xyz[2], xyI[0], xyI[1],
380 vert[1]->xyz[0], vert[1]->xyz[1], vert[1]->xyz[2], xyJ[0], xyJ[1],
381 vert[2]->xyz[0], vert[2]->xyz[1], vert[2]->xyz[2], xyK[0], xyK[1]
384 /* print brush side */
385 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
386 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
387 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
388 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
389 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
390 buildSide->texMat[0][0], buildSide->texMat[0][1], FRAC(buildSide->texMat[0][2]),
391 buildSide->texMat[1][0], buildSide->texMat[1][1], FRAC(buildSide->texMat[1][2]),
393 // DEBUG: valid ? 0 : C_DETAIL
399 // invert QuakeTextureVecs
403 vec2_t stI, stJ, stK;
409 TextureAxisFromPlane(buildPlane, vecs[0], vecs[1]);
423 stI[0] = vert[0]->st[0] * buildSide->shaderInfo->shaderWidth; stI[1] = vert[0]->st[1] * buildSide->shaderInfo->shaderHeight;
424 stJ[0] = vert[1]->st[0] * buildSide->shaderInfo->shaderWidth; stJ[1] = vert[1]->st[1] * buildSide->shaderInfo->shaderHeight;
425 stK[0] = vert[2]->st[0] * buildSide->shaderInfo->shaderWidth; stK[1] = vert[2]->st[1] * buildSide->shaderInfo->shaderHeight;
428 vert[0]->xyz[sv], vert[0]->xyz[tv], 1,
429 vert[1]->xyz[sv], vert[1]->xyz[tv], 1,
430 vert[2]->xyz[sv], vert[2]->xyz[tv], 1
434 for(i = 0; i < 2; ++i)
437 stI[i], vert[0]->xyz[tv], 1,
438 stJ[i], vert[1]->xyz[tv], 1,
439 stK[i], vert[2]->xyz[tv], 1
442 vert[0]->xyz[sv], stI[i], 1,
443 vert[1]->xyz[sv], stJ[i], 1,
444 vert[2]->xyz[sv], stK[i], 1
447 vert[0]->xyz[sv], vert[0]->xyz[tv], stI[i],
448 vert[1]->xyz[sv], vert[1]->xyz[tv], stJ[i],
449 vert[2]->xyz[sv], vert[2]->xyz[tv], stK[i]
451 VectorSet(sts[i], D0 / D, D1 / D, D2 / D);
456 fprintf(stderr, "degenerate triangle found when solving texDef equations\n"); // FIXME add stuff here
458 // now we must solve:
459 // // now we must invert:
460 // ang = rotate / 180 * Q_PI;
463 // ns = cosv * vecs[0][sv];
464 // nt = sinv * vecs[0][sv];
465 // vecsrotscaled[0][sv] = ns / scale[0];
466 // vecsrotscaled[0][tv] = nt / scale[0];
467 // ns = -sinv * vecs[1][tv];
468 // nt = cosv * vecs[1][tv];
469 // vecsrotscaled[1][sv] = ns / scale[1];
470 // vecsrotscaled[1][tv] = nt / scale[1];
471 scale[0] = 1.0/sqrt(sts[0][0] * sts[0][0] + sts[0][1] * sts[0][1]);
472 scale[1] = 1.0/sqrt(sts[1][0] * sts[1][0] + sts[1][1] * sts[1][1]);
473 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);
474 shift[0] = buildSide->shaderInfo->shaderWidth * FRAC(sts[0][2] / buildSide->shaderInfo->shaderWidth);
475 shift[1] = buildSide->shaderInfo->shaderHeight * FRAC(sts[1][2] / buildSide->shaderInfo->shaderHeight);
477 /* print brush side */
478 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
479 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s %.8f %.8f %.8f %.8f %.8f %d 0 0\n",
480 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
481 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
482 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
484 shift[0], shift[1], rotate, scale[0], scale[1],
485 // DEBUG: valid ? 0 : C_DETAIL
493 if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
494 if(strcmp(buildSide->shaderInfo->shader, "noshader"))
495 if(strcmp(buildSide->shaderInfo->shader, "default"))
497 fprintf(stderr, "no matching triangle for brushside using %s (hopefully nobody can see this side anyway)\n", buildSide->shaderInfo->shader);
498 texture = "common/WTF";
501 MakeNormalVectors( buildPlane->normal, vecs[ 0 ], vecs[ 1 ] );
502 VectorMA( vec3_origin, buildPlane->dist, buildPlane->normal, pts[ 0 ] );
503 VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
504 VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
507 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
508 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
509 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
510 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
511 1.0f/16.0f, 0.0f, 0.0f,
512 0.0f, 1.0f/16.0f, 0.0f,
514 // DEBUG: valid ? 0 : C_DETAIL
520 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s %.8f %.8f %.8f %.8f %.8f %d 0 0\n",
521 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
522 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
523 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
525 0.0f, 0.0f, 0.0f, 0.25f, 0.25f,
526 // DEBUG: valid ? 0 : C_DETAIL
536 fprintf( f, "\t}\n" );
538 fprintf( f, "\t}\n\n" );
543 /* iterate through the brush sides (ignore the first 6 bevel planes) */
544 for( i = 0; i < brush->numSides; i++ )
547 side = &bspBrushSides[ brush->firstSide + i ];
550 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
552 shader = &bspShaders[ side->shaderNum ];
553 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
556 /* get texture name */
557 if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
558 texture = shader->shader + 9;
560 texture = shader->shader;
563 plane = &bspPlanes[ side->planeNum ];
565 /* make plane points */
570 MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
571 VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
572 VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
573 VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
576 /* offset by origin */
577 for( j = 0; j < 3; j++ )
578 VectorAdd( pts[ j ], origin, pts[ j ] );
580 /* print brush side */
581 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
582 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",
583 pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
584 pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
585 pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
594 converts a bsp patch to a map patch
602 ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
610 static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )
620 if( ds->surfaceType != MST_PATCH )
624 if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )
626 shader = &bspShaders[ ds->shaderNum ];
628 /* get texture name */
629 if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
630 texture = shader->shader + 9;
632 texture = shader->shader;
635 fprintf( f, "\t// patch %d\n", num );
636 fprintf( f, "\t{\n" );
637 fprintf( f, "\t\tpatchDef2\n" );
638 fprintf( f, "\t\t{\n" );
639 fprintf( f, "\t\t\t%s\n", texture );
640 fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
641 fprintf( f, "\t\t\t(\n" );
643 /* iterate through the verts */
644 for( x = 0; x < ds->patchWidth; x++ )
647 fprintf( f, "\t\t\t\t(" );
649 /* iterate through the row */
650 for( y = 0; y < ds->patchHeight; y++ )
653 dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];
656 VectorAdd( origin, dv->xyz, xyz );
659 fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
663 fprintf( f, " )\n" );
667 fprintf( f, "\t\t\t)\n" );
668 fprintf( f, "\t\t}\n" );
669 fprintf( f, "\t}\n\n" );
676 exports a bsp model to a map file
679 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin, qboolean brushPrimitives )
683 bspDrawSurface_t *ds;
686 /* convert bsp planes to map planes */
687 nummapplanes = numBSPPlanes;
688 AUTOEXPAND_BY_REALLOC(mapplanes, nummapplanes, allocatedmapplanes, 1024);
689 for( i = 0; i < numBSPPlanes; i++ )
691 VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
692 mapplanes[ i ].dist = bspPlanes[ i ].dist;
693 mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
694 mapplanes[ i ].hash_chain = 0;
697 /* allocate a build brush */
698 buildBrush = AllocBrush( 512 );
699 buildBrush->entityNum = 0;
700 buildBrush->original = buildBrush;
702 if(origin[0] != 0 || origin[1] != 0 || origin[2] != 0)
703 ConvertOriginBrush(f, -1, origin, brushPrimitives);
705 /* go through each brush in the model */
706 for( i = 0; i < model->numBSPBrushes; i++ )
708 num = i + model->firstBSPBrush;
709 brush = &bspBrushes[ num ];
710 ConvertBrush( f, num, brush, origin, brushPrimitives );
713 /* free the build brush */
716 /* go through each drawsurf in the model */
717 for( i = 0; i < model->numBSPSurfaces; i++ )
719 num = i + model->firstBSPSurface;
720 ds = &bspDrawSurfaces[ num ];
722 /* we only love patches */
723 if( ds->surfaceType == MST_PATCH )
724 ConvertPatch( f, num, ds, origin );
732 exports entity key/value pairs to a map file
735 static void ConvertEPairs( FILE *f, entity_t *e, qboolean skip_origin )
741 for( ep = e->epairs; ep != NULL; ep = ep->next )
743 /* ignore empty keys/values */
744 if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )
747 /* ignore model keys with * prefixed values */
748 if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )
751 /* ignore origin keys if skip_origin is set */
752 if( skip_origin && !Q_stricmp( ep->key, "origin" ) )
756 fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
764 exports an quake map file from the bsp
767 int ConvertBSPToMap_Ext( char *bspName, qboolean brushPrimitives )
775 char name[ 1024 ], base[ 1024 ];
779 Sys_Printf( "--- Convert BSP to MAP ---\n" );
781 /* create the bsp filename from the bsp name */
782 strcpy( name, bspName );
783 StripExtension( name );
784 strcat( name, "_converted.map" );
785 Sys_Printf( "writing %s\n", name );
787 ExtractFileBase( bspName, base );
788 strcat( base, ".bsp" );
791 f = fopen( name, "wb" );
793 Error( "Open failed on %s\n", name );
796 fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
798 /* walk entity list */
799 for( i = 0; i < numEntities; i++ )
805 fprintf( f, "// entity %d\n", i );
813 value = ValueForKey( e, "model" );
814 if( value[ 0 ] == '*' )
815 modelNum = atoi( value + 1 );
821 ConvertEPairs( f, e, modelNum >= 0 );
824 /* only handle bsp models */
828 model = &bspModels[ modelNum ];
830 /* get entity origin */
831 value = ValueForKey( e, "origin" );
832 if( value[ 0 ] == '\0' )
833 VectorClear( origin );
835 GetVectorForKey( e, "origin", origin );
838 ConvertModel( f, model, modelNum, origin, brushPrimitives );
842 fprintf( f, "}\n\n" );
845 /* close the file and return */
848 /* return to sender */
852 int ConvertBSPToMap( char *bspName )
854 return ConvertBSPToMap_Ext(bspName, qfalse);
857 int ConvertBSPToMap_BP( char *bspName )
859 return ConvertBSPToMap_Ext(bspName, qtrue);