视锥裁剪

背景

视锥体(frustum),是指场景中摄像机的可见的一个锥体范围。它有上、下、左、右、近、远,共6个面组成。在视锥体内的景物可见,反之则不可见。为提高性能,只对其中与视锥体有交集的对象进行绘制。
视锥体

我们计算出视锥体六个面的空间平面方程,将点坐标分别代入六个面的平面方程做比较,则可以判断点是否在视锥体内。

空间平面方程可表示为:

	Ax+By+Cz=0


对于点(x1, y1, z1),有
	若 Ax1+By1+Cz1 = 0,则点在平面上;
	若 Ax1+By1+Cz1 < 0,则点在平面的一侧;
	若 Ax1+By1+Cz1 = 0,则点在平面的另一侧;


求视锥平面系数1

这里介绍的算法,可以直接从世界、观察以及投影矩阵中计算出Viewing Frustum的六个面。它快速,准确,并且允许我们在相机空间(camera space)、世界空间(world space)或着物体空间(object space)快速确定Frustum planes。

我们先仅仅从投影矩阵(project)开始,也就是假设世界矩阵(world)和观察矩阵(view)都是单位化了的矩阵。这就意味着相机位于世界坐标系下的原点,并且朝向Z轴的正方向。

定义一个顶点v(x y z w=1)和一个4*4的投影矩阵M=m(i,j),然后我们使用该矩阵M对顶点v进行转换,转换后的顶点为v'= (x' y' z' w'),可以写成这样:


转换后,viewing frustum实际上就变成了一个与轴平行的盒子,如果顶点 v' 在这个盒子里,那么转换前的顶点 v 就在转换前的viewing frustum里。在 OpenGL 下,如果下面的几个不等式都成立的话,那么 v' 就在这个盒子里。
	-w' < x' < w'
   	-w' < y' < w'
  	-w' < z' < w'

可得到如下结论,列在下表里:

我们假设现在想测试 x' 是否在左半边空间,只需判断

 -w < x'

用上面的信息,等式我们可以写成:

    −(v • row4 ) < (v • row1 )

    0 < (v • row4 ) + (v • row1 )

    0 < v • (row4 + row1 )

写到这里,其实已经等于描绘出了转换前的viewing frustum的左裁剪面的平面方程:

x(m41 + m11) + y(m42 + m12) + z(m43 + m13) + w(m44 + m14) = 0
当W = 1,我们可简单成如下形式:
x(m41 + m11) + y(m42 + m12) + z(m43 + m13) + (m44 + m14) = 0


这就给出了一个基本平面方程:

    
ax + by + cz + d = 0
其中,a = ( m41 + m11) , b = ( m42 + m12 ), c = ( m43 + m13) , d = ( m44 + m14 )到这里左裁剪面就得到了。重复以上几步,可推导出到其他的几个裁剪面,具体见参考文献1.

需要注意的是:最终得到的平面方程都是没有单位化的(平面的法向量不是单位向量),并且法向量指向空间的内部。这就是说,如果要判断 v 在空间内部,那么6个面必须都满足ax + by + cz + d > 0

到目前为止,我们都是假设世界矩阵( world )和观察矩阵( view )都是单位化了的矩阵。但是,本算法并不想受这种条件的限制,而是希望可以在任何条件下都能使用。实际上,这也并不复杂,并且简单得令人难以置信。如果你仔细想一下就会立刻明白了,所以我们不再对此进行详细解释了,下面给出3个结论:

  • 1. 如果矩阵 M 等于投影矩阵 P ( M = P ),那么算法给出的裁剪面是在相机空间(camera space)
  • 2. 如果矩阵 M 等于观察矩阵 V 和投影矩阵 P 的组合( M = V * P ),那么算法给出的裁剪面是在世界空间(world space)
  • 3. 如果矩阵 M 等于世界矩阵 W,观察矩阵 V 和投影矩阵 P 的组合( M = W* V * P ),呢么算法给出的裁剪面是在物体空间(object space)
判断节点是否在视锥内

