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 ------------------------------------------------------------------------------- */
42 typedef struct edgePoint_s {
45 struct edgePoint_s *prev, *next;
48 typedef struct edgeLine_s {
58 edgePoint_t chain; // unused element of doubly linked list
66 #define MAX_ORIGINAL_EDGES 0x20000
67 originalEdge_t originalEdges[MAX_ORIGINAL_EDGES];
71 #define MAX_EDGE_LINES 0x10000
72 edgeLine_t edgeLines[MAX_EDGE_LINES];
75 int c_degenerateEdges;
79 int c_natural, c_rotate, c_cant;
81 // these should be whatever epsilon we actually expect,
82 // plus SNAP_INT_TO_FLOAT
83 #define LINE_POSITION_EPSILON 0.25
84 #define POINT_ON_LINE_EPSILON 0.25
91 void InsertPointOnEdge( vec3_t v, edgeLine_t *e ) {
94 edgePoint_t *p, *scan;
96 VectorSubtract( v, e->origin, delta );
97 d = DotProduct( delta, e->dir );
99 p = safe_malloc( sizeof(edgePoint_t) );
101 VectorCopy( v, p->xyz );
103 if ( e->chain.next == &e->chain ) {
104 e->chain.next = e->chain.prev = p;
105 p->next = p->prev = &e->chain;
109 scan = e->chain.next;
110 for ( ; scan != &e->chain ; scan = scan->next ) {
111 d = p->intercept - scan->intercept;
112 if ( d > -LINE_POSITION_EPSILON && d < LINE_POSITION_EPSILON ) {
114 return; // the point is already set
117 if ( p->intercept < scan->intercept ) {
119 p->prev = scan->prev;
121 scan->prev->next = p;
128 p->prev = scan->prev;
130 scan->prev->next = p;
140 int AddEdge( vec3_t v1, vec3_t v2, qboolean createNonAxial ) {
146 VectorSubtract( v2, v1, dir );
147 d = VectorNormalize( dir, dir );
149 // if we added a 0 length vector, it would make degenerate planes
154 if ( !createNonAxial ) {
155 if ( fabs( dir[0] + dir[1] + dir[2] ) != 1.0 ) {
156 if ( numOriginalEdges == MAX_ORIGINAL_EDGES ) {
157 Error( "MAX_ORIGINAL_EDGES" );
159 originalEdges[ numOriginalEdges ].dv[0] = (bspDrawVert_t *)v1;
160 originalEdges[ numOriginalEdges ].dv[1] = (bspDrawVert_t *)v2;
161 originalEdges[ numOriginalEdges ].length = d;
167 for ( i = 0 ; i < numEdgeLines ; i++ ) {
170 d = DotProduct( v1, e->normal1 ) - e->dist1;
171 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
174 d = DotProduct( v1, e->normal2 ) - e->dist2;
175 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
179 d = DotProduct( v2, e->normal1 ) - e->dist1;
180 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
183 d = DotProduct( v2, e->normal2 ) - e->dist2;
184 if ( d < -POINT_ON_LINE_EPSILON || d > POINT_ON_LINE_EPSILON ) {
189 InsertPointOnEdge( v1, e );
190 InsertPointOnEdge( v2, e );
195 if ( numEdgeLines >= MAX_EDGE_LINES ) {
196 Error( "MAX_EDGE_LINES" );
199 e = &edgeLines[ numEdgeLines ];
202 e->chain.next = e->chain.prev = &e->chain;
204 VectorCopy( v1, e->origin );
205 VectorCopy( dir, e->dir );
207 MakeNormalVectors( e->dir, e->normal1, e->normal2 );
208 e->dist1 = DotProduct( e->origin, e->normal1 );
209 e->dist2 = DotProduct( e->origin, e->normal2 );
211 InsertPointOnEdge( v1, e );
212 InsertPointOnEdge( v2, e );
214 return numEdgeLines - 1;
221 adds a surface's edges
224 void AddSurfaceEdges( mapDrawSurface_t *ds )
229 for( i = 0; i < ds->numVerts; i++ )
231 /* save the edge number in the lightmap field so we don't need to look it up again */
232 ds->verts[i].lightmap[ 0 ][ 0 ] =
233 AddEdge( ds->verts[ i ].xyz, ds->verts[ (i + 1) % ds->numVerts ].xyz, qfalse );
241 determines if an edge is colinear
244 qboolean ColinearEdge( vec3_t v1, vec3_t v2, vec3_t v3 )
246 vec3_t midpoint, dir, offset, on;
249 VectorSubtract( v2, v1, midpoint );
250 VectorSubtract( v3, v1, dir );
251 d = VectorNormalize( dir, dir );
253 return qfalse; // degenerate
256 d = DotProduct( midpoint, dir );
257 VectorScale( dir, d, on );
258 VectorSubtract( midpoint, on, offset );
259 d = VectorLength ( offset );
274 Add colinear border edges, which will fix some classes of patch to
278 void AddPatchEdges( mapDrawSurface_t *ds ) {
282 for ( i = 0 ; i < ds->patchWidth - 2; i+=2 ) {
283 v1 = ds->verts[ i ].xyz;
284 v2 = ds->verts[ i + 1 ].xyz;
285 v3 = ds->verts[ i + 2 ].xyz;
287 // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3
288 if ( ColinearEdge( v1, v2, v3 ) ) {
289 AddEdge( v1, v3, qfalse );
292 v1 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i ].xyz;
293 v2 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i + 1 ].xyz;
294 v3 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i + 2 ].xyz;
296 // if v2 is on the v1 to v3 line, add an edge from v1 to v3
297 if ( ColinearEdge( v1, v2, v3 ) ) {
298 AddEdge( v1, v3, qfalse );
302 for ( i = 0 ; i < ds->patchHeight - 2 ; i+=2 ) {
303 v1 = ds->verts[ i * ds->patchWidth ].xyz;
304 v2 = ds->verts[ ( i + 1 ) * ds->patchWidth ].xyz;
305 v3 = ds->verts[ ( i + 2 ) * ds->patchWidth ].xyz;
307 // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3
308 if ( ColinearEdge( v1, v2, v3 ) ) {
309 AddEdge( v1, v3, qfalse );
312 v1 = ds->verts[ ( ds->patchWidth - 1 ) + i * ds->patchWidth ].xyz;
313 v2 = ds->verts[ ( ds->patchWidth - 1 ) + ( i + 1 ) * ds->patchWidth ].xyz;
314 v3 = ds->verts[ ( ds->patchWidth - 1 ) + ( i + 2 ) * ds->patchWidth ].xyz;
316 // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3
317 if ( ColinearEdge( v1, v2, v3 ) ) {
318 AddEdge( v1, v3, qfalse );
331 #define MAX_SURFACE_VERTS 256
332 void FixSurfaceJunctions( mapDrawSurface_t *ds ) {
337 int counts[MAX_SURFACE_VERTS];
338 int originals[MAX_SURFACE_VERTS];
339 int firstVert[MAX_SURFACE_VERTS];
340 bspDrawVert_t verts[MAX_SURFACE_VERTS], *v1, *v2;
342 float start, end, frac, c;
346 originalVerts = ds->numVerts;
349 for ( i = 0 ; i < ds->numVerts ; i++ )
352 firstVert[i] = numVerts;
355 if ( numVerts == MAX_SURFACE_VERTS ) {
356 Error( "MAX_SURFACE_VERTS" );
358 verts[numVerts] = ds->verts[i];
359 originals[numVerts] = i;
362 // check to see if there are any t junctions before the next vert
364 v2 = &ds->verts[ (i+1) % ds->numVerts ];
366 j = (int)ds->verts[i].lightmap[ 0 ][ 0 ];
368 continue; // degenerate edge
372 VectorSubtract( v1->xyz, e->origin, delta );
373 start = DotProduct( delta, e->dir );
375 VectorSubtract( v2->xyz, e->origin, delta );
376 end = DotProduct( delta, e->dir );
385 for ( ; p != &e->chain ; ) {
387 if ( p->intercept > end - ON_EPSILON ) {
391 if ( p->intercept < end + ON_EPSILON ) {
397 ( start < end && p->intercept > start + ON_EPSILON ) ||
398 ( start > end && p->intercept < start - ON_EPSILON ) ) {
400 if ( numVerts == MAX_SURFACE_VERTS ) {
401 Error( "MAX_SURFACE_VERTS" );
404 /* take the exact intercept point */
405 VectorCopy( p->xyz, verts[ numVerts ].xyz );
407 /* interpolate the texture coordinates */
408 frac = ( p->intercept - start ) / ( end - start );
409 for ( j = 0 ; j < 2 ; j++ ) {
410 verts[ numVerts ].st[j] = v1->st[j] +
411 frac * ( v2->st[j] - v1->st[j] );
414 /* copy the normal (FIXME: what about nonplanar surfaces? */
415 VectorCopy( v1->normal, verts[ numVerts ].normal );
417 /* ydnar: interpolate the color */
418 for( k = 0; k < MAX_LIGHTMAPS; k++ )
420 for( j = 0; j < 4; j++ )
422 c = (float) v1->color[ k ][ j ] + frac * ((float) v2->color[ k ][ j ] - (float) v1->color[ k ][ j ]);
423 verts[ numVerts ].color[ k ][ j ] = (byte) (c < 255.0f ? c : 255);
428 originals[ numVerts ] = i;
441 c_addedVerts += numVerts - ds->numVerts;
442 c_totalVerts += numVerts;
445 // FIXME: check to see if the entire surface degenerated
448 // rotate the points so that the initial vertex is between
449 // two non-subdivided edges
450 for ( i = 0 ; i < numVerts ; i++ ) {
451 if ( originals[ (i+1) % numVerts ] == originals[ i ] ) {
454 j = (i + numVerts - 1 ) % numVerts;
455 k = (i + numVerts - 2 ) % numVerts;
456 if ( originals[ j ] == originals[ k ] ) {
463 // fine the way it is
466 ds->numVerts = numVerts;
467 ds->verts = safe_malloc( numVerts * sizeof( *ds->verts ) );
468 memcpy( ds->verts, verts, numVerts * sizeof( *ds->verts ) );
472 if ( i == numVerts ) {
473 // create a vertex in the middle to start the fan
477 memset ( &verts[numVerts], 0, sizeof( verts[numVerts] ) );
478 for ( i = 0 ; i < numVerts ; i++ ) {
479 for ( j = 0 ; j < 10 ; j++ ) {
480 verts[numVerts].xyz[j] += verts[i].xyz[j];
483 for ( j = 0 ; j < 10 ; j++ ) {
484 verts[numVerts].xyz[j] /= numVerts;
491 // just rotate the vertexes
496 ds->numVerts = numVerts;
497 ds->verts = safe_malloc( numVerts * sizeof( *ds->verts ) );
499 for ( j = 0 ; j < ds->numVerts ; j++ ) {
500 ds->verts[j] = verts[ ( j + i ) % ds->numVerts ];
509 FixBrokenSurface() - ydnar
510 removes nearly coincident verts from a planar winding surface
511 returns qfalse if the surface is broken
514 extern void SnapWeldVector( vec3_t a, vec3_t b, vec3_t out );
516 #define DEGENERATE_EPSILON 0.1
520 qboolean FixBrokenSurface( mapDrawSurface_t *ds )
522 qboolean valid = qtrue;
523 bspDrawVert_t *dv1, *dv2, avg;
531 if( ds->type != SURFACE_FACE )
534 /* check all verts */
535 for( i = 0; i < ds->numVerts; i++ )
537 /* don't remove points if winding is a triangle */
538 if( ds->numVerts == 3 )
542 dv1 = &ds->verts[ i ];
543 dv2 = &ds->verts[ (i + 1) % ds->numVerts ];
545 /* degenerate edge? */
546 VectorSubtract( dv1->xyz, dv2->xyz, avg.xyz );
547 dist = VectorLength( avg.xyz );
548 if( dist < DEGENERATE_EPSILON )
551 Sys_FPrintf( SYS_VRB, "WARNING: Degenerate T-junction edge found, fixing...\n" );
553 /* create an average drawvert */
554 /* ydnar 2002-01-26: added nearest-integer welding preference */
555 SnapWeldVector( dv1->xyz, dv2->xyz, avg.xyz );
556 VectorAdd( dv1->normal, dv2->normal, avg.normal );
557 VectorNormalize( avg.normal, avg.normal );
558 avg.st[ 0 ] = (dv1->st[ 0 ] + dv2->st[ 0 ]) * 0.5f;
559 avg.st[ 1 ] = (dv1->st[ 1 ] + dv2->st[ 1 ]) * 0.5f;
561 /* lightmap st/colors */
562 for( k = 0; k < MAX_LIGHTMAPS; k++ )
564 avg.lightmap[ k ][ 0 ] = (dv1->lightmap[ k ][ 0 ] + dv2->lightmap[ k ][ 0 ]) * 0.5f;
565 avg.lightmap[ k ][ 1 ] = (dv1->lightmap[ k ][ 1 ] + dv2->lightmap[ k ][ 1 ]) * 0.5f;
566 for( j = 0; j < 4; j++ )
567 avg.color[ k ][ j ] = (int) (dv1->color[ k ][ j ] + dv2->color[ k ][ j ]) >> 1;
571 memcpy( dv1, &avg, sizeof( avg ) );
573 /* move the remaining verts */
574 for( k = i + 2; k < ds->numVerts; k++ )
577 dv1 = &ds->verts[ k ];
578 dv2 = &ds->verts[ k - 1 ];
581 memcpy( dv2, dv1, sizeof( bspDrawVert_t ) );
587 /* one last check and return */
588 if( ds->numVerts < 3 )
606 int EdgeCompare( const void *elem1, const void *elem2 ) {
609 d1 = ((originalEdge_t *)elem1)->length;
610 d2 = ((originalEdge_t *)elem2)->length;
625 call after the surface list has been pruned
628 void FixTJunctions( entity_t *ent )
631 mapDrawSurface_t *ds;
637 /* meta mode has its own t-junction code (currently not as good as this code) */
642 Sys_FPrintf( SYS_VRB, "--- FixTJunctions ---\n" );
644 numOriginalEdges = 0;
647 // this actually creates axial edges, but it
648 // only creates originalEdge_t structures
649 // for non-axial edges
650 for ( i = ent->firstDrawSurf ; i < numMapDrawSurfs ; i++ )
652 /* get surface and early out if possible */
653 ds = &mapDrawSurfs[ i ];
655 if( (si->compileFlags & C_NODRAW) || si->autosprite || si->notjunc || ds->numVerts == 0 )
658 /* ydnar: gs mods: handle the various types of surfaces */
661 /* handle brush faces */
663 AddSurfaceEdges( ds );
671 /* fixme: make triangle surfaces t-junction */
677 axialEdgeLines = numEdgeLines;
679 // sort the non-axial edges by length
680 qsort( originalEdges, numOriginalEdges, sizeof(originalEdges[0]), EdgeCompare );
682 // add the non-axial edges, longest first
683 // this gives the most accurate edge description
684 for ( i = 0 ; i < numOriginalEdges ; i++ ) {
685 e = &originalEdges[i];
686 e->dv[ 0 ]->lightmap[ 0 ][ 0 ] = AddEdge( e->dv[ 0 ]->xyz, e->dv[ 1 ]->xyz, qtrue );
689 Sys_FPrintf( SYS_VRB, "%9d axial edge lines\n", axialEdgeLines );
690 Sys_FPrintf( SYS_VRB, "%9d non-axial edge lines\n", numEdgeLines - axialEdgeLines );
691 Sys_FPrintf( SYS_VRB, "%9d degenerate edges\n", c_degenerateEdges );
693 // insert any needed vertexes
694 for( i = ent->firstDrawSurf; i < numMapDrawSurfs ; i++ )
696 /* get surface and early out if possible */
697 ds = &mapDrawSurfs[ i ];
699 if( (si->compileFlags & C_NODRAW) || si->autosprite || si->notjunc || ds->numVerts == 0 || ds->type != SURFACE_FACE )
702 /* ydnar: gs mods: handle the various types of surfaces */
705 /* handle brush faces */
707 FixSurfaceJunctions( ds );
708 if( FixBrokenSurface( ds ) == qfalse )
715 /* fixme: t-junction triangle models and patches */
721 /* emit some statistics */
722 Sys_FPrintf( SYS_VRB, "%9d verts added for T-junctions\n", c_addedVerts );
723 Sys_FPrintf( SYS_VRB, "%9d total verts\n", c_totalVerts );
724 Sys_FPrintf( SYS_VRB, "%9d naturally ordered\n", c_natural );
725 Sys_FPrintf( SYS_VRB, "%9d rotated orders\n", c_rotate );
726 Sys_FPrintf( SYS_VRB, "%9d can't order\n", c_cant );
727 Sys_FPrintf( SYS_VRB, "%9d broken (degenerate) surfaces removed\n", c_broken );