A note about this week's course notes and assignment: We went over a lot of topics in class this week. For next week (Oct 9), I am only going to ask you to finish implementing the Matrix object class at the bottom of these notes. Meanwhile, feel free to work on reflection, refraction and other ray tracing topics that interest you. Your work on those techniques can be used toward an assignment that will be due two weeks from now, on October 16. NOTES ABOUT RAY TRACING REFLECTION IN RAY TRACING (1) Shoot ray (V, W) to find the point P on the surface and the surface normal N. (2) Use P, W and N to find a reflection direction R: R = W - 2 (NW) N (3) Shoot a ray from (P + ε R, R) into the scene, for some very small ε. (4) If an object is hit by this ray, do surface shading, and mix into the final color. REFRACTION IN RAY TRACING (1) Shoot ray (V, W1) to find the point P1 on the surface and surface normal N1. (2) Use Snell's law sin(θ1)/sin(θ2) = n2/n1 to find direction W2 of the refracting ray into the object: C1 = (N1W1) * N1 S1 = W1 - C1 S2 = n1/n2 * S1 C2 = - √1-S22 * N1 W2 = C2 + S2 (3) Shoot ray (P1 + ε W2, W2) to find the point P2 where the ray exits the object. If the object is a sphere, you will need to find the second of its two quadratic roots. (4) Repeat the refraction calculation. This time use the normal N2 at P2, and reverse n1 and n2. C3 = (N2W2) * N2 S3 = W2 - C3 S4 = n2/n1 * S3 C4 = √1-S42 * N2 W3 = C4 + S4 (5) Shoot ray (P2 + ε W3, W3) into the scene. (6) If an object is hit by this ray, do surface shading, and mix into the final color. RAY TRACING TO THE INTERSECTING OF TWO OBJECTS Suppose we want to find the intersection C of two objects A and B along ray (V,W). Suppose also that the ray enters A at t = Ain and exits A at t = Aout. Similarly, suppose the ray enters B at t = Bin and exits B at t = Bout. The intersection C of the two objects along the ray is then given by the interval: Cin = max(Ain, Bin) Cout = min(Aout, Bout) If Cin > Cout, then the ray has missed C. RAY TRACING TO A PLANE The equation of plane L = [a,b,c,d] is given by: ax + by + cz + d = 0 In other words, plane L consists of all points (x,y,z) for which ax + by + cz + d = 0 To intersect ray V+tW with plane L, we just need to substitute variables: a(Vx + t Wx) + b(Vy + t Wy) + c(Vz + t Wz) + d = 0 When we represent V and W as 1x4 column vectors: Vx Wx Vy Wy Vz Wz 1 0 it becomes clear that the intersection of the ray and the plane can be expressed as: LV + t LW = 0 From this we can see that we can solve for t by: t = -LV / LW Suppose we are using L to describe the boundary of a half space. Then: If LW < 0 then the ray is entering the half space. If LW > 0 then the ray is exiting the half space. RAY TRACING TO A TRIANGLE We can find out whether any ray intersects a triangle by first precomputing four plane equations. The first plane contains the triangle's three vertices A,B,C. (1) Normalize the cross product (B-A) x (C-B) to get N0. (2) Plane L0 = [ N0x, N0y, N0z, -AN0 ] Then for edge A,B we can compute plane L1 as follows: (1) Normalize the cross product (B-A) x N0 to get N1. (2) Plane L1 = [ N1x, N1y, N1z, -AN1 ] Similarly we can use edges B,C and C,A to compute planes L2 and L3. Now we have everything we need to determine whether any ray V+tW intersects the triangle: (1) Compute point P where ray V+tW intersects L0 (2) P is inside the triangle iff L1P < 0 and L2P < 0 L3P < 0 RAY TRACING TO A GENERAL QUADRATIC SURFACE We are going to go over this topic next week in more detail, so I am going to hold off for now on notes about this. NOTES ABOUT MATRICES THE 16 VALUES OF A 4x4 MATRIX CAN BE STORED AS AN ARRAY IN COLUMN-MAJOR FORMAT 0 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15 YOU CAN USE A MATRIX TO TRANSFORM A VECTOR: B = M A
1x4 | 4x4 | 1x4 | |||
---|---|---|---|---|---|
ax+by+cz+dw ex+fy+gz+hw ix+jy+kz+lw mx+ny+oz+pw | ⟵ | a b c d e f g h i j k l m n o p | | x y z w |