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
我的意思是,在OpenGL标准化设备空间中,xyz分量应全部在-1~1范围内,因此相机应该位于平面z=-1的中心位置。所以方向向量应为:视角目标位置 - 相机位置,而z分量应为1-(-1)=2.0f。(在DirectX标准化设备空间中,xy分量在-1~1范围内,z分量在0~1范围内,相机位置应该位于z=0的平面中心位置,例如(0,0,0),并且方向向量的z分量应为1-0=1)