GLSL rayQueryGetIntersectionWorldToObjectEXT Matrix
In inline GLSL ray tracing using the GL_EXT_ray_query
extension, converting between coordinate spaces is essential for
procedural shading, custom intersection tests, and physics calculations.
This article details the specific matrix returned by the
rayQueryGetIntersectionWorldToObjectEXT function, its
structural format in GLSL, and how it is applied to transform spatial
data from world space into an instance's local object space.
The Returned Matrix
The rayQueryGetIntersectionWorldToObjectEXT function
returns a 4x3 affine transformation matrix (represented
by the mat4x3 type in GLSL).
This matrix represents the World-to-Object transformation for the geometry instance involved in a ray intersection. It is the direct inverse of the object-to-world transformation matrix defined for the instance inside the top-level acceleration structure (TLAS).
Function Signature
mat4x3 rayQueryGetIntersectionWorldToObjectEXT(rayQueryEXT rayQuery, bool committed);rayQuery: The inline ray query object holding the traversal state.committed: A boolean flag specifying which intersection to query. Passingtrueretrieves the matrix for the currently committed (confirmed) intersection, whilefalseretrieves the matrix for the current candidate intersection during traversal.
Matrix Structure and GLSL Usage
In GLSL, a mat4x3 consists of 4 columns and 3 rows. This
non-square compact format accommodates standard 3D affine
transformations, which combine rotation, scale, and translation without
allocating an unused fourth row ([0, 0, 0, 1]).
When multiplying a position vector by this matrix:
- The world-space point must be promoted to a homogeneous 4-component
vector with
w = 1.0. - The resulting object-space vector is a 3-component vector.
// Retrieve the transformation matrix for the committed hit
mat4x3 worldToObject = rayQueryGetIntersectionWorldToObjectEXT(myRayQuery, true);
// Transform a world-space position into object space
vec4 worldPos = vec4(inWorldPosition, 1.0);
vec3 objectPos = worldToObject * worldPos;
// Transform a world-space direction vector (ignoring translation)
vec3 objectDir = mat3(worldToObject) * inWorldDirection;Primary Use Cases
- Procedural Intersection and Texturing: Evaluating analytical primitives (such as spheres, boxes, or signed distance fields) or mapping local UV coordinates requires ray parameters to be expressed relative to the object's origin and orientation.
- Normal Transformations: Transforming object-space geometric normals into world space or converting world-space lights and incident rays into local space for microfacet calculations.