Ray Tracer
2020
|
A Scene to be ray traced. More...
#include <Scene.h>
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< Camera > | camera_ |
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... | |
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:
Scene::Scene | ( | ) |
Scene::~Scene | ( | ) |
Scene destructor.
|
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.
light | A std::shared_ptr to the new LightSource to add. |
|
inline |
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.
\[ 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.
ray | The Ray to intersect with the Objects in the Scene. |
rayDepth | The maximum number of reflection Rays that can be cast. |
bool Scene::hasCamera | ( | ) | const |
|
private |
void Scene::render | ( | ) | const |
|
inline |
Since there may be different types of Camera, a smart pointer is used to refer to the Camera. For example:
Note that a Scene has only one Camera, so calling setCamera() will replace any existing camera.
camera | The new Camera to add. |
std::string Scene::filename |
File to save the image to.
|
private |
Collection of LightSources in the Scene.
unsigned int Scene::maxRayDepth |
Maximum number of reflected Rays to trace.
unsigned int Scene::renderHeight |
Height in pixels of the image to render.
unsigned int Scene::renderWidth |
Width in pixels of the image to render.