我找了又找,找不到任何资源。我想用一个轴对齐的边界框来裁剪一个三角形(我经常看到相反的情况,一个三角形用轴对齐的边框裁剪,但从来没有相反的情况)。我试着计算裁剪的天角,然后从中构建一个边界框。但这非常无效,我认为我的代码不正确。有人知道如何如此有效地将边界框夹在三角形上吗?
这是我的代码:
typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef int8_t s8; typedef int16_t s16; typedef int32_t s32; typedef int64_t s64; typedef float f32; //floating 32 typedef double f64; //floating 64 typedef struct Point { union { f32 a[3]; struct { f32 x; f32 y; f32 z; }; }; } Point; typedef struct Vec4f { union { f32 v[4]; struct { f32 x; f32 y; f32 z; f32 w; }; }; } Vec4f; typedef struct BoundingBox { Vec4f m_vMin = {FLT_MAX,FLT_MAX,FLT_MAX,0.f}; Vec4f m_vMax = {-FLT_MAX,-FLT_MAX,-FLT_MAX,0.f}; } Vec4f; inline s8 Classify( s8 sign, u8 axis, const Point *c_v, const Point *p_v ) { const f64 d = sign * ( p_v->a[axis] - c_v->a[axis] ); if ( d > EPSILON ) { return 1; } else if ( d < -EPSILON ) { return -1; } return 0; } #define POINT_BUFFER_SIZE 9 inline void Clip3D_plane( Point *pVerts, s8 sign, u8 axis, u8 *pdwNumVerts, const Point *pPointOnPlane ) { u8 dwNumVerts = ( *pdwNumVerts ); if ( dwNumVerts == 0 ) { return; } else if ( dwNumVerts == 1 ) { *pdwNumVerts = 0; return; } Point vNewVerts[POINT_BUFFER_SIZE]; u8 k = 0; bool b = true; // polygon is fully located on clipping plane Point v1 = pVerts[dwNumVerts - 1]; s8 d1 = Classify( sign, axis, pPointOnPlane, &v1 ); for ( u8 j = 0; j < dwNumVerts; ++j ) { const Point &v2 = pVerts[j]; s8 d2 = Classify( sign, axis, pPointOnPlane, &v2 ); if ( d2 != 0 ) { b = false; if ( ( 0x80 & ( d2 ^ d1 ) ) != 0 ) //if signs differ { const f32 fAlpha = ( v2.a[axis] - pPointOnPlane->a[axis] ) / ( v2.a[axis] - v1.a[axis] ); Point_Lerp( &v2, &v1, fAlpha, &vNewVerts[k++] ); } else if ( d1 == 0 && ( k == 0 || !Point_Equals( &vNewVerts[k - 1], &v1 ) ) ) { vNewVerts[k++] = v1; } if ( d2 > 0 ) { vNewVerts[k++] = v2; } } else { if ( d1 != 0 ) { vNewVerts[k++] = v2; } } v1 = v2; d1 = d2; } if ( b ) { return; } *pdwNumVerts = k; for ( u8 j = 0; j < k; ++j ) { pVerts[j] = vNewVerts[j]; } } inline void BoundingBox_Append( BoundingBox3 *pBB, const Point *pvPoint ) { pBB->m_vMin.x = min( pBB->m_vMin.x, pvPoint->x ); pBB->m_vMin.y = min( pBB->m_vMin.y, pvPoint->y ); pBB->m_vMin.z = min( pBB->m_vMin.z, pvPoint->z ); pBB->m_vMax.x = max( pBB->m_vMax.x, pvPoint->x ); pBB->m_vMax.y = max( pBB->m_vMax.y, pvPoint->y ); pBB->m_vMax.z = max( pBB->m_vMax.z, pvPoint->z ); } void BoundingBox_ClipAndAppendTri( BoundingBox3 *pBB3, Point *pVerts, u8 *phwNumVerts, const BoundingBox3 *pClipBox ) { for ( u8 axis = 0; axis < 3; ++axis ) { Clip3D_plane( pVerts, 1, axis, phwNumVerts, &pClipBox->m_vMin ); Clip3D_plane( pVerts, -1, axis, phwNumVerts, &pClipBox->m_vMax ); } for ( u8 vert = 0; vert < *phwNumVerts; ++vert ) { BoundingBox_Append( pBB3, &pVerts[vert] ); } }
下面是描述我目前所做工作的图片