Oblique Frustum Clipping

这里是例子代码:
http://www.terathon.com/code/oblique.html


具体的说明可以参见 http://www.opengl.org/discussion_boards/cgi_directory/ultimatebb.cgi?ubb=get_topic;f=6;t=000170
其重要部分摘录如下:


Let P = (Px, Py, Pz, Pw) be the eye-space plane to which you would like to clip.
This will replace your ordinary near plane and should be facing away from the
camera, so Pz < 0.

Let Q be an eye-space point. If P dot Q = 0, then Q lies on the plane P.

What we want to do is modify the projection matrix M so that points Q for
which P dot Q = 0 get transformed in such a way that the transformed z-coordinate
is the negation of the transformed w-coordinate. This corresponds to
the projected z-coordinate that you would ordinarily get for a point lying
on the near plane. After division by the w-coordinate, you get -1.

The projection matrix M transforms points from eye space into homogeneous clip
space. To obtain the clip-space plane P', we need to multiply P by the inverse
transpose of M. (This is because planes are covariant vectors.) The clip-space
plane P' is given by

-1 T
P' = (M ) P

Let Q' be the projection of the point Q, given by Q' = MQ. If Q lies on the plane P,
then P' dot Q' = 0, but we want it to be -Q'w so that it corresponds to the near plane.
So we force it by subtracting 1 from the w-coordinate of P'. Before doing this,
however, we want to divide P' by its z-coordinate as to avoid scaling the z
direction in clip space. Doing all of this gives us the following plane P":

P'x P'y P'w
P" = (-----, -----, 1, ----- - 1)
P'z P'z P'z

We integrate this into the projection matrix M by constructing a new projection
matrix M' as follows.

[ 1 0 0 0 ]
[ ]
[ 0 1 0 0 ]
M' = [ ] M
[ P"x P"y 1 P"w ]
[ ]
[ 0 0 0 1 ]

The matrices M and M' only differ in the entries of the 3rd row, so we could just
calculate replacements for 3rd-row entries of M as follows:

M'(3,1) = P" dot M_1
M'(3,2) = P" dot M_2
M'(3,3) = P" dot M_3
M'(3,4) = P" dot M_4

where M_i means the i-th column of M and M'(i,j) means the (i,j)-th entry of M'.


Okay -- here's the short, short version of the optimal implementation of the oblique frustum.

Let C = camera-space clipping plane. Assume the camera is on the negative side of the plane: C_w < 0.

Let M be the original projection matrix; M must be invertible, but otherwise we don't care what
it is. We'll modify the third row of M so that the near plane coincides with the arbitrary
clipping plane C. We aren't allowed to modify the fourth row of M because doing so would screw
up the perspective-correct vertex attribute interpolation. (The fourth row usually just
moves the z-coordinate of a camera-space point into the w-coordinate of a clip-space point.)

Given a projection matrix M, the near plane is always M_4 + M_3, and the far plane is always
M_4 - M_3. (M_i means the i-th row of M.) To force the near plane to coincide with the
clipping plane C, we must have M_3 = aC - M_4, where a is some positive scale factor that we
can adjust.

Why adjust a? Because now our far plane F has been moved to F = 2*M_4 - aC, which is not
generally parallel to C. F intersects C on the x-y plane, so it's really not in a good
position. The best we can do is minimize the size of the view frustum by choosing the
constant a so that F contains the corner Q of the view frustum opposite the near plane C.
This point Q is given by

Q = M^-1 * (sgn(C_x), sgn(C_y), 1, 1),

where M^-1 is the inverse of the projection matrix. (The points (+/-1, +/-1, 1, 1) are the
four corners of the view frustum on the far plane in clip space.) The scale factor a is now
given by

a = (2*M_4 dot Q) / (C dot Q).

Since M_4 is usually (0, 0, -1, 0), this can be simplified a little.

All we have to do to the original projection matrix M is replace the third row with aC - M_4.

-- Eric Lengyel


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值