MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ screen_to_ray()

Ray MayaFlux::Kinesis::screen_to_ray ( double  window_x,
double  window_y,
uint32_t  width,
uint32_t  height,
const ViewTransform vt 
)
inline

Construct a world-space ray from window pixel coordinates.

Parameters
window_xX in window space [0, width]
window_yY in window space [0, height] (top-left origin)
widthWindow width in pixels.
heightWindow height in pixels.
vtActive 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}
uint32_t width

References MayaFlux::Kinesis::ViewTransform::projection, MayaFlux::Kinesis::ViewTransform::view, and width.