通过各种包围体方法求出近似包围体,对包围体上的各个点对视锥六个面作判断,存在以下三种情况:

  • 如果所有顶点都在视锥范围内,则待判区域一定在视锥范围内;
  • 如果只有部分顶点在视锥范围内,则待判区域与视锥体相交,我们同样视为可见;
  • 如果所有顶点都不在视锥范围内,那么待判区域很可能不可见了,但有一种情况例外,就是视锥体在长方体以内,这种情况我们要加以区分。
基于OpenGL实现

float g_frustumPlanes[6][4];

void calculateFrustumPlanes( void )
{
    float p[16];   // projection matrix
    float mv[16];  // model-view matrix
    float mvp[16]; // model-view-projection matrix
    float t;



    glGetFloatv( GL_PROJECTION_MATRIX, p );
    glGetFloatv( GL_MODELVIEW_MATRIX, mv );

    //
    // Concatenate the projection matrix and the model-view matrix to produce
    // a combined model-view-projection matrix.
    //

    mvp[ 0] = mv[ 0] * p[ 0] + mv[ 1] * p[ 4] + mv[ 2] * p[ 8] + mv[ 3] * p[12];
    mvp[ 1] = mv[ 0] * p[ 1] + mv[ 1] * p[ 5] + mv[ 2] * p[ 9] + mv[ 3] * p[13];
    mvp[ 2] = mv[ 0] * p[ 2] + mv[ 1] * p[ 6] + mv[ 2] * p[10] + mv[ 3] * p[14];
    mvp[ 3] = mv[ 0] * p[ 3] + mv[ 1] * p[ 7] + mv[ 2] * p[11] + mv[ 3] * p[15];

    mvp[ 4] = mv[ 4] * p[ 0] + mv[ 5] * p[ 4] + mv[ 6] * p[ 8] + mv[ 7] * p[12];
    mvp[ 5] = mv[ 4] * p[ 1] + mv[ 5] * p[ 5] + mv[ 6] * p[ 9] + mv[ 7] * p[13];
    mvp[ 6] = mv[ 4] * p[ 2] + mv[ 5] * p[ 6] + mv[ 6] * p[10] + mv[ 7] * p[14];
    mvp[ 7] = mv[ 4] * p[ 3] + mv[ 5] * p[ 7] + mv[ 6] * p[11] + mv[ 7] * p[15];

    mvp[ 8] = mv[ 8] * p[ 0] + mv[ 9] * p[ 4] + mv[10] * p[ 8] + mv[11] * p[12];
    mvp[ 9] = mv[ 8] * p[ 1] + mv[ 9] * p[ 5] + mv[10] * p[ 9] + mv[11] * p[13];
    mvp[10] = mv[ 8] * p[ 2] + mv[ 9] * p[ 6] + mv[10] * p[10] + mv[11] * p[14];
    mvp[11] = mv[ 8] * p[ 3] + mv[ 9] * p[ 7] + mv[10] * p[11] + mv[11] * p[15];

    mvp[12] = mv[12] * p[ 0] + mv[13] * p[ 4] + mv[14] * p[ 8] + mv[15] * p[12];
    mvp[13] = mv[12] * p[ 1] + mv[13] * p[ 5] + mv[14] * p[ 9] + mv[15] * p[13];
    mvp[14] = mv[12] * p[ 2] + mv[13] * p[ 6] + mv[14] * p[10] + mv[15] * p[14];
    mvp[15] = mv[12] * p[ 3] + mv[13] * p[ 7] + mv[14] * p[11] + mv[15] * p[15];



    //
    // Extract the frustum's right clipping plane and normalize it.
    //

    g_frustumPlanes[0][0] = mvp[ 3] - mvp[ 0];
    g_frustumPlanes[0][1] = mvp[ 7] - mvp[ 4];
    g_frustumPlanes[0][2] = mvp[11] - mvp[ 8];
    g_frustumPlanes[0][3] = mvp[15] - mvp[12];

    t = (float) sqrt( g_frustumPlanes[0][0] * g_frustumPlanes[0][0] +
                      g_frustumPlanes[0][1] * g_frustumPlanes[0][1] +
                      g_frustumPlanes[0][2] * g_frustumPlanes[0][2] );

    g_frustumPlanes[0][0] /= t;
    g_frustumPlanes[0][1] /= t;
    g_frustumPlanes[0][2] /= t;
    g_frustumPlanes[0][3] /= t;

    //
    // Extract the frustum's left clipping plane and normalize it.
    //

    g_frustumPlanes[1][0] = mvp[ 3] + mvp[ 0];
    g_frustumPlanes[1][1] = mvp[ 7] + mvp[ 4];
    g_frustumPlanes[1][2] = mvp[11] + mvp[ 8];
    g_frustumPlanes[1][3] = mvp[15] + mvp[12];

    t = (float) sqrt( g_frustumPlanes[1][0] * g_frustumPlanes[1][0] +
                      g_frustumPlanes[1][1] * g_frustumPlanes[1][1] +
                      g_frustumPlanes[1][2] * g_frustumPlanes[1][2] );

    g_frustumPlanes[1][0] /= t;
    g_frustumPlanes[1][1] /= t;
    g_frustumPlanes[1][2] /= t;
    g_frustumPlanes[1][3] /= t;



    //
    // Extract the frustum's bottom clipping plane and normalize it.
    //

    g_frustumPlanes[2][0] = mvp[ 3] + mvp[ 1];
    g_frustumPlanes[2][1] = mvp[ 7] + mvp[ 5];
    g_frustumPlanes[2][2] = mvp[11] + mvp[ 9];
    g_frustumPlanes[2][3] = mvp[15] + mvp[13];

    t = (float) sqrt( g_frustumPlanes[2][0] * g_frustumPlanes[2][0] +
                      g_frustumPlanes[2][1] * g_frustumPlanes[2][1] +
                      g_frustumPlanes[2][2] * g_frustumPlanes[2][2] );

    g_frustumPlanes[2][0] /= t;
    g_frustumPlanes[2][1] /= t;
    g_frustumPlanes[2][2] /= t;
    g_frustumPlanes[2][3] /= t;

    //
    // Extract the frustum's top clipping plane and normalize it.
    //

    g_frustumPlanes[3][0] = mvp[ 3] - mvp[ 1];
    g_frustumPlanes[3][1] = mvp[ 7] - mvp[ 5];
    g_frustumPlanes[3][2] = mvp[11] - mvp[ 9];
    g_frustumPlanes[3][3] = mvp[15] - mvp[13];

    t = (float) sqrt( g_frustumPlanes[3][0] * g_frustumPlanes[3][0] +
                      g_frustumPlanes[3][1] * g_frustumPlanes[3][1] +
                      g_frustumPlanes[3][2] * g_frustumPlanes[3][2] );

    g_frustumPlanes[3][0] /= t;
    g_frustumPlanes[3][1] /= t;
    g_frustumPlanes[3][2] /= t;
    g_frustumPlanes[3][3] /= t;



    //
    // Extract the frustum's far clipping plane and normalize it.
    //

    g_frustumPlanes[4][0] = mvp[ 3] - mvp[ 2];
    g_frustumPlanes[4][1] = mvp[ 7] - mvp[ 6];
    g_frustumPlanes[4][2] = mvp[11] - mvp[10];
    g_frustumPlanes[4][3] = mvp[15] - mvp[14];

    t = (float) sqrt( g_frustumPlanes[4][0] * g_frustumPlanes[4][0] +
                      g_frustumPlanes[4][1] * g_frustumPlanes[4][1] +
                      g_frustumPlanes[4][2] * g_frustumPlanes[4][2] );

    g_frustumPlanes[4][0] /= t;
    g_frustumPlanes[4][1] /= t;
    g_frustumPlanes[4][2] /= t;
    g_frustumPlanes[4][3] /= t;

    //
    // Extract the frustum's near clipping plane and normalize it.
    //

    g_frustumPlanes[5][0] = mvp[ 3] + mvp[ 2];
    g_frustumPlanes[5][1] = mvp[ 7] + mvp[ 6];
    g_frustumPlanes[5][2] = mvp[11] + mvp[10];
    g_frustumPlanes[5][3] = mvp[15] + mvp[14];

    t = (float) sqrt( g_frustumPlanes[5][0] * g_frustumPlanes[5][0] +
                      g_frustumPlanes[5][1] * g_frustumPlanes[5][1] +
                      g_frustumPlanes[5][2] * g_frustumPlanes[5][2] );

    g_frustumPlanes[5][0] /= t;
    g_frustumPlanes[5][1] /= t;
    g_frustumPlanes[5][2] /= t;
    g_frustumPlanes[5][3] /= t;

}

