250 likes | 463 Views
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 ()
E N D
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() 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();
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
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 )
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; }
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
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
Space Partition • Uniform
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
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
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
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; } } }
Octree • KD-Tree
Ray-Box intersection txmax txmin tymax tymax txmax tymin tymin txmin
ac: center of box ai: normalized side direction of box hi: positive half length of box
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
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
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.
Lit light dL pixel dhit eye p dL≤dhit , not under shadow
Under shadow light dL eye dhit dL> dhit, under shadow
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