Visible Surface Detection Shmuel Wimer Bar Ilan Univ., Eng. Faculty Technion, EE Faculty May 2012 1 Back-Face Detection A point x, y, z is behind a polygon surface if Ax By Cz D 0, where A, B, C and D are the plane parameters of the polygon. Test is simplified by considering normal of face and viewing direction. N A, B, C Vview A polygon is back face if Vview N 0. In a right-handed viewing system viewing direction is along negative z axis, Vview 0, 0, 1 . Then, a polygon is a back face if C 0. May 2012 2 Depth-Buffer Methods S3 S2 S1 view plane yv xv x, y zv Three surfaces overlapping pixel position (x,y) on the view plane. The visible surface, S1, has the smallest depth value. May 2012 3 Z - Buffer Algorithm Initialize each pixel ( x, y ) of depth buffer and frame buffer (color): depthBuf x, y 1.0; // Z is normalized to [0,1.0] frameBuf x, y background color; // initialize to background for each polygon { // traverse all polygons for each pixel x, y polygon { // rasterization if (z x, y depthBuf x, y ) { // check closer pixel get (compute) color x, y ; // compute color of pixel depthBuf x, y z x, y ; frameBuf x, y color x, y ; }}} May 2012 4 Efficient Depth Calculation Given the depth value in a vertex of a polygon, the depth of any other point in the plane containing the polygon can be calculated efficiently (additions only). Depth is calculated from plane equatuion z Ax By D C . At rasterization x along scan-line changes by 1. Therefore, z x 1 A x 1 By D C z A C , and the ratio A C is constant for the entire polygon. May 2012 5 Scanning can start at the top vertex of polygon and obtain the first depth value. Progressing from scan line to next one y is decreased by 1, which eases the depth calculation of first pixel along the scan line. top scan line y scan line y+1 scan line x May 2012 x` 6 If m is the slope of the edge then x x 1 m . Therefore the depth at first pizel in new scan line is z z A m B C . All values of 1 m can be stored in a lookup table (memory). A m B C is calculated once for the entire edge, and A C and B C calculated once for the entire polygon. If the edge is vertical the slope is infinite and z z B C. The starting point of scan line can use mid-point method or Bernsham-type algorithm. May 2012 7 A-Buffer Method background opaque surface foreground transparent surface It is named so since z-buffer is used for depth semantics and in some sense a-buffer has the nature of the other end of alphabet. It is an extension using antialiasing, area-averaging visibility detection method, supporting transparency. May 2012 8 Ray Tracing opaque opaque transparent Projection reference point May 2012 9 Highly realistic. It detects visibility, transparency, shadows, illumination effects, and generates perspective views. Computational intensive. May 2012 10 Binary Ray-Tracing Tree R4 S3 transparent S4 opaque R3 T3 R1 R2 S2 opaque T1 S1 R1 T1 S3 S1 transparent R3 Projection reference point S2 T3 R2 S4 R4 May 2012 11 Left branches represent reflection paths, right branches represent transmission paths. A path is terminated if any of the following occurs: The ray intersects no surface. The ray intersects a light source that is not a reflecting surface. The tree reached a depth limit. May 2012 12 R L Ν u – incoming ray u H R – unit reflected ray N – unit surface normal L – unit vector pointing to light source H – unit vector half way between –u and L May 2012 13 The paths along the direction of L is referred as the shadow ray. source. If any object intersects the shadow ray between the surface and the point light source, the surface position is in shadow with respect to that source. For reflection path there exists R u 2 u N N, hence, R u 2u N N. May 2012 14 In transparent surface light is transmitted through material. The unit transmission vector T is obtained from u and N and refraction indices. i i T u cos r cos i N r r r T N u May 2012 i cosr 1 i r 1 cos2 i 2 15 Calculating Pixel Intensity It works bottom up, where the intensity at a child node is added to parent with an attenuation relative to the distance of the child’s surface from the parent’s surface. The intensity assigned to the pixel is the sum of attenuated intensities at the root. If the primary ray for a pixel doesn’t intersect with an object of the scene, the ray-tracing tree is empty and the pixel is assigned the background intensity. May 2012 16 Ray-Surface Intersection Calculations projection reference point pixel point y P0 u x z A point along the ray at distance s from P0 is given by P P0 su. Unit vector along initial ray path is u Ppixel P0 | Ppixel P0 |. At each surface intersection P0 and u are updated acoording to R and T. May 2012 17 Ray-tracing is computations intensive, requiring root finding to get the value of parameter s at ray-surface incidence point. Efficient techniques exists for spherical, planar and also spline surfaces. P y P0 r u Pc Ray-Sphere Intersections x z | P Pc |2 r 2 0 | P0 su Pc |2 r 2 0. May 2012 18 Set Pc P0 ΔP, then s 2 2 u ΔP s | ΔP |2 r 2 0. At intersection there exists s u ΔP 2 2 u ΔP | ΔP | r . 2 If the discriminant is negative then either the ray doesn’t intersect the sphere or the sphere is behind P0. In either case the sphere is eliminated from further consideration. May 2012 19 s u ΔP 2 2 u ΔP | ΔP | r is susceptible to 2 round-off error if the sphere is small and very far, since r 2 2 | ΔP | . This can be avoided in most cases by the rearrangement s u ΔP r | ΔP u ΔP u | , 2 2 yielding two terms of same order of magnitude. May 2012 20 Ray-Polyhedron Intersections Computing intersection with polyhedron is expensive. It is useful to include polyhedron in the smallest enclosing sphere and exclude any intersection calculation if the ray doesn’t intersect the sphere. Front faces of the polyhedron are identified first by u N 0. For those we find first the intersection point of the ray with D N P0 face's plane N P D N P0 su D s N u May 2012 21 To find whether the point is within the face an oddeven test is in order. Among all the faces intersected by the ray, the one with the smallest s is chosen. If there’s no intersection with any face, the polyhedron is excluded from further consideration. May 2012 22 Reducing Intersection Calculations Ray tracer consumes 95% time on intersection calculations. It is possible to enclose groups of adjacent objects in spheres, calculate intersection with enclosing sphere first, and proceed to real objects only if intersection exists. Another approach is space-subdivision, where the entire scene is enclosed with a cube. The cube is successively divided into smaller cubes until the number of spaces contained in a cube exceeds a predefined limit or the size of cube reaches some threshold. May 2012 23 pixel ray Cells are traced through the individual cells. intersection tests are performed only for cells containing surfaces. The first intersected surface is the visible for that ray. May 2012 24 N2 u Pin N1 Pout N1 1, 0, 0 N 2 0, 1, 0 N3 0, 0, 1 N3 Given a unit vector u of the ray and the entry point Pin , the candidate faces of exit point are those satisfying u Nk 0. The exit point satisfies Pout,k Pin sk u, sk is the distance along the ray from Pin to Pout,k . Exit point satisfies Nk Pout,k Dk . May 2012 25 Solving for sk yields sk Dk N k Pin N k u . The smallest value of sk defines the exit face. Notice that other 2 intersection must occure outside the cube since cube is convex. It is a good practice to look at the largest component of u and start with the face on the perpendicular plane. May 2012 26 Consider the cube plane attempted 6 for exit point calculation. The cube 1 face divides the plane into nine sectors. If the exit point is falling in 5 2 sector 0 we are done. If it is falling in any of 1, 3, 2 or 4 we know the 0 4 7 face containing the exit point and 3 then can find it. In case of 5, 6, 7 and 8 further test is required. May 2012 8 27