游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3086|回复: 9

有关裁剪转化的问题

[复制链接]

4

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
发表于 2004-11-4 14:03:00 | 显示全部楼层 |阅读模式
我是一个刚学习OpenGL的新手,昨天在www.gametutorials.com上看到一篇有关Frustum Culling 的文章,其中有一段代码我是百思不得其解,特请教各位高手,代码如下:
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);
}
我不明白为什么从那两个 GL_PROJECTION_MATRIX,GL_MODELVIEW_MATRIX  matrix得出的就可以转化出那六个面,请高手讲解一下他们之间的转化过程.多谢!

139

主题

2005

帖子

2057

积分

金牌会员

Rank: 6Rank: 6

积分
2057
QQ
发表于 2004-11-4 19:25:00 | 显示全部楼层

Re:有关裁剪转化的问题

数学问题。
这几天忙考试,考期过了给你详细地说吧……

4

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
 楼主| 发表于 2004-11-5 09:03:00 | 显示全部楼层

Re: 有关裁剪转化的问题

多谢,大哥你考完了一定要来啊!

4

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
 楼主| 发表于 2004-11-17 19:08:00 | 显示全部楼层

Re:有关裁剪转化的问题

2楼的,你还没考完吗?
难道就没有其他人会吗?

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2004-11-21 23:29:00 | 显示全部楼层

Re:有关裁剪转化的问题

我觉得问题可以归纳一下:
projection matrix怎么算能指出六个平面方程?
我也盼答

14

主题

245

帖子

256

积分

中级会员

Rank: 3Rank: 3

积分
256
QQ
发表于 2004-11-22 14:21:00 | 显示全部楼层

Re:有关裁剪转化的问题

这个是产生透视矩阵的问题.
你可以参考一下,Opengl超级宝典18页,再参考一下,138页,再弄清楚GL_PROJECTION_MATRIX,GL_MODELVIEW_MATRIX的含义,应该不难吧.

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2004-11-22 17:02:00 | 显示全部楼层

Re:有关裁剪转化的问题

这个应该都知道的呀
只是不明白这六个面的四个参数是怎么倒推出来的
不过我其实还是投影矩阵的正推没有搞清楚
图形学书上的投影矩阵与opengl的好象。。。回头再看看

// 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);

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2004-11-22 20:38:00 | 显示全部楼层

Re:有关裁剪转化的问题

its a key
http://rush3d.com/reference/opengl-redbook-1.1/figures/eqapg06.gif

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2004-11-24 00:03:00 | 显示全部楼层

Re:有关裁剪转化的问题

为什么是modl右乘 proj?

4

主题

21

帖子

21

积分

注册会员

Rank: 2

积分
21
 楼主| 发表于 2004-11-25 09:09:00 | 显示全部楼层

Re:有关裁剪转化的问题

我还是不明白!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-18 00:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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