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:551 ObjectiveYou 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 satisﬁed with theresult!2 Speciﬁcations • You will read a scene ﬁle, for example ”scene.txt”. This ﬁle includes all the basic information you need when rendering a scene. Details of this ﬁle is included in the next chapter. The scene ﬁle will have the extension ”.txt”. • The scene ﬁle 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 ﬁle are world coordinates. Which means, they are only relative to the origin, not to each other. • All of the lights provided in the scene ﬁle are point light sources, equally shining in all directions. NOTE: Intensity values of the light need not be between 0-255. They simply deﬁne a light source’s power, or the amount of light it delivers. When calculating the ﬁnal 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, diﬀuse shading, specular shading, shadows and object reﬂectance 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” ﬁle format is very easy to write. You may use the sample code we provide for writing to a ”.ppm” ﬁle. You may ﬁnd detailed information about the format here.
3 Scene FileThe scene ﬁle includes every detail you will use when drawing a scene. The scene ﬁle formatis 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 Ib3.1 Explanation of Scene FileThe ﬁrst three lines of a scene ﬁle are ﬁxed. The rest may be of any order. There will be onlyone camera entry and one ambient entry in the ﬁle. • ImageWidth ImageHeight // The width and height of the image to be rendered. Both Integer. • Tmin // When sending a reﬂecting ray from an object, you should ﬁrst 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 deﬁned. 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.
• Ray Reflect Count // A ray can bounce oﬀ 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 deﬁne the position of the camera. All Float. // gaze parameters deﬁne your viewing direction as a vector. All Float. // up parameters deﬁne the up vector. All Float. // left, right, bottom, up parameters together deﬁne the size of viewport. All Float. // distance is the distance of viewport from the camera. When sending the ray for // the ﬁrst 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 deﬁned with their material indexes. Materials deﬁne // the physical properties of objects. As an example, the material of a table object // can either be wood, or metal. However, we will only deﬁne materials with integer // indices, therefore you can think of them as substance 1 and substance 2. // ambient parameters deﬁne the percentage the object reﬂects 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 reﬂect the other half. Apply // to three bands. // diﬀuse parameters deﬁne the diﬀusal properties of the material. // specular parameters deﬁne 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 reﬂects. // reﬂective parameters are provided to be used with bouncing rays. The bouncing // oﬀ ray will contribute to the color of the point it hits ﬁrst. The reﬂective parameters // are provided to calculate the amount of of added light (in terms of all three bands) // to the point. If all reﬂective 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 deﬁned as three points in world coordinates. positions of three points //are given in an ordered manner. Normals should be calculated within this order. // MaterialIndex deﬁnes the material of the triangle. Use the indexed material for color // calculations. All parameters except MaterialIndex are ﬂoats. MaterialIndex is
// Integer. • #Sphere posx posy posz Radius MaterialIndex // Position parameters of a sphere deﬁne its center’s position in world coordinates. // Radius is the radius of the sphere. // MaterialIndex deﬁnes 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 deﬁne the light’s position in world coordinates. All // Floats. Intensity parameters of the light deﬁne 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 deﬁne 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.
• 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 SubmissionSubmission will be done via COW. You should upload a single zipped ﬁle called “hw1.zip”. Inaddition to your code, provide a makeﬁle. 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 thecourse’s web site.6 GradingGrading will be made using the scala on the course’s website.7 Cheating PolicyWe have zero tolerance policy for cheating. People involved in cheating will be punishedaccording to the university regulations. See the course website for more information.