Alex YAU
Important Notes Website for Assignment #2 You should use C++ and not use OpenGL Due: Monday 12 April, 12 pm (noon) Demo: 12 and 13 April in 4221 Grouping: You can change your group if you wish
Grading Scheme (Total: 100%) Loading scene file (10%) Ray tracing (45%) Scene preparing (5%) Ray-intersection (20%) Shadow casting (5%) Saving output image (10%) Bells and whistles (35%) Posted on the web, please check the details
Outline Overview of Ray Tracing Main Algorithm Loading Scene File How to Define Eye Ray Ray-intersection Lighting Model Saving Output Image Bells and Whistles
Overview of Ray Tracing Load a scene file Ray trace Save output image Input: Scene File Output: Image File
Overview of Ray Tracing Backward ray tracing Efficient Shoot rays from eye to the image plane Capture the scene
Overview of Ray Tracing One level ray tracing Ambient light source Directional light source Shadow casting
Main Algorithm for each pixel in the image create a ray from eye_position to the center of the pixel set nearest_t = infinity set nearest_object = NULL set surface_point = NULL for every objects in the scene if ray intersects this_object and t < nearest_t set nearest_t = t set nearest_object = this_object end if set pixel_color = background color if nearest_object != NULL set surface_point = eye_poistion + t * ray_direction for each light source if this surface_point is not in shadow pixel_color += compute_shading() end if done
Loading Scene File C++ I/O Stream 4 types of data section Viewer Light Sphere Triangle The first line is comment started with # Between each section in the file, there are exactly one blank line as a separator
Loading Scene File # comment line )
Loading Scene File – Viewer 1) VIEWER EYE_POS eye position in 3D VIEW_AT_POS position in 3D viewed by the eye VIEW_UP_VEC view up vector FOVX field of view along x radian) FOVY field of view along y (radian) RESOLUTION resolution of ray traced image
Loading Scene File – Light 2) LIGHT 2 there are two light sources TYPE LS_AMBIENT ambient light source COLOR RGB value [0,1] TYPE LS_DIRECTIONAL directional light source DIR light going from this direction COLOR RGB value [0,1]
Loading Scene File – Sphere 3) SPHERE 3 there are 3 spheres CENTER RADIUS 1.0 KD diffuse component KS specular component N 5.0 shininess...
Loading Scene File – Triangle 4) TRIANGLE 2 there are 2 triangles VERTEX each has three vertices VERTEX VERTEX KD diffuse component KS specular component N 10.0 shininess...
How to Define Eye Ray
Ray-intersection Parametric ray: r(t) = p + t d t ≥ 0 ||d|| = 1, thus t is distance traveled from p in d direction Implicit object: f(x) =0 Intersection occurs when f(r(t)) = 0 Real function of one real variable t So, intersection ≡ root finding
Intersect with Triangle Approach 1: Intersect the ray with the plane containing the triangle Then determine whether or not the intersection point is within the triangle Approach 2: Consider a ray as an origin and a direction vector Define the triangle as a tuple of vertices [v 0, v 1, v 2 ]
Intersect with Triangle Parametric ray: r(t) = p + t d Parametric plane: (x - v 0 ) · (v 1 × v 2 ) = 0 Solve (p + t d - v 0 ) · (v 1 × v 2 ) = 0 Verify x 0 is inside the triangle s 1 × u 1, s 2 × u 2, s 3 × u 3 are pointing at the same direction Hard to implement and inefficient
Intersect with Triangle Barycentric coordinates Any point in a triangle can be defined as P(a, b, c) = c v 0 + a v 1 + b v 2 where, a + b + c = 1 and a, b, c ≥ 0 So, p + t d = (1-a-b) v 0 + a v 1 + b v 2 Solve t, a, b Check 0 ≤ a ≤ 1, 0 ≤ b ≤ 1 and a + b ≤ 1 Easy to implement and efficient
Intersect with Sphere Parametric ray: r(t) = p + t d Parametric sphere: ||x – c|| 2 = r 2 Solve ||p + t d – c|| 2 = r 2 (t d + p – c) · (t d + p – c) – r 2 = 0 Test discriminant Δ to find the number of roots Test for intersection! Δ = (2 d · (p – c)) 2 – 4 ((d · d)(p – c) · (p – c) – r 2 )
Blinn-Phong Lighting Model
Directional Light Source
L a = intensity of ambient light source Simulate indirect global illumination Does not varies with lighting or viewing direction L i = intensity of the i-th directional light source k d = diffuse material property k s = specular material property
Directional Light Source Diffuse term Simulate reflection occurring on dull surfaces Varies only with lighting direction Specular term: Simulate reflection occurring on smooth surfaces Varies with both lighting and viewing direction
Directional Light Source
Saving Output Image Save output as PPM image View the output using IrfanViewIrfanView Use C++ I/O Stream with binary flag on #include std::ofstream fout; fout.open(“my_image.ppm”,ios::binary); Write binary number to the file stream
PPM File Format P6 Magic number for identification 256 Width, Height 256 Maximum value of a unit... Image as a sequence of R, G, B tuple