40

Alex YAU [email protected]. Important Notes Website for Assignment #2 You

Embed Size (px)

Citation preview

Alex YAU

[email protected]

Important NotesWebsite 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

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

OutlineOverview of Ray TracingMain AlgorithmLoading Scene FileHow to Define Eye RayRay-intersectionLighting ModelSaving Output ImageBells and Whistles

Overview of Ray TracingLoad a scene fileRay traceSave output image

Input: Scene FileOutput: Image File

Overview of Ray TracingBackward ray tracing

EfficientShoot rays from eye to the image plane

Capture the scene

Overview of Ray TracingOne level ray tracing

Ambient light sourceDirectional light sourceShadow casting

Main Algorithmfor each pixel in the image

create a ray from eye_position to the center of the pixelset nearest_t = infinityset nearest_object = NULLset surface_point = NULLfor every objects in the scene

if ray intersects this_object and t < nearest_tset nearest_t = tset nearest_object = this_object

end ifset pixel_color = background colorif nearest_object != NULL

set surface_point = eye_poistion + t * ray_directionfor each light source

if this surface_point is not in shadow pixel_color += compute_shading()

end ifend ifdone

done

Loading Scene FileC++ I/O Stream4 types of data section

ViewerLightSphereTriangle

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

<id #>) <id> <number>

<Property id> <Property values>

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

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]

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

. . .

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

. . .

How to Define Eye Ray

How to Define Eye Ray

How to Define Eye Ray

How to Define Eye Ray

Ray-intersectionParametric ray: r(t) = p + t d

t ≥ 0||d|| = 1, thus t is distance traveled from p in d direction

Implicit object: f(x) =0Intersection occurs when f(r(t)) = 0

Real function of one real variable tSo, intersection ≡ root finding

Intersect with TriangleApproach 1:

Intersect the ray with the plane containing the triangleThen determine whether or not the intersection point is

within the triangleApproach 2:

Consider a ray as an origin and a direction vectorDefine the triangle as a tuple of vertices [v0, v1, v2]

Intersect with Triangle Parametric ray: r(t) = p + t dParametric plane: (x - v0) · (v1 × v2) = 0

Solve (p + t d - v0) · (v1 × v2) = 0

Verify x0 is inside the triangle

s1 × u1, s2 × u2, s3 × u3 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 v0 + a v1 + b v2

where, a + b + c = 1 and a, b, c ≥ 0

So,p + t d = (1-a-b) v0 + a v1 + b v2

Solve t, a, bCheck 0 ≤ a ≤ 1, 0 ≤ b ≤ 1 and a + b ≤ 1

Easy to implement and efficient

Intersect with SphereParametric ray: r(t) = p + t dParametric sphere: ||x – c||2 = r2

Solve ||p + t d – c||2 = r2

(t d + p – c) · (t d + p – c) – r2 = 0Test discriminant Δ to find the number of roots

Test for intersection!

Δ = (2 d · (p – c))2 – 4 ((d · d)(p – c) · (p – c) – r2)

dd

cpd

2

))(2(t

Blinn-Phong Lighting Model

Directional Light Source

Directional Light SourceLa = intensity of ambient light source

Simulate indirect global illuminationDoes not varies with lighting or viewing direction

Li = intensity of the i-th directional light source

kd = diffuse material property

ks = specular material property

Directional Light Source

Diffuse termSimulate reflection occurring on dull surfacesVaries only with lighting direction

Specular term:Simulate reflection occurring on smooth surfacesVaries with both lighting and viewing direction

Directional Light Source

Directional Light Source

Saving Output ImageSave output as PPM imageView the output using IrfanView Use C++ I/O Stream with binary flag on

#include <iostream>

std::ofstream fout;

fout.open(“my_image.ppm”,ios::binary);• Write binary number to the file stream

PPM File FormatP6

Magic number for identification

256 256Width, Height

256Maximum value of a unit

<R><G><B><R><G><B>... Image as a sequence of R, G, B tuple