2008-07-25 07:31:37 +00:00
|
|
|
/* -------------------------------------------------------------------------------
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
----------------------------------------------------------------------------------
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This code has been altered significantly from its original form, to support
|
|
|
|
several games based on the Quake III Arena engine, in the form of "Q3Map2."
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
------------------------------------------------------------------------------- */
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* marker */
|
|
|
|
#define SURFACE_META_C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "q3map2.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define LIGHTMAP_EXCEEDED -1
|
|
|
|
#define S_EXCEEDED -2
|
|
|
|
#define T_EXCEEDED -3
|
|
|
|
#define ST_EXCEEDED -4
|
|
|
|
#define UNSUITABLE_TRIANGLE -10
|
|
|
|
#define VERTS_EXCEEDED -1000
|
|
|
|
#define INDEXES_EXCEEDED -2000
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define GROW_META_VERTS 1024
|
|
|
|
#define GROW_META_TRIANGLES 1024
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static int numMetaSurfaces, numPatchMetaSurfaces;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static int maxMetaVerts = 0;
|
|
|
|
static int numMetaVerts = 0;
|
|
|
|
static int firstSearchMetaVert = 0;
|
|
|
|
static bspDrawVert_t *metaVerts = NULL;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static int maxMetaTriangles = 0;
|
|
|
|
static int numMetaTriangles = 0;
|
|
|
|
static metaTriangle_t *metaTriangles = NULL;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
ClearMetaVertexes()
|
|
|
|
called before staring a new entity to clear out the triangle list
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void ClearMetaTriangles( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
numMetaVerts = 0;
|
|
|
|
numMetaTriangles = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
FindMetaVertex()
|
|
|
|
finds a matching metavertex in the global list, returning its index
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int FindMetaVertex( bspDrawVert_t *src ){
|
|
|
|
int i;
|
|
|
|
bspDrawVert_t *v, *temp;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* try to find an existing drawvert */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = firstSearchMetaVert, v = &metaVerts[ i ]; i < numMetaVerts; i++, v++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( memcmp( src, v, sizeof( bspDrawVert_t ) ) == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return i;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* enough space? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numMetaVerts >= maxMetaVerts ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* reallocate more room */
|
|
|
|
maxMetaVerts += GROW_META_VERTS;
|
|
|
|
temp = safe_malloc( maxMetaVerts * sizeof( bspDrawVert_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( metaVerts != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( temp, metaVerts, numMetaVerts * sizeof( bspDrawVert_t ) );
|
|
|
|
free( metaVerts );
|
|
|
|
}
|
|
|
|
metaVerts = temp;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add the triangle */
|
|
|
|
memcpy( &metaVerts[ numMetaVerts ], src, sizeof( bspDrawVert_t ) );
|
|
|
|
numMetaVerts++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* return the count */
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( numMetaVerts - 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
AddMetaTriangle()
|
|
|
|
adds a new meta triangle, allocating more memory if necessary
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int AddMetaTriangle( void ){
|
|
|
|
metaTriangle_t *temp;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* enough space? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numMetaTriangles >= maxMetaTriangles ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* reallocate more room */
|
|
|
|
maxMetaTriangles += GROW_META_TRIANGLES;
|
|
|
|
temp = safe_malloc( maxMetaTriangles * sizeof( metaTriangle_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( metaTriangles != NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( temp, metaTriangles, numMetaTriangles * sizeof( metaTriangle_t ) );
|
|
|
|
free( metaTriangles );
|
|
|
|
}
|
|
|
|
metaTriangles = temp;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* increment and return */
|
|
|
|
numMetaTriangles++;
|
|
|
|
return numMetaTriangles - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
FindMetaTriangle()
|
|
|
|
finds a matching metatriangle in the global list,
|
|
|
|
otherwise adds it and returns the index to the metatriangle
|
|
|
|
*/
|
|
|
|
|
|
|
|
int FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, bspDrawVert_t *c, int planeNum ){
|
|
|
|
int triIndex;
|
|
|
|
vec3_t dir;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* detect degenerate triangles fixme: do something proper here */
|
|
|
|
VectorSubtract( a->xyz, b->xyz, dir );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorLength( dir ) < 0.125f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorSubtract( b->xyz, c->xyz, dir );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorLength( dir ) < 0.125f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorSubtract( c->xyz, a->xyz, dir );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorLength( dir ) < 0.125f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* find plane */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( planeNum >= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* because of precision issues with small triangles, try to use the specified plane */
|
|
|
|
src->planeNum = planeNum;
|
|
|
|
VectorCopy( mapplanes[ planeNum ].normal, src->plane );
|
|
|
|
src->plane[ 3 ] = mapplanes[ planeNum ].dist;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* calculate a plane from the triangle's points (and bail if a plane can't be constructed) */
|
|
|
|
src->planeNum = -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( PlaneFromPoints( src->plane, a->xyz, b->xyz, c->xyz ) == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar 2002-10-03: repair any bogus normals (busted ase import kludge) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorLength( a->normal ) <= 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( src->plane, a->normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( VectorLength( b->normal ) <= 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( src->plane, b->normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( VectorLength( c->normal ) <= 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( src->plane, c->normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar 2002-10-04: set lightmap axis if not already set */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !( src->si->compileFlags & C_VERTEXLIT ) &&
|
|
|
|
src->lightmapAxis[ 0 ] == 0.0f && src->lightmapAxis[ 1 ] == 0.0f && src->lightmapAxis[ 2 ] == 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* the shader can specify an explicit lightmap axis */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( src->si->lightmapAxis[ 0 ] || src->si->lightmapAxis[ 1 ] || src->si->lightmapAxis[ 2 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( src->si->lightmapAxis, src->lightmapAxis );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* new axis-finding code */
|
2012-03-17 20:01:54 +00:00
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
CalcLightmapAxis( src->plane, src->lightmapAxis );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fill out the src triangle */
|
|
|
|
src->indexes[ 0 ] = FindMetaVertex( a );
|
|
|
|
src->indexes[ 1 ] = FindMetaVertex( b );
|
|
|
|
src->indexes[ 2 ] = FindMetaVertex( c );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* try to find an existing triangle */
|
|
|
|
#ifdef USE_EXHAUSTIVE_SEARCH
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int i;
|
|
|
|
metaTriangle_t *tri;
|
|
|
|
|
|
|
|
|
|
|
|
for ( i = 0, tri = metaTriangles; i < numMetaTriangles; i++, tri++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( memcmp( src, tri, sizeof( metaTriangle_t ) ) == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return i;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get a new triangle */
|
|
|
|
triIndex = AddMetaTriangle();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add the triangle */
|
|
|
|
memcpy( &metaTriangles[ triIndex ], src, sizeof( metaTriangle_t ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* return the triangle index */
|
|
|
|
return triIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
SurfaceToMetaTriangles()
|
|
|
|
converts a classified surface to metatriangles
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void SurfaceToMetaTriangles( mapDrawSurface_t *ds ){
|
|
|
|
int i;
|
|
|
|
metaTriangle_t src;
|
|
|
|
bspDrawVert_t a, b, c;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* only handle certain types of surfaces */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->type != SURFACE_FACE &&
|
|
|
|
ds->type != SURFACE_META &&
|
|
|
|
ds->type != SURFACE_FORCED_META &&
|
|
|
|
ds->type != SURFACE_DECAL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* speed at the expense of memory */
|
|
|
|
firstSearchMetaVert = numMetaVerts;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* only handle valid surfaces */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->type != SURFACE_BAD && ds->numVerts >= 3 && ds->numIndexes >= 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk the indexes and create triangles */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ds->numIndexes; i += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* sanity check the indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->indexes[ i ] == ds->indexes[ i + 1 ] ||
|
|
|
|
ds->indexes[ i ] == ds->indexes[ i + 2 ] ||
|
|
|
|
ds->indexes[ i + 1 ] == ds->indexes[ i + 2 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
//% Sys_Printf( "%d! ", ds->numVerts );
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* build a metatriangle */
|
|
|
|
src.si = ds->shaderInfo;
|
2012-03-17 20:01:54 +00:00
|
|
|
src.side = ( ds->sideRef != NULL ? ds->sideRef->side : NULL );
|
2007-11-04 03:34:51 +00:00
|
|
|
src.entityNum = ds->entityNum;
|
|
|
|
src.surfaceNum = ds->surfaceNum;
|
|
|
|
src.planeNum = ds->planeNum;
|
|
|
|
src.castShadows = ds->castShadows;
|
|
|
|
src.recvShadows = ds->recvShadows;
|
|
|
|
src.fogNum = ds->fogNum;
|
|
|
|
src.sampleSize = ds->sampleSize;
|
|
|
|
VectorCopy( ds->lightmapAxis, src.lightmapAxis );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy drawverts */
|
|
|
|
memcpy( &a, &ds->verts[ ds->indexes[ i ] ], sizeof( a ) );
|
|
|
|
memcpy( &b, &ds->verts[ ds->indexes[ i + 1 ] ], sizeof( b ) );
|
|
|
|
memcpy( &c, &ds->verts[ ds->indexes[ i + 2 ] ], sizeof( c ) );
|
|
|
|
FindMetaTriangle( &src, &a, &b, &c, ds->planeNum );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to count */
|
|
|
|
numMetaSurfaces++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear the surface (free verts and indexes, sets it to SURFACE_BAD) */
|
|
|
|
ClearSurface( ds );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
TriangulatePatchSurface()
|
|
|
|
creates triangles from a patch
|
|
|
|
*/
|
|
|
|
|
|
|
|
void TriangulatePatchSurface( mapDrawSurface_t *ds ){
|
|
|
|
int iterations, x, y, pw[ 5 ], r;
|
|
|
|
mapDrawSurface_t *dsNew;
|
|
|
|
mesh_t src, *subdivided, *mesh;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* try to early out */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numVerts == 0 || ds->type != SURFACE_PATCH || patchMeta == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make a mesh from the drawsurf */
|
2007-11-04 03:34:51 +00:00
|
|
|
src.width = ds->patchWidth;
|
|
|
|
src.height = ds->patchHeight;
|
|
|
|
src.verts = ds->verts;
|
|
|
|
//% subdivided = SubdivideMesh( src, 8, 999 );
|
|
|
|
iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions );
|
2012-03-17 20:01:54 +00:00
|
|
|
subdivided = SubdivideMesh2( src, iterations ); //% ds->maxIterations
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fit it to the curve and remove colinear verts on rows/columns */
|
|
|
|
PutMeshOnCurve( *subdivided );
|
|
|
|
mesh = RemoveLinearMeshColumnsRows( subdivided );
|
|
|
|
FreeMesh( subdivided );
|
|
|
|
//% MakeMeshNormals( mesh );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make a copy of the drawsurface */
|
|
|
|
dsNew = AllocDrawSurface( SURFACE_META );
|
|
|
|
memcpy( dsNew, ds, sizeof( *ds ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* if the patch is nonsolid, then discard it */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !( ds->shaderInfo->compileFlags & C_SOLID ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ClearSurface( ds );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set new pointer */
|
|
|
|
ds = dsNew;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* basic transmogrification */
|
|
|
|
ds->type = SURFACE_META;
|
|
|
|
ds->numIndexes = 0;
|
|
|
|
ds->indexes = safe_malloc( mesh->width * mesh->height * 6 * sizeof( int ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy the verts in */
|
2012-03-17 20:01:54 +00:00
|
|
|
ds->numVerts = ( mesh->width * mesh->height );
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->verts = mesh->verts;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* iterate through the mesh quads */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < ( mesh->height - 1 ); y++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0; x < ( mesh->width - 1 ); x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* set indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
pw[ 0 ] = x + ( y * mesh->width );
|
|
|
|
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
|
|
|
|
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
|
|
|
|
pw[ 3 ] = x + 1 + ( y * mesh->width );
|
|
|
|
pw[ 4 ] = x + ( y * mesh->width ); /* same as pw[ 0 ] */
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set radix */
|
2012-03-17 20:01:54 +00:00
|
|
|
r = ( x + y ) & 1;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make first triangle */
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = pw[ r + 0 ];
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = pw[ r + 1 ];
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = pw[ r + 2 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make second triangle */
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = pw[ r + 0 ];
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = pw[ r + 2 ];
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = pw[ r + 3 ];
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* free the mesh, but not the verts */
|
|
|
|
free( mesh );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to count */
|
|
|
|
numPatchMetaSurfaces++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* classify it */
|
|
|
|
ClassifySurfaces( 1, ds );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
FanFaceSurface() - ydnar
|
|
|
|
creates a tri-fan from a brush face winding
|
|
|
|
loosely based on SurfaceAsTriFan()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void FanFaceSurface( mapDrawSurface_t *ds ){
|
|
|
|
int i, j, k, a, b, c, color[ MAX_LIGHTMAPS ][ 4 ];
|
|
|
|
bspDrawVert_t *verts, *centroid, *dv;
|
|
|
|
double iv;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* try to early out */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !ds->numVerts || ( ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add a new vertex at the beginning of the surface */
|
2012-03-17 20:01:54 +00:00
|
|
|
verts = safe_malloc( ( ds->numVerts + 1 ) * sizeof( bspDrawVert_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
memset( verts, 0, sizeof( bspDrawVert_t ) );
|
|
|
|
memcpy( &verts[ 1 ], ds->verts, ds->numVerts * sizeof( bspDrawVert_t ) );
|
|
|
|
free( ds->verts );
|
|
|
|
ds->verts = verts;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add up the drawverts to create a centroid */
|
|
|
|
centroid = &verts[ 0 ];
|
|
|
|
memset( color, 0, 4 * MAX_LIGHTMAPS * sizeof( int ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 1, dv = &verts[ 1 ]; i < ( ds->numVerts + 1 ); i++, dv++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
VectorAdd( centroid->xyz, dv->xyz, centroid->xyz );
|
|
|
|
VectorAdd( centroid->normal, dv->normal, centroid->normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < 4; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
color[ k ][ j ] += dv->color[ k ][ j ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j < 2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
centroid->st[ j ] += dv->st[ j ];
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
centroid->lightmap[ k ][ j ] += dv->lightmap[ k ][ j ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* average the centroid */
|
|
|
|
iv = 1.0f / ds->numVerts;
|
|
|
|
VectorScale( centroid->xyz, iv, centroid->xyz );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorNormalize( centroid->normal, centroid->normal ) <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( verts[ 1 ].normal, centroid->normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
for ( j = 0; j < 4; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
color[ k ][ j ] /= ds->numVerts;
|
2012-03-17 20:01:54 +00:00
|
|
|
centroid->color[ k ][ j ] = ( color[ k ][ j ] < 255.0f ? color[ k ][ j ] : 255 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j < 2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
centroid->st[ j ] *= iv;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < MAX_LIGHTMAPS; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
centroid->lightmap[ k ][ j ] *= iv;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to vert count */
|
|
|
|
ds->numVerts++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fill indexes in triangle fan order */
|
|
|
|
ds->numIndexes = 0;
|
|
|
|
ds->indexes = safe_malloc( ds->numVerts * 3 * sizeof( int ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 1; i < ds->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
a = 0;
|
|
|
|
b = i;
|
2012-03-17 20:01:54 +00:00
|
|
|
c = ( i + 1 ) % ds->numVerts;
|
2007-11-04 03:34:51 +00:00
|
|
|
c = c ? c : 1;
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = a;
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = b;
|
|
|
|
ds->indexes[ ds->numIndexes++ ] = c;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to count */
|
|
|
|
numFanSurfaces++;
|
|
|
|
|
|
|
|
/* classify it */
|
|
|
|
ClassifySurfaces( 1, ds );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
StripFaceSurface() - ydnar
|
|
|
|
attempts to create a valid tri-strip w/o degenerate triangles from a brush face winding
|
|
|
|
based on SurfaceAsTriStrip()
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_INDEXES 1024
|
|
|
|
|
|
|
|
void StripFaceSurface( mapDrawSurface_t *ds ){
|
|
|
|
int i, r, least, rotate, numIndexes, ni, a, b, c, indexes[ MAX_INDEXES ];
|
|
|
|
vec_t *v1, *v2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* try to early out */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !ds->numVerts || ( ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* is this a simple triangle? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numVerts == 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
numIndexes = 3;
|
|
|
|
VectorSet( indexes, 0, 1, 2 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ydnar: find smallest coordinate */
|
|
|
|
least = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->shaderInfo != NULL && ds->shaderInfo->autosprite == qfalse ) {
|
|
|
|
for ( i = 0; i < ds->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get points */
|
|
|
|
v1 = ds->verts[ i ].xyz;
|
|
|
|
v2 = ds->verts[ least ].xyz;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* compare */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( v1[ 0 ] < v2[ 0 ] ||
|
|
|
|
( v1[ 0 ] == v2[ 0 ] && v1[ 1 ] < v2[ 1 ] ) ||
|
|
|
|
( v1[ 0 ] == v2[ 0 ] && v1[ 1 ] == v2[ 1 ] && v1[ 2 ] < v2[ 2 ] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
least = i;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* determine the triangle strip order */
|
2012-03-17 20:01:54 +00:00
|
|
|
numIndexes = ( ds->numVerts - 2 ) * 3;
|
|
|
|
if ( numIndexes > MAX_INDEXES ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "MAX_INDEXES exceeded for surface (%d > %d) (%d verts)", numIndexes, MAX_INDEXES, ds->numVerts );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* try all possible orderings of the points looking for a non-degenerate strip order */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( r = 0; r < ds->numVerts; r++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* set rotation */
|
2012-03-17 20:01:54 +00:00
|
|
|
rotate = ( r + least ) % ds->numVerts;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk the winding in both directions */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ni = 0, i = 0; i < ds->numVerts - 2 - i; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* make indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
a = ( ds->numVerts - 1 - i + rotate ) % ds->numVerts;
|
|
|
|
b = ( i + rotate ) % ds->numVerts;
|
|
|
|
c = ( ds->numVerts - 2 - i + rotate ) % ds->numVerts;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* test this triangle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numVerts > 4 && IsTriangleDegenerate( ds->verts, a, b, c ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
indexes[ ni++ ] = a;
|
|
|
|
indexes[ ni++ ] = b;
|
|
|
|
indexes[ ni++ ] = c;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* handle end case */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i + 1 != ds->numVerts - 1 - i ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
a = ( ds->numVerts - 2 - i + rotate ) % ds->numVerts;
|
|
|
|
b = ( i + rotate ) % ds->numVerts;
|
|
|
|
c = ( i + 1 + rotate ) % ds->numVerts;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* test triangle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numVerts > 4 && IsTriangleDegenerate( ds->verts, a, b, c ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
indexes[ ni++ ] = a;
|
|
|
|
indexes[ ni++ ] = b;
|
|
|
|
indexes[ ni++ ] = c;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* valid strip? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ni == numIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* if any triangle in the strip is degenerate, render from a centered fan point instead */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ni < numIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
FanFaceSurface( ds );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy strip triangle indexes */
|
|
|
|
ds->numIndexes = numIndexes;
|
|
|
|
ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) );
|
|
|
|
memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( int ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to count */
|
|
|
|
numStripSurfaces++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* classify it */
|
|
|
|
ClassifySurfaces( 1, ds );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
MakeEntityMetaTriangles()
|
|
|
|
builds meta triangles from brush faces (tristrips and fans)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void MakeEntityMetaTriangles( entity_t *e ){
|
|
|
|
int i, f, fOld, start;
|
|
|
|
mapDrawSurface_t *ds;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* note it */
|
|
|
|
Sys_FPrintf( SYS_VRB, "--- MakeEntityMetaTriangles ---\n" );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* init pacifier */
|
|
|
|
fOld = -1;
|
|
|
|
start = I_FloatTime();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk the list of surfaces in the entity */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* print pacifier */
|
2012-03-17 20:01:54 +00:00
|
|
|
f = 10 * ( i - e->firstDrawSurf ) / ( numMapDrawSurfs - e->firstDrawSurf );
|
|
|
|
if ( f != fOld ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
fOld = f;
|
|
|
|
Sys_FPrintf( SYS_VRB, "%d...", f );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get surface */
|
|
|
|
ds = &mapDrawSurfs[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numVerts <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ignore autosprite surfaces */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->shaderInfo->autosprite ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* meta this surface? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( meta == qfalse && ds->shaderInfo->forceMeta == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* switch on type */
|
2012-03-17 20:01:54 +00:00
|
|
|
switch ( ds->type )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
case SURFACE_FACE:
|
|
|
|
case SURFACE_DECAL:
|
|
|
|
StripFaceSurface( ds );
|
|
|
|
SurfaceToMetaTriangles( ds );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SURFACE_PATCH:
|
|
|
|
TriangulatePatchSurface( ds );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SURFACE_TRIANGLES:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SURFACE_FORCED_META:
|
|
|
|
case SURFACE_META:
|
|
|
|
SurfaceToMetaTriangles( ds );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* print time */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( numMapDrawSurfs - e->firstDrawSurf ) ) {
|
|
|
|
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) );
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* emit some stats */
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d total meta surfaces\n", numMetaSurfaces );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d stripped surfaces\n", numStripSurfaces );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d fanned surfaces\n", numFanSurfaces );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d patch meta surfaces\n", numPatchMetaSurfaces );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d meta verts\n", numMetaVerts );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d meta triangles\n", numMetaTriangles );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* tidy things up */
|
|
|
|
TidyEntitySurfaces( e );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
PointTriangleIntersect()
|
|
|
|
assuming that all points lie in plane, determine if pt
|
|
|
|
is inside the triangle abc
|
|
|
|
code originally (c) 2001 softSurfer (www.softsurfer.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MIN_OUTSIDE_EPSILON -0.01f
|
|
|
|
#define MAX_OUTSIDE_EPSILON 1.01f
|
|
|
|
|
|
|
|
static qboolean PointTriangleIntersect( vec3_t pt, vec4_t plane, vec3_t a, vec3_t b, vec3_t c, vec3_t bary ){
|
|
|
|
vec3_t u, v, w;
|
|
|
|
float uu, uv, vv, wu, wv, d;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* make vectors */
|
|
|
|
VectorSubtract( b, a, u );
|
|
|
|
VectorSubtract( c, a, v );
|
|
|
|
VectorSubtract( pt, a, w );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* more setup */
|
|
|
|
uu = DotProduct( u, u );
|
|
|
|
uv = DotProduct( u, v );
|
|
|
|
vv = DotProduct( v, v );
|
|
|
|
wu = DotProduct( w, u );
|
|
|
|
wv = DotProduct( w, v );
|
|
|
|
d = uv * uv - uu * vv;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* calculate barycentric coordinates */
|
2012-03-17 20:01:54 +00:00
|
|
|
bary[ 1 ] = ( uv * wv - vv * wu ) / d;
|
|
|
|
if ( bary[ 1 ] < MIN_OUTSIDE_EPSILON || bary[ 1 ] > MAX_OUTSIDE_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
bary[ 2 ] = ( uv * wv - uu * wv ) / d;
|
|
|
|
if ( bary[ 2 ] < MIN_OUTSIDE_EPSILON || bary[ 2 ] > MAX_OUTSIDE_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
bary[ 0 ] = 1.0f - ( bary[ 1 ] + bary[ 2 ] );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* point is in triangle */
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
CreateEdge()
|
|
|
|
sets up an edge structure from a plane and 2 points that the edge ab falls lies in
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct edge_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
vec3_t origin, edge;
|
|
|
|
vec_t length, kingpinLength;
|
|
|
|
int kingpin;
|
|
|
|
vec4_t plane;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
edge_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CreateEdge( vec4_t plane, vec3_t a, vec3_t b, edge_t *edge ){
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy edge origin */
|
|
|
|
VectorCopy( a, edge->origin );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create vector aligned with winding direction of edge */
|
|
|
|
VectorSubtract( b, a, edge->edge );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( fabs( edge->edge[ 0 ] ) > fabs( edge->edge[ 1 ] ) && fabs( edge->edge[ 0 ] ) > fabs( edge->edge[ 2 ] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
edge->kingpin = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( fabs( edge->edge[ 1 ] ) > fabs( edge->edge[ 0 ] ) && fabs( edge->edge[ 1 ] ) > fabs( edge->edge[ 2 ] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
edge->kingpin = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
edge->kingpin = 2;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
edge->kingpinLength = edge->edge[ edge->kingpin ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorNormalize( edge->edge, edge->edge );
|
|
|
|
edge->edge[ 3 ] = DotProduct( a, edge->edge );
|
|
|
|
edge->length = DotProduct( b, edge->edge ) - edge->edge[ 3 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* create perpendicular plane that edge lies in */
|
|
|
|
CrossProduct( plane, edge->edge, edge->plane );
|
|
|
|
edge->plane[ 3 ] = DotProduct( a, edge->plane );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
FixMetaTJunctions()
|
|
|
|
fixes t-junctions on meta triangles
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TJ_PLANE_EPSILON ( 1.0f / 8.0f )
|
|
|
|
#define TJ_EDGE_EPSILON ( 1.0f / 8.0f )
|
|
|
|
#define TJ_POINT_EPSILON ( 1.0f / 8.0f )
|
|
|
|
|
|
|
|
void FixMetaTJunctions( void ){
|
|
|
|
int i, j, k, f, fOld, start, vertIndex, triIndex, numTJuncs;
|
|
|
|
metaTriangle_t *tri, *newTri;
|
|
|
|
shaderInfo_t *si;
|
|
|
|
bspDrawVert_t *a, *b, *c, junc;
|
|
|
|
float dist, amount;
|
|
|
|
vec3_t pt;
|
|
|
|
vec4_t plane;
|
|
|
|
edge_t edges[ 3 ];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* this code is crap; revisit later */
|
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* note it */
|
|
|
|
Sys_FPrintf( SYS_VRB, "--- FixMetaTJunctions ---\n" );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* init pacifier */
|
|
|
|
fOld = -1;
|
|
|
|
start = I_FloatTime();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk triangle list */
|
|
|
|
numTJuncs = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numMetaTriangles; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get triangle */
|
|
|
|
tri = &metaTriangles[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* print pacifier */
|
|
|
|
f = 10 * i / numMetaTriangles;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( f != fOld ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
fOld = f;
|
|
|
|
Sys_FPrintf( SYS_VRB, "%d...", f );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* attempt to early out */
|
|
|
|
si = tri->si;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( si->compileFlags & C_NODRAW ) || si->autosprite || si->notjunc ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* calculate planes */
|
|
|
|
VectorCopy( tri->plane, plane );
|
|
|
|
plane[ 3 ] = tri->plane[ 3 ];
|
|
|
|
CreateEdge( plane, metaVerts[ tri->indexes[ 0 ] ].xyz, metaVerts[ tri->indexes[ 1 ] ].xyz, &edges[ 0 ] );
|
|
|
|
CreateEdge( plane, metaVerts[ tri->indexes[ 1 ] ].xyz, metaVerts[ tri->indexes[ 2 ] ].xyz, &edges[ 1 ] );
|
|
|
|
CreateEdge( plane, metaVerts[ tri->indexes[ 2 ] ].xyz, metaVerts[ tri->indexes[ 0 ] ].xyz, &edges[ 2 ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk meta vert list */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < numMetaVerts; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get vert */
|
|
|
|
VectorCopy( metaVerts[ j ].xyz, pt );
|
|
|
|
|
|
|
|
/* debug code: darken verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorSet( metaVerts[ j ].color[ 0 ], 8, 8, 8 );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* determine if point lies in the triangle's plane */
|
|
|
|
dist = DotProduct( pt, plane ) - plane[ 3 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( dist ) > TJ_PLANE_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* skip this point if it already exists in the triangle */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < 3; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( pt[ 0 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 0 ] ) <= TJ_POINT_EPSILON &&
|
|
|
|
fabs( pt[ 1 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 1 ] ) <= TJ_POINT_EPSILON &&
|
|
|
|
fabs( pt[ 2 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 2 ] ) <= TJ_POINT_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k < 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk edges */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < 3; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* ignore bogus edges */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( edges[ k ].kingpinLength ) < TJ_EDGE_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* determine if point lies on the edge */
|
|
|
|
dist = DotProduct( pt, edges[ k ].plane ) - edges[ k ].plane[ 3 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( dist ) > TJ_EDGE_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* determine how far along the edge the point lies */
|
2012-03-17 20:01:54 +00:00
|
|
|
amount = ( pt[ edges[ k ].kingpin ] - edges[ k ].origin[ edges[ k ].kingpin ] ) / edges[ k ].kingpinLength;
|
|
|
|
if ( amount <= 0.0f || amount >= 1.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
#if 0
|
|
|
|
dist = DotProduct( pt, edges[ k ].edge ) - edges[ k ].edge[ 3 ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dist <= -0.0f || dist >= edges[ k ].length ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
amount = dist / edges[ k ].length;
|
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* debug code: brighten this point */
|
|
|
|
//% metaVerts[ j ].color[ 0 ][ 0 ] += 5;
|
|
|
|
//% metaVerts[ j ].color[ 0 ][ 1 ] += 4;
|
|
|
|
VectorSet( metaVerts[ tri->indexes[ k ] ].color[ 0 ], 255, 204, 0 );
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSet( metaVerts[ tri->indexes[ ( k + 1 ) % 3 ] ].color[ 0 ], 255, 204, 0 );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* the edge opposite the zero-weighted vertex was hit, so use that as an amount */
|
|
|
|
a = &metaVerts[ tri->indexes[ k % 3 ] ];
|
2012-03-17 20:01:54 +00:00
|
|
|
b = &metaVerts[ tri->indexes[ ( k + 1 ) % 3 ] ];
|
|
|
|
c = &metaVerts[ tri->indexes[ ( k + 2 ) % 3 ] ];
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make new vert */
|
|
|
|
LerpDrawVertAmount( a, b, amount, &junc );
|
|
|
|
VectorCopy( pt, junc.xyz );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* compare against existing verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorCompare( junc.xyz, a->xyz ) || VectorCompare( junc.xyz, b->xyz ) || VectorCompare( junc.xyz, c->xyz ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* see if we can just re-use the existing vert */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !memcmp( &metaVerts[ j ], &junc, sizeof( junc ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
vertIndex = j;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* find new vertex (note: a and b are invalid pointers after this) */
|
|
|
|
firstSearchMetaVert = numMetaVerts;
|
|
|
|
vertIndex = FindMetaVertex( &junc );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vertIndex < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* make new triangle */
|
|
|
|
triIndex = AddMetaTriangle();
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( triIndex < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* get triangles */
|
|
|
|
tri = &metaTriangles[ i ];
|
|
|
|
newTri = &metaTriangles[ triIndex ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy the triangle */
|
|
|
|
memcpy( newTri, tri, sizeof( *tri ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* fix verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
tri->indexes[ ( k + 1 ) % 3 ] = vertIndex;
|
2007-11-04 03:34:51 +00:00
|
|
|
newTri->indexes[ k ] = vertIndex;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* recalculate edges */
|
|
|
|
CreateEdge( plane, metaVerts[ tri->indexes[ 0 ] ].xyz, metaVerts[ tri->indexes[ 1 ] ].xyz, &edges[ 0 ] );
|
|
|
|
CreateEdge( plane, metaVerts[ tri->indexes[ 1 ] ].xyz, metaVerts[ tri->indexes[ 2 ] ].xyz, &edges[ 1 ] );
|
|
|
|
CreateEdge( plane, metaVerts[ tri->indexes[ 2 ] ].xyz, metaVerts[ tri->indexes[ 0 ] ].xyz, &edges[ 2 ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* debug code */
|
|
|
|
metaVerts[ vertIndex ].color[ 0 ][ 0 ] = 255;
|
|
|
|
metaVerts[ vertIndex ].color[ 0 ][ 1 ] = 204;
|
|
|
|
metaVerts[ vertIndex ].color[ 0 ][ 2 ] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to counter and end processing of this vert */
|
|
|
|
numTJuncs++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* print time */
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* emit some stats */
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d T-junctions added\n", numTJuncs );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
SmoothMetaTriangles()
|
|
|
|
averages coincident vertex normals in the meta triangles
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_SAMPLES 256
|
|
|
|
#define THETA_EPSILON 0.000001
|
|
|
|
#define EQUAL_NORMAL_EPSILON 0.01
|
|
|
|
|
|
|
|
void SmoothMetaTriangles( void ){
|
|
|
|
int i, j, k, f, fOld, start, cs, numVerts, numVotes, numSmoothed;
|
|
|
|
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
|
|
|
|
metaTriangle_t *tri;
|
|
|
|
float *shadeAngles;
|
|
|
|
byte *smoothed;
|
|
|
|
vec3_t average, diff;
|
|
|
|
int indexes[ MAX_SAMPLES ];
|
|
|
|
vec3_t votes[ MAX_SAMPLES ];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* note it */
|
|
|
|
Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* allocate shade angle table */
|
|
|
|
shadeAngles = safe_malloc( numMetaVerts * sizeof( float ) );
|
|
|
|
memset( shadeAngles, 0, numMetaVerts * sizeof( float ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* allocate smoothed table */
|
2012-03-17 20:01:54 +00:00
|
|
|
cs = ( numMetaVerts / 8 ) + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
smoothed = safe_malloc( cs );
|
|
|
|
memset( smoothed, 0, cs );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set default shade angle */
|
|
|
|
defaultShadeAngle = DEG2RAD( npDegrees );
|
|
|
|
maxShadeAngle = 0.0f;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* run through every surface and flag verts belonging to non-lightmapped surfaces
|
|
|
|
and set per-vertex smoothing angle */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0, tri = &metaTriangles[ i ]; i < numMetaTriangles; i++, tri++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get shader for shade angle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( tri->si->shadeAngleDegrees > 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
shadeAngle = DEG2RAD( tri->si->shadeAngleDegrees );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
shadeAngle = defaultShadeAngle;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( shadeAngle > maxShadeAngle ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
maxShadeAngle = shadeAngle;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* flag its verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < 3; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
shadeAngles[ tri->indexes[ j ] ] = shadeAngle;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( shadeAngle <= 0 ) {
|
|
|
|
smoothed[ tri->indexes[ j ] >> 3 ] |= ( 1 << ( tri->indexes[ j ] & 7 ) );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* bail if no surfaces have a shade angle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( maxShadeAngle <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_FPrintf( SYS_VRB, "No smoothing angles specified, aborting\n" );
|
|
|
|
free( shadeAngles );
|
|
|
|
free( smoothed );
|
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* init pacifier */
|
|
|
|
fOld = -1;
|
|
|
|
start = I_FloatTime();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* go through the list of vertexes */
|
|
|
|
numSmoothed = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numMetaVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* print pacifier */
|
|
|
|
f = 10 * i / numMetaVerts;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( f != fOld ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
fOld = f;
|
|
|
|
Sys_FPrintf( SYS_VRB, "%d...", f );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* already smoothed? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( smoothed[ i >> 3 ] & ( 1 << ( i & 7 ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear */
|
|
|
|
VectorClear( average );
|
|
|
|
numVerts = 0;
|
|
|
|
numVotes = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* build a table of coincident vertexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = i; j < numMetaVerts && numVerts < MAX_SAMPLES; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* already smoothed? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( smoothed[ j >> 3 ] & ( 1 << ( j & 7 ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* test vertexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorCompare( metaVerts[ i ].xyz, metaVerts[ j ].xyz ) == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* use smallest shade angle */
|
2012-03-17 20:01:54 +00:00
|
|
|
shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check shade angle */
|
|
|
|
dot = DotProduct( metaVerts[ i ].normal, metaVerts[ j ].normal );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dot > 1.0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dot = 1.0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( dot < -1.0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dot = -1.0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
testAngle = acos( dot ) + THETA_EPSILON;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( testAngle >= shadeAngle ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to the list */
|
|
|
|
indexes[ numVerts++ ] = j;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* flag vertex */
|
2012-03-17 20:01:54 +00:00
|
|
|
smoothed[ j >> 3 ] |= ( 1 << ( j & 7 ) );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* see if this normal has already been voted */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < numVotes; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
VectorSubtract( metaVerts[ j ].normal, votes[ k ], diff );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( diff[ 0 ] ) < EQUAL_NORMAL_EPSILON &&
|
|
|
|
fabs( diff[ 1 ] ) < EQUAL_NORMAL_EPSILON &&
|
|
|
|
fabs( diff[ 2 ] ) < EQUAL_NORMAL_EPSILON ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add a new vote? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k == numVotes && numVotes < MAX_SAMPLES ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorAdd( average, metaVerts[ j ].normal, average );
|
|
|
|
VectorCopy( metaVerts[ j ].normal, votes[ numVotes ] );
|
|
|
|
numVotes++;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* don't average for less than 2 verts */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numVerts < 2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* average normal */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorNormalize( average, average ) > 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* smooth */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < numVerts; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
VectorCopy( average, metaVerts[ indexes[ j ] ].normal );
|
|
|
|
numSmoothed++;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* free the tables */
|
|
|
|
free( shadeAngles );
|
|
|
|
free( smoothed );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* print time */
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* emit some stats */
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d smoothed vertexes\n", numSmoothed );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
AddMetaVertToSurface()
|
|
|
|
adds a drawvert to a surface unless an existing vert matching already exists
|
|
|
|
returns the index of that vert (or < 0 on failure)
|
|
|
|
*/
|
|
|
|
|
|
|
|
int AddMetaVertToSurface( mapDrawSurface_t *ds, bspDrawVert_t *dv1, int *coincident ){
|
|
|
|
int i;
|
|
|
|
bspDrawVert_t *dv2;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* go through the verts and find a suitable candidate */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ds->numVerts; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get test vert */
|
|
|
|
dv2 = &ds->verts[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* compare xyz and normal */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( VectorCompare( dv1->xyz, dv2->xyz ) == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( VectorCompare( dv1->normal, dv2->normal ) == qfalse ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* good enough at this point */
|
2012-03-17 20:01:54 +00:00
|
|
|
( *coincident )++;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* compare texture coordinates and color */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dv1->st[ 0 ] != dv2->st[ 0 ] || dv1->st[ 1 ] != dv2->st[ 1 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( dv1->color[ 0 ][ 3 ] != dv2->color[ 0 ][ 3 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* found a winner */
|
|
|
|
numMergedVerts++;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overflow check */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numVerts >= ( ( ds->shaderInfo->compileFlags & C_VERTEXLIT ) ? maxSurfaceVerts : maxLMSurfaceVerts ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return VERTS_EXCEEDED;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* made it this far, add the vert and return */
|
|
|
|
dv2 = &ds->verts[ ds->numVerts++ ];
|
|
|
|
*dv2 = *dv1;
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( ds->numVerts - 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
AddMetaTriangleToSurface()
|
|
|
|
attempts to add a metatriangle to a surface
|
|
|
|
returns the score of the triangle added
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AXIS_SCORE 100000
|
|
|
|
#define AXIS_MIN 100000
|
|
|
|
#define VERT_SCORE 10000
|
|
|
|
#define SURFACE_SCORE 1000
|
|
|
|
#define ST_SCORE 50
|
|
|
|
#define ST_SCORE2 ( 2 * ( ST_SCORE ) )
|
|
|
|
|
|
|
|
#define ADEQUATE_SCORE ( (AXIS_MIN) +1 * ( VERT_SCORE ) )
|
|
|
|
#define GOOD_SCORE ( (AXIS_MIN) +2 * (VERT_SCORE) +4 * ( ST_SCORE ) )
|
|
|
|
#define PERFECT_SCORE ( ( AXIS_MIN ) + +3 * ( VERT_SCORE ) + (SURFACE_SCORE) +4 * ( ST_SCORE ) )
|
|
|
|
|
|
|
|
static int AddMetaTriangleToSurface( mapDrawSurface_t *ds, metaTriangle_t *tri, qboolean testAdd ){
|
|
|
|
int i, score, coincident, ai, bi, ci, oldTexRange[ 2 ];
|
|
|
|
float lmMax;
|
|
|
|
vec3_t mins, maxs;
|
|
|
|
qboolean inTexRange, es, et;
|
|
|
|
mapDrawSurface_t old;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* overflow check */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numIndexes >= maxSurfaceIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* test the triangle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->entityNum != tri->entityNum ) { /* ydnar: added 2002-07-06 */
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ds->castShadows != tri->castShadows || ds->recvShadows != tri->recvShadows ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ds->shaderInfo != tri->si || ds->fogNum != tri->fogNum || ds->sampleSize != tri->sampleSize ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#if 0
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !( ds->shaderInfo->compileFlags & C_VERTEXLIT ) &&
|
|
|
|
//% VectorCompare( ds->lightmapAxis, tri->lightmapAxis ) == qfalse )
|
|
|
|
DotProduct( ds->lightmapAxis, tri->plane ) < 0.25f ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* planar surfaces will only merge with triangles in the same plane */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( npDegrees == 0.0f && ds->shaderInfo->nonplanar == qfalse && ds->planeNum >= 0 ) {
|
|
|
|
if ( VectorCompare( mapplanes[ ds->planeNum ].normal, tri->plane ) == qfalse || mapplanes[ ds->planeNum ].dist != tri->plane[ 3 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( tri->planeNum >= 0 && tri->planeNum != ds->planeNum ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set initial score */
|
|
|
|
score = tri->surfaceNum == ds->surfaceNum ? SURFACE_SCORE : 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* score the the dot product of lightmap axis to plane */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( ds->shaderInfo->compileFlags & C_VERTEXLIT ) || VectorCompare( ds->lightmapAxis, tri->lightmapAxis ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
score += AXIS_SCORE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
score += AXIS_SCORE * DotProduct( ds->lightmapAxis, tri->plane );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* preserve old drawsurface if this fails */
|
|
|
|
memcpy( &old, ds, sizeof( *ds ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* attempt to add the verts */
|
|
|
|
coincident = 0;
|
|
|
|
ai = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 0 ] ], &coincident );
|
|
|
|
bi = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 1 ] ], &coincident );
|
|
|
|
ci = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 2 ] ], &coincident );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check vertex underflow */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ai < 0 || bi < 0 || ci < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* score coincident vertex count (2003-02-14: changed so this only matters on planar surfaces) */
|
2012-03-17 20:01:54 +00:00
|
|
|
score += ( coincident * VERT_SCORE );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add new vertex bounds to mins/maxs */
|
|
|
|
VectorCopy( ds->mins, mins );
|
|
|
|
VectorCopy( ds->maxs, maxs );
|
|
|
|
AddPointToBounds( metaVerts[ tri->indexes[ 0 ] ].xyz, mins, maxs );
|
|
|
|
AddPointToBounds( metaVerts[ tri->indexes[ 1 ] ].xyz, mins, maxs );
|
|
|
|
AddPointToBounds( metaVerts[ tri->indexes[ 2 ] ].xyz, mins, maxs );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check lightmap bounds overflow (after at least 1 triangle has been added) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !( ds->shaderInfo->compileFlags & C_VERTEXLIT ) &&
|
|
|
|
ds->numIndexes > 0 && VectorLength( ds->lightmapAxis ) > 0.0f &&
|
|
|
|
( VectorCompare( ds->mins, mins ) == qfalse || VectorCompare( ds->maxs, maxs ) == qfalse ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* set maximum size before lightmap scaling (normally 2032 units) */
|
2008-07-25 07:31:37 +00:00
|
|
|
/* 2004-02-24: scale lightmap test size by 2 to catch larger brush faces */
|
|
|
|
/* 2004-04-11: reverting to actual lightmap size */
|
2012-03-17 20:01:54 +00:00
|
|
|
lmMax = ( ds->sampleSize * ( ds->shaderInfo->lmCustomWidth - 1 ) );
|
|
|
|
for ( i = 0; i < 3; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( maxs[ i ] - mins[ i ] ) > lmMax ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check texture range overflow */
|
|
|
|
oldTexRange[ 0 ] = ds->texRange[ 0 ];
|
|
|
|
oldTexRange[ 1 ] = ds->texRange[ 1 ];
|
|
|
|
inTexRange = CalcSurfaceTextureRange( ds );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
es = ( ds->texRange[ 0 ] > oldTexRange[ 0 ] ) ? qtrue : qfalse;
|
|
|
|
et = ( ds->texRange[ 1 ] > oldTexRange[ 1 ] ) ? qtrue : qfalse;
|
|
|
|
|
|
|
|
if ( inTexRange == qfalse && ds->numIndexes > 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
|
|
|
return UNSUITABLE_TRIANGLE;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* score texture range */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->texRange[ 0 ] <= oldTexRange[ 0 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
score += ST_SCORE2;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( ds->texRange[ 0 ] > oldTexRange[ 0 ] && oldTexRange[ 1 ] > oldTexRange[ 0 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
score += ST_SCORE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ds->texRange[ 1 ] <= oldTexRange[ 1 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
score += ST_SCORE2;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( ds->texRange[ 1 ] > oldTexRange[ 1 ] && oldTexRange[ 0 ] > oldTexRange[ 1 ] ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
score += ST_SCORE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* go through the indexes and try to find an existing triangle that matches abc */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ds->numIndexes; i += 3 )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* 2002-03-11 (birthday!): rotate the triangle 3x to find an existing triangle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( ai == ds->indexes[ i ] && bi == ds->indexes[ i + 1 ] && ci == ds->indexes[ i + 2 ] ) ||
|
|
|
|
( bi == ds->indexes[ i ] && ci == ds->indexes[ i + 1 ] && ai == ds->indexes[ i + 2 ] ) ||
|
|
|
|
( ci == ds->indexes[ i ] && ai == ds->indexes[ i + 1 ] && bi == ds->indexes[ i + 2 ] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* triangle already present */
|
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
|
|
|
tri->si = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* rotate the triangle 3x to find an inverse triangle (error case) */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( ai == ds->indexes[ i ] && bi == ds->indexes[ i + 2 ] && ci == ds->indexes[ i + 1 ] ) ||
|
|
|
|
( bi == ds->indexes[ i ] && ci == ds->indexes[ i + 2 ] && ai == ds->indexes[ i + 1 ] ) ||
|
|
|
|
( ci == ds->indexes[ i ] && ai == ds->indexes[ i + 2 ] && bi == ds->indexes[ i + 1 ] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
/* warn about it */
|
|
|
|
Sys_Printf( "WARNING: Flipped triangle: (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f)\n",
|
2012-03-17 20:01:54 +00:00
|
|
|
ds->verts[ ai ].xyz[ 0 ], ds->verts[ ai ].xyz[ 1 ], ds->verts[ ai ].xyz[ 2 ],
|
|
|
|
ds->verts[ bi ].xyz[ 0 ], ds->verts[ bi ].xyz[ 1 ], ds->verts[ bi ].xyz[ 2 ],
|
|
|
|
ds->verts[ ci ].xyz[ 0 ], ds->verts[ ci ].xyz[ 1 ], ds->verts[ ci ].xyz[ 2 ] );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* reverse triangle already present */
|
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
|
|
|
tri->si = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add the triangle indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numIndexes < maxSurfaceIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->indexes[ ds->numIndexes++ ] = ai;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ds->numIndexes < maxSurfaceIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->indexes[ ds->numIndexes++ ] = bi;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ds->numIndexes < maxSurfaceIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->indexes[ ds->numIndexes++ ] = ci;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* check index overflow */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numIndexes >= maxSurfaceIndexes ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* sanity check the indexes */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ds->numIndexes >= 3 &&
|
|
|
|
( ds->indexes[ ds->numIndexes - 3 ] == ds->indexes[ ds->numIndexes - 2 ] ||
|
|
|
|
ds->indexes[ ds->numIndexes - 3 ] == ds->indexes[ ds->numIndexes - 1 ] ||
|
|
|
|
ds->indexes[ ds->numIndexes - 2 ] == ds->indexes[ ds->numIndexes - 1 ] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "DEG:%d! ", ds->numVerts );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* testing only? */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( testAdd ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
memcpy( ds, &old, sizeof( *ds ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* copy bounds back to surface */
|
|
|
|
VectorCopy( mins, ds->mins );
|
|
|
|
VectorCopy( maxs, ds->maxs );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* mark triangle as used */
|
|
|
|
tri->si = NULL;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add a side reference */
|
|
|
|
ds->sideRef = AllocSideRef( tri->side, ds->sideRef );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* return to sender */
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
MetaTrianglesToSurface()
|
|
|
|
creates map drawsurface(s) from the list of possibles
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles, int *fOld, int *numAdded ){
|
|
|
|
int i, j, f, best, score, bestScore;
|
|
|
|
metaTriangle_t *seed, *test;
|
|
|
|
mapDrawSurface_t *ds;
|
|
|
|
bspDrawVert_t *verts;
|
|
|
|
int *indexes;
|
|
|
|
qboolean added;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* allocate arrays */
|
|
|
|
verts = safe_malloc( sizeof( *verts ) * maxSurfaceVerts );
|
|
|
|
indexes = safe_malloc( sizeof( *indexes ) * maxSurfaceIndexes );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk the list of triangles */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0, seed = possibles; i < numPossibles; i++, seed++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* skip this triangle if it has already been merged */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( seed->si == NULL ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
initial drawsurf construction
|
|
|
|
----------------------------------------------------------------- */
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* start a new drawsurface */
|
|
|
|
ds = AllocDrawSurface( SURFACE_META );
|
|
|
|
ds->entityNum = seed->entityNum;
|
|
|
|
ds->surfaceNum = seed->surfaceNum;
|
|
|
|
ds->castShadows = seed->castShadows;
|
|
|
|
ds->recvShadows = seed->recvShadows;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
ds->shaderInfo = seed->si;
|
|
|
|
ds->planeNum = seed->planeNum;
|
|
|
|
ds->fogNum = seed->fogNum;
|
|
|
|
ds->sampleSize = seed->sampleSize;
|
|
|
|
ds->verts = verts;
|
|
|
|
ds->indexes = indexes;
|
|
|
|
VectorCopy( seed->lightmapAxis, ds->lightmapAxis );
|
|
|
|
ds->sideRef = AllocSideRef( seed->side, NULL );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
ClearBounds( ds->mins, ds->maxs );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear verts/indexes */
|
|
|
|
memset( verts, 0, sizeof( verts ) );
|
|
|
|
memset( indexes, 0, sizeof( indexes ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add the first triangle */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( AddMetaTriangleToSurface( ds, seed, qfalse ) ) {
|
|
|
|
( *numAdded )++;
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
add triangles
|
|
|
|
----------------------------------------------------------------- */
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* progressively walk the list until no more triangles can be added */
|
|
|
|
added = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( added )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* print pacifier */
|
|
|
|
f = 10 * *numAdded / numMetaTriangles;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( f > *fOld ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
*fOld = f;
|
|
|
|
Sys_FPrintf( SYS_VRB, "%d...", f );
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* reset best score */
|
|
|
|
best = -1;
|
|
|
|
bestScore = 0;
|
|
|
|
added = qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* walk the list of possible candidates for merging */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = i + 1, test = &possibles[ j ]; j < numPossibles; j++, test++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2008-07-25 07:31:37 +00:00
|
|
|
/* skip this triangle if it has already been merged */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( test->si == NULL ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* score this triangle */
|
|
|
|
score = AddMetaTriangleToSurface( ds, test, qtrue );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( score > bestScore ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
best = j;
|
|
|
|
bestScore = score;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* if we have a score over a certain threshold, just use it */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bestScore >= GOOD_SCORE ) {
|
|
|
|
if ( AddMetaTriangleToSurface( ds, &possibles[ best ], qfalse ) ) {
|
|
|
|
( *numAdded )++;
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* reset */
|
|
|
|
best = -1;
|
|
|
|
bestScore = 0;
|
|
|
|
added = qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add best candidate */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( best >= 0 && bestScore > ADEQUATE_SCORE ) {
|
|
|
|
if ( AddMetaTriangleToSurface( ds, &possibles[ best ], qfalse ) ) {
|
|
|
|
( *numAdded )++;
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* reset */
|
|
|
|
added = qtrue;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* copy the verts and indexes to the new surface */
|
|
|
|
ds->verts = safe_malloc( ds->numVerts * sizeof( bspDrawVert_t ) );
|
|
|
|
memcpy( ds->verts, verts, ds->numVerts * sizeof( bspDrawVert_t ) );
|
|
|
|
ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) );
|
|
|
|
memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( int ) );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* classify the surface */
|
|
|
|
ClassifySurfaces( 1, ds );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* add to count */
|
|
|
|
numMergedSurfaces++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* free arrays */
|
|
|
|
free( verts );
|
|
|
|
free( indexes );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
CompareMetaTriangles()
|
|
|
|
compare function for qsort()
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int CompareMetaTriangles( const void *a, const void *b ){
|
|
|
|
int i, j, av, bv;
|
|
|
|
vec3_t aMins, bMins;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* shader first */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( (metaTriangle_t*) a )->si < ( (metaTriangle_t*) b )->si ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->si > ( (metaTriangle_t*) b )->si ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* then fog */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( ( (metaTriangle_t*) a )->fogNum < ( (metaTriangle_t*) b )->fogNum ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->fogNum > ( (metaTriangle_t*) b )->fogNum ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
/* then plane */
|
|
|
|
#if 0
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( npDegrees == 0.0f && ( (metaTriangle_t*) a )->si->nonplanar == qfalse &&
|
|
|
|
( (metaTriangle_t*) a )->planeNum >= 0 && ( (metaTriangle_t*) a )->planeNum >= 0 ) {
|
|
|
|
if ( ( (metaTriangle_t*) a )->plane[ 3 ] < ( (metaTriangle_t*) b )->plane[ 3 ] ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 3 ] > ( (metaTriangle_t*) b )->plane[ 3 ] ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 0 ] < ( (metaTriangle_t*) b )->plane[ 0 ] ) {
|
|
|
|
return 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 0 ] > ( (metaTriangle_t*) b )->plane[ 0 ] ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 1 ] < ( (metaTriangle_t*) b )->plane[ 1 ] ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 1 ] > ( (metaTriangle_t*) b )->plane[ 1 ] ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 2 ] < ( (metaTriangle_t*) b )->plane[ 2 ] ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( ( (metaTriangle_t*) a )->plane[ 2 ] > ( (metaTriangle_t*) b )->plane[ 2 ] ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2008-07-25 07:31:37 +00:00
|
|
|
#endif
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
/* then position in world */
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
/* find mins */
|
|
|
|
VectorSet( aMins, 999999, 999999, 999999 );
|
|
|
|
VectorSet( bMins, 999999, 999999, 999999 );
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
2008-07-25 07:31:37 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
av = ( (metaTriangle_t*) a )->indexes[ i ];
|
|
|
|
bv = ( (metaTriangle_t*) b )->indexes[ i ];
|
|
|
|
for ( j = 0; j < 3; j++ )
|
2008-07-25 07:31:37 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( metaVerts[ av ].xyz[ j ] < aMins[ j ] ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
aMins[ j ] = metaVerts[ av ].xyz[ j ];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( metaVerts[ bv ].xyz[ j ] < bMins[ j ] ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
bMins[ j ] = metaVerts[ bv ].xyz[ j ];
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-07-25 07:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
/* test it */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < 3; i++ )
|
2008-07-25 07:31:37 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( aMins[ i ] < bMins[ i ] ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
return 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( aMins[ i ] > bMins[ i ] ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* functionally equivalent */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
MergeMetaTriangles()
|
|
|
|
merges meta triangles into drawsurfaces
|
|
|
|
*/
|
|
|
|
|
|
|
|
void MergeMetaTriangles( void ){
|
|
|
|
int i, j, fOld, start, numAdded;
|
|
|
|
metaTriangle_t *head, *end;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* only do this if there are meta triangles */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numMetaTriangles <= 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* note it */
|
|
|
|
Sys_FPrintf( SYS_VRB, "--- MergeMetaTriangles ---\n" );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* sort the triangles by shader major, fognum minor */
|
|
|
|
qsort( metaTriangles, numMetaTriangles, sizeof( metaTriangle_t ), CompareMetaTriangles );
|
|
|
|
|
|
|
|
/* init pacifier */
|
|
|
|
fOld = -1;
|
|
|
|
start = I_FloatTime();
|
|
|
|
numAdded = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* merge */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0, j = 0; i < numMetaTriangles; i = j )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
/* get head of list */
|
|
|
|
head = &metaTriangles[ i ];
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
/* skip this triangle if it has already been merged */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( head->si == NULL ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* find end */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j <= i ) {
|
|
|
|
for ( j = i + 1; j < numMetaTriangles; j++ )
|
2008-07-25 07:31:37 +00:00
|
|
|
{
|
|
|
|
/* get end of list */
|
|
|
|
end = &metaTriangles[ j ];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( head->si != end->si || head->fogNum != end->fogNum ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2008-07-25 07:31:37 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* try to merge this list of possible merge candidates */
|
2012-03-17 20:01:54 +00:00
|
|
|
MetaTrianglesToSurface( ( j - i ), head, &fOld, &numAdded );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* clear meta triangle list */
|
|
|
|
ClearMetaTriangles();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* print time */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i ) {
|
|
|
|
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) );
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* emit some stats */
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d surfaces merged\n", numMergedSurfaces );
|
|
|
|
Sys_FPrintf( SYS_VRB, "%9d vertexes merged\n", numMergedVerts );
|
|
|
|
}
|