Construct a world-space ray from window pixel coordinates.
- Parameters
-
| window_x | X in window space [0, width] |
| window_y | Y in window space [0, height] (top-left origin) |
| width | Window width in pixels. |
| height | Window height in pixels. |
| vt | Active ViewTransform (view + projection matrices). |
- Returns
- Ray with origin at the near plane and normalized direction into the scene.
Unprojects two points at z=0 (near) and z=1 (far) in NDC through the inverse of projection * view. The resulting world-space positions define the ray origin and direction.
Definition at line 53 of file HitTest.hpp.
57{
58 float ndc_x = (
static_cast<float>(window_x) /
static_cast<float>(
width)) * 2.0F - 1.0F;
59 float ndc_y = 1.0F - (
static_cast<float>(window_y) /
static_cast<float>(
height)) * 2.0F;
60
61 glm::mat4 inv_vp = glm::inverse(
vt.projection *
vt.view);
62
63 glm::vec4 near_h = inv_vp * glm::vec4(ndc_x, ndc_y, 0.0F, 1.0F);
64 glm::vec4 far_h = inv_vp * glm::vec4(ndc_x, ndc_y, 1.0F, 1.0F);
65
66 glm::vec3 near_w = glm::vec3(near_h) / near_h.w;
67 glm::vec3 far_w = glm::vec3(far_h) / far_h.w;
68
69 return { .origin = near_w, .direction = glm::normalize(far_w - near_w) };
70}
References height, vt, and width.