游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2013|回复: 6

frustum culling

[复制链接]

21

主题

27

帖子

33

积分

注册会员

Rank: 2

积分
33
发表于 2005-8-25 16:49:00 | 显示全部楼层 |阅读模式
void CFrustum::CalculateFrustum()
{   
float   proj[16]; // This will hold our projection matrix
float   modl[16]; // This will hold our modelview matrix
float   clip[16]; // This will hold the clipping planes

// glGetFloatv() is used to extract information about our OpenGL world.
// Below, we pass in GL_PROJECTION_MATRIX to abstract our projection matrix.
// It then stores the matrix into an array of [16].
glGetFloatv( GL_PROJECTION_MATRIX, proj );

// By passing in GL_MODELVIEW_MATRIX, we can abstract our model view matrix.
// This also stores it in an array of [16].
glGetFloatv( GL_MODELVIEW_MATRIX, modl );

// Now that we have our modelview and projection matrix, if we combine these 2 matrices,
// it will give us our clipping planes.  To combine 2 matrices, we multiply them.

clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12];
clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13];
clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14];
clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15];

clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12];
clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13];
clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14];
clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15];

clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12];
clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13];
clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14];
clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15];

clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12];
clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13];
clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14];
clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15];

// Now we actually want to get the sides of the frustum.  To do this we take
// the clipping planes we received above and extract the sides from them.

// This will extract the RIGHT side of the frustum
m_Frustum[RIGHT][A] = clip[ 3] - clip[ 0];
m_Frustum[RIGHT][B] = clip[ 7] - clip[ 4];
m_Frustum[RIGHT][C] = clip[11] - clip[ 8];
m_Frustum[RIGHT][D] = clip[15] - clip[12];

// Now that we have a normal (A,B,C) and a distance (D) to the plane,
// we want to normalize that normal and distance.

// Normalize the RIGHT side
NormalizePlane(m_Frustum, RIGHT);

// This will extract the LEFT side of the frustum
m_Frustum[LEFT][A] = clip[ 3] + clip[ 0];
m_Frustum[LEFT][B] = clip[ 7] + clip[ 4];
m_Frustum[LEFT][C] = clip[11] + clip[ 8];
m_Frustum[LEFT][D] = clip[15] + clip[12];

// Normalize the LEFT side
NormalizePlane(m_Frustum, LEFT);

// This will extract the BOTTOM side of the frustum
m_Frustum[BOTTOM][A] = clip[ 3] + clip[ 1];
m_Frustum[BOTTOM][B] = clip[ 7] + clip[ 5];
m_Frustum[BOTTOM][C] = clip[11] + clip[ 9];
m_Frustum[BOTTOM][D] = clip[15] + clip[13];

// Normalize the BOTTOM side
NormalizePlane(m_Frustum, BOTTOM);

// This will extract the TOP side of the frustum
m_Frustum[TOP][A] = clip[ 3] - clip[ 1];
m_Frustum[TOP][B] = clip[ 7] - clip[ 5];
m_Frustum[TOP][C] = clip[11] - clip[ 9];
m_Frustum[TOP][D] = clip[15] - clip[13];

// Normalize the TOP side
NormalizePlane(m_Frustum, TOP);

// This will extract the BACK side of the frustum
m_Frustum[BACK][A] = clip[ 3] - clip[ 2];
m_Frustum[BACK][B] = clip[ 7] - clip[ 6];
m_Frustum[BACK][C] = clip[11] - clip[10];
m_Frustum[BACK][D] = clip[15] - clip[14];

// Normalize the BACK side
NormalizePlane(m_Frustum, BACK);

// This will extract the FRONT side of the frustum
m_Frustum[FRONT][A] = clip[ 3] + clip[ 2];
m_Frustum[FRONT][B] = clip[ 7] + clip[ 6];
m_Frustum[FRONT][C] = clip[11] + clip[10];
m_Frustum[FRONT][D] = clip[15] + clip[14];

// Normalize the FRONT side
NormalizePlane(m_Frustum, FRONT);
}

能不能解释一下原理阿

39

主题

102

帖子

102

积分

注册会员

Rank: 2

积分
102
发表于 2005-8-25 21:58:00 | 显示全部楼层

Re:frustum culling

这个是OGL下的经典投影剔除吧!我也非常的想知道这个的原理!哪位高人给解释一下啊!

44

主题

248

帖子

274

积分

中级会员

Rank: 3Rank: 3

积分
274
发表于 2005-8-26 10:02:00 | 显示全部楼层

Re:frustum culling

已经注释的很详细了阿,通过构造一个视景体,然后逐个判断点,或者三角形是不是在视景体内部,来实现,主要牵扯到构造视景体的6个平面的方程,这个搞懂了就没什么问题了

89

主题

4036

帖子

4132

积分

论坛元老

Rank: 8Rank: 8

积分
4132
发表于 2005-8-27 17:53:00 | 显示全部楼层

Re:frustum culling

long long ago , 我写过一个论文.论文里有很详细的解释.. 不过有地方我写错了。把一个-1写成0了。不过看懂了应该能看出来。
http://xreal.51.net/

18

主题

971

帖子

982

积分

高级会员

Rank: 4

积分
982
发表于 2005-8-28 13:54:00 | 显示全部楼层

Re:frustum culling

0是dx的的截头体,过两天我把详细的数学推导过程写成文章发到这儿来吧。frustum是最重要的场景剔除技术之一,能去年大部分的多余节点.

33

主题

118

帖子

173

积分

注册会员

Rank: 2

积分
173
发表于 2005-8-28 16:54:00 | 显示全部楼层

Re:frustum culling

自己看一下real-time rendering,里面有详细的推导过程。

69

主题

450

帖子

473

积分

中级会员

战魂缔造者

Rank: 3Rank: 3

积分
473
QQ
发表于 2005-8-29 17:49:00 | 显示全部楼层

Re:frustum culling

将包裹物体的8个顶点和12条线与视锥矩形的6跟边进行比较
具体可以看下DXSDK里的Cull,虽然你是写OGL吧
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-27 15:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表