1 / 25

Recursive Ray Tracing

Recursive Ray Tracing. 黃聰賢. Overview. Program Framework Generate Eye Ray Trace Ray Get Nearest Intersection Ray-Triangle Intersection Space Partition Ray-Box Intersection Visibility Lighting. Framework. Model view: only glLoadIdentity ()

denim
Télécharger la présentation

Recursive Ray Tracing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursive Ray Tracing 黃聰賢

  2. Overview • Program Framework • Generate Eye Ray • Trace Ray • Get Nearest Intersection • Ray-Triangle Intersection • Space Partition • Ray-Box Intersection • Visibility • Lighting

  3. Framework Model view: only glLoadIdentity() Projection: use glLoadIdentity() and glOrtho(0, screen_width, 0, screen_height, -1, 1) Viewport: the screen size For i from 0 to screen_width-1 For j from 0 to screen_height-1 vec3 rayDir = GenerateEyeRay(i, j); vec3 pixelColor = TraceRay(eye, rayDir, maxDepth) glBegin(GL_POINTS); glColor3fv(pixelColor.rgb); glVertex3i(i,j, 0); glEnd();

  4. Generate Eye Ray • Input • Eye • position, direction, up direction • The index of the pixel in the screen • (i, j) • The screen resolution • screen_width, screen_height • Projection setting • near, right, left, top, bottom • Output • Ray Direction

  5. pixel(i,j) Top i top H Right j near right (0,0) Ray up eye direction W (W/2,H/2) R2 Screen Resolution : W * H R3 Frustum near, right, top eye Right = normalize(eye direction × up direction) Top = normalize(Right ×eye direction) (P.S. top ≠ up) Raydirection = normalize (near * normalize(eye direction) +[(i-W/2)/(W/2)]*right*Right +[(j-H/2)/(H/2)]*top*Top )

  6. Trace Ray TraceRay(start, direction, depth) { if(depth ≤ 0) return black; Point p = GetNearestIntersection(start, direction) if(no intersection) return background_color; vec3 color = DirectLighting(p) * k_local; Compute reflection direction r color += TraceRay(p, r, depth-1) * k_reflection; Compute refraction direction t color += TraceRay(p, t, depth-1) * k_refraction; return color; }

  7. Reflection and Refraction • Reflection • R = 2 (N‧(-Ray))N + Ray • Refraction • For cosθ1>0 • T = (n1/n2)Ray – (n1cosθ1/n2-cosθ2)N • Otherwise • T = (n1/n2)Ray + (n1cosθ1/n2-cosθ2)N

  8. Get Nearest Intersection Input : Ray r (start position and ray direction) Output : Nearest intersection point p Point hitPoint; float minDistance = FLT_MAX; For each face f { float distance = RayTriangle(&f, &r, &hitPoint); if(distance < minDistance && distance > ε) { minDistance = distance; p = hitPoint; } } ε = 10^-5

  9. Ray-Triangle Intersection => =>

  10. =>

  11. Space Partition • Uniform

  12. Grid Traversal Algorithm • Ray • o+td • float t, vec3 o,d; (d is unit vector.) • Grid Index • int X, Y • The step to the next grid index • intstepX, stepY; (-1 or 1) • The value of t when the ray will cross boundary • float tMaxX, tMaxY • The distance for the ray to cross to two parallel boundaries • float tDeltaX, tDeltaY

  13. tMaxX tDeltaX d tMaxY tDeltaY O First voxel tDeltaX= GridWidth/abs(d.x); tDeltaY= GridHeigth/abs(d.y); d.x>0, stepX =1 d.y>0, stepY=1

  14. 2D 1 loop { if(tMaxX < tMaxY) { tMaxX= tMaxX + tDeltaX; X= X + stepX; } else { tMaxY= tMaxY + tDeltaY; Y= Y + stepY; } NextVoxel(X,Y); } tMaxX tMaxY 2 tMaxX tMaxY 3 tMaxX tMaxY

  15. 3D loop{ if(tMaxX < tMaxY) { if(tMaxX < tMaxZ) { X= X + stepX; tMaxX= tMaxX + tDeltaX; } else { Z= Z + stepZ; tMaxZ= tMaxZ + tDeltaZ; } } else { if(tMaxY < tMaxZ) { Y= Y + stepY; tMaxY= tMaxY + tDeltaY; } else { Z= Z + stepZ; tMaxZ= tMaxZ + tDeltaZ; } } }

  16. Octree • KD-Tree

  17. Ray-Box intersection txmax txmin tymax tymax txmax tymin tymin txmin

  18. ac: center of box ai: normalized side direction of box hi: positive half length of box

  19. Is it really the nearest point? • Keep tracing if the intersection point is not in the current grid. • Check t ≤ min(tMaxX, tMaxY, tMaxZ) • t : the distance to intersection point • min(tMaxX, tMaxY, tMaxZ) : distance to the next boundary

  20. Avoid Redundant Test • Add an integer variable on ray and faces • It is called “Ray Id”. • The initial values of all faces are zero. • Compare the id of ray and face before test if(ray.id == face.rayId) skip test else test and update face.rayId

  21. Shadow • Shoot a ray to the light and try to get the distance to the nearest intersection point. • If the distance > the distance to the light,add the lighting effect • Space partition can speed up the computation.

  22. Lit light dL pixel dhit eye p dL≤dhit , not under shadow

  23. Under shadow light dL eye dhit dL> dhit, under shadow

  24. Direct Lighting • Use Phong model without ambient lighting. • Add local lighting if it is not under shadow. I * (Kd * dot(N, L) + Ks * pow(dot(E, R), Ns) ) N E L R

More Related