2

我的截锥体剔除实现无法正常工作;发生的情况是当我将相机向左旋转而不是向右旋转时它会剔除。如果我向后移动相机,它也会“剔除”,尽管对象消失且未渲染的距离对此非常短 -6.0f使用对象的中心(球体)和相机的位置时的单位计算距离。向上或向下运动/旋转并没有真正做任何事情。

我的投影远平面很高:500.0f,我的近平面是0.1f。当然,降低/升高远平面似乎并没有真正影响剔除。


代码

这是平面计算代码:

void Frustum::UpdateCamParams( Camera* camera )
{
    vec3 fartl, fartr, farbl, farbr;
    vec3 neartl, neartr, nearbl, nearbr;

    const vec3& camLook  = camera->GetLook();
    const vec3& camUp    = camera->GetUp();
    const vec3& camRight = camera->GetRight();
    const vec3& camPos   = camera->GetPosition();

    const float zFar    = camera->GetFarZ();
    const float zNear   = camera->GetNearZ();

    float halfFarWidth  = camera->GetFarWindowWidth() * 0.5f;
    float halfFarHeight = camera->GetFarWindowHeight() * 0.5f;

    float halfNearWidth     = camera->GetNearWindowWidth() * 0.5f;
    float halfNearHeight    = camera->GetNearWindowHeight() * 0.5f;

    const vec3& farClip = camPos + camLook * zFar;

    fartl  = farClip + ( camUp * halfFarHeight ) - ( camRight * halfFarWidth );
    fartr = farClip + ( camUp * halfFarHeight ) + ( camRight * halfFarWidth );
    farbl  = farClip - ( camUp * halfFarHeight ) - ( camRight * halfFarWidth );
    farbr = farClip - ( camUp * halfFarHeight ) + ( camRight * halfFarWidth );

    const vec3& nearClip = camPos + camLook * zNear;

    neartl  = nearClip + ( camUp * halfNearHeight ) - ( camRight * halfNearWidth );
    neartr  = nearClip + ( camUp * halfNearHeight ) + ( camRight * halfNearWidth );
    nearbl  = nearClip - ( camUp * halfNearHeight ) - ( camRight * halfNearWidth );
    nearbr  = nearClip - ( camUp * halfNearHeight ) + ( camRight * halfNearWidth );

    ////viewRay = camLook - camPos;

    // Points are set in a counter-clockwise order, for each plane

    planes[ FRUST_LEFT ].points[ 0 ] = nearbl;
    planes[ FRUST_LEFT ].points[ 1 ] = farbl;
    planes[ FRUST_LEFT ].points[ 2 ] = fartl;
    M_ComputePlane( &planes[ FRUST_LEFT ] );

    planes[ FRUST_RIGHT ].points[ 0 ] = nearbr;
    planes[ FRUST_RIGHT ].points[ 1 ] = farbr;
    planes[ FRUST_RIGHT ].points[ 2 ] = fartr;
    M_ComputePlane( &planes[ FRUST_RIGHT ] );

    planes[ FRUST_TOP ].points[ 0 ] = neartl;
    planes[ FRUST_TOP ].points[ 1 ] = neartr;
    planes[ FRUST_TOP ].points[ 2 ] = fartr;
    M_ComputePlane( &planes[ FRUST_TOP ] );

    planes[ FRUST_BOTTOM ].points[ 0 ] = nearbl;
    planes[ FRUST_BOTTOM ].points[ 1 ] = nearbr;
    planes[ FRUST_BOTTOM ].points[ 2 ] = farbr;
    M_ComputePlane( &planes[ FRUST_BOTTOM ] );

    planes[ FRUST_FAR ].points[ 0 ] = farbl;
    planes[ FRUST_FAR ].points[ 1 ] = farbr;
    planes[ FRUST_FAR ].points[ 2 ] = fartr;
    M_ComputePlane( &planes[ FRUST_FAR ] );

    planes[ FRUST_NEAR ].points[ 0 ] = nearbl;
    planes[ FRUST_NEAR ].points[ 1 ] = nearbr;
    planes[ FRUST_NEAR ].points[ 2 ] = neartr;
    M_ComputePlane( &planes[ FRUST_NEAR ] );
}

简而言之,首先计算远剪辑和近剪辑平面中心,然后计算平截头体两侧的四个角,以允许在planes数组中设置点,如下所示。该planes数组用于指代平截头体的 6 个面。

M_ComputePlane计算平面的法线和到原点的距离d

void M_ComputePlane( plane_t* p )
{
    vec3 e1 = p->points[ 2 ] - p->points[ 1 ];
    vec3 e3 = p->points[ 1 ] - p->points[ 0 ];

    vec3 normal = glm::cross( e1, e3 );

    p->normal = glm::length( normal ) > 1.0f ? glm::normalize( normal ) : normal;
    p->d      = glm::dot( p->points[ 0 ], p->normal );
} 

以下是执行的交叉测试,每个测试都经过测试并且基本上产生相同的结果:

/*
=============================

Frustum::BoundsInSphere

    returns true in the event that the given center and radius
    are within the view frustum

=============================
*/

bool Frustum::BoundsInSphere( const vec3& center, float radius )
{
    for ( uint32_t i = 0; i < 6; ++i )
    {
        // center dot planes[ i ].normal = magnitude of projecting sphere center position onto plane normal
        // plane[ i ].d = distance from origin to plane

        float test = glm::dot( center, planes[ i ].normal ) + planes[ i ].d + radius;

        if ( test <= 0 )
            return false; // object is outside of this plane - reject!
    }

    return true;
}

/*
=============================

Frustum::BoundsInAABB_AllPoints

    Tests each vertex of the AABB and only
    returns false if all points are outside of one of
    the frustum planes. Otherwise, the object
    is considered renderable.

=============================
*/

bool Frustum::BoundsInAABB_AllPoints( const AABB& box )
{
    for ( uint32_t i = 0; i < 6; ++i )
    {
        int out = 0;

        for ( uint32_t k = 0; k < 8; ++k )
        {
            float test = glm::dot( box.Corner( k ), planes[ i ].normal ) + planes[ i ].d;

            if ( test < 0 )
                ++out;
        }

        if ( out == 8 ) // all points rejected for this plane - bail
            return false;
    }

    return true;
}

/*
=============================

Frustum::BoundsInAABB_Closest

    Find the closest boint of the AABB
    to frustum plane[ i ]. If that point
    has a signed distance less than 0
    to the plane, we reject it.
    Otherwise, we continue to test with the farthest
    point of the AABB to the given plane, and update
    our intersection enum accordingly.

=============================
*/

bool Frustum::BoundsInAABB_Closest( const AABB& box, frustumPlane_t* intersection )
{
    *intersection = FRUST_NONE;

    for ( uint32_t i = 0; i < 6; ++i )
    {
        const vec3& positive = box.GetPointRelativeToNormal( planes[ i ].normal, true );

        if ( M_SignedPlaneDistance( &planes[ i ], positive ) < 0 )
            return false; // closest point is outside; reject.

        const vec3& negative = box.GetPointRelativeToNormal( planes[ i ].normal, false );

        if ( M_SignedPlaneDistance( &planes[ i ], negative ) < 0 )
            *intersection = ( frustumPlane_t ) i;
    }

    return true;
}
4

0 回答 0