CHAPTER 12 Height Maps, Hidden Surface Removal, Clipping and Level of Detail Algorithms © 2008 Cengage Learning EMEA LEARNING OBJECTIVES In this chapter you will learn about: – Height maps – Creating and rendering a height map – Hidden surface removal – The Z-buffer algorithm – The painter’s algorithm – Clipping – Two-dimensional line clipping – Cohen-Sutherland clipping – Liang-Barsky clipping – The clipping of polygons – Three-dimensional clipping – Level of Detail (LOD) algorithms HEIGHT MAPS Height maps, also called height fields, are predominantly used in computer games for realistic terrain generation. A height map is stored as a two-dimensional array consisting of x- and z-values. Each x- and z-coordinate is assigned a certain value (a ycoordinate) representing the height of a specific point on a 3D surface. Height maps can thus be described as simple twodimensional arrays containing height values, sampled at evenly spaced intervals. HEIGHT MAPS Height maps, as a function of x- and zcoordinates, can be represented using the following equation: HEIGHT MAPS Creating and Rendering a Height Map [see the textbook and online source code for a detailed discussion and examples]. HIDDEN SURFACE REMOVAL Culling is the process of comparing the position and orientation of polygons against the view volume’s field of view, with polygons facing away from the camera being eliminated. This elimination minimized the amount of computational overhead involved with hidden surface removal. Culling is basically a test determining the visibility of an object, and based on this test the object can be removed if not visible – a process known as hidden surface removal. A culling test is based on a simple vector calculation, specifically, the dot product between a polygon’s normal and the line-of-sight vector (the vector from the center of projection to the polygon). If the result of the dot product is positive then we can flag the polygon as visible (with its normal facing towards the viewer). HIDDEN SURFACE REMOVAL There are two main hidden surface [see the textbook for a detailed example and discussion]. removal algorithm classes: – object space algorithms – image space algorithms. The Z-Buffer Algorithm The most common hidden surface removal algorithm is called the z-buffer algorithm. This image space algorithm was developed in 1975 by Edwin Catmull, current president of Walt Disney and Pixar Animation Studios. Z-buffering, sometimes referred to as depthbuffering, stores the depth values for each pixel being drawn in the frame buffer’s depth buffer. This stored depth data, or z-values, are then used by the algorithm to determine the pixels that should be drawn. [see the textbook for a detailed example and discussion]. The Painter’s Algorithm The painter’s algorithm orders polygons based on their distance from the viewer. This distance is calculated from the polygon’s centroid to the point of view. The Painter’s Algorithm It is also possible to draw the objects of a scene in a front-to-back manner, resulting in the reverse painter’s algorithm. There is one serious flaw in the painter’s hidden surface removal algorithm – the overlapping of three or more polygons can cause it to fail. Another problem is encountered when two or more polygons intersect each other. CLIPPING Clipping is an optimization operation resulting in the display of only visible objects. Clipping can be performed at the image space level, object space level or at both. Image space clipping, as with image space hidden surface removal, occurs during the rasterization phase and is done on a per-pixel basis. It defines a clipping filter operating on a pixel-by-pixel basis, determining whether a particular pixel lies within the specified viewport or not. Object space clipping is on the other hand either planebased in object space or boundary-based in screen space. When performing plane-based clipping, we have to convert our view space boundaries to an object space clipping-plane, subsequently testing for the visibility of vertices inside this clipping-plane. CLIPPING Boundary-based clipping clips against screen boundaries while in screen space by flagging the intersection of objects with planes adjacent to the x-, y- and z-axes. Two-Dimensional Line Clipping Two-dimensional line clipping or line-segment clipping determines the primitives, or sections of primitives, visible to the viewer. All lines located within the predefined view volume pass the clipping test and are sent to the rasterizer for display. Primitives not located within the view volume are culled or eliminated. The lines, or primitives, partially located within the view volume are clipped so that only their visible sections are sent to the rasterizer for display. Two-Dimensional Line Clipping The subsequent figure illustrates the following four possible clipping situations: A The line is completely visible within the predefined view volume, thus passing the clipping test. B The line is completely outside the predefined view volume and can be eliminated from the drawing process. C The line extends from outside the clipping region, enters the view volume and exits it again at another point. Lines like these must be clipped at both ends of the view volume. D The line segment is only partially within the predefined view volume. Its section outside of the clipping region must thus be clipped. Two-Dimensional Line Clipping Two-Dimensional Line Clipping There are several line clipping algorithms, such as the Cohen-Sutherland clipping algorithm, Liang-Barsky clipping, and Cyrus-Beck clipping. The core issue with clipping algorithms is determining the intersection of two lines. Two-Dimensional Line Clipping The clipping process determines these intersections and can be described by the following three steps: 1 Take the input coordinates of a line, (x1, y1) and (x2, y2), as input. 2 Read the coordinates of the clipping rectangle. 3 Generate the clipped line coordinates, (x1’, y1’) and (x2’, y2’), by comparing the input values of the line to the coordinates of the clipping region. Two-Dimensional Line Clipping Extending the mathematical approach used for calculating the intersection of two lines to determine the point of intersection of a line and clipping rectangle is relatively simple. Cohen-Sutherland Clipping The Cohen-Sutherland two-dimensional line clipping algorithm can be considered a brute-force approach to the clipping problem. The algorithm basically divides the clipping region into a number of sections (specifically nine regions), each with its own unique 4-bit binary number, called an outcode. Cohen-Sutherland Clipping [see the textbook for a detailed discussion and example]. Liang-Barsky Clipping The Liang-Barsky two-dimensional line clipping algorithm uses the parametric form of a line and the clipping window to determine the clipping coordinates of lines intersecting the clipping volume. This algorithm is significantly more efficient than Cohen-Sutherland clipping. Liang-Barsky Clipping Liang-Barsky Clipping To summarize, the Liang-Barsky clipping algorithm consists of the following steps: 1 Define all line segments in parametric form. 2 Set the minimum and maximum values for t (tmin ¼ 0 and tmax ¼ 1). 3 Calculate all the t values located between tmin and tmax. 4 Determine whether the arbitrary values of each parametric line lie within the clipping window. a If the line intersects, shorten the line segment via a floatingpoint division. b If the line doesn’t intersect the clipping window, discard it. 5 Display all clipped line segments. Clipping Polygons Extending two-dimensional line clipping to polygons involves the clipping of polygons against a clipping rectangle or against other polygons, as is the case with hidden surface removal. Polygon clipping algorithms are derived from simple line clipping algorithms. We can thus clip a polygon against the edges of a clipping rectangle. There is, however, a problem with this approach – clipping concave polygons can result in several additional polygons as shown in Figure 12-19. Clipping Polygons Clipping Polygons The illustrated problem is not encountered when working with convex polygons. The only solution to this problem is to consider the clipped region as a single polygon or to tessellate (divide) the polygon into a number of convex polygons Clipping Polygons Three-Dimensional Clipping Three-dimensional objects are clipped against a bounding volume as opposed to a clipping rectangle. Specifically, when clipping in 3D space we need to consider the intersections of lines and planes or polygons and planes as opposed to the clipping of lines against lines, which is the case with 2D space. A simple technique often employed to reject objects completely outside the clipping volume and to accept objects entirely inside it is to create a bounding sphere for the object and to subsequently test its edges against that of the view volume. A bounding volume, or bounding box, is simply a volume containing a geometric object. Three-Dimensional Clipping Three-Dimensional Clipping INTRODUCTION TO LEVEL OF DETAIL (LOD) ALGORITHMS Level of Detail algorithms are concerned with the complexity of geometric objects; basically decreasing an object’s mesh complexity (the number of polygons) as the distance between the object and the viewer increases. The main reason for using these algorithms is to increase the efficiency of a rendering operation by lessening the workload on the graphics system. INTRODUCTION TO LEVEL OF DETAIL (LOD) ALGORITHMS INTRODUCTION TO LEVEL OF DETAIL (LOD) ALGORITHMS INTRODUCTION TO LEVEL OF DETAIL (LOD) ALGORITHMS INTRODUCTION TO LEVEL OF DETAIL (LOD) ALGORITHMS