Basic API usage

Using the API, you can interact directly with the renderer.

Image generation

Let's assume we want to render a sphere in the origin using a flat renderer.

First, import the module:

julia> using Raytracer

We need to create the scene. All the shapes of the image must be included in a World instance.

julia> world = World()Shape[]

Then, we need to create the shape and add it to world. Here we create a Sphere with radius 2 in the origin, with a DiffuseBRDF. The pigment of the BRDF is a CheckeredPigment with a RED and GREEN pattern.

julia> sphere = Sphere(
           transformation = scaling(2),
           material = Material(
               brdf = DiffuseBRDF(
                   pigment = CheckeredPigment{8}(
                       color_on = RED,
                       color_off = GREEN
                   )
               )
           )
       )Sphere
 ↳ transformation = Transformation(Float32[2.0 0.0 0.0 0.0; 0.0 2.0 0.0 0.0; 0.0 0.0 2.0 0.0; 0.0 0.0 0.0 1.0], Float32[0.5 0.0 0.0 0.0; 0.0 0.5 0.0 0.0; 0.0 0.0 0.5 0.0; 0.0 0.0 0.0 1.0])
 ↳ material       = Material(DiffuseBRDF(CheckeredPigment{8}((1.0 0.0 0.0), (0.0 1.0 0.0))), UniformPigment((0.0 0.0 0.0)))
julia> append!(world, [sphere])1-element Vector{Shape}: Sphere(Transformation(Float32[2.0 0.0 0.0 0.0; 0.0 2.0 0.0 0.0; 0.0 0.0 2.0 0.0; 0.0 0.0 0.0 1.0], Float32[0.5 0.0 0.0 0.0; 0.0 0.5 0.0 0.0; 0.0 0.0 0.5 0.0; 0.0 0.0 0.0 1.0]), Material(DiffuseBRDF(CheckeredPigment{8}((1.0 0.0 0.0), (0.0 1.0 0.0))), UniformPigment((0.0 0.0 0.0))))

Now we can choose the renderer. For this example, we use a FlatRenderer.

julia> renderer = FlatRenderer(world)FlatRenderer(Shape[Sphere(Transformation(Float32[2.0 0.0 0.0 0.0; 0.0 2.0 0.0 0.0; 0.0 0.0 2.0 0.0; 0.0 0.0 0.0 1.0], Float32[0.5 0.0 0.0 0.0; 0.0 0.5 0.0 0.0; 0.0 0.0 0.5 0.0; 0.0 0.0 0.0 1.0]), Material(DiffuseBRDF(CheckeredPigment{8}((1.0 0.0 0.0), (0.0 1.0 0.0))), UniformPigment((0.0 0.0 0.0))))], (0.0 0.0 0.0))

Now we need to create a Camera, representing an observer. For this example, we use a PerspectiveCamera positioned at the point $(-10,0,0)$ and looking the origin.

julia> camera = PerspectiveCamera(
           aspect_ratio = 16//9,
           screen_distance = 3,
           transformation = translation(-10,0,0)
       )PerspectiveCamera(1.7777778f0, Transformation(Float32[1.0 0.0 0.0 -10.0; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0], Float32[1.0 0.0 0.0 10.0; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0]), 3.0f0)

Now we can create the ImageTracer, with the observer informations, an empty FHD (i.e. with resolution of 1920x1080) HdrImage and no antialiasing:

julia> image = HdrImage(1920, 1080)1920x1080 HdrImage:
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)  …  (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)  …  (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 ⋮                                            ⋱
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)  …  (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
 (0.0 0.0 0.0)  (0.0 0.0 0.0)  (0.0 0.0 0.0)     (0.0 0.0 0.0)  (0.0 0.0 0.0)
julia> image_tracer = ImageTracer(image, camera)ImageTracer with camera of type PerspectiveCamera image of size 1920x1080 and of type HdrImage

Now we can render the image, using the function fire_all_rays!:

julia> fire_all_rays!(image_tracer, renderer)
Progress: 100%|███████████████████████████████████████| Time: 0:00:03

Finally, we can save the generated HDR image. Remember to use permutedims when saving the image, otherwise you will have an output image reflected along the diagonal!

julia> save("myimage.pfm", permutedims(image_tracer.image.pixel_matrix))
24883218

Tone mapping

We can apply tone mapping to an HDR image to get a LDR image, for example a jpeg or png file.

julia> tonemapping("myimage.pfm", "myimage.jpg", 0.5f0, 1f0)

-> TONE MAPPING PROCESS
Loading input file 'myimage.pfm'... done!
Applying tone mapping...  done!
Saving final image to 'myimage.jpg'... done!

The final image is: