TinyRaytracer 0.1
A simple C++ raytracer
Loading...
Searching...
No Matches
Macros | Functions
draw.cpp File Reference
#include "../include/all.hpp"
#include <cmath>
#include <algorithm>
#include <climits>
#include <random>
#include <omp.h>

Macros

#define EPSILON   0.001
 

Functions

void render (Image &img)
 The render function that loops over the screen of pixels. More...
 
RGBA shootPrimaryRay (double x, double y)
 Shoots a primary ray into the scene, at pixel location (x,y). More...
 
ObjectInfo hitNearest (Ray &ray)
 Gets the nearest object in the path of the ray. More...
 
ObjectInfo hitMiss ()
 Does nothing. More...
 
RGBA diffuseLight (const ObjectInfo &obj)
 Get the diffuse light color,with shadow checking. More...
 
RGBA reflectionLight (const Ray &ray, const ObjectInfo &obj)
 Get the reflection light color. More...
 
RGBA refractionLight (const Ray &ray, const ObjectInfo &obj)
 Get the refraction light color. More...
 
RGBA globalIllumination (const ObjectInfo &obj, int gi_bounce)
 Get the global illumination light color. More...
 
ObjectInfo checkPlane (Ray &ray, bool exit_early)
 Check if any planes are intersecting with the ray. More...
 
RGBA getColorSun (double lambert, RGB objColor, RGB lightColor)
 Get the Color of Sun (directional light) More...
 
RGBA getColorBulb (double lambert, RGB objColor, RGB lightColor, double t)
 Get the Color of Bulb (scene light) More...
 

Detailed Description

Author
Jin (jinj2.nosp@m.@ill.nosp@m.inois.nosp@m..edu)
Version
0.1
Date
2024-11-07

Function Documentation

◆ checkPlane()

ObjectInfo checkPlane ( Ray ray,
bool  exit_early 
)

Check if any planes are intersecting with the ray.

Parameters
rayThe ray to check against.
exit_earlyFor shadow checking purposes, exit early if a plane is in the way, casting shadows. Do not set to true with bulb(light in scene).
Returns
ObjectInfo The objectInfo instance which contains all intersection informations.

The parametric distance t is calculated. If t < 0, this means that the ray intersection is behind the ray origin, which means no intersection. Else, calculate the intersection point with t.

◆ diffuseLight()

RGBA diffuseLight ( const ObjectInfo obj)

Get the diffuse light color,with shadow checking.

Parameters
objThe objectInfo instance with the intersection information.
Returns
RGBA The RGBA linear color at the location.

The diffuse light color is checked by creating a shadow ray from the intersection point and the normal. This ray is checked against all light sources, and the actual color is found with the lambert light model. If a object totally obstructs a shadow ray, that ray does not contribute to the diffuse lighting.

◆ getColorBulb()

RGBA getColorBulb ( double  lambert,
RGB  objColor,
RGB  lightColor,
double  t 
)

Get the Color of Bulb (scene light)

Parameters
lambertThe lambert constant.
objColorThe color of the object.
lightColorThe color of the light.
Returns
RGB The linear RGB color, after blending.

This function applies the lambert constant to the light color and get the correct color by blending the colors of the light and the object, and applys light intensity falloff. Also takes care of exposure.`

◆ getColorSun()

RGBA getColorSun ( double  lambert,
RGB  objColor,
RGB  lightColor 
)

Get the Color of Sun (directional light)

Parameters
lambertThe lambert constant.
objColorThe color of the object.
lightColorThe color of the light.
Returns
RGB The linear RGB color, after blending.

This function applies the lambert constant to the light color and get the correct color by blending the colors of the light and the object. Also takes care of exposure.

◆ globalIllumination()

RGBA globalIllumination ( const ObjectInfo obj,
int  gi_bounce 
)

Get the global illumination light color.

Parameters
objThe objectInfo instance with the intersection information.
gi_bounceRemaining bounces for global illumination rays.
Returns
RGBA The RGBA linear color at the location.

A gi ray is created by selecting a new ray in the sphere at the intersection point. This gi ray, after intersection, can create more gi rays based on the remaining gi bounce.

Deprecated:
This version of global illumination is too slow and does not work that well. Will be replaced by something better later.

◆ hitMiss()

ObjectInfo hitMiss ( )

Does nothing.

Returns
ObjectInfo A default no-hit objectInfo.

◆ hitNearest()

ObjectInfo hitNearest ( Ray ray)

Gets the nearest object in the path of the ray.

Parameters
rayThe ray to trace.
Returns
ObjectInfo All the related info for the object, if any is hit.

◆ reflectionLight()

RGBA reflectionLight ( const Ray ray,
const ObjectInfo obj 
)

Get the reflection light color.

Parameters
rayThe ray which caused the reflection.
objThe objectInfo instance with the intersection information.
Returns
RGBA The RGBA linear color at the location.

The reflection ray is calculated and checked against all objects in the scene. If the reflection ray did hit an object, a full light calculation is performed to achieve the reflection lighting effect.

◆ refractionLight()

RGBA refractionLight ( const Ray ray,
const ObjectInfo obj 
)

Get the refraction light color.

Parameters
rayThe ray which caused the refraction.
objThe objectInfo instance with the intersection information.
Returns
RGBA The RGBA linear color at the location.

The refraction discriminant (k) is calculated and if k < 0, total internal refraction occur, which is treated as reflection. If not, the refraction ray is calculated, and checked against all objects again(this currently only works for spheres and could be optimized). When the ray hits the object again, the final refraction ray is calculated using the inverse of the ior ratio during ray entrance. After the ray exits, full light calculation is performed.

Note
Currently this function only handles air-object intersection. It also assumes that total internal refraction will not happen inside the object itself, and the sphere-intersection could be optimized when the ray is inside the object.

◆ render()

void render ( Image &  img)

The render function that loops over the screen of pixels.

Parameters
imgThe image to render on.

Loops over all pixels in the canvas and shoots a primary ray at that pixel. Shoot multiple rays if anti-alising is on.

◆ shootPrimaryRay()

RGBA shootPrimaryRay ( double  x,
double  y 
)

Shoots a primary ray into the scene, at pixel location (x,y).

Parameters
xx coordinate of the pixel.
yy coordinate of the pixel.
Returns
RGBA the final pixel color value, in linear RGB color space.

This function shoots one primary ray for the pixel (x,y). It will calculate the color for the pixel with the nearest object the primary ray hit, and the final color will be the blend of diffuse, reflect, refract and global illumination color. The latter three could create more rays that will bounce in the scene.