Ray Tracer  2020
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
Scene Class Reference

A Scene to be ray traced. More...

#include <Scene.h>

Inheritance diagram for Scene:
NonCopyable

Public Member Functions

 Scene ()
 Default Scene constructor. This creates an empty Scene, with a black background and no ambient light. By default the images are rendered at 800x600 pixel resolution, saved to render.png, and allow for up to 3 reflected rays. More...
 
 ~Scene ()
 Scene destructor. More...
 
void setCamera (std::shared_ptr< Camera > camera)
 Set the Scene's Camera. More...
 
void addObject (std::shared_ptr< Object > object)
 Add a new Object. More...
 
void addLight (std::shared_ptr< LightSource > light)
 Add a new LightSource. More...
 
void render () const
 Render an image of the Scene. More...
 
bool hasCamera () const
 Check if the Scene has a Camera. More...
 

Public Attributes

Colour backgroundColour
 Colour for any Ray that does not hit an Object. More...
 
Colour ambientLight
 Ambient light level and Colour in the Scene. More...
 
unsigned int maxRayDepth
 Maximum number of reflected Rays to trace. More...
 
unsigned int renderWidth
 Width in pixels of the image to render. More...
 
unsigned int renderHeight
 Height in pixels of the image to render. More...
 
std::string filename
 File to save the image to. More...
 

Private Member Functions

RayIntersection intersect (const Ray &ray) const
 Intersect a Ray with the Objects in a Scene. More...
 
Colour computeColour (const Ray &ray, unsigned int rayDepth=0) const
 Compute the Colour seen by a Ray in the Scene. More...
 
- Private Member Functions inherited from NonCopyable
 NonCopyable ()
 NonCopyable default constructor. More...
 
 ~NonCopyable ()
 NonCopyable destructor. More...
 

Private Attributes

std::shared_ptr< Cameracamera_
 Camera to render the image with. More...
 
std::vector< std::shared_ptr< Object > > objects_
 Collection of Objects in the Scene. More...
 
std::vector< std::shared_ptr< LightSource > > lights_
 Collection of LightSources in the Scene. More...
 

Detailed Description

A Scene to be ray traced.

A Scene collects Object, LightSource, and Camera data together for rendering. It also includes basic information about the environment (backgroundColour, ambientLight), the image to be produced (renderWidth, renderHeight, filename), and the maximum number of reflections to allow (maxRayDepth).

Internally it provides the methods required to render an image, such as intersecting a Ray with the Objects in the Scene, and computing the Colour from a Ray.

Scene makes use of std::shared_ptr, which is a 'smart pointer' available in C++ since the 2011 revision. These provide a safer alternative to raw pointers, since deallocation is handled automatically. You can treat them mostly like normal pointers though, so if you have a shared pointer to a Sphere, you can get it's properties like this:

// sphere_ptr is a std::shared_ptr<Sphere>
sphere_ptr->transform.rotateX(30);

Constructor & Destructor Documentation

◆ Scene()

Scene::Scene ( )

Default Scene constructor. This creates an empty Scene, with a black background and no ambient light. By default the images are rendered at 800x600 pixel resolution, saved to render.png, and allow for up to 3 reflected rays.

◆ ~Scene()

Scene::~Scene ( )

Scene destructor.

Member Function Documentation

◆ addLight()

void Scene::addLight ( std::shared_ptr< LightSource light)
inline

Add a new LightSource.

Note that the Scene has a collection of LightSources, and there is no way to remove an LightSource once added.

Parameters
lightA std::shared_ptr to the new LightSource to add.

◆ addObject()

void Scene::addObject ( std::shared_ptr< Object object)
inline

Add a new Object.

Note that the Scene has a collection of Objects, and there is no way to remove an Object once added.

Parameters
objectA std::shared_ptr to the new Object to add.

◆ computeColour()

Colour Scene::computeColour ( const Ray ray,
unsigned int  rayDepth = 0 
) const
private

