2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
41 typedef struct edgePoint_s {
44 struct edgePoint_s *prev, *next;
47 typedef struct edgeLine_s {
57 edgePoint_t chain; // unused element of doubly linked list
65 #define MAX_ORIGINAL_EDGES 0x10000
66 originalEdge_t originalEdges[MAX_ORIGINAL_EDGES];
70 #define MAX_EDGE_LINES 0x10000
71 edgeLine_t edgeLines[MAX_EDGE_LINES];
74 int c_degenerateEdges;
78 int c_natural, c_rotate, c_cant;
80 // these should be whatever epsilon we actually expect,
81 // plus SNAP_INT_TO_FLOAT
82 #define LINE_POSITION_EPSILON 0.25
83 #define POINT_ON_LINE_EPSILON 0.25
90 void InsertPointOnEdge( vec3_t v, edgeLine_t *e ) {
93 edgePoint_t *p, *scan;
95 VectorSubtract( v, e->origin, delta );
96 d = DotProduct( delta, e->dir );
98 p = safe_malloc( sizeof(edgePoint_t) );
100 VectorCopy( v, p->xyz );
102 if ( e->chain.next == &e->chain ) {
103 e->chain.next = e->chain.prev = p;
104 p->next = p->prev = &e->chain;
108 scan = e->chain.next;
109 for ( ; scan != &e->chain ; scan = scan->next ) {
110 d = p->intercept - scan->intercept;
111 if ( d > -LINE_POSITION_EPSILON && d < LINE_POSITION_EPSILON ) {
113 return; // the point is already set
116 if ( p->intercept < scan->intercept ) {
118 p->prev = scan->prev;
120 scan->prev->next = p;
127 p->prev = scan->prev;
129 scan->prev->next = p;
139 int AddEdge( vec3_t v1, vec3_t v2, qboolean createNonAxial ) {
145 VectorSubtract( v2, v1, dir );
146 d = VectorNormalize( dir, dir );
148 // if we added a 0 length vector, it would make degenerate planes
153 if ( !createNonAxial ) {
154 if ( fabs( dir[0] + dir[1] + dir[2] ) != 1.0 ) {
155 if ( numOriginalEdges == MAX_ORIGINAL_EDGES ) {
156 Error( "MAX_ORIGINAL_EDGES" );
158 originalEdges[ numOriginalEdges ].dv[0] = (bspDrawVert_t *)v1;
159 originalEdges[ numOriginalEdges ].dv[1] = (bspDrawVert_t *)v2;
160 originalEdges[ numOriginalEdges ].length = d;
166 for ( i = 0 ; i < numEdgeLines ; i++ ) {
169 d = DotProduct( v1, e->normal1 ) - e->dist1;
170 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
173 d = DotProduct( v1, e->normal2 ) - e->dist2;
174 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
178 d = DotProduct( v2, e->normal1 ) - e->dist1;
179 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
182 d = DotProduct( v2, e->normal2 ) - e->dist2;
183 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
188 InsertPointOnEdge( v1, e );
189 InsertPointOnEdge( v2, e );
194 if ( numEdgeLines >= MAX_EDGE_LINES ) {
195 Error( "MAX_EDGE_LINES" );
198 e = &edgeLines[ numEdgeLines ];
201 e->chain.next = e->chain.prev = &e->chain;
203 VectorCopy( v1, e->origin );
204 VectorCopy( dir, e->dir );
206 MakeNormalVectors( e->dir, e->normal1, e->normal2 );
207 e->dist1 = DotProduct( e->origin, e->normal1 );
208 e->dist2 = DotProduct( e->origin, e->normal2 );
210 InsertPointOnEdge( v1, e );
211 InsertPointOnEdge( v2, e );
213 return numEdgeLines - 1;
220 adds a surface's edges
223 void AddSurfaceEdges( mapDrawSurface_t *ds )
228 for( i = 0; i < ds->numVerts; i++ )
230 /* save the edge number in the lightmap field so we don't need to look it up again */
231 ds->verts[i].lightmap[ 0 ][ 0 ] =
232 AddEdge( ds->verts[ i ].xyz, ds->verts[ (i + 1) % ds->numVerts ].xyz, qfalse );
240 determines if an edge is colinear
243 qboolean ColinearEdge( vec3_t v1, vec3_t v2, vec3_t v3 )
245 vec3_t midpoint, dir, offset, on;
248 VectorSubtract( v2, v1, midpoint );
249 VectorSubtract( v3, v1, dir );
250 d = VectorNormalize( dir, dir );
252 return qfalse; // degenerate
255 d = DotProduct( midpoint, dir );
256 VectorScale( dir, d, on );
257 VectorSubtract( midpoint, on, offset );
258 d = VectorLength ( offset );
273 Add colinear border edges, which will fix some classes of patch to
277 void AddPatchEdges( mapDrawSurface_t *ds ) {
281 for ( i = 0 ; i < ds->patchWidth - 2; i+=2 ) {
282 v1 = ds->verts[ i ].xyz;
283 v2 = ds->verts[ i + 1 ].xyz;
284 v3 = ds->verts[ i + 2 ].xyz;
286 // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3
287 if ( ColinearEdge( v1, v2, v3 ) ) {
288 AddEdge( v1, v3, qfalse );
291 v1 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i ].xyz;
292 v2 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i + 1 ].xyz;
293 v3 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i + 2 ].xyz;
295 // if v2 is on the v1 to v3 line, add an edge from v1 to v3
296 if ( ColinearEdge( v1, v2, v3 ) ) {
297 AddEdge( v1, v3, qfalse );
301 for ( i = 0 ; i < ds->patchHeight - 2 ; i+=2 ) {
302 v1 = ds->verts[ i * ds->patchWidth ].xyz;
303 v2 = ds->verts[ ( i + 1 ) * ds->patchWidth ].xyz;
304 v3 = ds->verts[ ( i + 2 ) * ds->patchWidth ].xyz;
306 // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3
307 if ( ColinearEdge( v1, v2, v3 ) ) {
308 AddEdge( v1, v3, qfalse );
311 v1 = ds->verts[ ( ds->patchWidth - 1 ) + i * ds->patchWidth ].xyz;
312 v2 = ds->verts[ ( ds->patchWidth - 1 ) + ( i + 1 ) * ds->patchWidth ].xyz;
313 v3 = ds->verts[ ( ds->patchWidth - 1 ) + ( i + 2 ) * ds->patchWidth ].xyz;
315 // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3
316 if ( ColinearEdge( v1, v2, v3 ) ) {
317 AddEdge( v1, v3, qfalse );
330 #define MAX_SURFACE_VERTS 256
331 void FixSurfaceJunctions( mapDrawSurface_t *ds ) {
336 int counts[MAX_SURFACE_VERTS];
337 int originals[MAX_SURFACE_VERTS];
338 int firstVert[MAX_SURFACE_VERTS];
339 bspDrawVert_t verts[MAX_SURFACE_VERTS], *v1, *v2;
341 float start, end, frac, c;
345 originalVerts = ds->numVerts;
348 for ( i = 0 ; i < ds->numVerts ; i++ )
351 firstVert[i] = numVerts;
354 if ( numVerts == MAX_SURFACE_VERTS ) {
355 Error( "MAX_SURFACE_VERTS" );
357 verts[numVerts] = ds->verts[i];
358 originals[numVerts] = i;
361 // check to see if there are any t junctions before the next vert
363 v2 = &ds->verts[ (i+1) % ds->numVerts ];
365 j = (int)ds->verts[i].lightmap[ 0 ][ 0 ];
367 continue; // degenerate edge
371 VectorSubtract( v1->xyz, e->origin, delta );
372 start = DotProduct( delta, e->dir );
374 VectorSubtract( v2->xyz, e->origin, delta );
375 end = DotProduct( delta, e->dir );
384 for ( ; p != &e->chain ; ) {
386 if ( p->intercept > end - ON_EPSILON ) {
390 if ( p->intercept < end + ON_EPSILON ) {
396 ( start < end && p->intercept > start + ON_EPSILON ) ||
397 ( start > end && p->intercept < start - ON_EPSILON ) ) {
399 if ( numVerts == MAX_SURFACE_VERTS ) {
400 Error( "MAX_SURFACE_VERTS" );
403 /* take the exact intercept point */
404 VectorCopy( p->xyz, verts[ numVerts ].xyz );
406 /* interpolate the texture coordinates */
407 frac = ( p->intercept - start ) / ( end - start );
408 for ( j = 0 ; j < 2 ; j++ ) {
409 verts[ numVerts ].st[j] = v1->st[j] +
410 frac * ( v2->st[j] - v1->st[j] );
413 /* copy the normal (FIXME: what about nonplanar surfaces? */
414 VectorCopy( v1->normal, verts[ numVerts ].normal );
416 /* ydnar: interpolate the color */
417 for( k = 0; k < MAX_LIGHTMAPS; k++ )
419 for( j = 0; j < 4; j++ )
421 c = (float) v1->color[ k ][ j ] + frac * ((float) v2->color[ k ][ j ] - (float) v1->color[ k ][ j ]);
422 verts[ numVerts ].color[ k ][ j ] = (byte) (c < 255.0f ? c : 255);
427 originals[ numVerts ] = i;
440 c_addedVerts += numVerts - ds->numVerts;
441 c_totalVerts += numVerts;
444 // FIXME: check to see if the entire surface degenerated
447 // rotate the points so that the initial vertex is between
448 // two non-subdivided edges
449 for ( i = 0 ; i < numVerts ; i++ ) {
450 if ( originals[ (i+1) % numVerts ] == originals[ i ] ) {
453 j = (i + numVerts - 1 ) % numVerts;
454 k = (i + numVerts - 2 ) % numVerts;
455 if ( originals[ j ] == originals[ k ] ) {
462 // fine the way it is
465 ds->numVerts = numVerts;
466 ds->verts = safe_malloc( numVerts * sizeof( *ds->verts ) );
467 memcpy( ds->verts, verts, numVerts * sizeof( *ds->verts ) );
471 if ( i == numVerts ) {
472 // create a vertex in the middle to start the fan
476 memset ( &verts[numVerts], 0, sizeof( verts[numVerts] ) );
477 for ( i = 0 ; i < numVerts ; i++ ) {
478 for ( j = 0 ; j < 10 ; j++ ) {
479 verts[numVerts].xyz[j] += verts[i].xyz[j];
482 for ( j = 0 ; j < 10 ; j++ ) {
483 verts[numVerts].xyz[j] /= numVerts;
490 // just rotate the vertexes
495 ds->numVerts = numVerts;
496 ds->verts = safe_malloc( numVerts * sizeof( *ds->verts ) );
498 for ( j = 0 ; j < ds->numVerts ; j++ ) {
499 ds->verts[j] = verts[ ( j + i ) % ds->numVerts ];
508 FixBrokenSurface() - ydnar
509 removes nearly coincident verts from a planar winding surface
510 returns qfalse if the surface is broken
513 extern void SnapWeldVector( vec3_t a, vec3_t b, vec3_t out );
515 #define DEGENERATE_EPSILON 0.1
519 qboolean FixBrokenSurface( mapDrawSurface_t *ds )
521 qboolean valid = qtrue;
522 bspDrawVert_t *dv1, *dv2, avg;
530 if( ds->type != SURFACE_FACE )
533 /* check all verts */
534 for( i = 0; i < ds->numVerts; i++ )
536 /* don't remove points if winding is a triangle */
537 if( ds->numVerts == 3 )
541 dv1 = &ds->verts[ i ];
542 dv2 = &ds->verts[ (i + 1) % ds->numVerts ];
544 /* degenerate edge? */
545 VectorSubtract( dv1->xyz, dv2->xyz, avg.xyz );
546 dist = VectorLength( avg.xyz );
547 if( dist < DEGENERATE_EPSILON )
550 Sys_FPrintf( SYS_VRB, "WARNING: Degenerate T-junction edge found, fixing...\n" );
552 /* create an average drawvert */
553 /* ydnar 2002-01-26: added nearest-integer welding preference */
554 SnapWeldVector( dv1->xyz, dv2->xyz, avg.xyz );
555 VectorAdd( dv1->normal, dv2->normal, avg.normal );
556 VectorNormalize( avg.normal, avg.normal );
557 avg.st[ 0 ] = (dv1->st[ 0 ] + dv2->st[ 0 ]) * 0.5f;
558 avg.st[ 1 ] = (dv1->st[ 1 ] + dv2->st[ 1 ]) * 0.5f;
560 /* lightmap st/colors */
561 for( k = 0; k < MAX_LIGHTMAPS; k++ )
563 avg.lightmap[ k ][ 0 ] = (dv1->lightmap[ k ][ 0 ] + dv2->lightmap[ k ][ 0 ]) * 0.5f;
564 avg.lightmap[ k ][ 1 ] = (dv1->lightmap[ k ][ 1 ] + dv2->lightmap[ k ][ 1 ]) * 0.5f;
565 for( j = 0; j < 4; j++ )
566 avg.color[ k ][ j ] = (int) (dv1->color[ k ][ j ] + dv2->color[ k ][ j ]) >> 1;
570 memcpy( dv1, &avg, sizeof( avg ) );
572 /* move the remaining verts */
573 for( k = i + 2; k < ds->numVerts; k++ )
576 dv1 = &ds->verts[ k ];
577 dv2 = &ds->verts[ k - 1 ];
580 memcpy( dv2, dv1, sizeof( bspDrawVert_t ) );
586 /* one last check and return */
587 if( ds->numVerts < 3 )
605 int EdgeCompare( const void *elem1, const void *elem2 ) {
608 d1 = ((originalEdge_t *)elem1)->length;
609 d2 = ((originalEdge_t *)elem2)->length;
624 call after the surface list has been pruned
627 void FixTJunctions( entity_t *ent )
630 mapDrawSurface_t *ds;
636 /* meta mode has its own t-junction code (currently not as good as this code) */
641 Sys_FPrintf( SYS_VRB, "--- FixTJunctions ---\n" );
643 numOriginalEdges = 0;
646 // this actually creates axial edges, but it
647 // only creates originalEdge_t structures
648 // for non-axial edges
649 for ( i = ent->firstDrawSurf ; i < numMapDrawSurfs ; i++ )
651 /* get surface and early out if possible */
652 ds = &mapDrawSurfs[ i ];
654 if( (si->compileFlags & C_NODRAW) || si->autosprite || si->notjunc || ds->numVerts == 0 )
657 /* ydnar: gs mods: handle the various types of surfaces */
660 /* handle brush faces */
662 AddSurfaceEdges( ds );
670 /* fixme: make triangle surfaces t-junction */
676 axialEdgeLines = numEdgeLines;
678 // sort the non-axial edges by length
679 qsort( originalEdges, numOriginalEdges, sizeof(originalEdges[0]), EdgeCompare );
681 // add the non-axial edges, longest first
682 // this gives the most accurate edge description
683 for ( i = 0 ; i < numOriginalEdges ; i++ ) {
684 e = &originalEdges[i];
685 e->dv[ 0 ]->lightmap[ 0 ][ 0 ] = AddEdge( e->dv[ 0 ]->xyz, e->dv[ 1 ]->xyz, qtrue );
688 Sys_FPrintf( SYS_VRB, "%9d axial edge lines\n", axialEdgeLines );
689 Sys_FPrintf( SYS_VRB, "%9d non-axial edge lines\n", numEdgeLines - axialEdgeLines );
690 Sys_FPrintf( SYS_VRB, "%9d degenerate edges\n", c_degenerateEdges );
692 // insert any needed vertexes
693 for( i = ent->firstDrawSurf; i < numMapDrawSurfs ; i++ )
695 /* get surface and early out if possible */
696 ds = &mapDrawSurfs[ i ];
698 if( (si->compileFlags & C_NODRAW) || si->autosprite || si->notjunc || ds->numVerts == 0 || ds->type != SURFACE_FACE )
701 /* ydnar: gs mods: handle the various types of surfaces */
704 /* handle brush faces */
706 FixSurfaceJunctions( ds );
707 if( FixBrokenSurface( ds ) == qfalse )
714 /* fixme: t-junction triangle models and patches */
720 /* emit some statistics */
721 Sys_FPrintf( SYS_VRB, "%9d verts added for T-junctions\n", c_addedVerts );
722 Sys_FPrintf( SYS_VRB, "%9d total verts\n", c_totalVerts );
723 Sys_FPrintf( SYS_VRB, "%9d naturally ordered\n", c_natural );
724 Sys_FPrintf( SYS_VRB, "%9d rotated orders\n", c_rotate );
725 Sys_FPrintf( SYS_VRB, "%9d can't order\n", c_cant );
726 Sys_FPrintf( SYS_VRB, "%9d broken (degenerate) surfaces removed\n", c_broken );