Skip to main content

render_raytrace_rgba

Function render_raytrace_rgba 

Source
pub fn render_raytrace_rgba(
    scene: &RayTraceScene,
    camera: &Camera3D,
    lights: &LightingUniforms,
    settings: &RayTraceSettings,
    width: u32,
    height: u32,
    out: &mut Vec<u8>,
) -> Result<(), String>
Expand description

Renders a ray-traced RGBA frame on the CPU.

This is the CPU reference and the Canvas 2D backend’s renderer. It does not re-implement traversal or shading: each pixel builds a Ray — a stack value, so no allocation happens per pixel, per bounce, or per frame — and hands it to RayTraceScene::trace_with_bounces, which runs the scene’s own bounding volume hierarchy and the real LightingUniforms shading. The two GPU backends implement the same math in shader form against the packed buffers; they are the only duplicates, and they must agree with this path rather than with each other.

The frame is described by a [FrameBasis], a stack value built by the same validator pack_raytrace_frame uses, so this path never packs a GPU buffer, never re-encodes the scene, and allocates nothing at all beyond the caller-owned out. A steady-state frame therefore performs exactly one heap operation: none.

Each pixel traces samples_per_axis squared supersampled rays, averages them, clamps the linear color to [0, 1], applies the 1 / 2.2 gamma curve, and writes an opaque alpha. Rows are emitted top-down.

§Arguments

  • &RayTraceScene - The scene to render.
  • &Camera3D - The camera supplying the eye, target, up, fov, and near/far range.
  • &LightingUniforms - The lights, ambient, and shading eye.
  • &RayTraceSettings - The sampling grid and bounce limit.
  • u32 - The frame width in pixels.
  • u32 - The frame height in pixels.
  • &mut Vec<u8> - The destination RGBA buffer, cleared and reused.

§Returns

  • Result<(), String> - Ok once the frame is written, or the reason the frame cannot be rendered.