Compute the Colour seen by a Ray in the Scene.

The Colour seen by a Ray depends on the ligthing, the first Object that it hits, and the Material properties of that Object. This method performs these computations and comptues the observed Colour. For some Objects it may be necessary to cast other Rays to deal with reflections. This can conceivably recurse forever, so a maximum number of reflections is set.

If the Ray does not hit any Object, then the Scene's backgroundColour should be returned.

Todo:
Currently this method only deals with ambient lighting and Material properties. It needs to be extended to account for the diffuse, specular, and mirror properties of the Material of the Object that is hit. Recall from lectures that the formula for a combination of ambient, diffuse, and specular lighting is

\[ I = I_ak_a + \sum_j{I_j\left( k_d(\hat{\mathbf{\ell}}_j\cdot\hat{\mathbf{n}}) + k_s(\hat{\mathbf{e}}\cdot\hat{\mathbf{r}}_j)^n \right)},\]

where the sum is over the LightSources in the Scene. You will also need to cast additional Rays to check for shadows and for reflections. The number of recursions made for reflections should be limited by the rayDepth parameter passed to this method.

Parameters
rayThe Ray to intersect with the Objects in the Scene.
rayDepthThe maximum number of reflection Rays that can be cast.
Returns
The Colour observed by the viewRay.

◆ hasCamera()

bool Scene::hasCamera ( ) const

Check if the Scene has a Camera.

To render a scene, a Camera is required. It is possible (although possibly not interesting) to render a scene with no Object or LightSource. This method checks to see if there is a Camera.

Returns
true if the Scene has a Camera, false otherwise.

◆ intersect()

RayIntersection Scene::intersect ( const Ray ray) const
private

Intersect a Ray with the Objects in a Scene.

This intersects the Ray with all of the Objects in the Scene and returns the first hit. If there is no hit, then a RayIntersection with infinite distance is returned.

Parameters
rayThe Ray to intersect with the Objects.
Returns
The first intersection of the Ray with the Scene.

◆ render()

void Scene::render ( ) const

Render an image of the Scene.

This method renders an image of the Scene. The size of the image is (renderWidth x renderHeight), and it is saved to a file specified by the Scene's filename property. The format of the file is determined by its extension.

Attempts to render a Scene with no Camera will end badly.

◆ setCamera()

void Scene::setCamera ( std::shared_ptr< Camera camera)
inline

Set the Scene's Camera.

Since there may be different types of Camera, a smart pointer is used to refer to the Camera. For example:

Scene scene;
std::shared_ptr<PinholeCamera> camera(new PinholeCamera(1));
scene.setCamera(camera);
PinholeCamera class.
Definition: PinholeCamera.h:21
A Scene to be ray traced.
Definition: Scene.h:44
void setCamera(std::shared_ptr< Camera > camera)
Set the Scene's Camera.
Definition: Scene.h:73

Note that a Scene has only one Camera, so calling setCamera() will replace any existing camera.

Parameters
cameraThe new Camera to add.

Member Data Documentation

◆ ambientLight

Colour Scene::ambientLight

Ambient light level and Colour in the Scene.

◆ backgroundColour

Colour Scene::backgroundColour

Colour for any Ray that does not hit an Object.

◆ camera_

std::shared_ptr<Camera> Scene::camera_
private

Camera to render the image with.

◆ filename

std::string Scene::filename

File to save the image to.

◆ lights_

std::vector<std::shared_ptr<LightSource> > Scene::lights_
private

Collection of LightSources in the Scene.

◆ maxRayDepth

unsigned int Scene::maxRayDepth

Maximum number of reflected Rays to trace.

◆ objects_

std::vector<std::shared_ptr<Object> > Scene::objects_
private

Collection of Objects in the Scene.

◆ renderHeight

unsigned int Scene::renderHeight

Height in pixels of the image to render.

◆ renderWidth

unsigned int Scene::renderWidth

Width in pixels of the image to render.


The documentation for this class was generated from the following files: