Skip to content

Engine → Core → ray.h

ray

Syntax

struct CONJURE_API ray

Variables

AccessNameTypeDefaultDescription
publicdirectionvec3vec3(0.0f, 0.0f, -1.0f)
publicoriginvec3vec3(0.0f)

Functions

Public

at

[[nodiscard]] vec3 at(
    f32 t) const noexcept;

The point at parameter t.

from_points

[[nodiscard]] static ray from_points(
    vec3 a,
    vec3 b) noexcept;

The ray from a to b with a normalised direction; a == b yields the default direction.

from_viewport

[[nodiscard]] static ray from_viewport(
    vec2 pixel,
    uvec2 viewportSize,
    const mat4 &inverseViewProjection) noexcept;

The world-space pick ray through a viewport pixel (origin top-left, y down; pixel centres are at +0.5). The origin lies on the near plane (depth 0). Works for perspective, infinite and orthographic projections. A zero-sized viewport asserts and uses the viewport centre. Assumes forward depth (near plane at depth 0, far plane at 1, as math::perspective builds). The renderer's matrices are reverse-Z: use renderer::ray_from_viewport for them.

intersect

[[nodiscard]] std::optional<f32> intersect(
    const aabb &box) const noexcept;

Slab test against a box. Returns 0 when the origin is inside, nullopt for a miss, an invalid box or a box entirely behind the origin. Rays parallel to a slab are handled.

intersect_plane

[[nodiscard]] std::optional<f32> intersect_plane(
    const plane &surface) const noexcept;

Hit with an infinite plane from either side. Returns nullopt when the ray is parallel to the plane (including when it lies in it) or the plane is behind the origin.

intersect_sphere

[[nodiscard]] std::optional<f32> intersect_sphere(
    vec3 center,
    f32 radius) const noexcept;

Nearest hit with a sphere. Returns 0 when the origin is inside; nullopt for a miss, a sphere behind the origin or a negative radius.

intersect_triangle

[[nodiscard]] std::optional<f32> intersect_triangle(
    vec3 a,
    vec3 b,
    vec3 c,
    bool cullBackFaces = true) const noexcept;

Moller-Trumbore triangle test. Triangles are counter-clockwise when seen from the front; with cullBackFaces the back side is ignored. Degenerate (collinear) triangles and rays parallel to the triangle never hit; the test is relative to the triangle size and the direction length, so millimetre triangles and grazing angles work. Edges and vertices count as hits.

Operator Overloads

Public

operator==

[[nodiscard]] bool operator==(
    const ray &other) const = default;