Shading for Ray Tracing Computer Graphics: Project 3B


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (4 votes)

1 – Objective
The goal of this project is to complete your ray tracing renderer. In this project you will expand your Ray Tracer
to detect intersections between rays and triangles. You will also expand your shading function to include ambient
and specular color as well as cast shadows (via shadow rays) and reflection (via recursive ray casting).
2 – Deadline
This project is due on T-Square by 11:55PM on Friday, November 10, 2017.
3 – Process
3.1 Source Code for this Project
You will base your code on your code from project P3A.
3.2 Project Description and Suggested Approach
You have four primary goals for this second part of the ray tracing project:
1. Detect ray/triangle collisions
2. Expand your shading function to include ambient and specular light
3. Cast shadow rays to determine when a light should not contribute to the color
4. Implement reflection by recursively casting new rays into the scene inside of your shading function
You can accomplish these goals however you see fit. A good approach would be to use object oriented
programming practices and create objects for each of the major scene components (scene, light, surface material,
ray, sphere, triangle, hit information). Global lists of scene objects could be stored to allow for easy access. Even
if you got everything working correctly for part 3A, you may find that it is better to re-arrange your code to
prepare for the second part of this assignment.
Note that your code will need to determine whether or not a ray hits an object in three different cases: eye rays,
shadow rays, and reflection rays. We strongly urge you to write a single routine that helps you to accomplish
these separate tasks. You could call such a routine RayIntersectScene. You would pass to this routine a ray, and
then have this routine return information about whether the ray hit anything. The routine should test the given ray
for intersection with each of the visible objects in the scene, that is, all spheres and all triangles. It should
determine the closest such hit, creating a Hit data structure that specifies information about this nearest hit, and
finally returning this Hit object. Inside the Hit object would be all the information that you will need to know
about what the ray hit, including the ray “t” value, the 3D point of intersection, the surface normal, and a
reference to the surface material that was hit (e.g. surface colors and so on). If your ray misses all objects, the
RayIntersectScene routine could return a null pointer, or your Hit object could include a boolean that says
whether or not the ray hit anything.
For shadow rays, all you will need to know is whether the ray hit any objects. For eye and reflection rays, you will
need to take the Hit information that RayIntersectScene returned and pass it to a shading function that will
determine the color for the given ray. In the case of reflections, the shading function will need to call itself
recursively in order to get the color for the reflection ray. Such an approach would be easy to test and clean to
No matter how you implement the Ray Tracer, your results should appear like the examples below. Note that we
have added a new scene file (i10.cli) that was not present in project 3A.
3.3 Result Images
Below are the correct images for the ten given scene files (i1.cli through i10.cli).
3.4 Example Scene Files
You will need to download the new .cli files for this project. To make sure the new file i10.cli loads properly, you
should add a command to render it on keypress ‘0’.
3.5 Your Code
You should modify your project 3A code in any way you see fit to solve 3B. Visit “”
for more information on Processing. You are NOT allowed to use most of the built in processing/openGL
functions in this project. The exceptions are that you can use PVectors and the standard math functions. When in
doubt about this, ask the instructor. As with project 3A, you must set the color of each pixel manually, such as by
using rect() or set().
3.6 Scene Description Language
Each scene is described in a .cli file using the grammar described below. These files are contained in the /data
folder. The suffix “cli” stands for “command language interpreter”.
fov angle
Specifies the field of view (in degrees) for a perspective projection. The viewer’s eye position is assumed to
be at the origin and to be looking down the negative z-axis (giving us a right-handed coordinate system). The
y-axis points up.
background r g b
Background color. If a ray misses all the objects in the scene, the pixel should be given this color.
light x y z r g b
Point light source at position (x,y,z) and its color (r, g, b). Your code should allow up to 10 light sources. For
this second part of the assignment, you will cause these lights to cast shadows.
surface Cdr Cdg Cdb Car Cag Cab Csr Csg Csb P Krefl
This command describes the reflectance properties of a surface, and this reflectance should be given to the
objects that follow the command in the scene description, such as spheres and triangles. The first three values
are the diffuse coefficients (red, green, blue), followed by ambient and specular coefficients. Next comes the
specular power P (the Phong exponent), which says how shiny the highlight of the surface should be. The
final value is the reflection coefficient (0 = no reflection, 1 = perfect mirror).
Usually, 0 <= Cd,Ca,Cs,Krefl <= 1. When Krefl is larger than one, this indicates that you will need to create reflection rays for this surface. sphere radius x y z A sphere with its center at (x, y, z) and the given radius. begin Begins the definition of a polygon. Should be followed by "vertex" commands, and the polygon definition is terminated by an "end". vertex x y z One vertex of a polygon. For this project, all of the provided polygons will be triangles. This means you can assume that there will be exactly three "vertex" commands between a "begin" and "end". end Ends the definition of a polygon. write [filename].png Ray-traces the scene and saves the image to a PNG image file. Note on color specification: Each of the red, green, and blue components range from 0.0 to 1.0. CS 3451 Fall 2017 Project 3B: Shading for Ray Tracing 4 / 4 3.7 Debugging Suggestions If you have a bug in your ray tracer, you will want to print out some values to the screen (hit position, surface normal, light vector, etc.) We strongly suggest you should only print out information for ONE PIXEL, and not for all of the pixels on the screen. You could do this by creating a Boolean variable “debug_flag”. In the loop that creates the rays from the eye, only set this Boolean to “true” for one pixel on the screen. Then make sure all of your debug print statements only print when debug_flag is set to “True”. It is sometimes useful to use the mouse to select a particular pixel to debug with. You can determine the coordinates of a given pixel in your image by clicking in the image with the mouse. You can do this by defining a routine MousePressed() that will be called every time a mouse button is clicked. Processing will call this named routine if you define it. You can then print the built-in variables mouseX and mouseY to show what pixel was clicked on. Then you can use these numbers to set debug_flag to “true” just for this one pixel. Don’t forget that a zero value for mouseY indicates that the cursor is at the top of the screen. Keep in mind that all of the scene description files (t01.cli to t10.cli) are text files that you can view and modify. It may be helpful for you to look at or modify one of these files to help you figure out what is going on with your ray tracing code. 3.8 Authorship Rules The code that you turn in should be entirely your own. You are allowed to talk to other members of the class and to the Professor and the TA about general implementation of the assignment. It is also fine to seek the help of others for general Processing/Java programming questions. You may not, however, use code that anyone other than yourself has written. Code that is explicitly not allowed includes code taken from the Web, from books, or from any source other than yourself. The only exception to this is that you may include code that was specifically provided for this assignment or the previous one (Project 3A and 3B). You should not show your code to other students. Feel free to seek the help of the Professor and the TA's for suggestions about debugging your code. 3.9 Submission In order to run the source code, it must be in a folder named after the main file. When submitting any assignment, leave it in this folder, compress into a zip file (not rar or tar, please!) and submit via T-square.