Clipping, Culling, Picking & Selection Trivial Rejection Outcode Clipping Plane-at-a-time Clipping Backface Culling Picking Selection Programming Assignment #2 Lecture 9 Comp 236 Spring 2005 What is Clipping? 1. Clipping is a procedure for spatially partitioning geometric primitives, according to their containment within some region. Clipping can be used to: Distinguish whether geometric primitives are inside or outside of a viewing frustum Distinguish whether geometric primitives are inside or outside of a picking frustum Detecting intersections between primitives 2/17/2005 Lecture 10 2 What is Clipping? 2. Clipping is a procedure for subdividing geometric primitives. This view of clipping admits several other potential applications. Binning geometric primitives into spatial data structures. Computing analytical shadows. Computing intersections between primitives Determining visibility Clipping is an important optimization Why do we Clip? Clipping is a visibility preprocess. In real-world scenes clipping can remove a substantial percentage of the environment from consideration. Assures that only potentially visible primitives are rasterized. What advantage does this have over twodimensional clipping. Are there cases where you have to clip?
Where do we Clip? There are at least 3 different stages in the rendering pipeline where we do various forms of clipping. In the trivial rejection stage we remove objects that can not be seen. The clipping stage removes objects and parts of objects that fall outside of the viewing frustum. And, when rasterizing, clipping is used to remove parts of objects outside of the view port. xmin = (int) (v[sort[xflag][0]].x); xmax =(int) (v[sort[xflag][1]].x + 1); ymin = (int) (v[sort[yflag][1]].y); ymax = (int) (v[sort[yflag][0]].y + 1); /* clip triangle s bounding box to raster */ xmin = (xmin < 0)? 0 : xmin; xmax = (xmax >= width)? width - 1 : xmax; ymin = (ymin < 0)? 0 : ymin; ymax = (ymax >= height)? height - 1 : ymax; 2/17/2005 Lecture 10 5 Trivial Rejection Clipping One of the keys to all clipping algorithms is the notion of half-space partitioning. We ve seen this before when we discussed how edge equations partition the image plane into two regions, one negative the other non-negative. The same notion extends to 3-dimensions, but partitioning elements are planes rather than lines. The equation of a plane in point-normal form 3D is given as: x y z 1 [ n n n 0] d A vector dotted with a point. The vector is the plane s normal. The points of the plane are a fixed distance, d, from the origin, after projection onto the normal. x y z = 2/17/2005 Lecture 10 6 Trivial Rejection Clipping Here is a simple Example: If we orient a plane so that it passes through our viewing position and our look-at direction is aligned with its normal. Then we can easily partition objects into three classes, those behind our viewing frustum, those in front, and those that are partially in both half-spaces. Outcode Clipping (a.k.a. Cohen-Sutherland Clipping) The extension of plane partitioning to multiple planes, gives a simple form of clipping called Cohen-Sutherland Clipping. This is a rough approach to clipping in that it only classifies each of it s input primitives, rather than forces them to conform to the viewing window. A simple 2-D example is shown on the right. This technique classifies each vertex of a primitive, by generating an outcode. An outcode identifies the appropriate half space location of each vertex relative to all of the clipping planes. Outcodes are usually stored as bit vectors. By comparing the bit vectors from all of the vertices associated with a primitive we can develop conclusions about the whole primitive.
Outcode Clipping of Lines First, let s consider line segments. if (outcode1 == 0) and (outcode2 == 0) : line segment is inside if ((outcode1 & outcode2) == 0): line segment potentially crosses clip region line is entirely outside of clip region Outcode Clipping of Triangles For triangles we modify the tests so all vertices are considered: if (outcode1 == 0) and (outcode2 == 0) and (outcode3 == 0): triangle is inside if ((outcode1 & outcode2 & outcode3) == 0): line segment potentially crosses clip region line is entirely outside of clip region Notice that the test cannot conclusively state whether the segment crosses the clip region. This might cause some segments that are located entirely outside of the clipping volume to be subsequently processed. This form of clipping is not limited to triangles or convex polygons. Is is simple to implement. But, it won t handle all of our problems... 2/17/2005 Lecture 10 9 2/17/2005 Lecture 10 10 Dealing with Crossing Cases The hard part of clipping is handling objects and primitives that straddle clipping planes. In some cases we can ignore these problems because the combination of screen-space clipping and outcode clipping will handle most cases. However, there is one case in general that cannot be handled this way. This is the case when parts of a primitive lies both in front of and behind the viewpoint. This complication is caused by our projection stage. It has the nasty habit of mapping objects behind the viewpoint to positions in front of it. One-Plane-at-a-Time Clipping (a.k.a. Sutherland-Hodgeman Clipping) The Sutherland-Hodgeman triangle clipping algorithm uses a divide-andconquer strategy. It first solves the simple problem of clipping a triangle against a single plane. There are four possible relationships that a triangle can have relative to a clipping plane as shown in the figures on the right. Each of the clipping planes are applied in succession to every triangle. There is minimal storage requirements for this algorithm, and it is well suited to pipelining. As a result it is often used in hardware implementations.
Plane-at-a-Time Clipping The Trick: Interpolating Parameters The results of Sutherland-Hodgeman clipping can get complicated very quickly once multiple clip-planes are considered. However, the algorithm is still very simple. Each clip plane is treated independently, and each traingle is treated by on of the four cases mentioned previously. It is straightforward to extend this algorithm to 3D. The complication of clipping is computing the new vertices. This process is greatly simplified by using NDC. We mentioned last lecture that it is often desirable to introduce an intermediate coordinate frame in-between eye-space and the final projection stage (i.e. the dividing through by w). In this space the viewable region is mapped into a volume that ranges from - 1 to +1 in all dimensions after projection. This space has several advantages. It simplifies the clipping test (all dimensions are compared against the w component of the vertex) and it is the perfect place in the pipeline to transition from a floating-point to a fixed-point representation. It also simplifies the interpolation of the new vertex positions and triangle parameters as shown in the figure. 2/17/2005 Lecture 10 13 2/17/2005 Lecture 10 14 Recap of Plane-at-a-time Clipping Advantages: Elegant (few special cases) Robust (handles boundary and edge conditions well) Well suited to hardware Canonical clipping makes fixed-point implementations manageable Disadvantages: Hard to write as clean code (Reentrant, objects spawn new short-lived objects) Only works for convex clipping volumes Often generates more than the minimum number of triangles needed Requires a divide per clipped edge Alternatives to Plane-at-a-time Over the years there have been several improvements to plane-at-a-time clipping. Clipping against concave volumes (Wieler-Atherton clipping) Can clip arbitrary polygons against arbitrary polygons Maintains more state than plane-at-a-time clipping Handle all planes at once (Nicholle-Lee-Nicholle clipping) It waits before generating triangles to reduce the number of clip sections generated. Tracks polygon through the 27 sub-regions relative to the clip volume Might need to generate a "corner vertex"
2-D/3-D/Outcode Clipping Hybrids Do we really need to implement 6 clipping planes? The trend is to do less and less clipping! If rasterization is limited to a bounding box it is easy to see how only near and far clipping are needed. Olano showed that it is possible to even eliminate this case! Back-Face Culling Back-face culling addresses a special case of occlusion called convex self-occlusion. Basically, if an object is closed (having a well defined inside and outside) then some parts of the outer surface must be blocked by other parts of the same surface. We ll be more precise with our definitions in a minute. On such surfaces we need only consider the normals of surface elements to determine if they are visible. 2/17/2005 Lecture 10 17 2/17/2005 Lecture 10 18 Manifold We can apply back-face culling to any orientable two-manifold. Orientable two-manifolds have the following properties. 1. Everywhere on their surface are locally like a plane. They have no holes, cracks, or self-intersections. 2. Their boundary partitions 3D space into interior and exterior regions In our case, manifolds will be composite objects made of many primitives, generally triangles. Back-face culling eliminates a subset of these primitives. Assumes that you are outside of all objects. Removing Back-Faces Idea: Compare the normal of each face with the viewing direction Given n, the outward-pointing normal of F foreach face F of object if (n. v> 0) throw away the face Does it work?
Fixing the Problem We can t do view direction clipping just anywhere! Culling Technique #2 Detect a change in screen-space orientation. If all face vertices are ordered in a consistent way, back-facing primitives can be found by detecting a reversal in this order. One choice is a counterclockwise ordering when viewed from outside of the manifold. This is consistent with computing face normals (Why?). If, after projection, we ever see a clockwise face, it must be back facing. Downside: Projection comes fairly late in the pipeline. It would be nice to cull objects sooner. Upside: Computing the dot product is simpler. You need only look at the sign of the plane s z component. 2/17/2005 Lecture 10 21 This approach will work for all cases, but it comes even later in the pipe, at triangle setup. We already do this calculation in our triangle rasterizer. It is equivalent to determining a triangle with negative area. 2/17/2005 Lecture 10 22 Culling Plane-Test Here is a culling test that will work anywhere in the pipeline. Remove faces that have the eye in their negative half-space. This requires computing a plane equation for each face considered. x y z 1 [ n n n 0] d 0 x y z = Culling Plane-Test Once we have the plane equation, we substitute the coordinate of the viewing point (the eye coordinate in our viewing matrix). If it is negative, then the surface is back-facing. Example: We will still need to compute the normal (How?). But, we don t have to normalize it. (How do we go about computing a value for d?)
Back Face Culling in OpenGL Back Face Culling is available as a mode setting in OpenGL. very flexible (can cull fronts as well as backs) it can double your performance! depends on consistent models Models should be a closed manifold Clipping will expose the culling if (cull): gldisable(gl_cull_face) glfrontface(gl_ccw) glenable(gl_cull_face) glcullface(gl_back) # define front faces # enable Culling # tell which faces to cull 2/17/2005 Lecture 10 25 Picking Picking is a user interface technique that allows the user to get access to all geometry within some picking region on the screen Picking is a variant of Clipping, it defines a small viewport, and all geometry that survives culling and has a fragment within the window is reported back in a Select Buffer to the user. The user can then go through this list to decide which geometry was intended by the application. Basics set-up: Set up a feedback display buffer Set rendering mode to selection Set up a clipping viewport with glupickmatrix() Render the scene See what primitives fell within the pick window 2/17/2005 Lecture 10 26 Picking Fragments To set up the Selection buffer: glselectbuffer(buffersize, intarray) Set the rendering mode (default is GL_RENDER) glrendermode(gl_select) Set the clipping viewport on the PROJECTION matrix glmatrixmode(gl_projection) glpushmatrix() glgetfloatv(gl_projection_matrix, projection) glgetintegerv(gl_viewport, viewport) glloadidentity() glupickmatrix(x, height - y, width, height, viewport) glmultmatrixd(projection) glmatrixmode(gl_modelview) glinitnames() Render scene using the Name Stack Rendering for Picking def display(): glclearcolor(0, 0, 0, 1) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT) setcamera() glloadname(1) drawfloor() glloadname(2) drawcow() glloadname(3) drawbet() glflush() glutswapbuffers() frame += 1 The Names will appear in the selection buffer in the order they are drawn, regardless of whether they are visible (assuming they fall in the pick window) An entire OpenGL Name stack to support hierarchy in picking with functions: glpushname(intval) glpopname() glloadname(intval)
Selecting Selection is an alternative interaction paradigm in which a user is restricted to choosing only objects that are visible from the current view Depends on Double-buffering On a mousedown() event the application re-renders the scene into the back buffer as a second image where each object is colored with a unique objectid Then use glreadpixels() to read the single pixel at the reported mousedown() Position. The back-buffer is never displayed def unmunge(r, g, b): x = 0 for i in xrange(8): x = (x << 1) + (b & 1) b >>= 1 x = (x << 1) + (g & 1) g >>= 1 x = (x << 1) + (r & 1) r >>= 1 return x Selection Code Fragments def munge(x): r, g, b = 0, 0, 0 for i in xrange(8): r = (r << 1) + (x & 1) x >>= 1 g = (g << 1) + (x & 1) x >>= 1 b = (b << 1) + (x & 1) x >>= 1 r = r/float(255) g = g/float(255) b = b/float(255) return (r, g, b) if (selectmode): gldisable(gl_lighting) r, g, b = munge(32) glcolor3d(r, g, b) drawframe(5) frontcolor = [0.8, 0.2, 0.9, 1.0] glenable(gl_lighting) glmaterialfv(gl_front, GL_AMBIENT, frontcolor) glmaterialfv(gl_front, GL_DIFFUSE, frontcolor) 2/17/2005 Lecture 10 29 2/17/2005 Lecture 10 30 More Code Fragments A simpler, faster, but less colorful munger def munge(x): r = (x & 255)/float(255) g = ((x >> 8) & 255)/float(255) b = ((x >> 16) & 255)/float(255) return (r, g, b) def unmunge(r, g, b): return (r + (g << 8) + (b << 16)) if (selectmode): gldisable(gl_lighting) r, g, b = munge(32) glcolor3d(r, g, b) drawframe(5) frontcolor = [0.8, 0.2, 0.9, 1.0] glenable(gl_lighting) glmaterialfv(gl_front, GL_AMBIENT, frontcolor) glmaterialfv(gl_front, GL_DIFFUSE, frontcolor) def display(): global frame if (selectmode): glclearcolor(0, 0, 0, 1) glclearcolor(0, 0.6, 0.8, 1) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT) setcamera() drawfloor() drawcow() drawbet() glflush() if (not selectmode): glutswapbuffers() frame += 1 Hiding Selection
Performing the Selection Picking vs Selection def onmousebutton(button, state, x, y): global selectmode, oldx, oldy y = height -y -1 if (button == GLUT_LEFT_BUTTON) and (state == GLUT_DOWN): selectmode = 1 display() glreadbuffer(gl_back) pixel = glreadpixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE) print "pixel =", unmunge(ord(pixel[0]),ord(pixel[1]),ord(pixel[2])) selectmode = 0 Picking specifies a region, Selection specify a point Picking gives you everything that falls within the window Selecting only provides the visible object Picking has lower latency (objects are not rasterized) and doesn t mess up a buffer Selection is often much easier to manage, and H/W is so fast that the latency is not an issue additional trick can be used to speed it up like setting and rendering into a sub-viewport (as picking did) Selection is more commonly used these days 2/17/2005 Lecture 10 33 2/17/2005 Lecture 10 34 Next Time