bool isBoundingSphereInFrustum( float x, float y, float z)
{
    for( int i = 0; i < 6; ++i )
    {
        if( g_frustumPlanes[i][0] * x +
            g_frustumPlanes[i][1] * y +
            g_frustumPlanes[i][2] * z +
            g_frustumPlanes[i][3] <= 0)
            return false;
    }

    return true;
}

原文链接http://www.linuxgraphics.cn/graphics/opengl_view_frustum_culling.html
### 回答1: 相机视锥(Camera Frustum)是在计算机图形学中用于表示相机在三维空间中可见范围的几何形状。通过计算相机视锥代码,我们可以获得相机的射线方向和视野范围,从而帮助我们进行可视化渲染等操作。 以下是一个简单的计算相机视锥的代码示例: 1. 首先,我们需要获取相机的投影矩阵(projection matrix)和相机的视图矩阵(view matrix),这两个矩阵通常由引擎或库提供。 2. 然后,我们可以使用这两个矩阵来计算相机视锥的六个面(near, far, left, right, top, bottom)。 3. 首先,我们需要将投影矩阵和视图矩阵相乘,得到投影视图矩阵(projection-view matrix)。 4. 接下来,我们使用逆转置矩阵的转置以及四个坐标分量计算各个面的法线和距离。根据相机视锥的特性,这些面的法线向量和距离可以帮助我们计算视锥的六个面的方程式。 5. 最后,我们可以将这些值存储在一个数据结构中,以供后续的计算和使用。 这只是计算相机视锥的一个简单示例,实际上,相机视锥的计算可能涉及更多复杂的操作和算法,我们需要考虑到相机的位置、方向、视角等因素。 通过计算相机视锥,我们可以实现一些基于相机的功能和效果,比如裁剪不可见物体、实现可见性检测、绘制远近景物体等。这对于实现高性能的可视化渲染是非常重要的。 ### 回答2: 相机视锥是指在计算机图形学中,用来确定在屏幕上显示的可见场景的范围。在3D图形渲染中,相机视锥由近裁剪面、远裁剪面、左裁剪面、右裁剪面、上裁剪面和下裁剪面六个平面组成,形成一个截锥体。计算相机视锥的代码通常需要用到视图矩阵、投影矩阵和顶点坐标等信息。 一个简单的计算相机视锥的代码示例如下: 1. 定义视图矩阵和投影矩阵。 ``` matrix4 viewMatrix = ...; matrix4 projectionMatrix = ...; ``` 2. 定义顶点坐标。 ``` vec3 vertex1 = ...; vec3 vertex2 = ...; ... ``` 3. 将顶点坐标变换到裁剪空间。 ``` vec4 clipVertex1 = projectionMatrix * viewMatrix * vec4(vertex1, 1.0); vec4 clipVertex2 = projectionMatrix * viewMatrix * vec4(vertex2, 1.0); ... ``` 4. 计算裁剪空间中的四个顶点(左下、左上、右下、右上)。 ``` vec4 bottomLeft = clipVertex1; vec4 topLeft = clipVertex2; vec4 bottomRight = clipVertex2; vec4 topRight = clipVertex1; ``` 5. 将裁剪空间中的顶点坐标变换到规范化设备坐标空间(NDC)。 ``` vec3 ndcVertex1 = bottomLeft.xyz / bottomLeft.w; vec3 ndcVertex2 = topLeft.xyz / topLeft.w; ... ``` 6. 计算相机视锥的近裁剪面、远裁剪面以及左、右、上、下裁剪面在规范化设备坐标空间中的位置。 ``` float nearPlane = ndcVertex1.z; float farPlane = -ndcVertex2.z; float leftPlane = ndcVertex1.x; float rightPlane = ndcVertex2.x; float bottomPlane = ndcVertex1.y; float topPlane = ndcVertex2.y; ``` 以上是一个简单的计算相机视锥的代码示例。一般来说,还可以根据需要添加更多功能,例如视锥的可视体积、剔除不可见的物体等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值