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 ofFunction
that takeWorld
and optionally aPCG
object, instructing on how a ray should be traced through the scene.ImageTracer
is a function that takes thehdrimg
,AbstractCamera
, andrenderer
(and optionally aPCG
) 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.hdrimg
— Typestruct 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.
The we can set the camera. We have two types of cameras: Perspective
and Orthogonal
, both subtypes of AbstractCamera
.
jujutracer.Perspective
— TypePerspective
Perspective camera type
Fields
t::Transformation
the transformation related to the camera's standinga_ratio::Float64
aspect ratio of the screen of the camera
Constructor
Perspective()
creates a perspective camera with IdentityTransformation
, a 16:9 aspect ratio placed in (-1, 0, 0)Perspective(d = dist, t = trans, a_ratio = a)
creates a perspective camera with transTransformation
, ana
aspect ratio placed in (-d, 0, 0)
jujutracer.Orthogonal
— TypeOrthogonal
Orthogonal camera type
Fields
t::Transformation
the transformation related to the camera's standinga_ratio::Float64
aspect ratio of the screen of the camera
Constructor
Orthogonal()
creates an orthogonal camera with IdentityTransformation
and a 16:9 aspect ratioOrtoghonal(t = trans, a_ratio = a)
creates an orthogonal camera with transTransformation
and ana
aspect ratio
A PCG
object is used for pseudo-random number generation.
jujutracer.PCG
— Typemutable 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 are42
and54
respectively.
Renderers
We have several renderers available, each with different complexity and quality.
jujutracer.OnOff
— TypeOnOff(world::World)
OnOff renderer of the scene. Returns white if the ray hits something, balck otherwise
Fields
world::World
the world containing the scenebackground_color::RGB
the color of the background when the ray doesn't hit anythingforeground_color::RGB
the color of the foreground when the ray hits something
Functional Usage
OnOff(ray::Ray)
functional renderer on a ray
jujutracer.Flat
— TypeFlat(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
jujutracer.PathTracer
— TypePathTracer(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 scenebackg::RGB
: the background color when the ray doesn't intersect anythingrnd::PCG
: the random number generatorn_rays::Int64
: the number of rays fired from the hitten pointdepth::Int64
: the maximum depth to be reached by a rayrussian::Int64
: number of iteration before playing with Russian Roulet
Functional Usage
PathTracer(ray::Ray)
functional renderer on a ray
jujutracer.PointLight
— TypePointLight(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 sourcebackground_color::RGB
: the color of the background when the ray doesn't hit anythingambient_color::RGB
: the ambient color of the scenemax_depth::Int64
: the maximum depth of the ray tracing
Functional Usage
PointLight(ray::Ray)
functional renderer on a ray
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.ImageTracer
— Typestruct 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 eitherOrthogonal
orPerspective
.
Constructor
ImageTracer()
: Creates a newImageTracer
with a default HDR image and an orthogonal camera.ImageTracer(img::hdrimg, camera::AbstractCamera)
: Creates a newImageTracer
with the specified HDR image and camera.
Functional Usage
ImageTracer(fun::Function)
: tracing the image withfun
renderer.ImageTracer(fun::Function, AA::Int64, pcg::PCG)
: tracing the image withfun
renderer and Anti-Aliasing method with AA^2 subdivision in the pixel.
Saving
jujutracer provides functions to save the rendered image boht in HDR and LDR formats.
jujutracer.write_pfm_image
— Functionwrite_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).
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".
jujutracer.tone_mapping
— Functiontone_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.
jujutracer.get_matrix
— Functionget_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.
jujutracer.save_ldrimage
— Functionsave_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.