Rendering

Rendering a scene needs the following ingredients:

  • hdrimg holds the HDR image data.
  • AbstractCamera object defines the camera parameters.
  • PCG is a pseudo-random number generator used for integration and sampling.
  • renderer is an instance of Function that take World and optionally a PCG object, instructing on how a ray should be traced through the scene.
  • ImageTracer is a function that takes the hdrimg, AbstractCamera, and renderer (and optionally a PCG) to render the scene.

First we need to create an hdrimg object to hold the rendered image data. The hdrimg constructor takes the width and height of the image as parameters.

jujutracer.hdrimgType
struct hdrimg(w::Int, h::Int)

A struct representing a high dynamic range image (HDR image). Getter and setter methods are defined for accessing and modifying pixel values. Access is done using 0-based indexing, so the first pixel is at (0, 0): img[0, 0]; while the bottom right pixel is at (w-1, h-1): img[w-1, h-1].

    img = [ (0  , 0)   (0  , 1) ... (0  , w-1)
            (1  , 0)   (1  , 1) ... (1  , w-1)
             ...
            (h-1, 1)   (h-1, 2) ... (h-1, w-1) ]

Fields

  • img::Matrix{RGB}: A matrix of RGB values representing the HDR image. Not intedend to be accessed directly.
  • w::Int64: The width of the image in pixels.
  • h::Int64: The height of the image in pixels.
source

The we can set the camera. We have two types of cameras: Perspective and Orthogonal, both subtypes of AbstractCamera.

jujutracer.PerspectiveType
Perspective

Perspective camera type

Fields

  • t::Transformation the transformation related to the camera's standing
  • a_ratio::Float64 aspect ratio of the screen of the camera

Constructor

  • Perspective() creates a perspective camera with Identity Transformation, a 16:9 aspect ratio placed in (-1, 0, 0)
  • Perspective(d = dist, t = trans, a_ratio = a) creates a perspective camera with trans Transformation, an a aspect ratio placed in (-d, 0, 0)
source
jujutracer.OrthogonalType
Orthogonal

Orthogonal camera type

Fields

  • t::Transformation the transformation related to the camera's standing
  • a_ratio::Float64 aspect ratio of the screen of the camera

Constructor

  • Orthogonal() creates an orthogonal camera with Identity Transformation and a 16:9 aspect ratio
  • Ortoghonal(t = trans, a_ratio = a) creates an orthogonal camera with trans Transformation and an a aspect ratio
source

A PCG object is used for pseudo-random number generation.

jujutracer.PCGType
mutable struct PCG

A Pseudo-Random Number Generator (PRNG) based on the PCG algorithm.

Fields

  • state::UInt64: The current state of the PCG. It is atomically wrapped to ensure thread safety.
  • inc::UInt64: The increment value used in the PCG algorithm.

Constructor

  • PCG(init_state::UInt64, init_seq::UInt64): Creates a new PCG instance with the given initial state and sequence. Defaults are 42 and 54 respectively.
source

Renderers

We have several renderers available, each with different complexity and quality.

jujutracer.OnOffType
OnOff(world::World)

OnOff renderer of the scene. Returns white if the ray hits something, balck otherwise

Fields

  • world::World the world containing the scene
  • background_color::RGB the color of the background when the ray doesn't hit anything
  • foreground_color::RGB the color of the foreground when the ray hits something

Functional Usage

OnOff(ray::Ray) functional renderer on a ray

source
jujutracer.FlatType
Flat(world::World)

Flat renderer of the scene. Returns the Emition pigment of the hitten shapes

Fields

  • world::World the world containing the scene

Functional Usage

Flat(ray::Ray) functional renderer on a ray

source
jujutracer.PathTracerType
PathTracer(world::World, backg::RGB, rnd::PCG, n_rays::Int64, depth::Int64, russian::Int64)

Path Tracer renderer of the scene.

