This document provides instructions for Assignment 1 of the CENG 477 Introduction to Computer Graphics course. Students are asked to implement a ray tracer that takes in a scene file as input and renders the scene using ray tracing techniques. The scene file format is defined, including specifications for the camera, materials, triangles, spheres, lights, ambient light and background color. Hints are provided about using object-oriented design with classes like Vector3, Camera, Ray, Shape, and Scene. Students should submit a single zip file called "hw1.zip" containing their C++ code and makefile.
Measures of Central Tendency: Mean, Median and Mode
Hw1 updated
1. Middle East Technical University Department of Computer Engineering
CENG 477
Introduction to Computer Graphics
Fall ’2011-2012
Assignment 1 - Ray Tracer
Due date: 13 November 2011, Friday, 23:55
1 Objective
You will implement a ray tracer. In this homework, you will dig into the details of ray tracing.
When you see the light at the end of the tunnel, we promise you’ll be very satisfied with the
result!
2 Specifications
• You will read a scene file, for example ”scene.txt”. This file includes all the basic
information you need when rendering a scene. Details of this file is included in the next
chapter. The scene file will have the extension ”.txt”.
• The scene file will exist in the workspace, and we will provide its name as the only
command line argument to your program.
• You will render the given scene using perspective projection.
• The time limit is 15 minutes on ineks. If you exceed this time, your program will be
terminated.
• All coordinates provided in the scene file are world coordinates. Which means, they are
only relative to the origin, not to each other.
• All of the lights provided in the scene file are point light sources, equally shining in all
directions.
NOTE: Intensity values of the light need not be between 0-255. They simply define a
light source’s power, or the amount of light it delivers. When calculating the final color
of a pixel, you need to map values higher than 255 to maximum pixel intensity 255, for
each band.
• Your ray tracing algorithm will implement ambient shading, diffuse shading, specular
shading, shadows and object reflectance properties.
• You will save the rendered image to ”$SCENE FILE NAME$.ppm” in the workspace.
For example, if you are asked to render ”scene.txt”, the output image should be ”scene.ppm”.
”.ppm” file format is very easy to write. You may use the sample code we provide for
writing to a ”.ppm” file. You may find detailed information about the format here.
2. 3 Scene File
The scene file includes every detail you will use when drawing a scene. The scene file format
is given as follows:
ImageWidth ImageHeight
Tmin
Ray Reflect Count
#Camera
posx posy posz gazex gazey gazez uprx upy upz left right bottom top distance
#Material MaterialIndex
ambr ambg ambb difr difg difb sper speg speb specExpP refr refg refb
#Triangle
pos1x pos1y pos1z pos2x pos2y pos2z pos3x pos3y pos3z MaterialIndex
#Sphere
posx posy posz Radius MaterialIndex
#Light
posx posy posz Ir Ig Ib
#Ambient
Ir Ig Ib
#Background
Ir Ig Ib
3.1 Explanation of Scene File
The first three lines of a scene file are fixed. The rest may be of any order. There will be only
one camera entry and one ambient entry in the file.
• ImageWidth ImageHeight
// The width and height of the image to be rendered. Both Integer.
• Tmin
// When sending a reflecting ray from an object, you should first normalize the ray.
// When checking for an intersection with other objects, you should take Tmin
// as minimum t parameter for the ray. Intersections with smaller t (such as the
// object itself) should not be taken into account.
// IMPORTANT: The frustum’s far edge is not defined. There is no limit on the
// maximum distance of intersection. You should check ray-object intersection with
// each object. If the ray does not intersect with any object in the frustum, then
// you assign the pixel background color.
3. • Ray Reflect Count
// A ray can bounce off this many times. Integer.
• #Camera
posx posy posz gazex gazey gazez uprx upy upz left right bottom top distance
// You will send rays from the camera that is put into scene with the given parameters.
// position parameters define the position of the camera. All Float.
// gaze parameters define your viewing direction as a vector. All Float.
// up parameters define the up vector. All Float.
// left, right, bottom, up parameters together define the size of viewport. All Float.
// distance is the distance of viewport from the camera. When sending the ray for
// the first time, you should check if an intersecting object is on the other side of the
// viewport, meaning that it is in the frustum. There are two ways for doing this. When
// calculating the ray’s intersecting pixel’s position on the viewport, you create the ray’s
// direction vector. If you don’t normalize this direction vector, pixel’s t parameter is
// equal to 1. Therefore, an intersecting object’s t parameter should be larger than 1.
// If you normalize the direction vector, then you should calculate t by taking magnitude
// of it before normalization. Then, intersecting object’s t parameter should be larger
// than that value. Float.
• #Material MaterialIndex
ambr ambg ambb difr difg difb sper speg speb specExpP refr refg refb
// Objects in your scene will be defined with their material indexes. Materials define
// the physical properties of objects. As an example, the material of a table object
// can either be wood, or metal. However, we will only define materials with integer
// indices, therefore you can think of them as substance 1 and substance 2.
// ambient parameters define the percentage the object reflects each band of ambient
// light that is cast onto it. For example, if the ambient red property of a material is
// 0.5, it will absorbe half of the red light it receives, and reflect the other half. Apply
// to three bands.
// diffuse parameters define the diffusal properties of the material.
// specular parameters define the specular properties of the material. Specular
// exponent specExpP should also be taken into account, when calculating an the
// amount of specular light an object reflects.
// reflective parameters are provided to be used with bouncing rays. The bouncing
// off ray will contribute to the color of the point it hits first. The reflective parameters
// are provided to calculate the amount of of added light (in terms of all three bands)
// to the point. If all reflective parameters are close to 1, this is a mirror-like material.
// All parameters except specExpP are Floats. specExpP is Integer.
• #Triangle
pos1x pos1y pos1z pos2x pos2y pos2z pos3x pos3y pos3z MaterialIndex
// A triangle is defined as three points in world coordinates. positions of three points
//are given in an ordered manner. Normals should be calculated within this order.
// MaterialIndex defines the material of the triangle. Use the indexed material for color
// calculations. All parameters except MaterialIndex are floats. MaterialIndex is
4. // Integer.
• #Sphere
posx posy posz Radius MaterialIndex
// Position parameters of a sphere define its center’s position in world coordinates.
// Radius is the radius of the sphere.
// MaterialIndex defines the material of the sphere. Use the indexed material.
// All parameters except MaterialIndex are Floats. MaterialIndex is Integer.
• #Light
posx posy posz Ir Ig Ib
// Position parameters of the light define the light’s position in world coordinates. All
// Floats. Intensity parameters of the light define the light’s intensity values it delivers
// in all directions. Intensity values could be higher than 255. Integers.
• #Ambient
Ir Ig Ib
// Intensity parameters of the light define the ambient light’s intensity values in all three
// bands. This is the amount of light each object’s each point receives even if it is under
// shadow. Intensity values could be higher than 255. Integers.
• #Background
Ir Ig Ib
// If a ray does not hit anything, the corresponding pixel will take this value. Just as
// simple as that.
4 Hints & Tips
• You will implement the ray tracer in C++.
• You are highly advised to follow a object-oriented approach. The basic classes you may
need to create are Vector3, Camera, Ray, Shape, Triangle, Sphere, Scene classes. Writing
a ray tracer is a tedious work, and writing a code neatly by using classes properly will
save you a huge amount of time, especially when debugging.
• You are encouraged to use the Vector3 class you implemented in the warm-up.
• We will test your codes on departmental machines using “g++”. Please make sure to
run tests on ineks.
• You may compile your code with -O2 for optimization.
• In triangle calculations, if the values of beta, gamma and t are not in the expected
interval, you may choose not to calculate the others. This will provide a certain speed-
up.
5. • Normal vectors of triangles are needed for each ray. Pre-computation of normal vectors
before casting rays will greatly speed-up the process.
• For a triangle<a,b,c>, you may pre-compute the b-a and c-a vectors and access them
when casting rays.
5 Submission
Submission will be done via COW. You should upload a single zipped file called “hw1.zip”. In
addition to your code, provide a makefile. Your executable should have the name ”raytracer”.
We will test your code as:
$./raytracer "$SCENE FILE NAME$.txt"
Follow the newsgroup for details.
Late submissions are allowed for this homework, regarding to the policy on the
course’s web site.
6 Grading
Grading will be made using the scala on the course’s website.
7 Cheating Policy
We have zero tolerance policy for cheating. People involved in cheating will be punished
according to the university regulations. See the course website for more information.