问题描述
我有一个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]
但是当查看方程式时,我们会发现我们只被插入逆矩阵的第四列,该矩阵将包含
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
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];
同样,当我们查看方程式时,我们将看到这仅考虑了逆矩阵的第三行,该行由
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 by
camera_dir_ws = [-view_matrix[2], -view_matrix[6], -view_matrix[10]]
计算交叉点
我们现在知道摄像机的位置P和视角D,因此我们必须沿着光线R(x,y,z) = P + l * D
找到x,z值,其中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.
注释
索引假定矩阵存储在以列为主的线性数组中.
The indices assume that the matrix is stored in a column-major linear array.
注意,形式为矩阵的逆
Note, that the inverse of a matrix of the form
M = [ R T ]
0 1
,其中R是3x3正交矩阵,由给出
, where R is a orthogonal 3x3 matrix, is given by
inv(M) = [ transpose(R) -T ]
0 1
这篇关于如何确定相机方向与平面的交点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!