Ray tracing New Concepts • The recursive ray tracing algorithm • Generating eye rays • Non Real-time rendering Ray tracing • Using ray tracing you can achieve photorealistic effects • You can achieve many complex lighting effects, such as shadows, refraction and reflection. • It is incredibly hard, if not impossible to achieve these accurate effects with the standard rendering pipeline. • While you get a huge advance in visual quality, it comes with the price of very long rendering times. Camera Rays • First you shoot rays from the camera out into the scene. • Pixels can be rendered in any order, but in this lab we will go from top to bottom left to right. • We loop over the pixels and generate an initial primary ray (eye ray) • The ray origin is just the camera’s position • The direction is computed by first finding the 4 corners of a virtual image in world space, then interpolating to the correct spot, and finally computing a normalized direction from the camera to the virtual pixel. Ray Intersection • The eye ray is then tested for intersection against every object in the scene. • If there is no intersection then we just color that pixel the specified ‘background’ color. • If there is a hit, then we want to find out what the closest object hit was. • At the intersection, we need to know the position, normal, color, texture coordinates, material etc…about that exact location. • If the hit is inside a triangle, then all that information is just interpolated from the vertex data. Lighting • Once we have the key intersection information, we can apply any lighting model we want. • This can include procedural shaders, lighting computations, texture lookups, bump mapping etc… • Many of the most interesting forms of lighting involve spawning additional rays and tracing them recursively. (Which we will be doing in this lab for shadows) • The result of the lighting equation is a color which is used to color the pixel. Shadows • Shadows are really easy to achieve • Just trace another ray from a hit point to every light in the scene, if you hit an object before you hit the light, then that light cannot contribute to the color. • Shadow rays only need to know if something is hit, where as normal rays need to know position, color, normal, etc. • When we spawn new rays off of a surface, it is a good idea to add a bit of an offset to the origin. That is to push it up slightly (0.0001) along the normal of the surface. – This prevents the ray from intersecting with the object it spawned off of. Reflection Rays • Reflections are another awesome and easy to do effect with ray tracing. • Instead of calculating the lighting equation at a particular hit point, you just trace a secondary ray, called the reflection ray, and trace it into the scene. • Reflection rays, like shadow rays, should also be nudged up a bit. Reflection Rays • If the reflection ray hits a normal material, we compute the illumination and use that as the final color. • But if the reflection hits another mirror, then we just recursively generate a new reflection ray and trace it. • To prevent the system from getting trapped in an infinite loop, you can set a limit to the depth of the recursion. 10 is a good number… Intersection Test • For this lab, everything has been created by using triangles, so you will only have to write an intersection test for ray-triangle intersections. • Once we know a ray hits a triangle at a point q we must verify that q lies inside the 3 edges of the triangle. • We do this by calculating the barycentric coordinates. – q’ = q-v0 – e1 = v1 - v0 – e2 = v2- v0 – b1 = (q’ * e2)/(e1 * e2) – b2 = (q’ * e1)/(e1 * e2) – Reject if b1 <0 , b2 <0 or b1 + b2 > 1 File Overview • Camera.[cpp h] : You will do most of the work in these files, including writing the actual RayTrace algorithm, and all the code for producing eye rays. • Image.[cpp h] : All the functions for drawing, and setting the screen pixels. You don’t have to write any code here • Light.[cpp h] : All the information for the lights. You don’t have to write any code. • Model.[cpp h] : You will have to write the triangle and model intersection code here • Ray.h : Has the structures for ray’s and intersections defined here • Vector.h : All of the vector manipulation stuff. • Scene.[cpp h] : The scene is defined in here • Raytracer.cpp : this is the main file. Requirements Complete the ray tracer so that you end up with an image that has shadows and mirror reflections. Similar to this: Implementation Suggestions • MAKE SURE YOU LOOK THROUGH ALL THE FILES AND UNDERSTAND THEM • There is a TODO in every function where you need to write code. “grep TODO *” <--- type that in the terminal • It helps to have a function like bool inShadow(…) • You will probably have to write a function called Trace() !!! • The first thing your gonna have to work on is writing the intersection functions and the eyeray generating function. • Work on this every day. Resources • There are resources on the webpage. PLEASE download them, there are helpful pictures, and information on eye ray generation.