]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/patch.c
split bsp conversion stuff from q3map2
[xonotic/netradiant.git] / tools / quake3 / q3map2 / patch.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 PATCH_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42    ExpandLongestCurve() - ydnar
43    finds length of quadratic curve specified and determines if length is longer than the supplied max
44  */
45
46 #define APPROX_SUBDIVISION  8
47
48 static void ExpandLongestCurve( float *longestCurve, vec3_t a, vec3_t b, vec3_t c ){
49         int i;
50         float t, len;
51         vec3_t ab, bc, ac, pt, last, delta;
52
53
54         /* calc vectors */
55         VectorSubtract( b, a, ab );
56         if ( VectorNormalize( ab, ab ) < 0.125f ) {
57                 return;
58         }
59         VectorSubtract( c, b, bc );
60         if ( VectorNormalize( bc, bc ) < 0.125f ) {
61                 return;
62         }
63         VectorSubtract( c, a, ac );
64         if ( VectorNormalize( ac, ac ) < 0.125f ) {
65                 return;
66         }
67
68         /* if all 3 vectors are the same direction, then this edge is linear, so we ignore it */
69         if ( DotProduct( ab, bc ) > 0.99f && DotProduct( ab, ac ) > 0.99f ) {
70                 return;
71         }
72
73         /* recalculate vectors */
74         VectorSubtract( b, a, ab );
75         VectorSubtract( c, b, bc );
76
77         /* determine length */
78         VectorCopy( a, last );
79         for ( i = 0, len = 0.0f, t = 0.0f; i < APPROX_SUBDIVISION; i++, t += ( 1.0f / APPROX_SUBDIVISION ) )
80         {
81                 /* calculate delta */
82                 delta[ 0 ] = ( ( 1.0f - t ) * ab[ 0 ] ) + ( t * bc[ 0 ] );
83                 delta[ 1 ] = ( ( 1.0f - t ) * ab[ 1 ] ) + ( t * bc[ 1 ] );
84                 delta[ 2 ] = ( ( 1.0f - t ) * ab[ 2 ] ) + ( t * bc[ 2 ] );
85
86                 /* add to first point and calculate pt-pt delta */
87                 VectorAdd( a, delta, pt );
88                 VectorSubtract( pt, last, delta );
89
90                 /* add it to length and store last point */
91                 len += VectorLength( delta );
92                 VectorCopy( pt, last );
93         }
94
95         /* longer? */
96         if ( len > *longestCurve ) {
97                 *longestCurve = len;
98         }
99 }
100
101
102
103 /*
104    ExpandMaxIterations() - ydnar
105    determines how many iterations a quadratic curve needs to be subdivided with to fit the specified error
106  */
107
108 static void ExpandMaxIterations( int *maxIterations, int maxError, vec3_t a, vec3_t b, vec3_t c ){
109         int i, j;
110         vec3_t prev, next, mid, delta, delta2;
111         float len, len2;
112         int numPoints, iterations;
113         vec3_t points[ MAX_EXPANDED_AXIS ];
114
115
116         /* initial setup */
117         numPoints = 3;
118         VectorCopy( a, points[ 0 ] );
119         VectorCopy( b, points[ 1 ] );
120         VectorCopy( c, points[ 2 ] );
121
122         /* subdivide */
123         for ( i = 0; i + 2 < numPoints; i += 2 )
124         {
125                 /* check subdivision limit */
126                 if ( numPoints + 2 >= MAX_EXPANDED_AXIS ) {
127                         break;
128                 }
129
130                 /* calculate new curve deltas */
131                 for ( j = 0; j < 3; j++ )
132                 {
133                         prev[ j ] = points[ i + 1 ][ j ] - points[ i ][ j ];
134                         next[ j ] = points[ i + 2 ][ j ] - points[ i + 1 ][ j ];
135                         mid[ j ] = ( points[ i ][ j ] + points[ i + 1 ][ j ] * 2.0f + points[ i + 2 ][ j ] ) * 0.25f;
136                 }
137
138                 /* see if this midpoint is off far enough to subdivide */
139                 VectorSubtract( points[ i + 1 ], mid, delta );
140                 len = VectorLength( delta );
141                 if ( len < maxError ) {
142                         continue;
143                 }
144
145                 /* subdivide */
146                 numPoints += 2;
147
148                 /* create new points */
149                 for ( j = 0; j < 3; j++ )
150                 {
151                         prev[ j ] = 0.5f * ( points[ i ][ j ] + points[ i + 1 ][ j ] );
152                         next[ j ] = 0.5f * ( points[ i + 1 ][ j ] + points[ i + 2 ][ j ] );
153                         mid[ j ] = 0.5f * ( prev[ j ] + next[ j ] );
154                 }
155
156                 /* push points out */
157                 for ( j = numPoints - 1; j > i + 3; j-- )
158                         VectorCopy( points[ j - 2 ], points[ j ] );
159
160                 /* insert new points */
161                 VectorCopy( prev, points[ i + 1 ] );
162                 VectorCopy( mid, points[ i + 2 ] );
163                 VectorCopy( next, points[ i + 3 ] );
164
165                 /* back up and recheck this set again, it may need more subdivision */
166                 i -= 2;
167         }
168
169         /* put the line on the curve */
170         for ( i = 1; i < numPoints; i += 2 )
171         {
172                 for ( j = 0; j < 3; j++ )
173                 {
174                         prev[ j ] = 0.5f * ( points[ i ][ j ] + points[ i + 1 ][ j ] );
175                         next[ j ] = 0.5f * ( points[ i ][ j ] + points[ i - 1 ][ j ] );
176                         points[ i ][ j ] = 0.5f * ( prev[ j ] + next[ j ] );
177                 }
178         }
179
180         /* eliminate linear sections */
181         for ( i = 0; i + 2 < numPoints; i++ )
182         {
183                 /* create vectors */
184                 VectorSubtract( points[ i + 1 ], points[ i ], delta );
185                 len = VectorNormalize( delta, delta );
186                 VectorSubtract( points[ i + 2 ], points[ i + 1 ], delta2 );
187                 len2 = VectorNormalize( delta2, delta2 );
188
189                 /* if either edge is degenerate, then eliminate it */
190                 if ( len < 0.0625f || len2 < 0.0625f || DotProduct( delta, delta2 ) >= 1.0f ) {
191                         for ( j = i + 1; j + 1 < numPoints; j++ )
192                                 VectorCopy( points[ j + 1 ], points[ j ] );
193                         numPoints--;
194                         continue;
195                 }
196         }
197
198         /* the number of iterations is 2^(points - 1) - 1 */
199         numPoints >>= 1;
200         iterations = 0;
201         while ( numPoints > 1 )
202         {
203                 numPoints >>= 1;
204                 iterations++;
205         }
206
207         /* more? */
208         if ( iterations > *maxIterations ) {
209                 *maxIterations = iterations;
210         }
211 }
212
213
214
215 /*
216    ParsePatch()
217    creates a mapDrawSurface_t from the patch text
218  */
219
220 void ParsePatch( qboolean onlyLights ){
221         vec_t info[ 5 ];
222         int i, j, k;
223         parseMesh_t     *pm;
224         char texture[ MAX_QPATH ];
225         char shader[ MAX_QPATH ];
226         mesh_t m;
227         bspDrawVert_t   *verts;
228         epair_t         *ep;
229         vec4_t delta, delta2, delta3;
230         qboolean degenerate;
231         float longestCurve;
232         int maxIterations;
233
234         MatchToken( "{" );
235
236         /* get texture */
237         GetToken( qtrue );
238         strcpy( texture, token );
239
240         Parse1DMatrix( 5, info );
241         m.width = info[0];
242         m.height = info[1];
243         m.verts = verts = safe_malloc( m.width * m.height * sizeof( m.verts[0] ) );
244
245         if ( m.width < 0 || m.width > MAX_PATCH_SIZE || m.height < 0 || m.height > MAX_PATCH_SIZE ) {
246                 Error( "ParsePatch: bad size" );
247         }
248
249         MatchToken( "(" );
250         for ( j = 0; j < m.width ; j++ )
251         {
252                 MatchToken( "(" );
253                 for ( i = 0; i < m.height ; i++ )
254                 {
255                         Parse1DMatrix( 5, verts[ i * m.width + j ].xyz );
256
257                         /* ydnar: fix colors */
258                         for ( k = 0; k < MAX_LIGHTMAPS; k++ )
259                         {
260                                 verts[ i * m.width + j ].color[ k ][ 0 ] = 255;
261                                 verts[ i * m.width + j ].color[ k ][ 1 ] = 255;
262                                 verts[ i * m.width + j ].color[ k ][ 2 ] = 255;
263                                 verts[ i * m.width + j ].color[ k ][ 3 ] = 255;
264                         }
265                 }
266                 MatchToken( ")" );
267         }
268         MatchToken( ")" );
269
270         // if brush primitives format, we may have some epairs to ignore here
271         GetToken( qtrue );
272         if ( g_bBrushPrimit != BPRIMIT_OLDBRUSHES && strcmp( token,"}" ) ) {
273                 ep = ParseEPair();
274                 free( ep->key );
275                 free( ep->value );
276                 free( ep );
277         }
278         else{
279                 UnGetToken();
280         }
281
282         MatchToken( "}" );
283         MatchToken( "}" );
284
285         /* short circuit */
286         if ( noCurveBrushes || onlyLights ) {
287                 return;
288         }
289
290
291         /* ydnar: delete and warn about degenerate patches */
292         j = ( m.width * m.height );
293         VectorClear( delta );
294         delta[ 3 ] = 0;
295         degenerate = qtrue;
296
297         /* find first valid vector */
298         for ( i = 1; i < j && delta[ 3 ] == 0; i++ )
299         {
300                 VectorSubtract( m.verts[ 0 ].xyz, m.verts[ i ].xyz, delta );
301                 delta[ 3 ] = VectorNormalize( delta, delta );
302         }
303
304         /* secondary degenerate test */
305         if ( delta[ 3 ] == 0 ) {
306                 degenerate = qtrue;
307         }
308         else
309         {
310                 /* if all vectors match this or are zero, then this is a degenerate patch */
311                 for ( i = 1; i < j && degenerate == qtrue; i++ )
312                 {
313                         VectorSubtract( m.verts[ 0 ].xyz, m.verts[ i ].xyz, delta2 );
314                         delta2[ 3 ] = VectorNormalize( delta2, delta2 );
315                         if ( delta2[ 3 ] != 0 ) {
316                                 /* create inverse vector */
317                                 VectorCopy( delta2, delta3 );
318                                 delta3[ 3 ] = delta2[ 3 ];
319                                 VectorInverse( delta3 );
320
321                                 /* compare */
322                                 if ( VectorCompare( delta, delta2 ) == qfalse && VectorCompare( delta, delta3 ) == qfalse ) {
323                                         degenerate = qfalse;
324                                 }
325                         }
326                 }
327         }
328
329         /* warn and select degenerate patch */
330         if ( degenerate ) {
331                 xml_Select( "degenerate patch", mapEnt->mapEntityNum, entitySourceBrushes, qfalse );
332                 free( m.verts );
333                 return;
334         }
335
336         /* find longest curve on the mesh */
337         longestCurve = 0.0f;
338         maxIterations = 0;
339         for ( j = 0; j + 2 < m.width; j += 2 )
340         {
341                 for ( i = 0; i + 2 < m.height; i += 2 )
342                 {
343                         ExpandLongestCurve( &longestCurve, verts[ i * m.width + j ].xyz, verts[ i * m.width + ( j + 1 ) ].xyz, verts[ i * m.width + ( j + 2 ) ].xyz );      /* row */
344                         ExpandLongestCurve( &longestCurve, verts[ i * m.width + j ].xyz, verts[ ( i + 1 ) * m.width + j ].xyz, verts[ ( i + 2 ) * m.width + j ].xyz );      /* col */
345                         ExpandMaxIterations( &maxIterations, patchSubdivisions, verts[ i * m.width + j ].xyz, verts[ i * m.width + ( j + 1 ) ].xyz, verts[ i * m.width + ( j + 2 ) ].xyz );     /* row */
346                         ExpandMaxIterations( &maxIterations, patchSubdivisions, verts[ i * m.width + j ].xyz, verts[ ( i + 1 ) * m.width + j ].xyz, verts[ ( i + 2 ) * m.width + j ].xyz  );    /* col */
347                 }
348         }
349
350         /* allocate patch mesh */
351         pm = safe_malloc( sizeof( *pm ) );
352         memset( pm, 0, sizeof( *pm ) );
353
354         /* ydnar: add entity/brush numbering */
355         pm->entityNum = mapEnt->mapEntityNum;
356         pm->brushNum = entitySourceBrushes;
357
358         /* set shader */
359         sprintf( shader, "textures/%s", texture );
360         pm->shaderInfo = ShaderInfoForShader( shader );
361
362         /* set mesh */
363         pm->mesh = m;
364
365         /* set longest curve */
366         pm->longestCurve = longestCurve;
367         pm->maxIterations = maxIterations;
368
369         /* link to the entity */
370         pm->next = mapEnt->patches;
371         mapEnt->patches = pm;
372 }
373
374
375
376 /*
377    GrowGroup_r()
378    recursively adds patches to a lod group
379  */
380
381 static void GrowGroup_r( parseMesh_t *pm, int patchNum, int patchCount, parseMesh_t **meshes, byte *bordering, byte *group ){
382         int i;
383         const byte  *row;
384
385
386         /* early out check */
387         if ( group[ patchNum ] ) {
388                 return;
389         }
390
391
392         /* set it */
393         group[ patchNum ] = 1;
394         row = bordering + patchNum * patchCount;
395
396         /* check maximums */
397         if ( meshes[ patchNum ]->longestCurve > pm->longestCurve ) {
398                 pm->longestCurve = meshes[ patchNum ]->longestCurve;
399         }
400         if ( meshes[ patchNum ]->maxIterations > pm->maxIterations ) {
401                 pm->maxIterations = meshes[ patchNum ]->maxIterations;
402         }
403
404         /* walk other patches */
405         for ( i = 0; i < patchCount; i++ )
406         {
407                 if ( row[ i ] ) {
408                         GrowGroup_r( pm, i, patchCount, meshes, bordering, group );
409                 }
410         }
411 }
412
413
414 /*
415    PatchMapDrawSurfs()
416    any patches that share an edge need to choose their
417    level of detail as a unit, otherwise the edges would
418    pull apart.
419  */
420
421 void PatchMapDrawSurfs( entity_t *e ){
422         int i, j, k, l, c1, c2;
423         parseMesh_t             *pm;
424         parseMesh_t             *check, *scan;
425         mapDrawSurface_t        *ds;
426         int patchCount, groupCount;
427         bspDrawVert_t           *v1, *v2;
428         vec3_t bounds[ 2 ];
429         byte                    *bordering;
430
431         /* ydnar: mac os x fails with these if not static */
432         MAC_STATIC parseMesh_t  *meshes[ MAX_MAP_DRAW_SURFS ];
433         MAC_STATIC qb_t grouped[ MAX_MAP_DRAW_SURFS ];
434         MAC_STATIC byte group[ MAX_MAP_DRAW_SURFS ];
435
436
437         /* note it */
438         Sys_FPrintf( SYS_VRB, "--- PatchMapDrawSurfs ---\n" );
439
440         patchCount = 0;
441         for ( pm = e->patches ; pm ; pm = pm->next  ) {
442                 meshes[patchCount] = pm;
443                 patchCount++;
444         }
445
446         if ( !patchCount ) {
447                 return;
448         }
449         bordering = safe_malloc( patchCount * patchCount );
450         memset( bordering, 0, patchCount * patchCount );
451
452         // build the bordering matrix
453         for ( k = 0 ; k < patchCount ; k++ ) {
454                 bordering[k * patchCount + k] = 1;
455
456                 for ( l = k + 1 ; l < patchCount ; l++ ) {
457                         check = meshes[k];
458                         scan = meshes[l];
459                         c1 = scan->mesh.width * scan->mesh.height;
460                         v1 = scan->mesh.verts;
461
462                         for ( i = 0 ; i < c1 ; i++, v1++ ) {
463                                 c2 = check->mesh.width * check->mesh.height;
464                                 v2 = check->mesh.verts;
465                                 for ( j = 0 ; j < c2 ; j++, v2++ ) {
466                                         if ( fabs( v1->xyz[0] - v2->xyz[0] ) < 1.0
467                                                  && fabs( v1->xyz[1] - v2->xyz[1] ) < 1.0
468                                                  && fabs( v1->xyz[2] - v2->xyz[2] ) < 1.0 ) {
469                                                 break;
470                                         }
471                                 }
472                                 if ( j != c2 ) {
473                                         break;
474                                 }
475                         }
476                         if ( i != c1 ) {
477                                 // we have a connection
478                                 bordering[k * patchCount + l] =
479                                         bordering[l * patchCount + k] = 1;
480                         }
481                         else {
482                                 // no connection
483                                 bordering[k * patchCount + l] =
484                                         bordering[l * patchCount + k] = 0;
485                         }
486
487                 }
488         }
489
490         /* build groups */
491         memset( grouped, 0, patchCount );
492         groupCount = 0;
493         for ( i = 0; i < patchCount; i++ )
494         {
495                 /* get patch */
496                 scan = meshes[ i ];
497
498                 /* start a new group */
499                 if ( !grouped[ i ] ) {
500                         groupCount++;
501                 }
502
503                 /* recursively find all patches that belong in the same group */
504                 memset( group, 0, patchCount );
505                 GrowGroup_r( scan, i, patchCount, meshes, bordering, group );
506
507                 /* bound them */
508                 ClearBounds( bounds[ 0 ], bounds[ 1 ] );
509                 for ( j = 0; j < patchCount; j++ )
510                 {
511                         if ( group[ j ] ) {
512                                 grouped[ j ] = qtrue;
513                                 check = meshes[ j ];
514                                 c1 = check->mesh.width * check->mesh.height;
515                                 v1 = check->mesh.verts;
516                                 for ( k = 0; k < c1; k++, v1++ )
517                                         AddPointToBounds( v1->xyz, bounds[ 0 ], bounds[ 1 ] );
518                         }
519                 }
520
521                 /* debug code */
522                 //%     Sys_Printf( "Longest curve: %f Iterations: %d\n", scan->longestCurve, scan->maxIterations );
523
524                 /* create drawsurf */
525                 scan->grouped = qtrue;
526                 ds = DrawSurfaceForMesh( e, scan, NULL );   /* ydnar */
527                 VectorCopy( bounds[ 0 ], ds->bounds[ 0 ] );
528                 VectorCopy( bounds[ 1 ], ds->bounds[ 1 ] );
529         }
530
531         /* emit some statistics */
532         Sys_FPrintf( SYS_VRB, "%9d patches\n", patchCount );
533         Sys_FPrintf( SYS_VRB, "%9d patch LOD groups\n", groupCount );
534 }