📄 cm_patch.c
字号:
/*
==================
CM_GridPlane
==================
*/
static int CM_GridPlane( int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2], int i, int j, int tri ) {
int p;
p = gridPlanes[i][j][tri];
if ( p != -1 ) {
return p;
}
p = gridPlanes[i][j][!tri];
if ( p != -1 ) {
return p;
}
// should never happen
Com_Printf( "WARNING: CM_GridPlane unresolvable\n" );
return -1;
}
/*
==================
CM_EdgePlaneNum
==================
*/
static int CM_EdgePlaneNum( cGrid_t *grid, int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2], int i, int j, int k ) {
float *p1, *p2;
vec3_t up;
int p;
switch ( k ) {
case 0: // top border
p1 = grid->points[i][j];
p2 = grid->points[i+1][j];
p = CM_GridPlane( gridPlanes, i, j, 0 );
VectorMA( p1, 4, planes[ p ].plane, up );
return CM_FindPlane( p1, p2, up );
case 2: // bottom border
p1 = grid->points[i][j+1];
p2 = grid->points[i+1][j+1];
p = CM_GridPlane( gridPlanes, i, j, 1 );
VectorMA( p1, 4, planes[ p ].plane, up );
return CM_FindPlane( p2, p1, up );
case 3: // left border
p1 = grid->points[i][j];
p2 = grid->points[i][j+1];
p = CM_GridPlane( gridPlanes, i, j, 1 );
VectorMA( p1, 4, planes[ p ].plane, up );
return CM_FindPlane( p2, p1, up );
case 1: // right border
p1 = grid->points[i+1][j];
p2 = grid->points[i+1][j+1];
p = CM_GridPlane( gridPlanes, i, j, 0 );
VectorMA( p1, 4, planes[ p ].plane, up );
return CM_FindPlane( p1, p2, up );
case 4: // diagonal out of triangle 0
p1 = grid->points[i+1][j+1];
p2 = grid->points[i][j];
p = CM_GridPlane( gridPlanes, i, j, 0 );
VectorMA( p1, 4, planes[ p ].plane, up );
return CM_FindPlane( p1, p2, up );
case 5: // diagonal out of triangle 1
p1 = grid->points[i][j];
p2 = grid->points[i+1][j+1];
p = CM_GridPlane( gridPlanes, i, j, 1 );
VectorMA( p1, 4, planes[ p ].plane, up );
return CM_FindPlane( p1, p2, up );
}
Com_Error( ERR_DROP, "CM_EdgePlaneNum: bad k" );
return -1;
}
/*
===================
CM_SetBorderInward
===================
*/
static void CM_SetBorderInward( facet_t *facet, cGrid_t *grid, int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2],
int i, int j, int which ) {
int k, l;
float *points[4];
int numPoints;
switch ( which ) {
case -1:
points[0] = grid->points[i][j];
points[1] = grid->points[i+1][j];
points[2] = grid->points[i+1][j+1];
points[3] = grid->points[i][j+1];
numPoints = 4;
break;
case 0:
points[0] = grid->points[i][j];
points[1] = grid->points[i+1][j];
points[2] = grid->points[i+1][j+1];
numPoints = 3;
break;
case 1:
points[0] = grid->points[i+1][j+1];
points[1] = grid->points[i][j+1];
points[2] = grid->points[i][j];
numPoints = 3;
break;
default:
Com_Error( ERR_FATAL, "CM_SetBorderInward: bad parameter" );
numPoints = 0;
break;
}
for ( k = 0 ; k < facet->numBorders ; k++ ) {
int front, back;
front = 0;
back = 0;
for ( l = 0 ; l < numPoints ; l++ ) {
int side;
side = CM_PointOnPlaneSide( points[l], facet->borderPlanes[k] );
if ( side == SIDE_FRONT ) {
front++;
} if ( side == SIDE_BACK ) {
back++;
}
}
if ( front && !back ) {
facet->borderInward[k] = qtrue;
} else if ( back && !front ) {
facet->borderInward[k] = qfalse;
} else if ( !front && !back ) {
// flat side border
facet->borderPlanes[k] = -1;
} else {
// bisecting side border
Com_DPrintf( "WARNING: CM_SetBorderInward: mixed plane sides\n" );
facet->borderInward[k] = qfalse;
if ( !debugBlock ) {
debugBlock = qtrue;
VectorCopy( grid->points[i][j], debugBlockPoints[0] );
VectorCopy( grid->points[i+1][j], debugBlockPoints[1] );
VectorCopy( grid->points[i+1][j+1], debugBlockPoints[2] );
VectorCopy( grid->points[i][j+1], debugBlockPoints[3] );
}
}
}
}
/*
==================
CM_ValidateFacet
If the facet isn't bounded by its borders, we screwed up.
==================
*/
static qboolean CM_ValidateFacet( facet_t *facet ) {
float plane[4];
int j;
winding_t *w;
vec3_t bounds[2];
if ( facet->surfacePlane == -1 ) {
return qfalse;
}
Vector4Copy( planes[ facet->surfacePlane ].plane, plane );
w = BaseWindingForPlane( plane, plane[3] );
for ( j = 0 ; j < facet->numBorders && w ; j++ ) {
if ( facet->borderPlanes[j] == -1 ) {
return qfalse;
}
Vector4Copy( planes[ facet->borderPlanes[j] ].plane, plane );
if ( !facet->borderInward[j] ) {
VectorSubtract( vec3_origin, plane, plane );
plane[3] = -plane[3];
}
ChopWindingInPlace( &w, plane, plane[3], 0.1f );
}
if ( !w ) {
return qfalse; // winding was completely chopped away
}
// see if the facet is unreasonably large
WindingBounds( w, bounds[0], bounds[1] );
FreeWinding( w );
for ( j = 0 ; j < 3 ; j++ ) {
if ( bounds[1][j] - bounds[0][j] > MAX_MAP_BOUNDS ) {
return qfalse; // we must be missing a plane
}
if ( bounds[0][j] >= MAX_MAP_BOUNDS ) {
return qfalse;
}
if ( bounds[1][j] <= -MAX_MAP_BOUNDS ) {
return qfalse;
}
}
return qtrue; // winding is fine
}
/*
==================
CM_AddFacetBevels
==================
*/
void CM_AddFacetBevels( facet_t *facet ) {
int i, j, k, l;
int axis, dir, order, flipped;
float plane[4], d, newplane[4];
winding_t *w, *w2;
vec3_t mins, maxs, vec, vec2;
Vector4Copy( planes[ facet->surfacePlane ].plane, plane );
w = BaseWindingForPlane( plane, plane[3] );
for ( j = 0 ; j < facet->numBorders && w ; j++ ) {
if (facet->borderPlanes[j] == facet->surfacePlane) continue;
Vector4Copy( planes[ facet->borderPlanes[j] ].plane, plane );
if ( !facet->borderInward[j] ) {
VectorSubtract( vec3_origin, plane, plane );
plane[3] = -plane[3];
}
ChopWindingInPlace( &w, plane, plane[3], 0.1f );
}
if ( !w ) {
return;
}
WindingBounds(w, mins, maxs);
// add the axial planes
order = 0;
for ( axis = 0 ; axis < 3 ; axis++ )
{
for ( dir = -1 ; dir <= 1 ; dir += 2, order++ )
{
VectorClear(plane);
plane[axis] = dir;
if (dir == 1) {
plane[3] = maxs[axis];
}
else {
plane[3] = -mins[axis];
}
//if it's the surface plane
if (CM_PlaneEqual(&planes[facet->surfacePlane], plane, &flipped)) {
continue;
}
// see if the plane is allready present
for ( i = 0 ; i < facet->numBorders ; i++ ) {
if (CM_PlaneEqual(&planes[facet->borderPlanes[i]], plane, &flipped))
break;
}
if ( i == facet->numBorders ) {
if (facet->numBorders > 4 + 6 + 16) Com_Printf("ERROR: too many bevels\n");
facet->borderPlanes[facet->numBorders] = CM_FindPlane2(plane, &flipped);
facet->borderNoAdjust[facet->numBorders] = 0;
facet->borderInward[facet->numBorders] = flipped;
facet->numBorders++;
}
}
}
//
// add the edge bevels
//
// test the non-axial plane edges
for ( j = 0 ; j < w->numpoints ; j++ )
{
k = (j+1)%w->numpoints;
VectorSubtract (w->p[j], w->p[k], vec);
//if it's a degenerate edge
if (VectorNormalize (vec) < 0.5)
continue;
CM_SnapVector(vec);
for ( k = 0; k < 3 ; k++ )
if ( vec[k] == -1 || vec[k] == 1 )
break; // axial
if ( k < 3 )
continue; // only test non-axial edges
// try the six possible slanted axials from this edge
for ( axis = 0 ; axis < 3 ; axis++ )
{
for ( dir = -1 ; dir <= 1 ; dir += 2 )
{
// construct a plane
VectorClear (vec2);
vec2[axis] = dir;
CrossProduct (vec, vec2, plane);
if (VectorNormalize (plane) < 0.5)
continue;
plane[3] = DotProduct (w->p[j], plane);
// if all the points of the facet winding are
// behind this plane, it is a proper edge bevel
for ( l = 0 ; l < w->numpoints ; l++ )
{
d = DotProduct (w->p[l], plane) - plane[3];
if (d > 0.1)
break; // point in front
}
if ( l < w->numpoints )
continue;
//if it's the surface plane
if (CM_PlaneEqual(&planes[facet->surfacePlane], plane, &flipped)) {
continue;
}
// see if the plane is allready present
for ( i = 0 ; i < facet->numBorders ; i++ ) {
if (CM_PlaneEqual(&planes[facet->borderPlanes[i]], plane, &flipped)) {
break;
}
}
if ( i == facet->numBorders ) {
if (facet->numBorders > 4 + 6 + 16) Com_Printf("ERROR: too many bevels\n");
facet->borderPlanes[facet->numBorders] = CM_FindPlane2(plane, &flipped);
for ( k = 0 ; k < facet->numBorders ; k++ ) {
if (facet->borderPlanes[facet->numBorders] ==
facet->borderPlanes[k]) Com_Printf("WARNING: bevel plane already used\n");
}
facet->borderNoAdjust[facet->numBorders] = 0;
facet->borderInward[facet->numBorders] = flipped;
//
w2 = CopyWinding(w);
Vector4Copy(planes[facet->borderPlanes[facet->numBorders]].plane, newplane);
if (!facet->borderInward[facet->numBorders])
{
VectorNegate(newplane, newplane);
newplane[3] = -newplane[3];
} //end if
ChopWindingInPlace( &w2, newplane, newplane[3], 0.1f );
if (!w2) {
Com_DPrintf("WARNING: CM_AddFacetBevels... invalid bevel\n");
continue;
}
else {
FreeWinding(w2);
}
//
facet->numBorders++;
//already got a bevel
// break;
}
}
}
}
FreeWinding( w );
#ifndef BSPC
//add opposite plane
facet->borderPlanes[facet->numBorders] = facet->surfacePlane;
facet->borderNoAdjust[facet->numBorders] = 0;
facet->borderInward[facet->numBorders] = qtrue;
facet->numBorders++;
#endif //BSPC
}
typedef enum {
EN_TOP,
EN_RIGHT,
EN_BOTTOM,
EN_LEFT
} edgeName_t;
/*
==================
CM_PatchCollideFromGrid
==================
*/
static void CM_PatchCollideFromGrid( cGrid_t *grid, patchCollide_t *pf ) {
int i, j;
float *p1, *p2, *p3;
MAC_STATIC int gridPlanes[MAX_GRID_SIZE][MAX_GRID_SIZE][2];
facet_t *facet;
int borders[4];
int noAdjust[4];
numPlanes = 0;
numFacets = 0;
// find the planes for each triangle of the grid
for ( i = 0 ; i < grid->width - 1 ; i++ ) {
for ( j = 0 ; j < grid->height - 1 ; j++ ) {
p1 = grid->points[i][j];
p2 = grid->points[i+1][j];
p3 = grid->points[i+1][j+1];
gridPlanes[i][j][0] = CM_FindPlane( p1, p2, p3 );
p1 = grid->points[i+1][j+1];
p2 = grid->points[i][j+1];
p3 = grid->points[i][j];
gridPlanes[i][j][1] = CM_FindPlane( p1, p2, p3 );
}
}
// create the borders for each facet
for ( i = 0 ; i < grid->width - 1 ; i++ ) {
for ( j = 0 ; j < grid->height - 1 ; j++ ) {
borders[EN_TOP] = -1;
if ( j > 0 ) {
borders[EN_TOP] = gridPlanes[i][j-1][1];
} else if ( grid->wrapHeight ) {
borders[EN_TOP] = gridPlanes[i][grid->height-2][1];
}
noAdjust[EN_TOP] = ( borders[EN_TOP] == gridPlanes[i][j][0] );
if ( borders[EN_TOP] == -1 || noAdjust[EN_TOP] ) {
borders[EN_TOP] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 0 );
}
borders[EN_BOTTOM] = -1;
if ( j < grid->height - 2 ) {
borders[EN_BOTTOM] = gridPlanes[i][j+1][0];
} else if ( grid->wrapHeight ) {
borders[EN_BOTTOM] = gridPlanes[i][0][0];
}
noAdjust[EN_BOTTOM] = ( borders[EN_BOTTOM] == gridPlanes[i][j][1] );
if ( borders[EN_BOTTOM] == -1 || noAdjust[EN_BOTTOM] ) {
borders[EN_BOTTOM] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 2 );
}
borders[EN_LEFT] = -1;
if ( i > 0 ) {
borders[EN_LEFT] = gridPlanes[i-1][j][0];
} else if ( grid->wrapWidth ) {
borders[EN_LEFT] = gridPlanes[grid->width-2][j][0];
}
noAdjust[EN_LEFT] = ( borders[EN_LEFT] == gridPlanes[i][j][1] );
if ( borders[EN_LEFT] == -1 || noAdjust[EN_LEFT] ) {
borders[EN_LEFT] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 3 );
}
borders[EN_RIGHT] = -1;
if ( i < grid->width - 2 ) {
borders[EN_RIGHT] = gridPlanes[i+1][j][1];
} else if ( grid->wrapWidth ) {
borders[EN_RIGHT] = gridPlanes[0][j][1];
}
noAdjust[EN_RIGHT] = ( borders[EN_RIGHT] == gridPlanes[i][j][0] );
if ( borders[EN_RIGHT] == -1 || noAdjust[EN_RIGHT] ) {
borders[EN_RIGHT] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 1 );
}
if ( numFacets == MAX_FACETS ) {
Com_Error( ERR_DROP, "MAX_FACETS" );
}
facet = &facets[numFacets];
Com_Memset( facet, 0, sizeof( *facet ) );
if ( gridPlanes[i][j][0] == gridPlanes[i][j][1] ) {
if ( gridPlanes[i][j][0] == -1 ) {
continue; // degenrate
}
facet->surfacePlane = gridPlanes[i][j][0];
facet->numBorders = 4;
facet->borderPlanes[0] = borders[EN_TOP];
facet->borderNoAdjust[0] = noAdjust[EN_TOP];
facet->borderPlanes[1] = borders[EN_RIGHT];
facet->borderNoAdjust[1] = noAdjust[EN_RIGHT];
facet->borderPlanes[2] = borders[EN_BOTTOM];
facet->borderNoAdjust[2] = noAdjust[EN_BOTTOM];
facet->borderPlanes[3] = borders[EN_LEFT];
facet->borderNoAdjust[3] = noAdjust[EN_LEFT];
CM_SetBorderInward( facet, grid, gridPlanes, i, j, -1 );
if ( CM_ValidateFacet( facet ) ) {
CM_AddFacetBevels( facet );
numFacets++;
}
} else {
// two seperate triangles
facet->surfacePlane = gridPlanes[i][j][0];
facet->numBorders = 3;
facet->borderPlanes[0] = borders[EN_TOP];
facet->borderNoAdjust[0] = noAdjust[EN_TOP];
facet->borderPlanes[1] = borders[EN_RIGHT];
facet->borderNoAdjust[1] = noAdjust[EN_RIGHT];
facet->borderPlanes[2] = gridPlanes[i][j][1];
if ( facet->borderPlanes[2] == -1 ) {
facet->borderPlanes[2] = borders[EN_BOTTOM];
if ( facet->borderPlanes[2] == -1 ) {
facet->borderPlanes[2] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 4 );
}
}
CM_SetBorderInward( facet, grid, gridPlanes, i, j, 0 );
if ( CM_ValidateFacet( facet ) ) {
CM_AddFacetBevels( facet );
numFacets++;
}
if ( numFacets == MAX_FACETS ) {
Com_Error( ERR_DROP, "MAX_FACETS" );
}
facet = &facets[numFacets];
Com_Memset( facet, 0, sizeof( *facet ) );
facet->surfacePlane = gridPlanes[i][j][1];
facet->numBorders = 3;
facet->borderPlanes[0] = borders[EN_BOTTOM];
facet->borderNoAdjust[0] = noAdjust[EN_BOTTOM];
facet->borderPlanes[1] = borders[EN_LEFT];
facet->borderNoAdjust[1] = noAdjust[EN_LEFT];
facet->borderPlanes[2] = gridPlanes[i][j][0];
if ( facet->borderPlanes[2] == -1 ) {
facet->borderPlanes[2] = borders[EN_TOP];
if ( facet->borderPlanes[2] == -1 ) {
facet->borderPlanes[2] = CM_EdgePlaneNum( grid, gridPlanes, i, j, 5 );
}
}
CM_SetBorderInward( facet, grid, gridPlanes, i, j, 1 );
if ( CM_ValidateFacet( facet ) ) {
CM_AddFacetBevels( facet );
numFacets++;
}
}
}
}
// copy the results out
pf->numPlanes = numPlanes;
pf->numFacets = numFacets;
pf->facets = Hunk_Alloc( numFacets * sizeof( *pf->facets ), h_high );
Com_Memcpy( pf->facets, facets, numFacets * sizeof( *pf->facets ) );
pf->planes = Hunk_Alloc( numPlanes * sizeof( *pf->planes ), h_high );
Com_Memcpy( pf->planes, planes, numPlanes * sizeof( *pf->planes ) );
}
/*
===================
CM_GeneratePatchCollide
Creates an internal structure that will be used to perform
collision detection with a patch mesh.
Points is packed as concatenated rows.
===================
*/
struct patchCollide_s *CM_GeneratePatchCollide( int width, int height, vec3_t *points ) {
patchCollide_t *pf;
MAC_STATIC cGrid_t grid;
int i, j;
if ( width <= 2 || height <= 2 || !points ) {
Com_Error( ERR_DROP, "CM_GeneratePatchFacets: bad parameters: (%i, %i, %p)",
width, height, points );
}
if ( !(width & 1) || !(height & 1) ) {
Com_Error( ERR_DROP, "CM_GeneratePatchFacets: even sizes are invalid for quadratic meshes" );
}
if ( width > MAX_GRID_SIZE || height > MAX_GRID_SIZE ) {
Com_Error( ERR_DROP, "CM_GeneratePatchFacets: source is > MAX_GRID_SIZE" );
}
// build a grid
grid.width = width;
grid.height = height;
grid.wrapWidth = qfalse;
grid.wrapHeight = qfalse;
for ( i = 0 ; i < width ; i++ ) {
for ( j = 0 ; j < height ; j++ ) {
VectorCopy( points[j*width + i], grid.points[i][j] );
}
}
// subdivide the grid
CM_SetGridWrapWidth( &grid );
CM_SubdivideGridColumns( &grid );
CM_RemoveDegenerateColumns( &grid );
CM_TransposeGrid( &grid );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -