460 likes | 471 Views
This lecture explores optimization methods for real-time 3D graphics, addressing projection issues, polygon clipping, visibility culling, and more. Learn about techniques such as ray-casting optimization and the Sutherland-Hodgman algorithm for polygon clipping.
E N D
Introduction to 3D GraphicsLecture 5: From Realism to Real-Time Anthony Steed University College London
Overview • Projecting • As an optimisation of ray-casting • New issues and problems • Clipping Polygons • 2D • 3D • Visibility Culling
Reflection on Ray Casting • 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 10000000 primary rays to cast (and then reflections, shadow rays, etc…) • 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 • 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
Why Triangles? • In general for an object representation (bezier, CSG) is it far from easy to find the 2D projection of the shape
Projection Problems (1) • Projecting the vertices • Needs to be done efficiently • Clipping to the view volume • Triangle might lie partially or totally off the screen • Visible surface determination • We now have to iterate through all the objects to see if they are visible.
Projection Problems (2) • Lighting • Now we have to re-construct the lighting across the screen • Depth test determination • Have to cope with overlapping and potential “difficult” coverage relations • Shadows and Inter-Reflections • Much more complicated
Full camera specification • We have already seen: • VRP, VPN, VUV, COP, view plane window • Some more parameters: • Viewplane Distance • Type of projection • Perspective – all rays converge to the COP • Parallel – parallel rays from points in the scene (DOP) • Front and back clip planes
Full Camera V truncated View Volume COP view window N VRP=O back clipping plane front clipping plane VP
Perspective Projection • In order to get a natural looking image we need the perspective • For a simple arrangement it is easy to find the projection Y P Q COP Z C A O viewplane
We use these as intermediate stages from which we know how to proceed 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) Canonical Frames View volume
Canonical Parallel • Orthographic parallel projection • Direction of projection (DOP) is (0,0,-1) • View volume bounded by –1 and +1 on U and V • And by 0 and 1 on the N axis • p’ = (x, y, 0) View volume V + DOP
From General Perspective to Canonical Parallel (Projection Space) +
Perspective to Canonical Perspective • We will apply a set of transformation matrices (4 in total) • Each one ‘corrects’ a particular aspect of the projection • Then we put them all together to get one matrix
V N View plane Step 1: Move the View Plane to the UV Plane (n = 0) V cop cop d N View plane
V N View plane Step 2: Translate the COP so that it Lies on the N axis V cop N cop View plane
V V D N N cop cop D View plane Step 3: Change the View Volume into a Regular Pyramid Where D = d – cz dx = x2 – x1 dy = y2 – y1 px = x2 + x1 py = y2 + y1
Step 4: Scale by 1/D V V 1 D N N cop cop 1 D
Canonical Perspective To Canonical Parallel • Projection space • Divide through by Z • Incorporation of front and back clipping planes (canonical parallel) + + Dmax 1 Dmin 0
Projection Space • A use for the right hand column! • x maps to x/z+1
Incorporate Front and Back Clipping Planes • (Actually this step occurs before the previous one)
Overview • Projecting • As an optimisation of ray-casting • New issues and problems • Clipping Polygons • 2D • 3D • Visibility Culling
Clipping Problem • 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) Clip to top • Clip the polygon against each boundary of the clip region successively • Result is possibly NUL if polygon is outside • Can be generalised to work for any polygonal clip region, not just rectangular Clip to right etc
P2 P1 P0 P3 Clipping to a Region Boundary right clip boundary • 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
p 1 p 0 p 1 p 0 p 1 p 0 p p 1 0 Clipping the Polygon Edge • Given an edge P0,P1 we have 4 case. It can be: • entering the clip region, add P and P1 • leaving the region, add only P • entirely outside, do nothing • entirely inside, add only P1 • Where P is the point of intersection V i s i b l e s i d e o f b o u n d a r y p p OUT IN
P1 P2 P0 P3 Wrapping Up • We can determine which of the 4 cases and also the point of intersection with just if statements • To sum it up, an example: Pa Pb P0 P3
Weiler-Atherton Algorithm • When we have non-convex polygons then the algorithm above might produce polygons with coincident edges • This is fine for rendering but maybe not for other applications (eg shadows) • The Weiler-Atherton algorithm produces separate polygons for each visible fragment
polygon 1 2 B 8 7 i j a b 0 9 6 k 5 A 4 l 3 c d clip region Weiler-Atherton Algorithm 0 1 a 2 3 b 4 5 c 6 7 d 8 loop of region vertices 9 loop of polygon vertices
polygon 1 2 B 8 7 i j a b 0 9 6 k 5 A 4 l 3 c d clip region After Inserting the first Intersection Vertex i 0 1 i a 2 3 l b 4 5 k c 6 7 j d 8 9
polygon 1 2 B 8 7 i j a b 0 9 6 k 5 A 4 l 3 c d clip region After Insertion of all Intersection Vertices 0 1 i a 2 3 l b 4 5 k c 6 7 j d 8 9
Collect Your Polygons • Traverse the vertex list from the top • When the intersection point is an exit switch to traversing the clip boundary • When the intersection point is an entry switch to traversing the polygon vertices
Clipping polygons in 3D • 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 • It can either be done in Projection Space or in Canonical Perspective
Clipping in Projection Space • The view volume is defined by:
Clipping in Canonical Perspective • 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 top View plane V + 1 p 2 p 1 q 1 projected external lines N O C O P q 2 - 1 bottom
Clipping in Homogeneous Co-Ordinates • The Sutherland-Hodgman can also be used for clipping in 4D before dividing the points by the w • This can have the advantage that is even more general, it even allows for the front clip plane to be behind the COP
Overview • Projecting • As an optimisation of ray-casting • New issues and problems • Clipping Polygons • 2D • 3D • Visibility Culling
Visibility Problem • Not all objects are visible • In ray-tracing only rays inside the view frustum could be created • Not all polygons are visible • Visibility “solved” by default by ray-tracing because of object intersection test • Visibility ordering is a problem • Ray-tracing “solved” this problem implicitly because closest ray intersection point was chosen
Object Culling • Any object outside the view frustum can be discarded • Bounding sphere/plane test is common • Remaining objects must be sent to clipping process
Back Face Culling • Polygons facing away from the COP can be discarded • Often over-ridden to model thin walled objects COP
List Priority Algorithms • Must sort the objects (or polygons or pixels) • Commonest approach is to sort the objects on their distance from the COP (z-sort)
Order of Polygons • For a single closed polyhedron ANY order of polygon drawing is correct • With multiple overlapping objects z-sort the individual polygons • Special cases galore! • In general these techniques are not sufficient, and we will buffer individual pixels with a z-buffer
Conclusion • We have moved a considerable way from realism to real-time • Reduced a 3D problems to a 2D problem (via a 4D problem!) • Remaining issues • Filling polygons to cope with local illumination and visibility