游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6473|回复: 3

可编程管道下的剪裁平面【原】

[复制链接]

34

主题

443

帖子

478

积分

中级会员

Rank: 3Rank: 3

积分
478
发表于 2010-1-20 22:07:00 | 显示全部楼层 |阅读模式
低手交流,高手勿入

作者:Bill Hsu

主页:http://www.graptor.com

GameRes发帖里的公式什么的可能不太整齐,
在这里看可能效果好一点~
http://hi.baidu.com/probill/blog/item/32111f1ccbc2cd8086d6b6f1.html


剪裁平面(Clip Plane)在图形学领域有着重要的作用,比如水面模拟中,渲染折射纹理时,我们就必须将水面以上的顶点通过剪裁平面剪裁掉。

在过去的固定渲染管道时代,剪裁平面的实现较为简单,比如在DirectX 9中,可以先设定剪裁平面在世界坐标系下的方程(ax+by+cz+d=0),再调用SetClipPlane(DWORD Index,CONST float * pPlane)这个API函数就可以了。



附上例子程序:

vPosition=D3DXVECTOR3(0,0,0);//平面上一个点
vNormal=D3DXVECTOR3(0,1,0);//法向量
D3DXPlaneFromPointNormal( &clipplane, &vPosition, &vNormal );//生成剪裁平面

m_pDevice()->SetClipPlane( 0, (float*)clipplane);



然而,在现在的可编程管道(programmable pipeline)下,设置的剪裁平面会被在剪裁坐标系下处理,而不是在世界坐标系下。
解决这个问题的方法有:

1)  给要剪裁的顶点做标记,在Pixel Shader中把它剪裁掉。

2)  使用近斜平面裁剪(Oblique Near-Plane Clipping),即修改投影矩阵,将要剪裁的顶点放在视截体之外,从而避免了该顶点的绘制。

3)  修改平面方程,使之从世界坐标系转换到剪裁坐标系。


上述方法中,第一种和第二种效率并不高:在Pixel Shader中剪裁没有减少任何不必要的顶点处理,而计算近斜平面裁剪矩阵较为繁琐。所以,方法三是最佳选择。

要将一个平面从世界坐标系转换到剪裁坐标系,必须求出这个变换矩阵。

设平面方程ax+by+cz+d=0,用一个4维向量来n表示(a,b,c,d),设平面上有个点px,y,z,1)。根据平面方程的定义,有:


nT p = ax + by + cz + d = 0

设矩阵R可以让点P从世界坐标系转换到剪裁坐标系,矩阵Q可以让平面n实现同样的变换。那么,有:

p'= R p
n'= Q n

其中p'、n'分别是转换后的点与平面。

n'T p'= 0
(Q n)T (R p) = 0
nT QT R p = 0


如果:QT R = I

那么:

nT QT R p = nT I p = nT p = 0

于是:
QT = R-1
Q = (R-1)T

在DirectX 3D中,将一个点从世界坐标系转换到剪裁坐标系,所用的矩阵为观察矩阵与投影矩阵的乘积,即:

D3DXMATRIX  TranMatrix = matView*matProj;

(TranMatrix为所求的变换矩阵,matView和matProj分别为观察矩阵与投影矩阵)



附上在D3D中变换的完整代码:

D3DXPLANE tempPlane = clipplane;
D3DXPlaneNormalize(&tempPlane, &tempPlane);

D3DXMATRIX  TranMatrix = matView*matProj;
D3DXMatrixInverse(&TranMatrix, NULL, &TranMatrix);
D3DXMatrixTranspose(&TranMatrix, &TranMatrix);
D3DXPlaneTransform(&tempPlane, &tempPlane, &TranMatrix);

参考资料:

1.Back Face Culling Notes ,Jordan Smith (University of California, Berkeley)

http://www.cs.berkeley.edu/~ug/slide/pipeline/assignments/backfacecull.shtml

2.GameDev Forum

http://www.gamedev.net/community/forums/topic.asp?topic_id=402381

3.Oblique Near-Plane Clipping with Orthographic Camera ,Aras

http://aras-p.info/texts/obliqueortho.html

15

主题

62

帖子

64

积分

注册会员

Rank: 2

积分
64
发表于 2010-1-20 23:42:00 | 显示全部楼层

Re:可编程管道下的剪裁平面【原】

顶,不错~~

0

主题

243

帖子

357

积分

中级会员

Rank: 3Rank: 3

积分
357
发表于 2010-1-21 09:04:00 | 显示全部楼层

Re:可编程管道下的剪裁平面【原】

恩。。。第三种方式唯一的问题就是场景里有的是固定管线渲染,有的不是的情况。。。

0

主题

23

帖子

23

积分

注册会员

Rank: 2

积分
23
发表于 2010-1-26 13:48:00 | 显示全部楼层

Re:可编程管道下的剪裁平面【原】

That's what makes living together on wow power leveling a day-to-day basis, with all its ups and downs, easy and

comfortable and why it's wow power leveling important for every couple to have intimacy rituals that can

world of warcraft power leveling be practiced daily.For you, an intimacy ritual might be calling each

world of warcraft power leveling other on  your lunch gold for wow

hour and taking 15 minutes to catch cheapest wow gold up on your day, cooking dinner together, doing an

evening crossword puzzle, playing dominoes, or wow gold uk turning off the TV, snuggling and chatting for 20

minutes before going to sleep.




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-16 00:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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