如何确定相机方向与平面的交点? [英] How to determine the intersection between the camera direction and a plane?

查看:23
本文介绍了如何确定相机方向与平面的交点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 3D 场景,在沿 Y 垂直轴的高度为 H 处有一个无限的水平面(平行于 xz 坐标).

I have a 3D scene with an infinite horizontal plane (parallel to the xz coordinates) at a height H along the Y vertical axis.

我想知道如何确定相机的轴与该平面的交点.

I would like to know how to determine the intersection between the axis of my camera and this plane.

相机由视图矩阵和投影矩阵定义.

The camera is defined by a view-matrix and a projection-matrix.

推荐答案

这里有两个子问题: 1) 从相机矩阵中提取位置和视图方向.2) 计算视图射线与平面的交点.

There are two sub-problems here: 1) Extracting the position and view-direction from the camera matrix. 2) Calculating the intersection between the view-ray and the plane.

提取位置和视图方向

视图矩阵描述了点如何从世界空间转换到视图空间.OpenGL 中的视图空间通常是这样定义的,即相机位于原点并朝 -z 方向看.

The view matrix describes how points are transformed from world-space to view space. The view-space in OpenGL is usually defined such that the camera is in the origin and looks into the -z direction.

要获得相机的位置,我们必须将视图空间的原点 [0,0,0] 转换回世界空间.从数学上讲,我们必须计算:

To get the position of the camera, we have to transform the origin [0,0,0] of the view-space back into world-space. Mathematically speaking, we have to calculate:

camera_pos_ws = inverse(view_matrix) * [0,0,0,1]

但是当查看等式时,我们会看到我们只在包含 1

but when looking at the equation we'll see that we are only interrested in the 4th column of the inverse matrix which will contain 1

camera_pos_ws = [-view_matrix[12], -view_matrix[13], -view_matrix[14]]

相机的方向可以通过类似的计算找到.我们知道相机在视图空间中是在 -z 方向上看的,因此世界空间方向由

The orientation of the camera can be found by a similar calculation. We know that the camera looks in -z direction in view-space thus the world space direction is given by

camera_dir_ws = inverse(view_matrix) * [0,0,-1,0];

同样,当查看等式时,我们会看到这仅考虑了逆矩阵的第三行,它由2

Again, when looking at the equation, we'll see that this only takes the third row of the inverse matrix into account which is given by2

camera_dir_ws = [-view_matrix[2], -view_matrix[6], -view_matrix[10]]

计算交点

我们现在知道相机位置 P 和观察方向 D,因此我们必须找到沿射线的 x,z 值 R(x,y,z) = P + l * D其中 y 等于 H.由于只有一个未知数 l,我们可以从

We now know the camera position P and the view direction D, thus we have to find the x,z value along the ray R(x,y,z) = P + l * D where y equals H. Since there is only one unknown, l, we can calculate that from

y = Py + l * Dy
H = Py + l * Dy
l = (H - Py) / Dy

然后通过将 l 粘贴回光线方程来给出交点.

The intersection point is then given by pasting l back into the ray equation.

注意事项

1 索引假设矩阵存储在以列为主的线性数组中.

1 The indices assume that the matrix is stored in a column-major linear array.

2 注意,形式矩阵的逆

M = [  R T ]
       0 1

,其中 R 是一个正交的 3x3 矩阵,由

, where R is a orthogonal 3x3 matrix, is given by

inv(M) = [ transpose(R)  -T ]
                0         1

这篇关于如何确定相机方向与平面的交点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