CPSC310A Assignment 5 - Materials

Due Monday February 13


Create a C++ program that can produce a ray traced image of a scene containing spheres with Phong reflectance materials. The reflectance is specified using a subset of the mtl file format. You should create a material class and a material library class to help write this program. Phong reflectance is a simple way to calculate the amount of light that is reflected off of a plastic-like surface. It sums the contribution of three different types of reflection, ambient, diffuse, and specular. The material class represents each of these types of reflectance and should have colors for the ambient reflection, the diffuse reflection, and the specular reflection. The material class should also have a string to store the name of the material and an int to store the specular exponent.

The materials used in a scene are specified using a material library, mtl, file. An mtl file can contain several types of lines. A line that begins with "newmtl" specifies the name of a material. A new material line looks like:

newmtl mtl_name

Where mtl_name is the name of the material. The name can not contain any spaces, but can contain any other characters. All lines following a newmtl line, until another newmtl line is encountered, specify the named material's reflectivity. There are four lines that can follow a newmtl line to specify a material's parameters. These lines look like:

Ka ar ag ab
Kd dr dg db
Ks sr sg sb
Ns exp

Where Ka, Kd, and Ks specify the ambient, diffuse, and specular rgb reflectance of a material and Ns specifies the specular exponent. These lines can appear in any order, or not at all. If any of the material parameter lines are not defined the parameter defaults to black or 0. Note, an mtl file can contain multiple material definitions.

The scene file from the last assignment can also specify the material that a sphere should be rendered with. The material of a sphere is specified using a line that begins with "usemtl" and looks like:

usemtl mtl_name

Where mtl_name is the name of a material defined in a material library file. All spheres that are defined after a usemtl line are rendered with the specified material until another usemtl line is encountered. If a sphere line occurs before a usemtl line, then the sphere should use a material that is has white for all three type of reflectance and a specular exponent of 0. Scene file sphere lines no longer specify the color of the sphere because the material will be used instead. As a result the sphere class should no longer have a color member value and instead it should have a pointer to a material.

The scene file can also specify the material library files that contain the materials used in the scene. A line that begins with "mtllib" specifies a list of material library files and looks like:

mtllib mtl_filename1 mtl_filename2 mtl_filename3 ...

Where any number of material library file names are separated by spaces. Note, this implies that material library files names can not contain spaces. All of the materials specified by the material library files should be stored in a list or an associative array. If there are multiple materials with the same name than the first material read should be used. The material list or associative array should be member data in a material library class. The class should contain the code to read and add all of the materials in a material library file and provide a function that returns a pointer to a material given material name.

The ray trace function should be modified so that it uses the ambient reflectivity of a sphere's material to determine the color of a pixel. As a result spheres will still appear as disks when rendered. This will be remedied in the next assignment.


Tar your code in a file that contains your name and submit it on the course Inquire site.