|
Ray Tracer
2020
|
Simple parser for Scene description files. More...
#include <SceneReader.h>
Public Member Functions | |
| SceneReader (Scene *scene) | |
| SceneReader constructor. More... | |
| ~SceneReader () | |
| SceneReader destructor. More... | |
| void | read (const std::string &filename) |
| Read Scene data from a file. More... | |
Private Member Functions | |
| void | parseTokenBlock (std::queue< std::string > &tokenBlock) |
| Parse a block of tokens. More... | |
| Colour | parseColour (std::queue< std::string > &tokenBlock) |
| Read Colour information from a block of tokens. More... | |
| double | parseNumber (std::queue< std::string > &tokenBlock) |
| Read a number information from a block of tokens. More... | |
| void | parseSceneBlock (std::queue< std::string > &tokenBlock) |
| Parse a block of tokens representing a Scene. More... | |
| void | parseCameraBlock (std::queue< std::string > &tokenBlock) |
| Parse a block of tokens representing a Scene. More... | |
| void | parseLightBlock (std::queue< std::string > &tokenBlock) |
| Parse a block of tokens representing a Camera. More... | |
| std::shared_ptr< Object > | parseObjectBlock (std::queue< std::string > &tokenBlock) |
| Parse a block of tokens representing a LightSource. More... | |
| void | parseMaterialBlock (std::queue< std::string > &tokenBlock) |
| Parse a block of tokens representing a Material. More... | |
Private Member Functions inherited from NonCopyable | |
| NonCopyable () | |
| NonCopyable default constructor. More... | |
| ~NonCopyable () | |
| NonCopyable destructor. More... | |
Private Attributes | |
| Scene * | scene_ |
| The Scene which information is read to. More... | |
| int | startLine_ |
| The first line of the current block being parsed, for error reporting. More... | |
| std::map< std::string, Material > | materials_ |
| A dictionary of Material types that have been read, and which can be used for subsequent Object properties. More... | |
Simple parser for Scene description files.
This class implements a simple parser for Scene description files. A SceneReader is linked to a Scene object, and can then read one or more text files describing Scene properties, Cameras, LightSources, and Objects. These files are defined in blocks, and are case-insensitive. Whitespace is contracted, so new lines, spaces, and tabs are all just token separators. Comments are introduced with #, and continue to the end of the line.
There are five main types of block:
Scene Blocks
Example:
Scene renderSize 800 600 backgroundColour 0.5 0.5 0.5 filename output.png rayDepth 5 End
Allowed elements within a Scene block are:
renderSize [width] [height]: Set the Scene's renderWidth and renderHeight properties to the given values.backgroundColour [red] [green] [blue]: Set the Scene's backgroundColour property to the given Colour.filename [file]: Set the Scene's filename property to the given value.rayDepth [number]: Set the Scene's rayDepth property to the given value.Camera Blocks
Example:
Camera PinholeCamera 1.5 Rotate X -15 Translate 0 -8 -15 End
A Camera block starts with a line giving the type of Camera and a parameter for that Camera if one can be provided. In the example, a PinholeCamera is created with a focalLength of 1.5 Allowed elements within a Scene block are:
Rotate [axis] [angle]: Apply a rotation of angle degrees about the specified axis (one of X, Y, or Z) to the Camera.Translate [x] [y] [z]: Apply the specified translation to the Camera.Scale [s]: Apply a uniform scaling by the given value to the Camera.Scale3 [sx] [sy] [sz]: Apply different scaling factors along the three axes to the Camera.Light Blocks
Examples:
Light PointLight Location 1 2 3 Colour 20 30 40 End Light SpotLight Location 0 -5 0 Colour 10 10 10 Direction 0 1 0 Angle 30 End
A Light block starts with a line giving the type of Light.
Allowed elements within a Light block are:
Colour [red] [green] [blue]: Set the Light's colour property to the given Colour.Location [x] [y] [z]: Set the Light's location property to the given co-ordinates.Direction [x] [y] [z]: Set the Light's location property to the given co-ordinates.Angle [x]Not all light source types use all parameters, and any surplus elements are ignored:
AmbientLights use Colour onlyPointLights use Colour and LocationDirectionalLights use Colour and DirectionSpotLights use Colour, Location, Direction, and AngleMaterial Blocks
Example:
Material Gold Colour 1 0.88 0.25 Specular 1 1 1 100 Mirror 1 0.88 0.25 End
A Material block starts with a line giving the name of a Material (in this example, 'Gold'). Allowed elements within a Material block are:
Ambient [red] [green] [blue]: Set the Material's ambientColour property to the given Colour.Diffuse [red] [green] [blue]: Set the Material's diffuseColour property to the given Colour.Colour [red] [green] [blue]: Set the Material's ambientColour and diffuseColour properties to the given Colour.Specular [red] [green] [blue] [exponent]: Set the Material's specularColour property to the given Colour, and its specularExponent to the given value.Mirror [red] [green] [blue]: Set the Material's diffuseColour property to the given Colour.Object Blocks
Example:
Object Sphere Material Gold Specular 0.7 0.7 0.7 75 Scale3 1 2 3 Rotate Y 45 Translate 0 0 3
An Object block starts with a line giving the type of Object to create (a Sphere in this case). Allowed elements within an Object block are:
Rotate [axis] [angle]: Apply a rotation of angle degrees about the specified axis (one of X, Y, or Z) to the Object.Translate [x] [y] [z]: Apply the specified translation to the Object.Scale [s]: Apply a uniform scaling by the given value to the Object.Scale3 [sx] [sy] [sz]: Apply different scaling factors along the three axes to the Object.Material [name]: Copy the properties of a previously defined Material to the Object.Ambient [red] [green] [blue]: Set the ambientColour property of the Object's Material to the given Colour.Diffuse [red] [green] [blue]: Set the diffuseColour property of the Object's Material to the given Colour.Colour [red] [green] [blue]: Set the ambientColour and diffuseColour properties of the Object's Material to the given Colour.Specular [red] [green] [blue] [exponent]: Set thespecularColour property to the given Colour, and its specularExponent to the given value.Mirror [red] [green] [blue]: Set the diffuseColour property of the Object's Material to the given Colour. Object types that can be read are: Cube, Cylinder, Octahedron, Plane, and Sphere | SceneReader::SceneReader | ( | Scene * | scene | ) |
SceneReader constructor.
| scene | A pointer to the Scene which data will be read into. |
| SceneReader::~SceneReader | ( | ) |
SceneReader destructor.
|
private |
|
private |
Read Colour information from a block of tokens.
This tries to interpret the next three items in a block of tokens as a Colour. Three tokens are taken from the block and interpreted as red, green, and blue values for the Colour.
If there is a problem in this process, the program is terminated.
| tokenBlock | A sequence of tokens to read the Colour from. |
|
private |
|
private |
|
private |
Read a number information from a block of tokens.
This tries to interpret the next item in a block of tokens as a number. The token is removed from the block.
If there is a problem in this process, the program is terminated.
| tokenBlock | A sequence of tokens to read the Colour from. |
|
private |
Parse a block of tokens representing a LightSource.
This method reads LightSource information from a block of tokens. The format for LightSource blocks is described above, and any errors in parsing the block will terminate the program.
| tokenBlock | A sequence of tokens to be interpreted. |
|
private |
|
private |
Parse a block of tokens.
When reading a file, it is separated into a squence of tokens (words and numbers) separated by whitespace. These are formed into blocks, each ending with the token 'End'. This method determines how to interpret each block.
| tokenBlock | A sequence of tokens to be interpreted. |
| void SceneReader::read | ( | const std::string & | filename | ) |
Read Scene data from a file.
This method reads data from a file in the format discussed above. It adds information to the Scene linked to this SceneReader, and so multiple files can be combined into one Scene.
If an error is encountered parsing the file, the program is terminated.
| filename | The name of the file to read. |
|
private |
|
private |
The first line of the current block being parsed, for error reporting.