gl_ObjectRayOriginEXT in GLSL Collision Calculations

In GLSL ray tracing extensions, gl_ObjectRayOriginEXT is a built-in input variable that provides the origin point of the active ray transformed into the local object space of the geometry instance being evaluated. This article explains the role of gl_ObjectRayOriginEXT in object-space collision and intersection calculations, detailing how it leverages hardware-level matrix transformations to simplify geometric algorithms, reduce memory overhead, and accelerate real-time collision detection.

Automatic Space Transformation

In standard ray tracing pipelines (such as Vulkan Ray Tracing using GL_EXT_ray_tracing), rays are typically cast into a 3D scene in world space using gl_WorldRayOriginEXT and gl_WorldRayDirectionEXT. When a ray intersects the acceleration structure of a specific instance, the ray tracing hardware applies the instance's inverse transformation matrix to the ray.

gl_ObjectRayOriginEXT contains the resulting local coordinates:

\[\text{ObjectRayOrigin} = \mathbf{M}_{\text{world-to-object}} \times \text{vec4}(\text{WorldRayOrigin}, 1.0)\]

This operation is executed transparently by the hardware or driver, freeing developers from manually tracking, binding, and multiplying dynamic instance transform matrices inside the shader stage.

Simplification of Canonical Intersection Tests

The primary utility of gl_ObjectRayOriginEXT in collision and intersection tests—typically evaluated inside intersection or any-hit shaders—is that it allows tests to run against canonical, axis-aligned geometries.

Instead of transforming a geometric primitive (such as an arbitrary oriented bounding box, capsule, or ellipsoid) into world space to check for a collision, the calculation tests the transformed ray against a standard shape centered at the object's local coordinate origin \((0, 0, 0)\):

  • Spheres: Testing a ray against a sphere centered at \((0, 0, 0)\) with radius \(r\) requires evaluating \(\| \text{gl\_ObjectRayOriginEXT} + t \cdot \text{gl\_ObjectRayDirectionEXT} \|^2 = r^2\). This eliminates complex offset math.
  • Axis-Aligned Bounding Boxes (AABBs): Ray-box slab tests require significantly fewer operations when the box faces align perfectly with the local \(X\), \(Y\), and \(Z\) axes defined relative to gl_ObjectRayOriginEXT.
  • SDF and Volumetric Collision: Signed distance field (SDF) raymarching routines can query local-space distance formulas directly without applying rotational or positional transforms inside iterative loops.

Parametric Consistency and Precision

When combined with gl_ObjectRayDirectionEXT, the ray origin forms the object-space ray definition:

\[P(t) = \text{gl\_ObjectRayOriginEXT} + t \cdot \text{gl\_ObjectRayDirectionEXT}\]

Because the ray direction is transformed using the same coordinate system, the parametric scalar \(t\) represents the exact distance factor along the ray. When reporting an intersection using reportIntersectionEXT(t, hitKind), the variable \(t\) translates consistently back to the world-space ray interval, preserving accurate collision distances and hit points across scaling, rotation, and translation transformations.

Additionally, evaluating collisions near the object's local origin avoids floating-point precision loss (jittering or self-intersection artifacts) that often occurs when performing calculations with large world-space coordinates far from the scene origin.