openGL光线选择 [英] openGL ray pick

查看:251
本文介绍了openGL光线选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般的Ray拾取过程应该如下(实验结果证明是正确的):

The general Ray picking process should be as follows(experiment result proved to be right):


  1. 标准化设备空间方向向量:

  1. transform screen point to normalized device space direction vector:

float x = (2.0f * mouse_x) / width - 1.0f;
float y = 1.0f - (2.0f * mouse_y) / height;
float z = 1.0f;
vec3 ray_nds = vec3 (x, y, z);


  • 将方向向量转换为均匀剪辑坐标

  • transform direction vector to Homogeneous Clip Coordinates

    vec4 ray_clip = vec4 (ray_nds.xy, -1.0, 1.0);
    


  • 将方向向量转换为眼睛空间方向向量

  • transform direction vector to eye space direction vector

    vec4 ray_eye = inverse (projection_matrix) * ray_clip;
    


  • 将方向向量转换为世界空间,获取世界空间相机位置方向向量

  • transform direction vector to world space, get a pick ray with world space camera position and the direction vector

    我的问题是,在规范化设备空间中,为什么方向向量的z分量是1.0?
    我的意思是,在OpenGL规范化的设备空间中,xyz分量应该在-1〜1的范围内,因此相机应该在平面的中心z = -1。所以方向矢量应该是:查看目标位置 - 摄像机位置,z分量应为1 - ( - 1)= 2.0f。 (在DirectX标准化的设备空间中,xy分量在-1〜1的范围内,z分量在0〜1的范围内,摄像机位置应该在平面的中心z = 0, 0,0),方向向量的z分量应为1-0 = 1)

    My problem is, in normalized device space, why the z component of the direction vector is 1.0? I mean, in OpenGL normalized device space, xyz component should all be in the range of -1~1, so the camera should be in the center of the plane z=-1. So the direction vector should be: view target position - camera position, and the z component should be 1-(-1)=2.0f. (in DirectX normalized device space, xy component is in the range of -1~1, z component is in the range of 0~1, the camera position should be in the center of the plane z=0, say, (0,0,0), and the z component of the direction vector should be 1-0=1)

    推荐答案

    ray_nds.z 是完全不相关的,因为你不使用它。这是因为你不知道像素的深度。

    ray_nds.z is completely irrelevant, because you don't use it anyway. That's because you don't know the pixel's depth.

    ray_clip 不是方向,近投影后的剪切平面(z = -1)。如果你撤消这个投影(使用逆投影矩阵),你最终与相机空间中的相同点。在相机空间中,相机以(0,0,0)为中心。光线的方向向量可以用 ray_eye - (0,0,0)计算,其实质上是 ray_eye 。因此,如果我们忽略w分量,我们可以使用位置作为方向。这只在相机空间工作!剪辑空间和世界空间都很可能在其他地方有投影中心。

    ray_clip is not a direction, but a position on the near clipping plane (z=-1) after projection. If you undo this projection (with the inverse projection matrix) you end up with the same point in camera space. In camera space, the camera is centered at (0, 0, 0). The direction vector of the ray can be calculated with ray_eye - (0, 0, 0), which is essentially ray_eye. So if we ignore the w-component, we can use the position as a direction. This does only work in camera space! Both clip space and world space are most likely to have the projection center somewhere else.

    不要混淆不同空间中的相机位置。在相机空间,它在原点。在剪辑空间中,可以假定为(0,0,-infinity)。点(x,y,...)只是由相应像素覆盖的任意点。你需要任何他们来定义射线。

    Don't mix up the camera position in the different spaces. In camera space, it is at the origin. In clip space it can be assumed to be at (0, 0, -infinity). The point (x, y, ...) is just an arbitrary point that is covered by the according pixel. And you need any of them to define the ray.

    这篇关于openGL光线选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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