Assignment 2 Ray Tracing Overview The concept of ray tracing: a technique for generating an image by tracing the path of light through pixels in an image plane and simulating the effects of its encounters with virtual objects. The technique is capable of producing a very high degree of visual realism, usually higher than that of typical scan line rendering methods, but at a greater computational cost. The objective of this exercise is to implement a ray casting/tracing engine. A ray tracer shoots rays from the observer s eye through a screen and into a scene of objects. It calculates the ray s intersection with objects, finds the nearest intersection and calculates the color of the surface 1
according to its material and lighting conditions. (This is the way you should think about it this will help your implementation). 2
Requirements Read this entire explanation before starting. Understand the slides taught in class especially. The feature set you are required to implement in your ray tracer is as follows: Background o Black Display geometric primitives in space: o Spheres o Planes Basic lighting o Directional lights o Spot lights o Ambient light o Simple materials (ambient, diffuse, specular...) Basic hard shadows One Reflection (10 Points bonus) Scene Definition The 3D scene your ray tracer which will be rendering will be defined in a scene definition text file. The scene definition contains all of the parameters required to render the scene and the objects in it. The specific language used in the definition file is defined later. 3
Geometry Geometric primitives Sphere A sphere is defined by a center point and scalar radius. The normal of each point on the sphere s surface is easily computed as the normalized subtraction of the point and the center. Plane Plane is always a rectangle. A plane is defined by a point represents the center of the plane, a normal vector to the plane, width and length. Every plane has white 32X32 checker board pattern. You can Assume that the normal of a plane has one zero coordinate and one edge of the plane is parallel to one of the axis. For example if the normal is (1,1,0) then one edge direction is (0,0,1) and second edge direction is (1,-1,0). Spheres and planes can intersect each other. Background The background of the rendered scene is black. Camera The camera is a simple pinhole camera as described in the lecture slides and will be located at (0,0,0). Camera direction will be toward (image plane center). Lighting Basic lights and shadows For basic lighting you need to implement: 4
1. Ambient lighting a color that reflects from all surfaces with equal intensity. 2. Directional A light source like the sun, which lies at infinity and has just a direction. 3. Spot light A point light source that illuminates in a direction given by a vector. This light source has a cutoff angle as describe in the following image. Every light source has its own intensity (color) and there can be multiple light sources in a scene. Shadows appear where objects obscure a light source. In the equation they are expressed in the term. To know if a point in space (usually on a surface) lies in a shadow of a light source, you need to shoot a ray from in the direction of the light and check if it hits something. If it does, make sure that it really hits it before reaching the light source and that the object hit is not actually a close intersection with the object the ray emanated from. Materials You need to implement the lighting formula (Phong) from the lecture slides. The material of a surface should be flat with ambient, diffuse, specular reflections, and a shininess parameter (the power of ). The first 4 parameters are RGB colors. Shadow You can choose to add shadow effect to your scene using parameter in Phong model (see lecture slides). Shadow appears when some object located between the light source and another object which can be observed by the viewer. In that case you should ignore the covered light source part in Phong model. Some common mistakes may cause spurious shadows to appear. Make sure you understand the vector math involved and all the edge-cases. Reflection (10 points bonus) You can choose implement planes which is defined in the objects file to be a mirror. Each ray hit a mirror plane breaks symmetrically to the normal (include light rays). The color of the pixel in a mirror plane is calculating according to the breaking ray. Don't forget to consider the light sources rays which break by the mirror plane. If you choose to implement this task ignore mirror objects material parameters: ambient, diffuse, specular reflections. Mirror will be uniform (without squares). Mirror plane will be signed by M at the end of the parameters list. 5
Fisheye lens (10 points bonus) Create a fisheye effect. You may make it by calculating the ray reflectance from fisheye lens which is located between the screen and the scene or between the eye and the screen. Further reading: http://en.wikipedia.org/wiki/fisheye_lens#mapping_function If you implement this bonus run your program with argument "fish" to see the scene through fish eye lens (when running without parameters the program show the original scene). Up to 10 bonus points will be given for extremely interesting scene you build. Submit init.txt file and output image of your ray tracer. Every additional advanced feature (as acceleration, transparency, environmental light) will give you extra points. 6
Input You will get one text file named init.txt. The first line in the file will start with the word "scene" following by (screen center coordinates x, y, z), up vector (x, y, z), w width of the scene, when ( ) is Image resolution (you can assume that ) and Ambient lighting (R, G, B coordinate). 12 parameters total which are separated by commas. Image plane normal is the vector From the second row we will describe the object in the scene: Light will describe by "light" keyword following by light direction (x, y, z), light intensity (R, G, B) and if it is a spotlight light position (x, y, z) and light cutoff angle. 6 or 10 parameters total. Sphere will describe by "spher" keyword following by the sphere center coordinate (x,y,z), radius, material parameters each one has 3 components (R, G, B) and shininess parameter. 14 parameters total. Plane will describe by "plane" keyword following by the a normal to the plane coordinates (x,y,z), center of plane point coordinates (x,y,z),width and length, material parameters each one has 3 components (R, G, B) and shininess parameter. 18 parameters total. If you choose to implement reflection bonus, you generate each object that signed with "M" at the end of the line as a mirror. Input example: scene 0,0,-2,0,1,0,2,512,512,0.5,0.1,0.8 light 1,1,1,0.3,0.2,0.6 light 1,-1,1,0.9,0.1,0.6,0,4,-5.5,30 spher 0,0,-5,1.5,0.3,0.3,0.3,0.4,0.4,0,0.1,0.7,0,5 plane 0,1,0,0,-2,0,100,100, 0.3,0,0.3,0.4,0,0,0.1,0.7,0,20 M Notes Recommended Milestones Design Before you start coding! You should think carefully about the structure and design of your system. Take the time to define interfaces, classes and interactions between objects. Implementing a ray tracer poses an array of opportunities for creating good OOP design Make sure you take advantage of them. Write incrementally. Start from a small scene with one or two objects. 7
Validation We will provide you with sample scenes for validation and the way they are supposed to render in your ray tracer. Your implementation may vary from the supplied image in little details but in general the scene should look the same. Some general hints: A color can be seen as a vector that points from black (0,0,0) to a point in the RGB space. Use the Vector3f class to handle vectors and RGB. Before plotting a pixel, make sure that its color does not exceed the range of 0-1 for every color channel. Right vector of the screen will be orthogonal to up vector and toward vector (see slide 42 of PS 3. Use 'fscanf' and fseek function to read parameter from file. Example: float var; FILE *f; //file discriptor Fseek(f,5,SEEK_CUR); //skip 5 character from the current position in the file fscanf(f,"%f,",&var); //put a float number into var and return 1 when succeed Submission: Submission is in pairs. Everything should be submitted inside a single zip file named <ID1> _<ID2>.zip The zip file should include all of your.h files and.cpp files or your project if you use VS. 8
Appendix A Step by Step Guidance Following are some practical suggestions that could make the exercise easier for you. There are many other ways to approach it as well, and you don't necessarily have to follow these directions. 1. Make sure you read the scene definition file correctly. 2. Define you C++ object for each light source, sphere and a plane. 3. Try to render small scenes with one light and one or two objects. 4. Add lights and compare your results which friends. 5. Implement lighting types. 6. Add support for shadows and reflection by recursively shooting additional rays. 7. Go through the exercise definition and make sure you haven't forgotten anything. 8. Debugging: An easy way to start debugging is to find a pixel where you know something is wrong and try to debug the calculation of that specific pixel. Say you found there is a problem at (344, 205), You can start by writing something like this: if (x == 344 && y == 205) printf("yo!"); // set breakpoint here. You can also print to a log file. Helpful links: http://en.wikipedia.org/wiki/ray_tracing_(graphics) http://www.cl.cam.ac.uk/teaching/1999/agraphhci/smag/node2.html http://www.siggraph.org/education/materials/hypergraph/raytrace/rtrace0.htm 9
Good Luck! 10