VISIBILITY & CULLING Don t draw what you can t see. Thomas Larsson, Afshin Ameri DVA338, Spring 2018, MDH
Visibility
Visibility Given a set of 3D objects, which surfaces are visible from a specific point of view? 3
What can t we see? Anything outside the view volume Anything occluded by another object Anything smaller than a pixel Anything in complete shadow 4
OPAQUE 5
Visibility What polygons are visible with respect to view frustum? Outside Frustum: View Frustum Culling, Clipping Inside Frustum: Hidden Surface Removal Backface culling Occlusion These are needed to avoid later expensive computations 6
Backface Culling 7
Backface Culling Do not draw polygons facing away from the viewer Front facing polygons will occlude these If the object is opaque And a closed polygon mesh 8
Where in the pipeline this test fits best? Find Backface Polygons Front facing polygons point towards the viewer Backface polygons point away from viewer If v is the vector to viewer then: n v 0 means that the primitive is backface 9
Find Backface Polygons In normalized device coordinates the test becomes very efficient since v = (0, 0, 1) So any normal with n z < 0 is a backface. Do not forget, this requires correct surface normal, and not the normal often used for special lighting effects. 10
OpenGL Backface Culling Does not use the above algorithm Works on the signed area of the projected primitive in screen space. This means that if vertices are organized in clockwise fashion then they are backfacing. 2 1 0 0 Front facing 1 Back facing 2 11
OpenGL Backface Culling Enable/Disable back-face removal with glenable(gl_cull_face)/gldisable(gl_cull_face) Select which polygons to remove with one of the following functions: glcullface(gl_back) glcullface(gl_front) glcullface(gl_front_and_back) The convention is to specify the faces so that the outward face is the front face. What if the camera happens to be inside the model. How would you set up the back-face removal? 12
Clipping 13
Clipping What about polygons piercing one or more frustum planes? Clipping is designed for them. Pixel-wise clipping is not a good idea, specially for large triangles Clipping happens just before rasterization Almost always we rely on the graphics system for that. 14
Polygon Clipping Given a polygon, find area of it within viewport. Usualy the viewport is axis-aligned rectangle. Number of vertices may be increased. Can result one or more polygons. 15
Sutherland-Hodgeman Clipping This is a divide-and-conquer algorithm Repeated clipping against half-space Walk around boundary of polygon Four cases for edge transition 16
Sutherland-Hodgeman Clipping This is a divide-and-conquer algorithm Repeated clipping against half-space Walk around boundary of polygon Four cases for edge transition in in: output next vertex in out: output intersection out out: output nothing out in: output intersection + next vertex 17
Clipping The method can be used for any convex clipping region Implementation issues: Care is needed to get it right We might need to create several separate polygons We might get very small areas along the edges Other algorithms: Cyrus-Beck Liang-Barsky & Weiler 18
Painter s Algorithm 19
Painter s Algorithm Assumption: Later projected polygons overwrite earlier ones. Idea: A painter creates a drawing by drawing background elements first. We can do the same and draw further objects first Need to sort polygons by depth. from Shirley 20
Visibility How to correctly preserve the 3D order of the objects in the rendering? 21
Painter s Algorithm Sort polygons by Z min and project those with higher Z min first z min z min z min z min 22
Painter s Algorithm Fails in cases of overlaps Might need to split polygons in order to work z min z min 23
Z-Buffer Algorithm 24
Visibility How to correctly preserve the 3D order of the objects in the rendering? 25
Z-Buffer Algorithm Draw every polygon the we can t reject (i.e. outside the view) If we find a pixel of a polygon that is closer to viewer than what we had drawn before, then overwrite it. It is an image-based (pixel-wise) hidden surface removal Upon rasterizing we need to store the z-value for each pixel as well and not only the color. Requires another buffer (memory) This buffer must have same pixel resolution as image buffer 26
DAM Entertainment Z-Buffer Color buffer Depth buffer 27
Z-Buffer Advantages Simple to implement in hardware It is natural part of primitive rasterization Requires no pre-processing, we can send the primitives to the pipeline in any order 28
Z-Buffer Disadvantages Memory requirements Imagine 32bits per depth value and HD resolution How much memory just for Z-Buffer? Accuracy problems Only considers visibility at discrete pixels => aliasing Depth buffer precision issue => Z-Fighting Potential inefficiency High depth-complexity scenes Might get many overdraws 29
Z-Buffer in OpenGL Tell GLUT that we need one glutinitdisplaymode(glut_double GLUT_RGB GLUT_DEPTH) Enable depth testing glenable(gl_depth_test) We need to reset it at every frame before drawing anything glclear(gl_depth_buffer_bit) Might need to change the comparison method gldepthfunc(gl_less) 30