Fields

  • world::World: the world containing the scene
  • backg::RGB: the background color when the ray doesn't intersect anything
  • rnd::PCG: the random number generator
  • n_rays::Int64: the number of rays fired from the hitten point
  • depth::Int64: the maximum depth to be reached by a ray
  • russian::Int64: number of iteration before playing with Russian Roulet

Functional Usage

PathTracer(ray::Ray) functional renderer on a ray

source
jujutracer.PointLightType
PointLight(world::World, background::RGB, ambient::RGB, max_depth::Int64)

PointLight renderer of the scene. Backtraces the light from the point light sources.

Fields

  • world::World: the world containing the scene. Must contain at least one light source
  • background_color::RGB: the color of the background when the ray doesn't hit anything
  • ambient_color::RGB: the ambient color of the scene
  • max_depth::Int64: the maximum depth of the ray tracing

Functional Usage

PointLight(ray::Ray) functional renderer on a ray

source

Rendering process

The rendering process is initiated by calling the ImageTracer on a hdrimage. Optionally, a PCG object can be passed to the ImageTracer for antialising and stratified sampling.

jujutracer.ImageTracerType
struct ImageTracer

Convenient struct to hold the hdrimage and the camera.

Fields

  • img::hdrimg: The HDR image to be traced.
  • camera::AbstractCamera: The camera used for tracing rays. Can be either Orthogonal or Perspective.

Constructor

  • ImageTracer(): Creates a new ImageTracer with a default HDR image and an orthogonal camera.
  • ImageTracer(img::hdrimg, camera::AbstractCamera): Creates a new ImageTracer with the specified HDR image and camera.

Functional Usage

  • ImageTracer(fun::Function): tracing the image with fun renderer.
  • ImageTracer(fun::Function, AA::Int64, pcg::PCG): tracing the image with fun renderer and Anti-Aliasing method with AA^2 subdivision in the pixel.
source

Saving

jujutracer provides functions to save the rendered image boht in HDR and LDR formats.

jujutracer.write_pfm_imageFunction
write_pfm_image(img::hdrimg, io, endianness::Bool=true)

Write a PFM file encodiding the content of an hdrimg

Arguments

  • img::hdrimg: The HDR image to be written to the PFM file.
  • io::IO: The output stream to which the PFM image will be written.
  • endianness::Bool: A boolean indicating whether to write the float values in little-endian format (default is true).
source
write_pfm_image(img::hdrimg, filename::String, endianness::Bool=true)

Write a PFM file encoding the content of an hdrimg.

Arguments

  • img::hdrimg: The HDR image to be written to the PFM file.
  • filename::String: The file path where the PFM image will be saved, including the ".pfm" extension.
  • endianness::Bool: A boolean indicating whether to write the float values in little-endian format (default is true).

Raises

  • InvalidFileFormat: If the file extension is not ".pfm".
source
jujutracer.tone_mappingFunction
tone_mapping(img::hdrimg; a=0.18, lum = nothing, γ = 1.0)

Apply tone mapping to the HDR image.

Arguments

  • img::hdrimg: The HDR image to be tone-mapped.
  • a::Float64: A positive value to be used in the normalization formula (default is 0.18).
  • lum::Float64: The average luminosity of the image. If not provided, it will be calculated using _average_luminosity.
  • γ::Float64: The gamma value to be used for correction. Must be a positive number (default is 1.0).

Returns

  • hdrimg: The tone-mapped HDR image.
source
jujutracer.get_matrixFunction
get_matrix(img::hdrimg)

Extract the matrix from an HDR image.

Arguments

  • img::hdrimg: The HDR image from which to extract the matrix.

Returns

  • Matrix: The matrix representation of the HDR image.
source
jujutracer.save_ldrimageFunction
save_ldrimage(img_matrix::Matrix, filename::String)

Save an LDR image to a file.

Arguments

  • img_matrix::Matrix: The matrix representation of the image to be saved.
  • filename::String: The name of the file to save the image to, including the file extension (".png" or ".jpg").

Returns

  • String: The path to the saved file.

Raises

  • ArgumentError: If the file extension is not valid or if the image values are not clamped.
source