/* ================== BrushMostlyOnSide ================== */ int BrushMostlyOnSide( uBrush_t *brush, idPlane &plane ) { int i, j; idWinding *w; float d, max = 0; int side = PSIDE_FRONT; for( i = 0; i < brush->numsides; i++ ) { w = brush->sides[i].winding; if( !w ) { continue; } for( j = 0; j < w->GetNumPoints(); j++ ) { d = plane.Distance( ( *w ) [j].ToVec3() ); if( d > max ) { max = d; side = PSIDE_FRONT; } if( -d > max ) { max = -d; side = PSIDE_BACK; } } } return side; }
/* =================== Cull cull points against given shadow frustum. Return true of all points are outside the frustum. =================== */ bool shadowMapFrustum_t::Cull( const idVec3 points[8] ) const { bool outsidePlane[6]; for (int i = 0; i < numPlanes; i++) { bool pointsCulled[8] = { true }; const idPlane plane = planes[i]; for (int j = 0; j < 8; j++) { const float distance = plane.Distance( points[j] ); pointsCulled[j] = distance < 0; } outsidePlane[i] = true; for (int j = 0; j < 8; j++) { if (!pointsCulled[j]) { outsidePlane[i] = false; } } } for (int i = 0; i < numPlanes; i++) { if (outsidePlane[i]) return true; } return false; }
/* ================ R_CalcInteractionFacing Determines which triangles of the surface are facing towards the light origin. The facing array should be allocated with one extra index than the number of surface triangles, which will be used to handle dangling edge silhouettes. ================ */ void R_CalcInteractionFacing( const idRenderEntityLocal *ent, const srfTriangles_t *tri, const idRenderLightLocal *light, srfCullInfo_t &cullInfo ) { SCOPED_PROFILE_EVENT( "R_CalcInteractionFacing" ); if ( cullInfo.facing != NULL ) { return; } idVec3 localLightOrigin; R_GlobalPointToLocal( ent->modelMatrix, light->globalLightOrigin, localLightOrigin ); const int numFaces = tri->numIndexes / 3; cullInfo.facing = (byte *) R_StaticAlloc( ( numFaces + 1 ) * sizeof( cullInfo.facing[0] ), TAG_RENDER_INTERACTION ); // exact geometric cull against face for ( int i = 0, face = 0; i < tri->numIndexes; i += 3, face++ ) { const idDrawVert & v0 = tri->verts[tri->indexes[i + 0]]; const idDrawVert & v1 = tri->verts[tri->indexes[i + 1]]; const idDrawVert & v2 = tri->verts[tri->indexes[i + 2]]; const idPlane plane( v0.xyz, v1.xyz, v2.xyz ); const float d = plane.Distance( localLightOrigin ); cullInfo.facing[face] = ( d >= 0.0f ); } cullInfo.facing[numFaces] = 1; // for dangling edges to reference }
/* ============ idAASBuild::IsLedgeSide_r ============ */ bool idAASBuild::IsLedgeSide_r( idBrushBSPNode *node, idFixedWinding *w, const idPlane &plane, const idVec3 &normal, const idVec3 &origin, const float radius ) { int res, i; idFixedWinding back; float dist; if ( !node ) { return false; } while ( node->GetChild(0) && node->GetChild(1) ) { dist = node->GetPlane().Distance( origin ); if ( dist > radius ) { res = SIDE_FRONT; } else if ( dist < -radius ) { res = SIDE_BACK; } else { res = w->Split( &back, node->GetPlane(), LEDGE_EPSILON ); } if ( res == SIDE_FRONT ) { node = node->GetChild(0); } else if ( res == SIDE_BACK ) { node = node->GetChild(1); } else if ( res == SIDE_ON ) { // continue with the side the winding faces if ( node->GetPlane().Normal() * normal > 0.0f ) { node = node->GetChild(0); } else { node = node->GetChild(1); } } else { if ( IsLedgeSide_r( node->GetChild(1), &back, plane, normal, origin, radius ) ) { return true; } node = node->GetChild(0); } } if ( node->GetContents() & AREACONTENTS_SOLID ) { return false; } for ( i = 0; i < w->GetNumPoints(); i++ ) { if ( plane.Distance( (*w)[i].ToVec3() ) > 0.0f ) { return true; } } return false; }
/* ================ idSphere::PlaneSide ================ */ int idSphere::PlaneSide( const idPlane &plane, const float epsilon ) const { float d; d = plane.Distance( origin ); if ( d > radius + epsilon ) { return PLANESIDE_FRONT; } if ( d < -radius - epsilon ) { return PLANESIDE_BACK; } return PLANESIDE_CROSS; }
/* ================ idSphere::PlaneDistance ================ */ float idSphere::PlaneDistance( const idPlane &plane ) const { float d; d = plane.Distance( origin ); if ( d > radius ) { return d - radius; } if ( d < -radius ) { return d + radius; } return 0.0f; }
/* ============ idAASLocal::FloorEdgeSplitPoint calculates either the closest or furthest point on the floor of the area which also lies on the pathPlane the point has to be on the front side of the frontPlane to be valid ============ */ bool idAASLocal::FloorEdgeSplitPoint( idVec3 &bestSplit, int areaNum, const idPlane &pathPlane, const idPlane &frontPlane, bool closest ) const { int i, j, faceNum, edgeNum; const aasArea_t *area; const aasFace_t *face; idVec3 split; float dist, bestDist; if ( closest ) { bestDist = maxWalkPathDistance; } else { bestDist = -0.1f; } area = &file->GetArea( areaNum ); for ( i = 0; i < area->numFaces; i++ ) { faceNum = file->GetFaceIndex( area->firstFace + i ); face = &file->GetFace( abs(faceNum) ); if ( !(face->flags & FACE_FLOOR ) ) { continue; } for ( j = 0; j < face->numEdges; j++ ) { edgeNum = file->GetEdgeIndex( face->firstEdge + j ); if ( !EdgeSplitPoint( split, abs( edgeNum ), pathPlane ) ) { continue; } dist = frontPlane.Distance( split ); if ( closest ) { if ( dist >= -0.1f && dist < bestDist ) { bestDist = dist; bestSplit = split; } } else { if ( dist > bestDist ) { bestDist = dist; bestSplit = split; } } } } if ( closest ) { return ( bestDist < maxWalkPathDistance ); } else { return ( bestDist > -0.1f ); } }
/* ================ idBox::PlaneSide ================ */ int idBox::PlaneSide( const idPlane &plane, const float epsilon ) const { float d1, d2; d1 = plane.Distance( center ); d2 = idMath::Fabs( extents[0] * plane.Normal()[0] ) + idMath::Fabs( extents[1] * plane.Normal()[1] ) + idMath::Fabs( extents[2] * plane.Normal()[2] ); if ( d1 - d2 > epsilon ) { return PLANESIDE_FRONT; } if ( d1 + d2 < -epsilon ) { return PLANESIDE_BACK; } return PLANESIDE_CROSS; }
/* ================ idBox::PlaneDistance ================ */ float idBox::PlaneDistance( const idPlane &plane ) const { float d1, d2; d1 = plane.Distance( center ); d2 = idMath::Fabs( extents[0] * plane.Normal()[0] ) + idMath::Fabs( extents[1] * plane.Normal()[1] ) + idMath::Fabs( extents[2] * plane.Normal()[2] ); if ( d1 - d2 > 0.0f ) { return d1 - d2; } if ( d1 + d2 < 0.0f ) { return d1 + d2; } return 0.0f; }
/* ================ idBounds::PlaneSide ================ */ int idBounds::PlaneSide( const idPlane &plane, const float epsilon ) const { idVec3 center; float d1, d2; center = ( b[0] + b[1] ) * 0.5f; d1 = plane.Distance( center ); d2 = idMath::Fabs( ( b[1][0] - center[0] ) * plane.Normal()[0] ) + idMath::Fabs( ( b[1][1] - center[1] ) * plane.Normal()[1] ) + idMath::Fabs( ( b[1][2] - center[2] ) * plane.Normal()[2] ); if ( d1 - d2 > epsilon ) { return PLANESIDE_FRONT; } if ( d1 + d2 < -epsilon ) { return PLANESIDE_BACK; } return PLANESIDE_CROSS; }
/* ================ idBounds::PlaneDistance ================ */ float idBounds::PlaneDistance( const idPlane &plane ) const { idVec3 center; float d1, d2; center = ( b[0] + b[1] ) * 0.5f; d1 = plane.Distance( center ); d2 = idMath::Fabs( ( b[1][0] - center[0] ) * plane.Normal()[0] ) + idMath::Fabs( ( b[1][1] - center[1] ) * plane.Normal()[1] ) + idMath::Fabs( ( b[1][2] - center[2] ) * plane.Normal()[2] ); if ( d1 - d2 > 0.0f ) { return d1 - d2; } if ( d1 + d2 < 0.0f ) { return d1 + d2; } return 0.0f; }
/* ============= R_ChopWinding Clips a triangle from one buffer to another, setting edge flags The returned buffer may be the same as inNum if no clipping is done If entirely clipped away, clipTris[returned].numVerts == 0 I have some worries about edge flag cases when polygons are clipped multiple times near the epsilon. ============= */ static int R_ChopWinding( clipTri_t clipTris[2], int inNum, const idPlane &plane ) { clipTri_t *in, *out; float dists[MAX_CLIPPED_POINTS]; int sides[MAX_CLIPPED_POINTS]; int counts[3]; float dot; int i, j; idVec3 *p1, *p2; idVec3 mid; in = &clipTris[inNum]; out = &clipTris[inNum^1]; counts[0] = counts[1] = counts[2] = 0; // determine sides for each point for ( i = 0 ; i < in->numVerts ; i++ ) { dot = plane.Distance( in->verts[i] ); dists[i] = dot; if ( dot < -LIGHT_CLIP_EPSILON ) { sides[i] = SIDE_BACK; } else if ( dot > LIGHT_CLIP_EPSILON ) { sides[i] = SIDE_FRONT; } else { sides[i] = SIDE_ON; } counts[sides[i]]++; } // if none in front, it is completely clipped away if ( !counts[SIDE_FRONT] ) { in->numVerts = 0; return inNum; } if ( !counts[SIDE_BACK] ) { return inNum; // inout stays the same } // avoid wrapping checks by duplicating first value to end sides[i] = sides[0]; dists[i] = dists[0]; in->verts[in->numVerts] = in->verts[0]; in->edgeFlags[in->numVerts] = in->edgeFlags[0]; out->numVerts = 0; for ( i = 0 ; i < in->numVerts ; i++ ) { p1 = &in->verts[i]; if ( sides[i] != SIDE_BACK ) { out->verts[out->numVerts] = *p1; if ( sides[i] == SIDE_ON && sides[i+1] == SIDE_BACK ) { out->edgeFlags[out->numVerts] = 1; } else { out->edgeFlags[out->numVerts] = in->edgeFlags[i]; } out->numVerts++; } if ( (sides[i] == SIDE_FRONT && sides[i+1] == SIDE_BACK) || (sides[i] == SIDE_BACK && sides[i+1] == SIDE_FRONT) ) { // generate a split point p2 = &in->verts[i+1]; dot = dists[i] / (dists[i]-dists[i+1]); for ( j=0 ; j<3 ; j++ ) { mid[j] = (*p1)[j] + dot*((*p2)[j]-(*p1)[j]); } out->verts[out->numVerts] = mid; // set the edge flag if ( sides[i+1] != SIDE_FRONT ) { out->edgeFlags[out->numVerts] = 1; } else { out->edgeFlags[out->numVerts] = in->edgeFlags[i]; } out->numVerts++; } } return inNum ^ 1; }
/* ================= idSurface::Split ================= */ int idSurface::Split( const idPlane &plane, const float epsilon, idSurface **front, idSurface **back, int *frontOnPlaneEdges, int *backOnPlaneEdges ) const { float * dists; float f; byte * sides; int counts[3]; int * edgeSplitVertex; int numEdgeSplitVertexes; int * vertexRemap[2]; int vertexIndexNum[2][2]; int * vertexCopyIndex[2]; int * indexPtr[2]; int indexNum[2]; int * index; int * onPlaneEdges[2]; int numOnPlaneEdges[2]; int maxOnPlaneEdges; int i; idSurface * surface[2]; idDrawVert v; dists = (float *) _alloca( verts.Num() * sizeof( float ) ); sides = (byte *) _alloca( verts.Num() * sizeof( byte ) ); counts[0] = counts[1] = counts[2] = 0; // determine side for each vertex for ( i = 0; i < verts.Num(); i++ ) { dists[i] = f = plane.Distance( verts[i].xyz ); if ( f > epsilon ) { sides[i] = SIDE_FRONT; } else if ( f < -epsilon ) { sides[i] = SIDE_BACK; } else { sides[i] = SIDE_ON; } counts[sides[i]]++; } *front = *back = NULL; // if coplanar, put on the front side if the normals match if ( !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) { f = ( verts[indexes[1]].xyz - verts[indexes[0]].xyz ).Cross( verts[indexes[0]].xyz - verts[indexes[2]].xyz ) * plane.Normal(); if ( FLOATSIGNBITSET( f ) ) { *back = new idSurface( *this ); return SIDE_BACK; } else { *front = new idSurface( *this ); return SIDE_FRONT; } } // if nothing at the front of the clipping plane if ( !counts[SIDE_FRONT] ) { *back = new idSurface( *this ); return SIDE_BACK; } // if nothing at the back of the clipping plane if ( !counts[SIDE_BACK] ) { *front = new idSurface( *this ); return SIDE_FRONT; } // allocate front and back surface *front = surface[0] = new idSurface(); *back = surface[1] = new idSurface(); edgeSplitVertex = (int *) _alloca( edges.Num() * sizeof( int ) ); numEdgeSplitVertexes = 0; maxOnPlaneEdges = 4 * counts[SIDE_ON]; counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0; // split edges for ( i = 0; i < edges.Num(); i++ ) { int v0 = edges[i].verts[0]; int v1 = edges[i].verts[1]; int sidesOr = ( sides[v0] | sides[v1] ); // if both vertexes are on the same side or one is on the clipping plane if ( !( sides[v0] ^ sides[v1] ) || ( sidesOr & SIDE_ON ) ) { edgeSplitVertex[i] = -1; counts[sidesOr & SIDE_BACK]++; counts[SIDE_ON] += ( sidesOr & SIDE_ON ) >> 1; } else {
/* ============ idBrush::Split ============ */ int idBrush::Split( const idPlane& plane, int planeNum, idBrush** front, idBrush** back ) const { int res, i, j; idBrushSide* side, *frontSide, *backSide; float dist, maxBack, maxFront, *maxBackWinding, *maxFrontWinding; idWinding* w, *mid; assert( windingsValid ); if( front ) { *front = NULL; } if( back ) { *back = NULL; } res = bounds.PlaneSide( plane, -BRUSH_EPSILON ); if( res == PLANESIDE_FRONT ) { if( front ) { *front = Copy(); } return res; } if( res == PLANESIDE_BACK ) { if( back ) { *back = Copy(); } return res; } maxBackWinding = ( float* ) _alloca16( sides.Num() * sizeof( float ) ); maxFrontWinding = ( float* ) _alloca16( sides.Num() * sizeof( float ) ); maxFront = maxBack = 0.0f; for( i = 0; i < sides.Num(); i++ ) { side = sides[i]; w = side->winding; if( !w ) { continue; } maxBackWinding[i] = 10.0f; maxFrontWinding[i] = -10.0f; for( j = 0; j < w->GetNumPoints(); j++ ) { dist = plane.Distance( ( *w )[j].ToVec3() ); if( dist > maxFrontWinding[i] ) { maxFrontWinding[i] = dist; } if( dist < maxBackWinding[i] ) { maxBackWinding[i] = dist; } } if( maxFrontWinding[i] > maxFront ) { maxFront = maxFrontWinding[i]; } if( maxBackWinding[i] < maxBack ) { maxBack = maxBackWinding[i]; } } if( maxFront < BRUSH_EPSILON ) { if( back ) { *back = Copy(); } return PLANESIDE_BACK; } if( maxBack > -BRUSH_EPSILON ) { if( front ) { *front = Copy(); } return PLANESIDE_FRONT; } mid = new idWinding( plane.Normal(), plane.Dist() ); for( i = 0; i < sides.Num() && mid; i++ ) { mid = mid->Clip( -sides[i]->plane, BRUSH_EPSILON, false ); } if( mid ) { if( mid->IsTiny() ) { delete mid; mid = NULL; } else if( mid->IsHuge() ) { // if the winding is huge then the brush is unbounded common->Warning( "brush %d on entity %d is unbounded" "( %1.2f %1.2f %1.2f )-( %1.2f %1.2f %1.2f )-( %1.2f %1.2f %1.2f )", primitiveNum, entityNum, bounds[0][0], bounds[0][1], bounds[0][2], bounds[1][0], bounds[1][1], bounds[1][2], bounds[1][0] - bounds[0][0], bounds[1][1] - bounds[0][1], bounds[1][2] - bounds[0][2] ); delete mid; mid = NULL; } } if( !mid ) { if( maxFront > - maxBack ) { if( front ) { *front = Copy(); } return PLANESIDE_FRONT; } else { if( back ) { *back = Copy(); } return PLANESIDE_BACK; } } if( !front && !back ) { delete mid; return PLANESIDE_CROSS; } *front = new idBrush(); ( *front )->SetContents( contents ); ( *front )->SetEntityNum( entityNum ); ( *front )->SetPrimitiveNum( primitiveNum ); *back = new idBrush(); ( *back )->SetContents( contents ); ( *back )->SetEntityNum( entityNum ); ( *back )->SetPrimitiveNum( primitiveNum ); for( i = 0; i < sides.Num(); i++ ) { side = sides[i]; if( !side->winding ) { continue; } // if completely at the front if( maxBackWinding[i] >= BRUSH_EPSILON ) { ( *front )->sides.Append( side->Copy() ); } // if completely at the back else if( maxFrontWinding[i] <= -BRUSH_EPSILON ) { ( *back )->sides.Append( side->Copy() ); } else { // split the side side->Split( plane, &frontSide, &backSide ); if( frontSide ) { ( *front )->sides.Append( frontSide ); } else if( maxFrontWinding[i] > -BRUSH_EPSILON ) { // favor an overconstrained brush side = side->Copy(); side->winding = side->winding->Clip( idPlane( plane.Normal(), ( plane.Dist() - ( BRUSH_EPSILON + 0.02f ) ) ), 0.01f, true ); assert( side->winding ); ( *front )->sides.Append( side ); } if( backSide ) { ( *back )->sides.Append( backSide ); } else if( maxBackWinding[i] < BRUSH_EPSILON ) { // favor an overconstrained brush side = side->Copy(); side->winding = side->winding->Clip( idPlane( -plane.Normal(), -( plane.Dist() + ( BRUSH_EPSILON + 0.02f ) ) ), 0.01f, true ); assert( side->winding ); ( *back )->sides.Append( side ); } } } side = new idBrushSide( -plane, planeNum ^ 1 ); side->winding = mid->Reverse(); side->flags |= SFL_SPLIT; ( *front )->sides.Append( side ); ( *front )->windingsValid = true; ( *front )->BoundBrush( this ); side = new idBrushSide( plane, planeNum ); side->winding = mid; side->flags |= SFL_SPLIT; ( *back )->sides.Append( side ); ( *back )->windingsValid = true; ( *back )->BoundBrush( this ); return PLANESIDE_CROSS; }
/* ============ idAASLocal::FloorEdgeSplitPoint calculates either the closest or furthest point on the floor of the area which also lies on the pathPlane the point has to be on the front side of the frontPlane to be valid ============ */ bool idAASLocal::FloorEdgeSplitPoint( idVec3 &bestSplit, int areaNum, const idPlane &pathPlane, const idPlane &frontPlane, bool closest ) const { int i, j, faceNum, edgeNum; const aasArea_t *area; const aasFace_t *face; idVec3 split; float dist, bestDist; const aasEdge_t *edge; idVec3 v1, v2; float d1, d2; area = &file->GetArea( areaNum ); if ( closest ) { bestDist = maxWalkPathDistance; for ( i = area->numFaces-1; i >= 0; i-- ) { faceNum = file->GetFaceIndex( area->firstFace + i ); face = &file->GetFace( abs(faceNum) ); if ( !(face->flags & FACE_FLOOR ) ) { continue; } for ( j = face->numEdges-1; j >= 0; j-- ) { edgeNum = file->GetEdgeIndex( face->firstEdge + j ); edge = &file->GetEdge( abs( edgeNum ) ); v1 = file->GetVertex( edge->vertexNum[0] ); v2 = file->GetVertex( edge->vertexNum[1] ); d1 = v1 * pathPlane.Normal() - pathPlane.Dist(); d2 = v2 * pathPlane.Normal() - pathPlane.Dist(); //if ( (d1 < CM_CLIP_EPSILON && d2 < CM_CLIP_EPSILON) || (d1 > -CM_CLIP_EPSILON && d2 > -CM_CLIP_EPSILON) ) { if ( FLOATSIGNBITSET( d1 ) == FLOATSIGNBITSET( d2 ) ) { continue; } split = v1 + (d1 / (d1 - d2)) * (v2 - v1); dist = frontPlane.Distance( split ); if ( dist >= -0.1f && dist < bestDist ) { bestDist = dist; bestSplit = split; } } } return ( bestDist < maxWalkPathDistance ); } else { bestDist = -0.1f; for ( i = area->numFaces-1; i >= 0; i-- ) { faceNum = file->GetFaceIndex( area->firstFace + i ); face = &file->GetFace( abs(faceNum) ); if ( !(face->flags & FACE_FLOOR ) ) { continue; } for ( j = face->numEdges-1; j >= 0; j-- ) { edgeNum = file->GetEdgeIndex( face->firstEdge + j ); edge = &file->GetEdge( abs( edgeNum ) ); v1 = file->GetVertex( edge->vertexNum[0] ); v2 = file->GetVertex( edge->vertexNum[1] ); d1 = v1 * pathPlane.Normal() - pathPlane.Dist(); d2 = v2 * pathPlane.Normal() - pathPlane.Dist(); //if ( (d1 < CM_CLIP_EPSILON && d2 < CM_CLIP_EPSILON) || (d1 > -CM_CLIP_EPSILON && d2 > -CM_CLIP_EPSILON) ) { if ( FLOATSIGNBITSET( d1 ) == FLOATSIGNBITSET( d2 ) ) { continue; } split = v1 + (d1 / (d1 - d2)) * (v2 - v1); dist = frontPlane.Distance( split ); if ( dist > bestDist ) { bestDist = dist; bestSplit = split; } } } return ( bestDist > -0.1f ); } }