COMP30019 – Project 1: Ray Tracer (Solution)

$ 24.99
Category:

Description

This project is individual work (30 marks).
Assignment Brief
There are various approaches to modelling how light interacts with surfaces in a scene. Almost always, the choice of approach comes down to a trade-off between computational complexity and realism. A ray tracing based approach can produce very realistic images, however this comes with a significant computational cost that generally makes it unsuitable for real-time rendering. Even if there are no real-time rendering requirements, we still have to approximate and optimise the ray tracing process, since simulating all rays in a scene is computationally intractable.
Template code
Stage 1 – Basic ray tracer (9 marks)
You will first implement the basic functionality of a ray tracer. At its core, ray tracing is an application of geometry and basic linear algebra (vector maths will become your bread and butter!). For example, a ray of light can be modelled by two three-dimensional vectors: a starting position and direction. Surfaces, light sources, and other entities in the environment can also be defined using vectors. Using geometry, it is possible to calculate how a ray reflects off a surface, or perhaps even refracts through it. Ultimately we are interested in simulating rays of light propagating throughout the environment, interacting with various surfaces, before finally reaching the viewer as pixels on their screen. If we are clever in utilising ‘real-life’ physical models for these interactions, we can generate incredibly realistic scenes.
In this first stage you will implement some basic vector functionality, and figure out how to shoot a ray for each pixel in a rendered image. We won’t yet be worrying about materials, lighting, shading, etc. Such fancy stuff will come later in the assignment.
Stage 1.1 – Familiarise yourself with the template
Before writing any code, try to understand how the template provided to you works. We have already taken care of quite a few details for you, such as input and output handling. A sample input scene is provided to you in a text file (tests/sample scene 1.txt), and a parser for this file has been written so you can access objects and resources directly within the Scene class (src/scene/Scene.cs). The core ray tracing logic (which you will write) should be implemented inside the Render() method in Scene.cs. This method takes an Image object for which you can set the individual colour of each pixel, as well as derive properties such as its width and height. When the program is run, this image will automatically be outputted as a PNG image file.
Try running the project so that you can see this in action. Open up the terminal in Visual Studio Code (or your preferred environment), and run :
dotnet run — -f tests/sample_scene_1.txt -o output.png
Although this looks like a bit of a mouthful at first, all it is doing is running the project with two command line arguments: an input text file (-f) and an output image file (-o). The input file will be read and parsed, and the output image written accordingly. Open the generated output file, and you will notice the entire image is black, since no ray tracing has been implemented yet. Before continuing, test your understanding by modifying the project code to output the image entirely in white instead.
Hint: Try using some loops inside the Render() method. The Image class has Width and Height properties which should be handy for determining the loop bounds. These properties are already determined by the command line arguments -w and -h, if specified.
Now take a look at the main Program.cs file. In the OptionsConf class, you can see all of the potential command line arguments and their default values (these are the values used if that argument is not specified at runtime – e.g., not entered on the command line). Don’t change these default values, instead, pass values using the appropriate flags on the command line, if you want to change parameters. At this point it’s worth stressing that you should not modify the Program.cs file at all. Doing so risks our automated test suites breaking when running your project during marking (see the ‘Submission‘ section for details).
Stage 1.2 – Implement vector mathematics
We have provided you with a C# struct template for representing a three-dimensional vector (src/math/Vector3.cs). Write code to complete the missing operations which are currently empty methods. Note that for convenience we have overloaded operators such as +, *, /. This is a handy language feature that allows us to perform vector arithmetic concisely:
Vector3 a = new Vector3(0, 1, 0);
Vector3 b = new Vector3(1, 1, 0);
Vector3 c = a + b; // We overloaded ‘+’ so c = (1, 2, 0)
As well as basic arithmetic, you will also need to complete functions to compute the dot product and cross product (at least). The dot product will tell you how much two vectors point in the same direction, and the cross product of two vectors will give you the vector which is perpendicular to both of them (e.g., crossing the x- and y-axes would give you the z-axis).
It is strongly recommended that you test your implementations here thoroughly. Vectors are utilised everywhere in this project, so a mistake here can lead to a major headache down the line.
Stage 1.3 – Fire a ray for each pixel
We have already provided you with a ‘ray’ structure (src/math/Ray.cs). Notice that it is simply a position (origin) and a direction, both represented as vectors. While it is possible to trace rays forwards from light sources in the scene, it is far more efficient to trace rays backwards from the camera. This is because most rays in the scene will never be seen by the viewer, and computing these would be a waste of resources.
Inside the Render() method, write code to iterate through each pixel and construct a corresponding ray that fires into the world. The biggest challenge here will be converting from a two-dimensional pixel coordinate to a three-dimensional ray. You might find it useful to consult external resources about the maths here.
In this project we want you to use a left-handed coordinate system. The camera should be situated at the origin of the scene – (0, 0, 0) – looking forward along the positive z-axis (‘into the screen’), with the positive x-axis pointing ‘right’, and the positive y-axis pointing ‘up’. You should ensure that there is a horizontal field-of-view (FOV) of 60◦. As a sanity check, a ray at the very center of the rendered image should point in the direction (0, 0, 1). Rays at the corners of the image should have directions (±i,±j,k), where i = j if the image is square. You should ensure that your solution works when different output image widths/heights are specified. For non-square images the vertical FOV should vary to maintain the correct aspect ratio.
Stage 1.4 – Calculate ray–entity intersections
In this project a scene can contain three types of primitive entities – planes, triangles and spheres. If you haven’t already, open the template classes provided in the src/primitives folder:
• Plane.cs – Represented by a point (center), and a vector representing the direction it faces (normal – i.e., perpendicular to the actual surface of the plane). Note this defines an ‘infinite’ plane.
• Triangle.cs – Represented by three points (v0, v1, v2). A clockwise winding order defines the front face of the triangle.
• Sphere.cs – Represented by a point (center) and a radius.
Stage 1.5 – Output primitives as solid colours
You are finally ready to generate some graphical output! Earlier you computed a ray for each pixel in the image. Extend this code to check for intersections with primitives in the scene. You will need to make further additions to the Render() method. For each ray, iterate through every entity in the scene and check whether there is an intersection between the ray and the entity. If so, you should set the corresponding pixel colour to the colour of the object. Ensure you correctly handle cases where there is more than one entity that coincides with a ray.
In case your object-oriented programming is rusty, here is a template for looping through all primitives/entities in the scene and checking if ray intersects with them: foreach (SceneEntity entity in this.entities)
{
RayHit hit = entity.Intersect(ray); if (hit != null)
{
// We got a hit with this entity!
// The colour of the entity is entity.Material.Color }
}
Note that entity is an interface, so we don’t know exactly which type of primitive it is (plane, triangle or sphere), but that does not matter since we are only interested in the intersection itself.
We have provided you with sample outputs in the images folder, so you have an indication of how your output should look for stages 1 and 2 respectively.
Stage 2 – Lighting and materials (9 marks)
In this stage you will extend the ray tracer to handle lighting, and model different types of materials. Some materials are more trivial to compute than others, and this complexity ultimately boils down to how light rays interact with them.
Stage 2.1 – Diffuse materials
We will first consider the case where a ray coincides with a diffuse surface which is directly illuminated by a light source. When light hits an ‘ideal’ diffuse material, it scatters uniformly in all directions. This means it is viewer-independent, and the intensity only varies depending on the angle of incidence between the light source and the surface. Diffuse lighting is so trivial to compute that it is regularly used in real-time rendering techniques (not just ray tracing).
In this stage you need to extend the ray tracer to handle materials with the Diffuse type. Objects should be smoothly lit when this is implemented correctly. As a starting point, take note of where you set the colour of a pixel currently. Instead of outputting the material colour directly, you should compute it based on the following function:
C = (Nˆ · Lˆ)CmCl
…where Nˆ is the normal of the surface at the hit point, Lˆ is the direction to the light source from the hit point, Cm is the material colour, Cl is the light colour and C is the resultant output colour. Note that all light sources are available in the Scene class, so you will likely have to iterate through these. You should sum the outputs of multiple light sources into the final pixel colour (if there is more than one).
Stage 2.2 – Shadow rays
Be careful when firing a ray away from the surface of an object. Numerical error could lead to a ‘premature’ hit with that same object! One solution is to offset the origin of the ray slightly away from the surface.
Stage 2.3 – Reflective materials
Stage 2.4 – Refractive materials
Stage 2.5 – The Fresnel effect
In the real world, refraction does not really occur in total isolation from reflection. When light hits a refractive surface, some proportion of it is reflected, while another proportion is refracted (these proportions sum to 1 since energy is conserved). This proportion is not uniform for all rays which hit the surface. As a ray’s angle of incidence decreases, there is greater reflection versus refraction. If you look at a sheet of glass from front on, and you will see that most of the light is refracted (transmits through). However, if you look at it almost side-on, it looks a lot more reflective!
This phenomenon is known as the Fresnel effect. Your next task is to improve refractive materials so that reflection is mixed into the corresponding lighting calculations according to the Fresnel equations. Note that this means that two rays need to be traced for every one ray that coincides with a refractive material. If this process repeats itself multiple times, the computational burden increases exponentially, so keep this in mind when coding your solution.
Stage 2.6 – Anti-aliasing
Modify your ray tracer to incorporate optional anti-aliasing during rendering. You should do this by firing more rays per pixel and then averaging the outputs for the final colour. There is another command-line argument accessible within your program which specifies the anti-aliasing multiplier you should use when rendering the scene. Here is an example:
dotnet run — -f tests/sample_scene.txt -o output.png -x 2
The argument -x specifies this multiplier, which in this example is 2. This means you should fire twice as many rays both horizontally and vertically (4x rays per pixel). If the multiplier is 3, then you should fire three times as many rays in both directions (9x rays per pixel). And so on. Note that we have already parsed this command-line argument for you! It is accessible within the Scene class as options.AAMultiplier, so you don’t need to worry about how to read it into your program.
Stage 3 – Advanced add-ons (9 marks)
In this stage you are given the opportunity to implement some advanced add-on effects of your choosing. Some are more trivial to implement than others, and the allocated marks reflect their approximate difficulty and/or time commitment. In completing these questions to a high standard, we expect that you research various approaches, and make informed decisions to maximise the outcomes of the intended effects. You should write some detailed comments in your README.md file which describe the approach you have taken. It is not possible to receive more than the allocated marks for this section (9 marks maximum), so if you complete more add-ons than required, clearly state which ones you want to contribute to your mark!
N.B.: Regardless of whether you complete this stage or not, you are still encouraged to show off your work by submitting a custom scene (see below).
Render quality
You do not have to utilise the -q parameter. It’s there purely so you have the opportunity to show what your ray tracer is capable of without having to be ultra-conservative with respect to time. It only applies to select stage 3 add-ons, and won’t be used elsewhere during testing. It is up to you to determine if it is relevant to the problem you are solving.
Stage 3, Option A – Emissive materials (+6)
Up until this point we considered lights to be infinitely small points. This is an approximation, and not how lighting typically works in the real world. Consider a fluorescent bulb which is a long cylindrical shape. It would be inappropriate to model this using a singular point. Even a standard light globe, which comes close to being modelled by a ‘point’, is still a physical object with a surface area that emits light. We call such surfaces emissive.
Stage 3, Option B – Ambient lighting/occlusion (+6)
Extend the ray tracer so that it is possible to optionally compute ambient lighting in a rendered image. You should utilise the -l command line flag which is available as options.AmbientLightingEnabled in the Scene class. In other words, if and only if -l is passed to your program, ambient lighting should be enabled.
Stage 3, Option C – OBJ models (+6)
We have provided you with a template class in the src/extensions folder called ObjModel.cs. In the same manner as the sphere, plane and triangle primitives defined earlier, ObjModel has an Intersect() method that needs to be implemented. You should read/parse the .obj file in the constructor, and use this data when computing intersections.
We have provided you with a sample scene (tests/sample scene obj.txt) that contains an OBJ, so you should examine this file first to see exactly how OBJ models are defined. In particular, observe there is a string that is the path to the .obj file itself. Also notice that we specify an offset vector and scale as parameters when loading a model. These parameters allow us to adjust the size and position of the model to fit the scene appropriately. The scale should be applied first, then the offset, as these are order dependent operations.
Stage 3, Option D – Glossy materials (+3)
At the moment we can model a few common materials, but there are still many material types which cannot be adequately simulated. Add support for ‘glossy’ materials to your ray tracer. The MaterialType enum has a Glossy type, which you should add functionality for. Unlike refractive materials, no light should transmit through the object. However, the object should not be completely reflective either. Instead, the object should be coloured based on the material’s Color property, but also appear to reflect some light. Exactly how you achieve this effect is up to you, but you should aim to make it convincing. If there are parameters that need to be tuned to determine the amount of ‘glossiness’, choose suitable defaults that demonstrate the effect.
Stage 3, Option E – Custom camera orientation (+3)
Currently the camera is assumed to be at the origin (0, 0, 0) looking along the z-axis (with the y-axis oriented ‘up’). Allow the user to apply a custom camera position and orientation to the camera based on the following parameters: a position vector (x, y, z), an axis of rotation vector (x, y, z) and an angle (in degrees). Note that this is an axis of rotation, not the ‘direction’ or local z-axis of the camera. The axis and angle are specified via the following command-line parameters where # denotes a floating point number:
• –cam-pos #,#,# – Available as options.CameraPosition
• –cam-axis #,#,# – Available as options.CameraAxis
• –cam-angle # – Available as options.CameraAngle (in degrees)
These parameters are automatically parsed for you by the template code, and so all you need to do is utilise the respective variables within the Scene class (e.g., as options.CameraPosition).
Stage 3, Option F – Beer’s law (+3)
Modify the ray tracer to obey Beer’s law for Refractive materials. Currently the colour of refractive materials has no effect in lighting calculations, but now it should be utilised. You should interpret this field to contain the absorbance of each colour channel. For example the RGB value of (4, 1, 1) means that red light will be absorbed at a faster rate than blue and green light.
Stage 3, Option G – Depth of field (+3)
Extend your ray tracer to account for two additional parameters: a focal length, and an aperture radius. These will be passed via the following command-line parameters where # denotes a floating point number:
• –aperture-radius # – Available as options.ApertureRadius
• –focal-length # – Available as options.FocalLength
We have already parsed these parameters for you and you can access them in the
Scene class (e.g. as options.FocalLength). The aperture radius parameter has a default value of 0, which reflects the idealised pinhole model we have used up until this point. However, if this value is larger than 0, then a circular pinhole should be simulated with the corresponding radius. Surfaces at a distance equal to the focal length (away from the camera) should be in focus.
Finally – Show off your work! (3 marks)
Congrats! At this point, your ray tracer should be able to generate some pretty impressive images. Now is your chance to show off what it can do. The tests/final scene.txt file is currently empty. You should populate this scene in order to demonstrate all the core features of the ray tracer, as well as your chosen add-ons. Choose suitable settings to render your final image, and replace the images/final scene.png placeholder image with it. Notice that this image is already embedded in the README.md.
Also record how long it takes to render the image on your PC and include that in the README.md (see the template provided). In case we need to verify your image, you must also include the exact command line used to generate it. We might use lower resolution/anti-aliasing/quality settings to speed up the verification process.
Verify that your image is visible on GitHub (instead of the placeholder image) to ensure that you receive credit for it.
Assessment
Unless explicitly stated, grading of your submission will be exclusively based on outputs produced by your ray tracer, not your code. We will use an automated process to pass hidden test scenes and/or specific command line arguments to your program. Failing tests won’t receive credit! Therefore, you must ensure that you do not change the inputs or outputs of the provided template. Furthermore, don’t rely on just the provided test scenes – think about possible edge cases and write your own! We will run your ray-tracer with both the provided test scene, and some hidden test scenes.
Finally, feel free to ask questions about the specification on the discussion forum (Ed). However, before doing so, please refer to our live FAQ which will be pinned for the duration of the project. This is based on questions from previous years, and will be updated further as new questions roll in!
Submission
Your submission must conform to the exact scene and command line specification that is provided with the template, since our testing system is semi-automated. If you do not modify the core template code, then this should not be an issue. Additionally, external libraries, dependencies, or packages are not allowed. Make it clear in your git README.md which add-ons you wish to be marked, and briefly describe your approach for these. Remember, you should frequently commit your work to GitHub while working on the project. Even though you are working solo, this serves as a backup and proof of your work on the project.

Reviews

There are no reviews yet.

Be the first to review “COMP30019 – Project 1: Ray Tracer (Solution)”

Your email address will not be published. Required fields are marked *