Introduction to 3D Graphics Lecture 5: From Realism to Real-Time Anthony Steed University College London
Overview n Projecting –As an optimisation of ray-casting –New issues and problems n Clipping Polygons –2D –3D n Visibility Culling
Reflection on Ray Casting n The process of casting rays is very slow –E.G. for a cow we have 10,000 triangles, but to render a 1000x1000 pixel image we have primary rays to cast (and then reflections, shadow rays, etc…) n Exploit point coherency –If ray through pixel (x,y) hits triangle T, then rays through adjacent pixels will likely hit the same triangle
Think Optimisation n But although point coherency is useful, we can easily enumerate ALL the rays that hit the triangle –Find which rays would hit the vertices of the triangle –These rays are defined by a 2D triangle on the image plane –Scan this 2D triangle out
COP
Why Triangles? n In general for an object representation (bezier, CSG) is it far from easy to find the 2D projection of the shape
Projection Problems (1) n Projecting the vertices –Needs to be done efficiently n Clipping to the view volume –Triangle might lie partially or totally off the screen n Visible surface determination –We now have to iterate through all the objects to see if they are visible.
Projection Problems (2) n Lighting –Now we have to re-construct the lighting across the screen n Depth test determination –Have to cope with overlapping and potential “difficult” coverage relations n Shadows and Inter-Reflections –Much more complicated
Full camera specification n We have already seen: –VRP, VPN, VUV, COP, view plane window n Some more parameters: –Viewplane Distance –Type of projection n Perspective – all rays converge to the COP n Parallel – parallel rays from points in the scene (DOP) n Front and back clip planes
Full Camera V N VRP=O COP VP truncated View Volume view window front clipping plane back clipping plane
Perspective Projection n In order to get a natural looking image we need the perspective n For a simple arrangement it is easy to find the projection COP C Q P A O viewplane Z Y
Canonical Frames n We use these as intermediate stages from which we know how to proceed n Canonical Perspective: –Cop at (0,0,-1) –Viewplane coincident with U-V plane –Viewplane window bounded by –1 to +1 –p’ = (x/z+1, y/z+1, 0) View volume
Canonical Parallel n Orthographic parallel projection n Direction of projection (DOP) is (0,0,-1) n View volume bounded by –1 and +1 on U and V n And by 0 and 1 on the N axis n p’ = (x, y, 0) V + DOP View volume
From General Perspective to Canonical Parallel (Projection Space) +
Perspective to Canonical Perspective n We will apply a set of transformation matrices (4 in total) n Each one ‘corrects’ a particular aspect of the projection n Then we put them all together to get one matrix
Step 1: Move the View Plane to the UV Plane (n = 0) cop View plane d cop V N View plane V N
Step 2: Translate the COP so that it Lies on the N axis View plane V N View plane V N cop
Step 3: Change the View Volume into a Regular Pyramid N View plane V N cop V D D Where D = d – c z dx = x 2 – x 1 dy = y 2 – y 1 px = x 2 + x 1 py = y 2 + y 1
Step 4: Scale by 1/D N V cop 1 1 N V D D
Multiplying Steps 1-4
Canonical Perspective To Canonical Parallel n Projection space –Divide through by Z n Incorporation of front and back clipping planes (canonical parallel) + Dmin Dmax + 0 1
Projection Space n A use for the right hand column! –x maps to x/z+1
Incorporate Front and Back Clipping Planes n (Actually this step occurs before the previous one)
The composite matrix T = MQ
Overview n Projecting –As an optimisation of ray-casting –New issues and problems n Clipping Polygons –2D –3D n Visibility Culling
Clipping Problem n Once we start projecting polygons we have to cope with cases where only some of the vertices project to the view window
Sutherland-Hodgman Algorithm(2D) n Clip the polygon against each boundary of the clip region successively n Result is possibly NUL if polygon is outside n Can be generalised to work for any polygonal clip region, not just rectangular Clip to top Clip to right etc
Clipping to a Region Boundary n To find the new polygon –iterate through each of the polygon edges and construct a new sequence of points –starting with an empty sequence –for each edge there are 4 possible cases to consider clip region right clip boundary P0P0 P1P1 P2P2 P3P3
Clipping the Polygon Edge n Given an edge P 0,P 1 we have 4 case. It can be: –entering the clip region, add P and P 1 –leaving the region, add only P –entirely outside, do nothing –entirely inside, add only P1 n Where P is the point of intersection Visible side of boundary p 0 p 1 p 0 p 0 p 0 p 1 p 1 p 1 p p OUT IN
Wrapping Up n We can determine which of the 4 cases and also the point of intersection with just if statements n To sum it up, an example: P0P0 P1P1 P2P2 P3P3 P0P0 PaPa PbPb P3P3
Weiler-Atherton Algorithm n When we have non-convex polygons then the algorithm above might produce polygons with coincident edges n This is fine for rendering but maybe not for other applications (eg shadows) n The Weiler-Atherton algorithm produces separate polygons for each visible fragment
Weiler-Atherton Algorithm a b c d ab c d A B ij k l clip region polygon loop of polygon vertices loop of region vertices
After Inserting the first Intersection Vertex i a b c d i l k j ab c d A B ij k l clip region polygon
After Insertion of all Intersection Vertices a b c d i l k j ab c d A B ij k l clip region polygon
Collect Your Polygons n Traverse the vertex list from the top n When the intersection point is an exit switch to traversing the clip boundary n When the intersection point is an entry switch to traversing the polygon vertices
Clipping polygons in 3D n The Sutherland-Hodgman can easily be extended to 3D –the clipping boundaries are 6 planes instead of 4 lines –intersection calculation is done by comparing an edge to a plane instead of edge to edge n It can either be done in Projection Space or in Canonical Perspective
Clipping in Projection Space n The view volume is defined by:
Clipping in Canonical Perspective n When we have an edge that extends from the front to behind the COP, then if we perform the clipping after the projection of the points (which in effect is what the PS does) we might get wrong results V N COP O p1 p2 q1 q2 top bottom View plane projected external lines
Clipping in Homogeneous Co-Ordinates n The Sutherland-Hodgman can also be used for clipping in 4D before dividing the points by the w n This can have the advantage that is even more general, it even allows for the front clip plane to be behind the COP
Overview n Projecting –As an optimisation of ray-casting –New issues and problems n Clipping Polygons –2D –3D n Visibility Culling
Visibility Problem n Not all objects are visible –In ray-tracing only rays inside the view frustum could be created n Not all polygons are visible –Visibility “solved” by default by ray-tracing because of object intersection test n Visibility ordering is a problem –Ray-tracing “solved” this problem implicitly because closest ray intersection point was chosen
Object Culling n Any object outside the view frustum can be discarded –Bounding sphere/plane test is common n Remaining objects must be sent to clipping process
Back Face Culling n Polygons facing away from the COP can be discarded –Often over-ridden to model thin walled objects COP
List Priority Algorithms n Must sort the objects (or polygons or pixels) n Commonest approach is to sort the objects on their distance from the COP (z-sort)
Order of Polygons n For a single closed polyhedron ANY order of polygon drawing is correct n With multiple overlapping objects z-sort the individual polygons –Special cases galore! n In general these techniques are not sufficient, and we will buffer individual pixels with a z-buffer
Conclusion n We have moved a considerable way from realism to real-time –Reduced a 3D problems to a 2D problem (via a 4D problem!) n Remaining issues –Filling polygons to cope with local illumination and visibility