Download presentation
Presentation is loading. Please wait.
Published byPercival Fleming Modified over 9 years ago
1
Alex YAU alexyau@ust.hk
2
Important Notes Website for Assignment #2 https://course.cse.ust.hk/comp341/S10/Password_Only/projects/project2/index.html 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
3
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
4
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
6
Overview of Ray Tracing Load a scene file Ray trace Save output image Input: Scene File Output: Image File
7
Overview of Ray Tracing Backward ray tracing Efficient Shoot rays from eye to the image plane Capture the scene
8
Overview of Ray Tracing One level ray tracing Ambient light source Directional light source Shadow casting
10
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
12
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
13
Loading Scene File # comment line )
14
Loading Scene File – Viewer 1) VIEWER EYE_POS 0.0 0.0 3.0 eye position in 3D VIEW_AT_POS 0.0 0.0 0.0 position in 3D viewed by the eye VIEW_UP_VEC 0.0 1.0 0.0 view up vector FOVX 1.5707963 field of view along x radian) FOVY 1.5707963 field of view along y (radian) RESOLUTION 256 256 resolution of ray traced image
15
Loading Scene File – Light 2) LIGHT 2 there are two light sources TYPE LS_AMBIENT ambient light source COLOR 0.2 0.2 0.2 RGB value [0,1] TYPE LS_DIRECTIONAL directional light source DIR 1.0 1.0 1.0 light going from this direction COLOR 0.8 0.8 0.8 RGB value [0,1]
16
Loading Scene File – Sphere 3) SPHERE 3 there are 3 spheres CENTER 0.0 0.0 0.0 RADIUS 1.0 KD 0.3 0.0 0.0 diffuse component KS 0.5 0.5 0.5 specular component N 5.0 shininess...
17
Loading Scene File – Triangle 4) TRIANGLE 2 there are 2 triangles VERTEX1 -1.0 -1.0 -1.0 each has three vertices VERTEX2 1.0 -1.0 -1.0 VERTEX3 -1.0 -1.0 1.0 KD 0.0 0.0 1.0 diffuse component KS 0.3 0.3 0.3 specular component N 10.0 shininess...
19
How to Define Eye Ray
24
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
25
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 ]
26
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
27
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
28
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 )
30
Blinn-Phong Lighting Model
31
Directional Light Source
32
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
33
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
34
Directional Light Source
37
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
38
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.