37
51 UNIT 3 VISUAL REALISM Structure 3.1 Introduction Objectives 3.2 Specific Needs for Realism 3.3 Hidden Line or Surface Removal 3.4 Rendering, Shading and Colouring 3.5 Approximating Smooth Surfaces with Polygon Nets 3.6 Colouring 3.7 Summary 3.1 INTRODUCTION CAD/CAM software has been recognized as an essential tool in the designing and manufacturing of a product due to its ability to depict the designs and tool paths with friendly visual displays and effects. There is no CAD/CAM package existing without visualization of the design of a product on the computer screen. Visualization embraces both image understanding and image synthesis. Thus, the visualization becomes one of the challenging tasks in computer graphics to generate images that appear realistic. The quest for visual realism began in the early 70s when memory prices dropped low enough to allow raster scan technologies to be cost-effective over the then prevailing calligraphic displays. The need of visual realism is increasing forever in every discipline of science and engineering. You may appreciate the visual realism applications in enhancing our knowledge and training skills as described below : (a) Robot Simulations : Visualization of movement of their links and joints and end effector movement etc. (b) CNC programs verification of tool movement along the path prescribed and estimation of cup height and surface finish etc. (c) Discrete Even Simulation : Most of DES packages provide the user to create shop floor environment on the screen to visualize layout of facilities, movement of material handling systems, performance of machines and tools. (d) Scientific Computing : Visualization of results of FEM analysis like iso-stress and iso-strain regions, deformed shapes and stress contours. Temperature and heat flux in heat-transfer analysis. Display and animation of mode shape in vibration analysis. (e) Flight Simulation : Cockpit training for pilots is first being provided with flight simulators, which virtually simulates the surrounding that an actual flight will pass through. Objectives After studying this unit, you should be able to know specific needs of realism, add realism to pictures by eliminating hidden lines and surfaces on solid objects, know several techniques for removing hidden lines and surfaces, determine how light reflects off the surfaces,

Unit 3 Visual Realism - eGyanKosh

Embed Size (px)

Citation preview

51

Visual RealismVisual RealismUNIT 3 VISUAL REALISM Structure

3.1 Introduction Objectives

3.2 Specific Needs for Realism

3.3 Hidden Line or Surface Removal

3.4 Rendering, Shading and Colouring

3.5 Approximating Smooth Surfaces with Polygon Nets

3.6 Colouring

3.7 Summary

3.1 INTRODUCTION

CAD/CAM software has been recognized as an essential tool in the designing and manufacturing of a product due to its ability to depict the designs and tool paths with friendly visual displays and effects. There is no CAD/CAM package existing without visualization of the design of a product on the computer screen. Visualization embraces both image understanding and image synthesis. Thus, the visualization becomes one of the challenging tasks in computer graphics to generate images that appear realistic. The quest for visual realism began in the early 70s when memory prices dropped low enough to allow raster scan technologies to be cost-effective over the then prevailing calligraphic displays. The need of visual realism is increasing forever in every discipline of science and engineering.

You may appreciate the visual realism applications in enhancing our knowledge and training skills as described below :

(a) Robot Simulations : Visualization of movement of their links and joints and end effector movement etc.

(b) CNC programs verification of tool movement along the path prescribed and estimation of cup height and surface finish etc.

(c) Discrete Even Simulation : Most of DES packages provide the user to create shop floor environment on the screen to visualize layout of facilities, movement of material handling systems, performance of machines and tools.

(d) Scientific Computing : Visualization of results of FEM analysis like iso-stress and iso-strain regions, deformed shapes and stress contours. Temperature and heat flux in heat-transfer analysis. Display and animation of mode shape in vibration analysis.

(e) Flight Simulation : Cockpit training for pilots is first being provided with flight simulators, which virtually simulates the surrounding that an actual flight will pass through.

Objectives After studying this unit, you should be able to

• know specific needs of realism,

• add realism to pictures by eliminating hidden lines and surfaces on solid objects,

• know several techniques for removing hidden lines and surfaces,

• determine how light reflects off the surfaces,

52

Introduction to Computer and Computer Graphics • see how to approximate a smooth surface with polygon net, and

• know the use of various colour models.

3.2 SPECIFIC NEEDS FOR REALISM

The graphical representation of solids are better dealt in 3-D graphics for visualization, which had to take the mathematical model and line representations typically as shown in Figure 3.1(a), and bring them up to the realism of the real world, which meant the following :

(a) Most real objects are opaque, and we only see the lines and surfaces at front. Those portions at the back had, therefore, to be eliminated from the mathematical model before plotting, as in Figure 3.1(b). We will discuss this under “Hidden-Line and Surface Removal”, which can also be referred to as Visible Surface Identification.

(b) All real objects appear smaller as they are farther from us, with parallel lines converging away from us. Hence, except for construction and fabrication drawings and scientific graphs, which should avoid spatial distortion, perspective projection as in Figure 3.1(c) should be preferred for all 3-D viewing. We will discuss this under “Perspective Projection”.

(c) All real objects exhibit surface characteristics due to two causes, namely the surface textures, and the effect of the light falling on them. Thus, blank areas or flat shades of gray or colour must be enhanced to simulate actual surfaces, as in Figure 3.1(d). We shall discuss this under “Surface Renderings”.

(a) (b) (c) (d)

Figure 3.1 : Progression of Realism : (a) Plotted as Computed; (b) Hidden Lines and Surfaces Removed; (c) Perspective; and (d) Surfaces Rendered

If after all this, we add a background of our choice, we have the total 3D image – except that it is virtual, imaginary, not real, but one fully under our control, to be manipulated as we wish.

Certainly, there is still one more major item missing, before we can look at a computer display or plot and perceive it just as we see a real object, namely the stereo vision we experience with our two eyes. That too is now available, with software producing two colour-coded, polarized, or alternating images for left and right eyes, and a set of special glasses to merge them for the observer. In fact, this technology is very much in use by architects who wish to show off their latest creations to their (rich) clients, and for doctor who wish to plan and rehearse microsurgery of the brain or heart.

The development of perspective projection made constructed objects look more realistic. But for a realistic scene, we should only draw those lines and polygons which could actually be seen, not those which would be hidden by other objects. What is hidden and what is visible depend upon the point of view.

The main problem in visualization is the display of three-dimensional objects and scenes on two-dimensional screens. How can the third dimension, the depth, be displayed on the screen? How can the visual complexities of the real environment such as lighting, colour, shadows, and texture be represented as image attributes? What complicates the display of three-dimensional objects even further is the centralized nature of the databases of their

53

Visual RealismVisual Realismgeometric models. If we project a complex three-dimensional model onto a screen, we get a complex maze of lines and curves. To interpret this maze, curves and surfaces that cannot be seen from the given viewpoint should be removed. Hidden line and surface removal eliminates the ambiguities of the displays of three-dimensional models and is considered the first step toward visual realism.

3.3 HIDDEN LINE OR SURFACE REMOVAL

The problem of hidden lines or surfaces was implicit even in 2-D graphics, but we did not mention it there, because what was intended to be viewed and displayed was already predetermined, and defined. But in 3-D graphics, we present solid objects, and we want to see them from different positions. With each transformation, different portions that were hidden would pop into view, and other portions that were visible would go into hiding. This is indeed a major complication. 3.3.1 Different Representations A typical axonometric view of a block (say a huge rectangular building), will appear as in Figure 3.2(a), as if the viewer is swinging round the block in a helicopter steeply banking (with its axis inclined), so that the ground seems to be sloping, and horizontal and even vertical lines are tilting.

(a) (b) (c)

Figure 3.2 : Three-Dimensional Isometric of Block : (a) Wire Frame; (b) Partial Wire Frame; and (c) Hidden Lines Removed and Flat Shade Rendering

However, the displayed image shows even the lines that are supposed to be hidden and hence invisible, interfering with and disturbing our perception of the object. In essence, the surfaces are all transparent and the edges are all visible, leading to ambiguity and confusion. For instance, in Figure 3.2(a), which of the two comers is nearer to the viewer : P or Q? This will be a problem in whatever view of the object we wish to display. This skeletal view is called a Wire-frame Model. Although not a realistic representation of the object, it is still very useful in the early stages of image development for making sure that all the corners and edges are where they should be. For some situations such as preliminary designs, centre-line diagrams of structural frames, etc. these are not only quite' adequate, but even preferred over the more realistic views. Sometimes, for checking during image creation, or for easier interpretation of the final display, hidden lines and surfaces are not completely omitted, but shown as dashed or lighter lines, as in Figure 3.2(b). This is known as Partial Wire-frame. The most realistic view, of course, is the one that our eyes would see, namely with the hidden lines and surfaces completely removed, as in Figures 3.1(b) and 3.2(c). Thus, for realism in 3-D representation, the lines and surfaces which are hidden by the surfaces visible to the observer must be rubbed out or covered up, or better yet, not drawn in the first place, removed from the plotting data by a computer graphics algorithm. Hidden-line removal refers to wire-frame diagrams without surface rendering and polygonal surfaces with straight edges. Hidden-surface removal refers to all kinds of surfaces, including curved ones. For the simplified and conceptual treatment planned herein, we shall use the phrases “hidden-line” and “hidden-surface” interchangeably. Hidden surface removal is the most troublesome and most expensive aspect of 3-D computer graphics. Not only is hidden surface removal extremely complex in

54

Introduction to Computer and Computer Graphics

conceptualization and computation, but it is also very tedious and time-consuming in implementation and processing. Various approaches to achieve visual realism exist. They are directly related to the types of geometric models utilized to represent three-dimensional objects. Thus, one would expect an upward trend in the efficiency and automation level of these approaches as the geometric modeling techniques have advanced from wire-frames, to surfaces, to solids. Among the existing visualization approaches are parallel projections, perspective projections, hidden line removal, hidden surface removal, hidden solid removal, and the generation of shaded images of models and scenes.

A wide variety of hidden line and hidden surface removing (visibility) algorithms are in existence today. The development of these algorithms is influenced by the types of graphics display devices they support (whether they are vector or raster) and by the type of data structure or geometric modeling they operate on (wire-frame, surface, or solid modeling). Some algorithms utilize parallel, over the traditional serial, processing to speed up their execution. The formalization and generalization of these algorithms are useful and are required if one attempts to design and build special-purpose hardware to support hidden line and hidden surface removal, which is not restricted to a single algorithm. However, it is not a trivial task to convert the different algorithmic formulations into a form that allows them to be mapped onto a generalized scheme.

Algorithms that are applied to a set of objects to remove hidden parts to create a more realistic image are usually classified into hidden line and hidden surface algorithms. The former supports line-drawing devices such as vector displays and plotters, while the latter supports raster displays.

The algorithms used for removal of hidden line and hidden surfaces are broadly classified according to whether they deal with object definitions directly or with their projected images. These two approaches are called object-space methods and image-space methods, respectively. An object-space method compares objects and parts of objects to each other within the scene definition to determine which surfaces, as a whole, we should label as visible. In an image-space algorithm, visibility is decided point by point at each pixel position on the projection plane. Most visible-surface algorithms use image-space methods, although object-space methods can be used effectively to locate visible surfaces in some cases. Line-display algorithms, on the other hand, generally use object-space methods to identify visible lines in wire-frame displays, but many image-space visible-surface algorithms can be adapted easily to visible-line detection.

3.3.2 Back Face Detection Method A single polyhedron is a convex solid, which has no external angle between faces less than 180° and there is a simple object space method to determine which faces are the visible front faces, and which are the hidden back faces.

The faces are characterized by their outward normals, and for this reason, the method is also known as the Outward Normal Method.

The outward normal for every face of the convex solid is determined by taking the cross-product of any two adjacent sides of the face. For consistency in checking, when the viewer looks directly at the face under consideration, the vertex numbering (naming) scheme for all faces must be in the same direction, say counter-clockwise.

We need to start with a single polygon numbered in the proper order. There are analytical methods for doing this, but where user has control over the input, it is much easier to number the first polygon counter-clockwise when visible to the viewer.

Once this is done, any adjacent polygon will have the common edge traversed in the opposite direction, and hence no further decision-making will be necessary regarding the direction of numbering. This logic is particularly helpful in naming the faces away from the observer and those that are approximately horizontal, eliminating the need for the observer to imagine himself at all sorts of odd viewing angles.

The algorithm of this method is as follows :

55

Visual RealismAlgorithm for Back Face Detection Method Visual Realism

j

)

1)

(a) Identify the first polygon of the solid, numbered in a counter-clockwise fashion for the viewer. Let the mj vertices of this polygon 1 be

(1), (2), . . . , ( )i j jv v v m

(b) Set . ( 1), (1)j j jv m v+

(c) Find , from the coordinates of the first three vertices of polygon 1. If k < 0, go to Step (a), renumbering if necessary.

3 2 1 2 3 2 1 2 3 2( ) ( ) ( ) ( ) (k x x x x y y y y z z= − − + − − + −

(d) Start polygon loop on i = 1 to n, n being the number of polygons.

(i) If i = 1 go to Step (e)

(ii) Let the number of vertices for polygon i be mi, and let the vertices be . Set (1), (2), . . . , ( )i i iv v v m ( 1) (i i iv m v+ = .

(iii) Start polygon loop on j = 1 to n.

(1) Let the number of vertices for polygon j be mj, and let the vertices be . Set (1), (2), . . . , ( )j j jv v v m j ( 1) (1)j j jv m v+ = .

(2) Compare every sequential pair of vertex numbers p and q for polygon i, with every sequential pair of vertex numbers r and s for polygon j, except for j = i.

(3) If p = r and q = s, a straight match, then reverse the order of all mi vertices for polygon i. Go to Step (v).

(4) If p = s and q = r, a reverse match, then go to Step (v).

(iv) End loop on j.

(v) Find , from the coordinates of the first three vertices of polygon i.

2 1 3 1 3 1 2 1( ) ( ) ( ) (k x x y y x x y y= − − − − − )

(vi) If k > 0, pass the coordinates of all mi vertices, and the edge and surface attributes of polygon i to the plot module.

(e) End loop on i.

Example of Back Face Detection Method

To illustrate the method, we shall start with the tetrahedron (pyramid) PQRS of Figure 3.3, with vertices P (1, 1, 2), Q (3, 2, 3), R (1, 4, 1), and S (2, 4,0). It is obviously a convex object, re-entrant angles just not being possible with triangles. In this case we may be able to guess that the vertex S is behind the triangle PQR, and also that faces PQR and QRS are visible, and the other two faces PQS and PRS are hidden. But the computer must be able to decide for itself and we too cannot decide for more than one tetrahedron.

So, let us start with the order PQR as counter-clockwise with the triangle facing the viewer, as shown by the arrows along the edges in Figure 3.3(a). Then, for the adjacent triangle QRS, at its common edge QR with the triangle PQR, the arrow must point in the opposite direction, namely from R to S as shown, and hence the vertices must indeed be ordered as RQS (or QSR, or SRQ), as in Figure 3.3(b).

The vertex sequences of the other two triangles are also followed by the same logic : For the triangle PRS, from the common edge PR with triangle PQR, the vertex sequence must be PRS (or RSP, or SPR), as in Figure 3.3(c). We could have also used the common edge RS with triangle QRS to get the same result. For triangle PQS, from anyone of the three common edges with the other three triangular faces, the vertex sequence will be determined as PSQ (or SQP, or QPS), as in Figure 3.3(d).

56

Introduction to Computer and Computer Graphics

Figure 3.3 : Back-Face Detection Method Naming Directions (or Faces (a) PQR, (b) QSR, (c) PRS and (d) PSQ

P

R

(d) (c) (b) (a)

QP P P

Q

SS R

R R

S

QQ

S SR

P Q

Now the outward normals of the four faces, found by taking the cross product of any two adjacent edges of each polygon are found to be as follows :

4 2 6PQRN i j k= − + +

2 4 2QSRN i j k= + +

3 3PRSN i j k= − − −

5 5 5PSQN i j k= − −

The sign of the coefficients of k will determine the direction in which each normal is pointing, positive being in the positive direction of the z-axis. In fact, it is sufficient to compute only the coefficient of k as

2 1 3 1 2 1 2 1( ) ( ) ( ) ( )x x y y x x y y− − − − − , and avoid calculation of the coefficients of i and j. The normal vectors are given in full only to indicate their orientations in Figure 3.4(a), with the two normals with positive k values being marked in full lines, and the two with negative k values being marked in broken lines.

NQSR

R S SRNPRS

NPQR

Q NPSQ

Q P P

(z-axis our of paper)

(a) (b)

Figure 3.4 : Back-Face Detection Worked Example : (a) Normals; and (b) Display

In the present case, the coefficients of k for the faces PQR and QSR are positive, and hence those two faces are visible, while the other two faces PRS and PSQ with negative k coefficients are hidden from view. Hence, the display will be as shown in Figure 3.4(b), with the hidden edge PS shown in thin broken line.

Application of Algorithm

If the algorithm given earlier is applied to the four polygons initially named (say) as PQR, PQS, PRS, and QRS, we get :

i = 1 : PQR chosen correctly. Confirm, k = 6 > 0, visible, plot.

57

Visual Realismi = 2 : j = 1 : PQS (P) versus PQR (P), PQ-PQ straight match, reverse PQS to SQP.

Visual Realism

k = − 5, < 0, not visible. (First vertex repeated at end, shown within brackets.)

i = 3 : j = 1 : PRS (P) versus PQR (P), PR-RP reverse match, keep PRS as is. k = − 3, < 0, not visible.

i = 4 : j = 1 : QRS (Q) versus PQR (P), QR-QR straight match, reverse QRS to SRQ.

k = 2, > 0, visible, plot.

Thus, faces PQR and SRQ are visible, and the other two faces PQS and PRS are hidden, as already shown.

The computer algorithm checks one face at a time, and plots the edges and fills in the area only if the k coefficient is positive. If it is desired to show the hidden edges in broken lines (or with thinner lines or lighter colour), the algorithm may plot the edges of the hidden polygons first as broken (or thinner or lighter) lines, without filling in the areas and then plot the visible edges and faces, so that the visible edges will overlap the earlier hidden-coded line rendering at the common edges.

3.3.3 Z-Buffer Algorithm Also known as the Depth-Buffer algorithm, this image-space method simply selects for display the polygon or portion of a polygon that is nearest to the viewer, by checking the z-value of every pixel. Let us consider the quadrilateral ABCD of Figure 3.5, with the triangle EFG in front of it, and rectangle : PQRS behind it.

(a) (b)

Figure 3.5 : Z-Buffer Algorithm : (a) Front View; and (b) Practical View

As shown in Figure 3.5(a), for any point H in the triangle, its projection H′ on the quadrilateral will be behind H, and ZH will be less than ZH′. Hence, the entire triangle EFG will be visible.

On the other hand, the rectangle PQRS is partially hidden by the quadrilateral, the portion PQUT being hidden and the portion TURS being visible. This will be mathematically known by the fact that for all points (x, y) within the region PQUT, the Z for the quadrilateral will be less than the Z for the rectangle, while for all points within the region TURS, there will be no common (x, y) between the quadrilateral and the rectangle, leaving only the rectangle portion to be visible.

The rays for the determination of H ′, T, and U ′ from H, T, and U are shown in parallel projection in Figure 3.5(b). The z-buffer memory size by this method is only dependent on the viewport, and any number of polygons can be handled.

The algorithm proceeds as follows :

Algorithm for Z-Buffer Method

(a) Initialize every pixel in the viewport to the smallest value of z, namely z0 the z-value of the rear clipping plane or “back-ground”. Store all values in the z-buffer.

58

Introduction to Computer and Computer Graphics

(b) Start polygon loop i = 1 to n, n being the number of polygons.

(i) Scan convert each pixel, that is, find the value of z for each (x, y) for the polygon.

(ii) Compare it with the z′ value stored for that (x, y). If z < z′, then the new point is closer to the viewer than the previous point, and hence is visible in front of the previous point; reset z′ to the new value.

(c) End loop on i.

(d) Plot only the final z′ values in the z-buffer for all (x, y) in the viewport, with the appropriate colour or gray scale and other attributes.

3.3.4 Scan-Line Algorithm This image-space method for removing hidden surfaces is an extension of the scan-line algorithm for filling polygon interiors. Instead of filling just one surface, we now deal with multiple surfaces. As each scan line is processed, all polygon surfaces intersecting that line are examined to determine which are visible. Across each scan line, depth calculations are made for each overlapping surface to determine which is nearest to the view plane. When the visible surface has been determined, the intensity value for that position is entered into the refresh buffer.

We assume that tables are set up for the various surfaces, which include both an edge table and a polygon table. The edge table contains coordinate end points for each line in the scene, the inverse slope of each line, and pointers into the polygon table to identify the surfaces bounded by each line. The polygon table contains coefficients of the plane equation for each surface, intensity information for the surfaces, and possibly pointers into the edge table. To facilitate the search for surfaces crossing a given scan line, we can set up an active list of edges from information in the edge table. This active list will contain only edges that cross the current scan line, sorted in order of increasing x. In addition, we define a flag for each surface that is set on or off to indicate whether a position along a scan line is inside or outside of the surface. Scan lines are processed from left to right. At the leftmost boundary of a surface, the surface flag is turned on; and at the rightmost boundary, it is turned off.

Figure 3.6 illustrates the scan-line method for locating visible portions of surface for pixel positions along the line. The active list for scan line 1 contains information from the edge table for edges AB and BC, only the flag for surface S1 is on. Therefore, no depth calculations are necessary, and intensity information for surface S1 is entered from the polygon table into the refresh buffer. Similarly, between edges EH and FG, only the flag for surface S2 is on. No other positions along scan line 1 intersect surfaces, so the intensity values in the other areas are set to the background intensity. The background intensity can be loaded throughout the buffer in an initialization routine.

For scan lines 2 and 3 in Figure 3.6, the active edge list contains edges AD, EH, BC, and FG. Along scan line 2 from edge AD to edge EH, only the flag for surface S1 is on. But between edges EH and BC, the flags for both surfaces are on. In this interval, depth calculations must be made using the plane coefficients for the two surfaces. For this example, the depth of surface S1 is assumed to be less than that of S2, so intensities for surface S1 are loaded into the refresh buffer until boundary BC is encountered. Then the flag for surface S1 goes off, and intensities for surface S2, so intensities for surface S1 are loaded into the refresh buffer until boundary BC is encountered. Then the flag for surface S1 goes off, and intensities for surface S2 are stored until edge FG is passed.

We can take advantage of coherence along the scan lines as we pass from one scan line to the next. In Figure 3.6, scan line 3 has the same active list of edges as scan line 2. Since no changes have occurred in line intersections, it is unnecessary again to make depth calculations between edges EH and BC. The two surfaces must be in the same orientation as determined on scan line 2, so the intensities for surface S1 can be entered without further calculations.

59

Visual Realism Visual Realism

Figure 3.6 : Scan Lines Crossing the Projection of Two Surfaces, S1 and S2, in the View Plane. Dashed

Lines Indicate the Boundaries of Hidden Surfaces

Any number of overlapping polygon surfaces can be processed with this scan-line method. Flags for the surfaces are set to indicate whether a position is inside or outside, and depth calculations are performed when surfaces overlap. When these coherence methods are used, we need to be careful to keep track of which surface section is visible on each scan line. This works only if surfaces do not cut through or otherwise cyclically overlap each other as shown in Figure 3.7. If any kind of cyclic overlap is present in a scene, we can divide the surfaces to eliminate the overlaps. The dashed lines in this figure indicate where planes could be subdivided to form two distinct surfaces, so that the cyclic overlaps are eliminated.

Subdividing Line Subdividing

Line

(a) (b) (c)

Figure 3.7 : Intersecting and Cyclically Overlapping Surfaces that Alternately Obscure One Another

SAQ 1 (a) Write a program to implement the z buffer to generate shaded images.

(b) Test the back-face removal algorithm by drawing a die. Provide different views of the die so that each face is displayed at least once.

3.3.5 Painter’s Algorithm As the name suggests, the algorithm follows the standard practice of a painter, who would paint the background (such as a backdrop) first, then the major details (such as a face), and finally the finer details (such as eyes). This is also called Depth Sort or Priority algorithm.

To determine the specific nature of the relative positions of polygons, it is necessary to define the x-, y-, and z-extents, of a polygon in 3-D space as the respective coordinate ranges of the bounding (i.e. enclosing) box, as shown in Figure 3.8. For the quadrilateral ABCD, the x-extent is (xD – xB), y-extent is (yB A – yC), and z-extent is (zBB – zD). The x- and

60

Introduction to Computer and Computer Graphics

y-extents of two polygons will enable decisions to be made on their overlap, and the z-extent will define their relative positions with respect to the viewer.

Figure 3.8 : Extents for a Polygon

Based on the extents, the farthest polygon is stored first, and over it the next farther polygon, and so on, until all the polygons are stored. The second and subsequent polygons automatically superpose themselves over the earlier ones, and reveal only the visible portions. Figure 3.9 shows the sequence of operations :

(a) (b) (c)

Figure 3.9 : Painter’s Algorithm

(a) The farthest polygon, namely the rectangle PQRS, is stored first. (b) The next farthest, the quadrilateral ABCD, is superposed, covering the

portion PQUT of the rectangle and leaving visible the portion TURS of the rectangle.

(c) Finally the closest, namely the triangle EFG, is superposed, entirely within the quadrilateral, and covering it in the region EFG.

The primary requirement for this algorithm is the sorting of polygons by priority, depending on whether a polygon covers (or is covered by) another polygon partly or wholly. This determination is made by comparing the x-, y-, and z-extents of the various polygons. In case the z-extents of any two polygons overlap, then, if the two polygon planes are parallel to each other there is obscuring of one by the other; if the two polygon planes are not parallel, they may intersect each other. In such cases, the covered/uncovered or penetrated invisible/visible portions are determined by geometrical and clipping procedures, including the determination of the line of intersection of the two polygons if necessary, and separated into independent regions. 3.3.6 Comparison Techniques There are several techniques for determining the relevancy and relative position of two polygons. Not all tests may be used with all hidden-surface algorithms, and some of the

61

Visual Realismtests are not always conclusive. Sometimes we can use simple tests for many of the cases and resort to more costly tests only when all else fails.

Visual Realism

One technique that is often useful is called the minimax test or boxing test. This test checks if two polygons overlap. The test provides a quick method to determine if two polygons do not overlap. It surrounds each polygon with a box by finding its extents (minimum and maximum x and y coordinates) and then checks for the intersection for any two boxes in both the X and Y directions. If two boxes do not intersect, their corresponding polygons do not overlap. In such a case, no further testing of the edges of the polygons is required. If the minimax test fails (two boxes intersect), the two polygons may or may not overlap, as shown in Figure 3.10. Each edge of one polygon is compared against all the edges of the other polygon to detect intersections. The minimax test can be applied first to any two edges to speed up this process. The minimax test can be applied in the Z direction to check if there is no overlap in this direction. In all tests, finding the extents themselves is the most critical part of the test. Typically, this can be achieved by iterating through the list of vertex coordinates of each polygon and recording the largest and the smallest values for each coordinate. (a) Boxes Polygons Do Not Overlap (b) Boxes Overlap and Polygons Do Not

(c) Boxes and Polygons Overlap (d) Minimax Test of Individual Edges

Figure 3.10 : Minimax Tests for Typical Polygons and Edges

3.3.7 Warnock’s Algorithm

An interesting approach to the hidden-surface problem was presented by Warnock. His method does not try to decide exactly what is happening in the scene but rather just tries to get the display right. As the resolution of the display increases, the amount of work which the algorithm must do to get the scene right also increases, (this is also true for scan-line algorithms). The algorithm divides the screen up into sample areas. In some sample areas it will be easy to decide what to do. If there are no faces within the area, then it is left blank. If the nearest polygon completely covers it, then it can be filled in with the colour of that polygon. If neither of these conditions holds, then the algorithm subdivides the sample area into smaller sample areas and considers each of them in turn. This process is repeated as needed. It stops when the sample area satisfies one of the two simple cases or when the sample area is only a single pixel (which can be given the colour of the foremost polygon). The process can also be allowed to continue to half or quarter pixel-sized sample areas, whose colour may be average over a pixel to provide antialiasing.

62

Introduction to Computer and Computer Graphics

The test for whether a polygon surrounds or is disjoint from the sample area is much like a clipping test to see if the polygon sides cross the sample-area boundaries. Actually the minimax test can be employed to identify many of the disjoint polygons. A simple test for whether a polygon is in front of another is a comparison of the z coordinates of the polygon planes at the corners of the sample area.

At each subdivision, information learned in the previous test can be used to simplify the problem. Polygons which are disjoint from the tested sample area will also be disjoint from all of the sub-areas and do not need further testing. Likewise, a polygon which surrounds the sample area will also surround the sub-areas.

Warnock’s Algorithm

A divide and conquer algorithm

Warnock (PolyList PL, ViewPort VP)

If (PL simple in VP)

then Draw PL in VP,

else

Split VP vertically and horizontally into VP1, VP2, VP3, VP4

Warnock (PL in VP1, VP1)

Warnock (PL in VP2, VP2)

Warnock (PL in VP3, VP3)

Warnock (PL in VP4, VP4)

End

What does “simple” mean?

No more than one polygon in viewport

Scan convert polygon clipped to viewport

Viewport only 1 pixel in size

Shade pixel based on closest polygon in the pixel

Figure 3.11 : Warnock's Algorithm

Runtime : O (p x n)

p : number of pixels

n : number of polygons.

3.3.8 Franklin Algorithm We mentioned how the number of possible comparisons of polygons grows as the square of the number of polygons in the scene. Many of the hidden-surface algorithms exhibit this behaviour and have serious performance problems on complex scenes. Franklin developed an approach which gives linear time behaviour for most scenes. This is done by overlaying a grid of cells on the scene (similar to Warnocks approach, only these cells

63

Visual RealismVisual Realismare not subdivided). The size of the cells is on the order of the size of an edge in the scene. At each cell the algorithm looks for a covering face and determines which edges are in front of this face. It then computes the intersections of these edges and determines their visibility. The idea is that as objects are added to the scene and the number of polygons increases, the new objects will either be hidden by objects already in the scene or will hide other objects in the scene. While the number of objects increases, the complexity of the final scene (after hidden portions are removed) does not increase. By considering only the edges in front of the covering face for a cell, the algorithm considers only the edges likely to be in the final image. Although the total number of edges may increase, this increase occurs, for the most part, behind the covering faces, and the number of edges in front will remain small.

3.3.9 Binary Space Partition A binary space-partitioning (BSP) tree is an efficient method for determining object visibility by painting surfaces onto the screen from back to front, as in the painter’s algorithm. The BSP tree is particularly useful when the view reference point changes, but the objects in a scene are at fixed positions.

Applying a BSP tree to visibility testing involves identifying surfaces that are “inside” and “outside” the partitioning plane at each step of the space subdivision, relative to the viewing direction. Figure 3.12 illustrates the basic concept in this algorithm. With plane P1, we first partition the space into two sets of objects. One set of objects is behind, or in back of, plane P1 relative to the view plane P1, we divide that object into two separate objects, labeled A and B. Objects A and C are in front of P1, and objects B and D are behind P1. We next partition the space again with plane P2 and construct the binary tree representation shown in Figure 3.12(b). In this tree, the objects are represented as terminal nodes, with front objects as left branches and back objects as right branches.

(a) (b)

Figure 3.12 : A Region of Space (a) is Partitioned with Two Planes P1 and P2 to form the BSP Tree Representation in (b)

For objects described with polygon facets, we chose the partitioning planes to coincide with the polygon planes. The polygon equations are then used to identify “inside” and “outside” polygons, and the tree is constructed with one partitioning plane for each polygon face. Any polygon intersected by a partitioning plane is split into two parts. When the BSP tree is complete, we process the tree by selecting the surfaces for display in the order back to front, so that foreground objects are painted over the background objects. Fast hardware implementations for constructing and processing BSP trees are used in some systems.

3.3.10 Area Subdivision Method In this method, the viewport is examined for clear decisions on the polygons situated in it, in regard to their overlap and visibility to the viewer. For the visibility of a polygon within the viewport, its x- and y-extents are compared with the horizontal and vertical spans of the viewport, and classified as one of the following, with respect to Figure 3.13, in which, the x-y extents of the polygon are marked by its bounding rectangle :

64

Introduction to Computer and Computer Graphics

(a) Surrounding (b) Contained (c) Disjoint (d) Overlapping or Intersecting

Surrounding A polygon surrounds a viewport if it completely encloses or covers the viewport. This happens if none of its sides cuts any edge of the viewport. It is not sufficient if the x-y extent encloses the viewport, because even with the same extent, if the right edge of the viewport were to be shifted a little more to the right as shown dotted in Figure 3.13(a), the polygon’s right edge would cut the top and right edges of the viewport. If the surrounding polygon is the closest to the viewer, the entire viewport is covered by the colour and other attributes of the polygon.

Current Background Polygon

(a) (b) (c) (d)

Figure 3.13 : Subdivision Algorithm : (a) Surrounding; (b) Contained; (c) Disjoint; and (d) Overlapping or Intersecting

Contained A polygon is contained in a viewport if no part of it is outside any of the edges of the viewport. In this case, if the z-extent of the polygon is behind the z-extent of the current background, the polygon is completely invisible. If the z-extent of the polygon is in front of the current background, the polygon is completely visible (Figure 3.13(b)).

Disjoint A polygon is disjoint from the viewport if the x- and y-extents of the polygon do not overlap the viewport anywhere. In this case; regardless of the relative z-extents of type polygon and the current background, and regardless of which one is in front of the other, the polygon is not visible (Figure 3.13(c)). The concept of disjointness may also be used while comparing different polygons in the same viewport. If, of all the polygons visible within a viewport, the x-y extent of only one polygon P is not intersected by the x-y extents of any of the other polygons, then P can be declared to be visible in the viewport, and no further evaluation will be necessary.

Overlapping or Intersecting A polygon overlaps or intersects the current background if any of its sides cuts the edges of the viewport as depicted at the top right corner of the viewport in Figure 3.13(d).

The extent of a polygon overlapping the viewport is only a necessary but not sufficient condition, because even if the extent overlaps, the polygon may be disjoint, as illustrated at the top left corner of the viewport in Figure 3.13(d). It may be seen that only the surrounding and contained cases (a) and (b) may be decided on the basis of the simple rules of relationship between the x-y extents of the polygon and the viewport. All other cases need further investigation, as follows :

65

Visual RealismWe start with the entire viewport, and examine all the polygons for surrounding or containment. Whatever polygons can be classified as Case (a) or Case (b) can be stored or not depending on whether they are visible or not.

Visual Realism

For any remaining polygons, the viewport is divided into four quarters, and each quarter representing a viewport segment is evaluated against the polygon portions in that quarter for cases (a) and (b). The quarter or quarters in which such determination cannot be made will again be subdivided into four further quarters each, and each quarter (which may be termed a “viewport segment”) examined for the same kind of relationship decisions. This procedure is applied with successive subdivisions until all the polygons and sub-regions can be resolved as (a) or (b). Frequently, the procedure may have to be repeated until the last quartering reduces to a single pixel. Because of the four branches formed with each quartering step, the method is also called the Quad tree method. Finally, the method illustrates the difference between hidden-line and hidden-surface algorithms. In a particular viewport segment, it may happen that there are no edges at all, but two or more surfaces are surrounding the viewport segment. Here, hidden line has no relevance, but hidden surface has.

3.3.11 Example of Area Subdivision Method The procedure will be explained with respect to an illustrative problem shown in Figure 3.15(a), with the image consisting of five objects, namely a triangle (T), quadrilateral (Q), square (S), horizontal rectangle (H), and vertical rectangle (V). The viewport PQRS is 16 pixels by 16 pixels, containing only part of the image. The square is behind the quadrilateral and the triangle and the two rectangles are in front of the quadrilateral. For ease of understanding, all five polygons are assumed to be parallel to the x-y plane, not intersecting one another. The same procedures can, however, be applied to handle the intersection of two polygons. Figure 3.14(a) shows the problem as presented; (b) is the pixel-wise representation of the polygon edges; (c) shows the external (disjoint) part of the quadrilateral clipped away.

(a) (b) (c)

Figure 3.14 : Subdivision Method Example : (a) Five Objects and Viewport; (b) Pixels Defining the Edges; and (c) External Portion Clipped

At the zero-th level, namely for the entire viewport, the only decision that can be made is that the x-y extent of the triangle does not intersect the extent of any of the other polygons, and hence can be declared visible. The subdivision of the viewport and further analysis are illustrated in Figure 3.15. In the figure, the z locations of the five objects are schematically indicated on the left, with T being closest to the viewer.

66

Introduction to Computer and Computer Graphics

Figure 3.15 : Subdivision Method Worked Example : (a) Stages and Decisions; (b) Details for Stages III and IV; (c) Final Plot; and (d) z Locations

It has to be recognized that, in a real-life problem, the resolution may be of the order of 1024 by 768, and many lines and surfaces will have to be reduced to the pixel level for decisions. However, the decisions themselves are very basic, involving only the identification of the attributes of the regions considered, with a few simple computations. The procedure is summarized in Table 3.1. If two polygons overlap or intersect, their edge crossings and penetration lines will have to be determined by computation, and then the algorithm applied.

Table 3.1 Level Segment Polygon Decision

0 I

All 1

2, 3, 4

T Q

Q, S, H, V

Contained. No other overlap. Visible. Extent Contained. Visible. Cannot decide. Further Subdivide.

II 2.1, 2.2 2.3 2.4 2.4

3.1, 3.3 3.2 3.4 4.1 4.2 4.3 4.4

Q Q Q S

None Q H Q

Q H Q V Q

Q, V

Contained, Visible. Surrounding, Visible. Surrounding, Visible. Contained, Behind Q. Hidden. Background colour. Q Surrounding, Visible. H Contained, in front of Q. Visible. Contained, Visible. Q Surrounding, Visible. H Contained, in front of Q. Visible. Q Surrounding, Visible. V Contained, in front of Q. Visible. Contained, Visible. Cannot decide. Further Subdivide.

III 4.4.1 4.4.2 4.4.3 4.4.4

Q V Q, V

V V

Contained, Visible. Surrounding, in front of Q, Visible, covers Q. Cannot decide. Further Subdivide. Contained, Visible. Surrounding, in front of Q, Visible, covers Q. Surrounding, Visible.

IV 4.4.2.1 4.4.2.2 4.4.2.3 4.4.2.4

Q, V Q V

None

Both Surrounding. V in front of Q, Visible, covers Q. Surrounding, Visible. Surrounding, Visible. Background colour.

SAQ 2 (a) Some special cases, which cause problems for some hidden-surface

algorithms, are penetrating faces and cyclic overlap. A penetrating face occurs when polygon A passes through polygon B. Cyclic overlap occurs when polygon A is in front of polygon B, which is in front of polygon C, which is in front of Polygon A. Actually, we need only two polygons for cyclic overlap; imagine a rectangle threaded through a polygon shaped like

67

Visual Realismthe letter C so that it is behind the top of the C but in front of the bottom part. For the various hidden-surface methods we have presented, discuss whether or not they can handle penetrating faces and cyclic overlap.

Visual Realism

(b) (i) Show that no polygon subdivision takes place in applying the binary space partition method to a convex object.

(ii) For the case of convex object compare the cost of the back-face removal method with that of the binary space partition method for a single view.

(iii) Suppose we wish to display a sequence of views of a convex object. How would the cost of using back-face removal compare to the binary space partition scheme?

(c) Modify the back-face algorithm for unifilled polygons so that instead of removing back faces it draws them in a less pronounced line style (e.g., as dashed lines).

(d) Test the painter’s algorithm by showing several filled polygons with different interior styles and different states of overlap, entered in mixed order.

(e) Test the painter’s algorithm by showing two houses composed of filled polygons with different interior styles. Select a view such that one house partially obscures the other house.

(f) Sketch the minimax boxes for the tangent polygons shown in figure. What conclusions can you make?

(g) Find the face and priority lists of scenes in figure. Assume that back faces have been removed to simplify the problem.

3.4 RENDERING, SHADING AND COLOURING

By introducing hidden line removal we have already taken one step away from wire-frame drawings towards being able to realistically model and display 3-D objects. Perhaps the biggest step down that road comes when attempting to “colour in” our simple line drawings. The various algorithms for rendering, the process of applying lighting,

68

Introduction to Computer and Computer Graphics

colouring, shadow and texture to an object or scene in order to obtain a realistic image, are all based to a greater or lesser extent on the study of the physical properties of light. In this unit we shall examine various properties of light and the way it interacts with objects and develop some simple mathematical models of its behaviour. It is worth setting the following discussion in the context of our system as developed so far. Currently our 3d model is made up of surfaces, each of which we represent on the screen by drawing its outline. If we wanted to shade each polygon (“colour it in”) what color would we use? What we basically are trying to achieve in this chapter is to derive a method for calculating that colour. Figure 3.16 shows the difference between a wire-frame representation and a simple rendered version.

Figure 3.16 : Wire-frame Representation and Rendered Representation

3.4.1 Illumination

The colour or shade that a surface appears to the human eye depends primarily on three factors :

• Colour and strength of incoming illumination

• Colour and texture (rough/smooth) of surface

• Relative positions and orientations of surface, light source and observer

3.4.2 Simplifying Assumptions

Neglect colour - consider Intensity: For now we shall forget about colour and restrict our discussion just to the intensity of light. We shall consider white light where the intensity of all colour components (red, green and blue) is equal. This will give us a monochrome (black and white) picture.

Intensity and the 1/r Effect: Usually a light source is a mixture of diffuse background illumination and one or more point sources of light. For the purpose of this discussion (mainly to simplify the mathematics) we shall assume that the light source we are considering is at a large distance from the scene.

This has two effects :

• All light rays from the source are (virtually) parallel

• There is no change in the intensity of the light across the scene – i.e. there is no fall off of intensity as a 1/r 2 effect.

69

Visual RealismVisual Realism

Figure 3.17 : Difference between Close and Distance Light Sources

In this simple diagram, the intensity of light falling on a surface can be thought of simply as the number of rays, which hit it. It can be seen that more rays fall on cube A than on cube B and it can quite easily be shown that the number is proportional to the reciprocal of the square of the distance between the light source and the object (i.e. 1/r where r is the distance between source and surface).

Parallel Rays : It can also be seen in Figure 3.17 that the light rays crossing cube B are nearly parallel whereas the rays crossing cube A are highly divergent. This means for distant illumination, there is little variation in intensity between one side of an object and the other (which means we only need to do one calculation of intensity for the whole surface), whereas this is not true for close illumination. If the need exists to implement a physically accurate illumination model, we could not make this assumption and would have to take account of these effects, but for most purposes, the simple model will suffice.

3.4.3 Components of Illumination Consider now the light reaching the eye of an observer of a scene:

Figure 3.18 : Basic Illumination

The light reaching the eye when looking at a surface has clearly come from a source (or sources) of illumination and bounced off the surface. In fact the light reaching the eye can be considered as being made up of 3 different components :

• that from diffuse illumination (incident rays come from all over not just one direction)

• that from a point source which is scattered diffusely from the surface • that from a point source which is specularly reflected.

We will consider each of these components separately and then combine them into one.

70

Introduction to Computer and Computer Graphics 3.4.4 Diffuse Illumination

Diffuse illumination means light that comes from all directions not from one particular source. Think about the light of a grey cloudy day as compared to a bright sunny one : On a cloudy day, there are no shadows cast, the light from the sun is scattered by the clouds and seems to come equally from all directions.

Surface

Figure 3.19 : Diffuse Illumination

Some proportion of the light reaching our surface is reflected back to the observer. That proportion is dependent simply on the properties (colour) of the surface and has no dependence on the angle of the viewer (that is why diffuses). If the strength of the incident illumination is Id and the observed intensity is Ed then the two are related by the simple formula

Ed = R . Id . . . (3.1) where, R is the reflection coefficient of the surface (0 ≤ R ≤ 1), which is a proportion of the light that is reflected back out. Diffuse illumination alone does not give visual realism. With no angular dependence in the light, the observer will see no difference between a sphere and a disc. For a perfect reflector, all the incident light would be reflected back out in the direction of S. In fact, when the light strikes a surface it is scattered diffusely (i.e. in all directions) : For an observer viewing at an angle (s) to the reflection direction S, some fraction of the reflected light is still visible (due to the fact that the surface isn’t a perfect reflector – some degree of diffusion takes place). The amount of proportion of light visible is a function of the angle s (in fact it is proportional to cos (s)). It also depends on the quality of the surface and the angle of incidence i. We can define a coefficient w (i) – the specular reflection coefficient – which is a function of the material of which the surface is made and of i. Each surface will have its own w (i).

Figure 3.20 : Specular Reflection Coefficient – w (i)

Putting all of this together gives :

( ) . cos ( ) .nss sE w i s I= . . . (3.2)

where, Ess is the intensity of the light ray in the direction of O, n is a fudge factor : n = 1 – rough surface (paper) n = 10 smooth surface (glass),

71

Visual Realismw (i) is usually never calculated – simply choose a constant (0.5?). Visual Realism

It is actually derived from the physical properties of the material from which the surface is made. Eq. (3.2) contains the term cosn (s). This is in fact a fudge which has no basis in physics, but works to produce reasonable results. By raising cos (s) to the power n, what we do is control how much the reflected ray spreads out as it leaves the surface.

cosn (s)

n = 1

n = 10

s 0

Figure 3.21 : Controlling the Reflection – the Action of n

3.4.5 Combining the Illumination Combining all three components (diffuse illumination, diffuse reflection from a point source and specular reflection from a point source) gives us the following expression:

d sdE E E E= + + ss . . . (3.3)

or written out in full :

. ( cos ( ) ( ) . cos ( )) .nd sE R I R i w i s I= + + . . . (3.4)

where, E is the total intensity of light seen reflected from the surface by the observer.

3.5 APPROXIMATING SMOOTH SURFACES WITH POLYGON NETS

Networks of polygons are used to represent smooth surfaces. They are, of course, only an approximation to the true surface, but they are adequate for most computer graphics tasks. One of the commonest uses of the technique is to approximate a cylinder by the use of a set of flat polygons.

The Lambert shading model is a great improvement over wire-frame drawings as far as realism is concerned. It is also a reasonably fast technique when compared to some of the alternatives that we are about to meet. It is, however, far from perfect. Using a different colour for each polygon means that the polygons show up very clearly (the appearance of the model is said to be faceted). This is fine for a cube, but not so good when attempting to use the polygon as part of a smooth surface. Figure 3.22 shows 32 flat surfaces, arranged in a circle, pretending to be a cylinder. The difference in intensity between neighbouring sections can easily be seen.

72

Introduction to Computer and Computer Graphics

Figure 3.22 : Faceted Appearance

The problem is made worse by a phenomena known as Mach bands. This is a physiological effect whereby the contrast between two areas of a different shade is enhanced as the border between the shades. If you stare closely at the boundary of two neighbouring surfaces in Figure 3.22, you can see that the joint appears to be slightly lighter than the surfaces.

Actual Intensity

Perceived Intensity

Figure 3.23 : Mach Banding

3.5.1 Gouraud Shading The faceted appearance of a Lambert shaded model is due to each polygon having only a single colour. To avoid this effect, it is necessary to vary the colour across a polygon :

Figure 3.24 : Variable Shading of an Individual Polygon

In order to achieve this, the colour must be calculated for each pixel instead of one colour for the entire polygon. By ensuring that the method we use to calculate the colour results in the neighbouring pixels across the border between two polygons end up with approximately the same colours, we will be able to blend the shades of the two polygons and avoid the sudden discontinuity at the border.

Lambert shading is based upon calculating a single normal vector for the surface (which is then compared to the lighting vector and the viewpoint vector to determine the colour). Gouraud shading is based upon calculating a vertex normal rather than a surface normal. A vertex normal is an artificial construct (a true normal cannot exist for a point such as a

73

Visual Realismvertex). A vertex normal can be thought of as the average of the normals of all the polygons that share that vertex.

Visual Realism

Figure 3.25 : A Vertex Normal

nv can be calculated from :

1

1

Ni

iv N

ii

nn

n

=

=

∑=

∑ . . . (3.5)

where, N is the number of polygons sharing the vertex.

Figure 3.26 : Find the Vertex Normal at Each Vertex

Having found the vertex normals for each vertex of the polygon we want to shade (Figure 3.26), we can calculate the colour at each vertex using the same formula that we did for Lambert Shading. Calculating the colour for all the remaining pixels in the polygon is simply a matter of interpolating from the vertices, i.e. if your half-way along one of the edges, the colour value needs to be half-way between the colour values at the ends of the edge. A value for the colour can be given more formally by considering a scan-line through the polygon.

Figure 3.27 : Find the Vertex Normal at Each Vertex

74

Introduction to Computer and Computer Graphics

If we stick simply to gray-scale values, the intensity of light at point P (Ip) can be calculated thus :

1 ( ) . c scas c c a

c a

y yI I I Iy y

n−= − −

2( ) . c sca

s c c bc b

y yI I I Iy y

n−= − −

2

2 2 12 1

( ) . s PP s s s

s s

x xI I I I

y y−

= − −−

By performing 3 separate calculations, one for red, one for green and one for blue, a complete colour value can be achieved.

3.5.2 Phong Shading Phong shading too is based on interpolation, but instead of interpolating the colour value, it is the normal vector, which is interpolated for each point and a colour value calculated for each pixel based on the interpolated value of the normal vector.

The interpolation is (like Gouraud shading) based upon calculating the vertex normals (red arrows in Figure 3.28), using these as the basis for interpolation along the polygon edges (blue arrows) and then using these as the basis for interpolating along a scan line to produce the internal normals (green vectors).

Figure 3.28 : Stages of Interpolation in Phong Shading

Phong shading allows us to counteract the fact that we are using a flat surface to approximate to a curved one.

The arrows (and thus the interpolated vectors) give an indication of the curvature of the smooth surface, which the flat polygon is approximating to.

3.5.3 Comparison of Gouraud and Phong Shading Phong shading requires more calculations, but produces better results for specular reflection than Gouraud shading in the form of more realistic highlights.

Consider the specular reflection term in Eq. (3.4) :

cosn (s)

If n is large (the surface is a good smooth reflector) and one vertex has a very small value of s (it is reflecting the light ray in the direction of the observer) whilst the rest of the vertices have large values of s – a highlight occurs somewhere on our polygon.

With Gouraud shading, nowhere on the polygon can have a brighter colour (i.e. higher value) than a vertex so unless the highlight occurs on or near a vertex, it will be missed out altogether. When it is near a vertex, its effect is spread over the whole polygon. With Phong shading however, an internal point may indeed have a higher value than a vertex and the highlight will occur tightly focused in the (approximately) correct position.

75

Visual Realism

Visual Realism

Figure 3.29 : Comparison of Highlights with Gouraud and Phong Shading

3.6 COLOURING

The use of colours in CAD/CAM has two main objectives : facilitate creating geometry and display images. Colours can be used in geometric construction. In this case, various wireframe, surface, or solid entities can be assigned different colours to distinguish them. Colour is one of the two main ingredients (the second being texture) of shaded images produced by shading algorithms. In some engineering applications such as finite element analysis, colours can be used effectively to display contour images such as stress or heat-flux contours.

Black and white raster displays provide achromatic colours while colour displays (or television sets) provide chromatic colour. Achromatic colors are described as black, various levels of gray (dark or light gray), and white. The only attribute of achromatic light is its intensity, or amount. A scalar value between 0 (as black) and 1 (as white) is usually associated with the intensity. Thus, a medium gray is assigned a value of 0.5. For multiple-plane displays, different levels (scale) of gray can be produced. For example, 256(28) different levels of gray (intensities) per pixel can be produced for an eight-plane display. The pixel value Vi (which is related to the voltage of the deflection beam) is related to the intensity level Ii by the following equation :

1/c

iIVC

γ⎛ ⎞= ⎜ ⎟⎝ ⎠

. . . (3.6)

The values C and γ depend on the display in use. If the raster display has no lookup table, Vi (e.g., 00010111 in an eight-plane display) is placed directly in the proper pixel. If there is a table, i is placed in the pixel and Vi is placed in entry i of the table. Use of the lookup table in this manner is called gamma correction, after the exponent in Eq. (3.6).

CMY

YIQ

HSV

HSL

RGB

Gamma Correction

Colour Lookup Table Values

Figure 3.30 : Transformation of a Colour Model to RGB

76

Introduction to Computer and Computer Graphics

Chromatic colours produce more pleasing effects on the human vision system than achromatic colours. However, they are more complex to study and generate. Colour is created by taking advantage of the fundamental trichromacy of the human eye. Three different coloured images are combined additively at photo-receptors in the eye to form a single perceived image whose colour is a combination of the three prime colours. Each of the three images is created by an electron gun acting on a colour phosphor. Using shadow-mask technology, it is possible to make the images intermingle on the screen, causing the colours to mix together because of spatial proximity. Well-saturated red, green, and blue colours are typically used to produce the wide range of desired colours.

Colour descriptions and specifications generally include three properties: hue; saturation and brightness. Hue associates a colour with some position in the colour spectrum. Red, green, and yellow are hue names. Saturation describes the vividness or purity of a colour or it describes how diluted the colour is by white light. Pure spectral colours are fully saturated colours and grays are desaturated colours. Brightness is related to the intensity, value, or lightness of the colour.

There exists a rich wealth of studies and measure colours. Some methods are subjective such as Munsell and pigment-mixing methods. The Munsell method is widely used and is based on visually comparing unknown colours against a set of standard colours. The pigment-mixing method is used by artists. Other methods used in physics are objective and treat visible light with a given wavelength as an electromagnetic energy with a spectral energy distribution.

3.6.1 Trivariate Colour Models Conventional colour models based on the tristimulus theory all contain three variables and so are called trivariate models. Let us now consider three of the most useful models, the conversion relationships between them, and the most widely accepted colour standard.

Figure 3.31

Projection of Three Primary Colours of the RGB Model

In this additive process, an equal mixture of red, green, and blue light produces white light, while mixtures of two of the three primaries produce cyan, magenta, and yellow (C, M, Y).

3.6.2 RGB Model

The RGB model is based on the assumption that any desired shade of colour can be obtained by mixing the correct amounts of red, green, and blue light. The exact hues chosen are not important as long as they include a long wavelength hue (red), a medium wavelength hue (green), and a short wavelength hue (blue). If, for instance, circular red, green, and blue beams are projected onto a white screen in a darkened room, we get the colour pattern shown in Figure 3.31.

The additive nature of the RGB model is very apparent in Figure 3.31. Adding red, green, and blue light produces white light, while adding red and blue light produces magenta light, and so on. This linear superposition is expressed mathematically as :

C rR gG bB= + + . . . (3.9)

77

Visual Realism where, C = colour of resulting light, Visual Realism

(r, g, b) = colour coordinates,

(R, G, B) = red, green, blue primary colours.

Figure 3.32 : RGB Colour Cube

Note how the primary colours define unit vectors along the axes. The three corners opposite R, G, and B are cyan (C), magenta (M), and yellow (Y), the basis of the CMY model. The line connecting black (0,0,0) and white (1,1,1) is the gray scale line.

Figure 3.32 illustrates the coordinates and colours of the corners of the RGB colour cube. Most light, however, can be represented by a 3-D colour vector which terminates at some arbitrary point in the interior of the cube. To understand the additional shadings possible with the colour cube representation, consider the shadings possible on the surface of the cube. In Figure 3.33, a transformed view of the colour cube is presented in which sub-cubes interpolate the colour between the four corners of the cube.

Figure 3.33 : Transformed RGB Colour Cube with Interpolated Hues

The RGB colour model is particularly important because it is the basis for control of most colour monitors. For this reason it is also the preferred colour model for graphics languages and image processing programs. A typical interactive RGB colour picker for selecting the three colour coordinates is shown in Figure 3.34.

78

Introduction to Computer and Computer Graphics

Figure 3.34

Filtering of White Light by Cyan, Magenta, and Yellow Filters

In this subtractive process, the magenta filter subtracts green light out of the white beam, leaving only its complement, magenta. Subtracting all three colors leaves no light at all, black.

3.6.3 CMY Model The cyan, magenta, yellow (CMY) colour model is a subtractive model based on the colour absorption properties of paints and inks. As such it has become the standard for many graphics output devices like ink jet and thermal transfer printers. The principle of the CMY model is illustrated in Figure 3.34 in which white light beamed toward the viewer is intercepted by partially overlapping cyan, magenta, and yellow filters. The cyan filter removes red light from the beam and passes only cyan, the complementary colour to red.

Figure 3.35

Interactive colour picker supporting both the RGB colour model and the HSV (hue, saturation, value) colour model (Figure 3.35). The user can select any hue from the colour wheel by either pointing and clicking or by numerical control of the RGB arrows. The brightness is controlled by the slide control along the right-hand side.

In the printing trade this model is frequently called the CMYK model in which the K stands for black. The reason for black is that, although theoretically the correct mixture of cyan, magenta, and yellow ink should absorb all the primary colours and print as black, the best that can be achieved in practice is a muddy brown. Therefore, printers like the Hewlett-Packard Paint Jet have a separate cartridge for black ink in addition to the cyan, magenta, and yellow ink cartridge(s).

The CMY model can also be represented as a colour cube as shown in Figure 3.36.

79

Visual Realism Visual Realism

Blue (1, 1, 0)

Cyan (1, 0, 0)

Green (1, 0, 1) Yellow (0, 0, 1)

Red (0, 1, 1)

Magenta (0, 1, 0)

Black (1, 1, 1)

White (0, 0, 0)

M

C

Y

Grey Axis

Figure 3.36 : The CMY Color Cube

Each corner is labeled with its (c, m, y) coordinates. Note that the RGB colour cube is transformed into a CMY colour cube by interchanging colours across the major diagonals.

One can understand the subtractive nature of the CMY model in the following sense. When white light falls on a white page, virtually all the light is reflected and so the page appears white. If white light strikes a region of the page, which has been printed with cyan ink, however, the ink absorbs the red portion of the spectrum and only the green and blue portions are reflected. This mixture of reflected light appears as the cyan hue.

In terms of the CMY colour cube coordinates, one can think of the origin, (0, 0, 0), as three colour filters with a tint so faint that they appear as clear glass. In terms of absorbing inks, the origin corresponds to pastel shades of cyan, magenta, and yellow so faint as to appear white. As one moves up along the M axes from (0, 0, 0) towards (0, 1, 0), it corresponds to turning the density of a tinted filter up towards the maximum possible. In terms of inks, this motion up the M axis corresponds to moving from a pale pastel towards a pure magenta. If one uses all three filters in sequence (or a mixture of C, M, and Y inks), eventually all light is absorbed as one gets to pure colours of filters or inks. This is point (1, 1, 1).

The RGB and CMY colour cubes are useful in expressing the transformations between the two colour models. Suppose, for instance, that we know a certain ink may be specified by the CMY coordinates, (C, M, Y), and we would like to know what mixture of light, specified as (R, G, B) in the RGB cube, is reflected. Looking at Figure 3.38 we note the following 3-D vector relationships:

The inverse transformation can be thought of as solving the following problem : Given light of a certain colour, (R, G, B), reflected from a page illuminated with white light, what mixture of ink, (C, M, Y), is required? Using Figure 3.32, we can write a set of equations resembling with White substituted for Black. Since, on the RGB colour cube, white has coordinates (1, 1, 1).

The CMYK colours are the Process Colours of offset printing. Several image processing, drawing, and desktop publishing programs now have the capability of the colour separation of colored images. The process of colour separation involves producing four black-and-white images (or negative images) corresponding to the four colours, cyan, magenta, yellow, and black. These separations are then used photographically to produce the four plates for each of the four inks of the offset press. To produce the final colour image, each sheet is printed separately with each of the four-colour plates. Since alignment is critical, accurate crosshairs are printed on each of the four-colour negatives to assist the printers in achieving good colour registry.

80

Introduction to Computer and Computer Graphics

We can express the conversion from an RGB representation to a CMY representation with the matrix transformation

111

C RM GY B

⎡ ⎤ ⎡ ⎤ ⎡ ⎤⎢ ⎥ ⎢ ⎥ ⎢= − ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦ ⎣ ⎦ ⎣ ⎦

where the white is represented in the RGB system as the unit column vector. Similarly, we convert from a CMY colour representation to an RGB representation with the matrix transformation

111

R CG MB Y

⎡ ⎤ ⎡ ⎤ ⎡ ⎤⎢ ⎥ ⎢ ⎥ ⎢= − ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦ ⎣ ⎦ ⎣ ⎦

where black is represented in the CMY system as the unit column vector.

3.6.4 YIQ Colour Model Whereas an RGB monitor requires separate signals for the red, green, and blue components of an image, a television monitor uses a single composite signal. The National Television System Committee (NTSC) colour model for forming the composite video signal is the YIQ model. In the YIQ colour model, luminance (brightness) information is contained in the Y parameter, while chromaticity information (hue and purity) is incorporated into the I and Q parameters. A combination of red, green, and blue intensities are chosen for the Y parameter to yield the standard luminosity curve. Since Y contains the luminance information, black-and-white television monitors use only the Y signal. The largest bandwidth in the NTSC video signal (about 4 MHz) is assigned to the Y information. Parameter I contains orange-cyan hue information that provides the flesh-tone shading, and occupies a bandwidth of approximately 1.5 MHz. Parameter Q carries green-magenta hue information in a bandwidth of about 0.6 MHz. An RGB signal can be converted to a television signal using an NTSC encoder, which converts RGB values to YIQ values, then modulates and superimposes the I and Q information on the Y signal. The conversion from RGB values to YIQ values is accomplished with the transformation

0.299 0.587 0.1440.596 0.275 0.3210.212 0.528 0.311

Y RI GQ B

⎡ ⎤ ⎡ ⎤ ⎡ ⎤⎢ ⎥ ⎢ ⎥ ⎢ ⎥= − −⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥−⎣ ⎦ ⎣ ⎦ ⎣ ⎦

This transformation is based on the NTSC standard RGB phosphor, whose chromaticity coordinates were given in the preceding section. The larger proportions of red and green assigned to parameter Y indicate the relative importance of these hues in determining brightness, compared to blue. An NTSC Video signal can be converted to an RGB signal using an NTSC decoder, which separates the video signal into the YIQ components, then converts to RGB values. We convert from YIQ space to RGB space with the inverse matrix transformation from above Equation.

1.000 0.956 0.6201.000 0.272 0.6471.000 1.108 1.705

R YG IB Q

⎡ ⎤ ⎡ ⎤ ⎡ ⎤⎢ ⎥ ⎢ ⎥ ⎢ ⎥= − −⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥−⎣ ⎦ ⎣ ⎦ ⎣ ⎦

81

Visual RealismVisual Realism3.6.5 HSV Colour Model

Instead of a set of colour primaries, the HSV model uses colour descriptions that have a more intuitive appeal to a user. To give a colour specification, a user selects a spectral colour and the amounts of white and black that are to be added to obtain different shades, tints, and tones. Colour parameters in this model are hue (H), saturation (S), and value (V),

(a) RGB Colour Cube (b) Colour Hexagon

Figure 3.37 : When the RGB Colour Cube (a) is Viewed along the Diagonal from White to Black, the Colour-cube Outline is a Hexagon (b)

The three-dimensional representation of the HSV model is derived from the RGB cube. If we imagine viewing the cube along the diagonal from the white vertex to the origin (black), we see an outline of the cube that has the hexagon shape shown in Figure 3.39. The boundary of the hexagon represents the various hues, and it is used as the top of the HSV hexcone (Figure 3.38). In the hexcone, saturation is measured along a horizontal axis, and value is along a vertical axis through the centre of the hexcone.

Hue is represented as an angle about the vertical axis, ranging from 0° at red through 360°. Vertices of the hexagon are separated by 60° intervals. Yellow is at 60°, green at 120°, and cyan opposite red at H = 180°. Complementary colours are 180° apart.

Figure 3.38 : The HSV Hexcone

82

Introduction to Computer and Computer Graphics

Figure 3.39 : Cross-section of the HSV Hexcone, Showing Regions for Shades, Tints, and Tones

Saturation S varies from 0 to 1. It is represented in this model as the ratio of the purity of a selected hue to its maximum purity at S = 1. A selected hue is said to be one-quarter pure at the value S = 0.25. At S = 0, we have the gray scale. Value V varies from 0 at the apex of the hexcone to 1 at the top. The apex represents black. At the top of the hexcone, colours have their maximum intensity. When V = 1 and S = 1, we have the “True” hues. White is the point at V = 1 and S = 0.

This is a more intuitive model for most users. Starting with a selection for a pure hue, which specifies the hue angle H and sets V = S = 1, we describe the colour we want in terms of adding either white or black to the pure hue. Adding black decreases the setting for V while S is held constant. To get a dark blue, V could be set to 0.4 with S = 1 and H = 240°. Similarly, when white is to be added to the hue selected, parameter S is decreased while keeping V constant. A light blue could be designated with S = 0.3 while V = 1 and H = 240°. By adding some black and some white, we decrease both V and S. An interface for this model typically presents the HSV parameter choices in a colour palette.

Shades

S

Colour concepts associated with the terms shades, tints, and tones are represented in across-sectional plane of the HSV hexcone (Figure 3.41). Adding black to a pure hue decreases V down the side of the hexcone. Thus, various shades are represented with values S = 1 and 0 ≤ V ≤ l. Adding white to a pure tone produces different tints across the top plane of the hexcone, where parameter values are S = 1 and 0 ≤ V ≤ l. Various tones are specified by adding both black and white, producing colour points within the triangular cross-sectional area of the hexcone.

The human eye can distinguish about 128 different hues and about 130 different tints (saturation levels). For each of these, a number of shades (value settings) can be detected, depending on the hue selected. About 23 shades are discernible with yellow colours, and about 16 different shades can be seen at the blue end of the spectrum. This means that we can distinguish about 128 × 130 × 23 = 82,720 different colours. For most graphics applications, 128 hues, 8 saturation levels, and 15 value settings are sufficient. With this range of parameters in the HSV colour model, 16,384 colours would be available to a user, and the system would need 14 bits of colour storage per pixel. Colour lookup tables would be used to reduce the storage requirements per pixel and to increase the number of available colours.

83

Visual Realism3.6.6 HLS Colour Model Visual Realism

This model has the double-cone representation shown in Figure 3.40. The three colour parameters in this model are called hue (H), lightness (L), and Saturation (S). Hue has the same meaning as in the HSV model. It specifies an angle about the vertical axis that locates a chosen hue. In this model, H = 00 corresponds to blue. The remaining colours are specified around the perimeter of the cone in the same order as in the HSV model. Magenta is at 60°, red is at 120°, and cyan is located at H = 180°. Again, complementary colours are 180° apart on the double cone. The vertical axis in this model is called lightness, L. At L = 0, we have black, and white is at L = 1. Gray scale is along the L axis, and the “pure hues” lie on the L = 0.5 plane. Saturation parameter S again specifies relative purity of a colour. This parameter varies from 0 to 1, and pure hues are those for which S = 1 and L = 0.5. As S decreases, the hues are said to be less pure. At S = 0, we have the gray scale. As in the HSV model, the HLS system allows a user to think in terms of making a selected hue darker or lighter. A hue is selected with hue angle H, and the desired shade, tint, or tone is obtained by adjusting L and S. Colours are made lighter by increasing L and made darker by decreasing L. When S is decreased, the colours move toward gray.

Figure 3.40 : The HLS Double Cone

3.7 SUMMARY

Hidden line and surface removal eliminates the ambiguities of the displays of three-dimensional models and is considered the first step toward visual realism.

84

Introduction to Computer and Computer Graphics

A large number of techniques have been developed for removing hidden surfaces and lines which is a fundamental problem in computer graphics. Hidden surface removal (HSR) algorithms are complex and take long execution times. A great effort is paid to improve their efficiency. Though wire-frame views of the objects can be drawn very rapidly, it is difficult to interpret them when a several objects in scene overlap. Visual realism is greatly enhanced when the faces of the objects are filled with some colours and the surfaces that should be hidden are removed.

This unit discusses about various algorithms for hidden line or hidden surface removal. The pictures that are rendered by filling the faces of the object with colours and removing the hidden surfaces still do not give the impression of objects residing in a scene, illuminated by light source. A shading model describes how light reflects off a surface, depending on the nature of the surface and its orientation to both light source and the camera’s eye. Shading procedure like Gouraud and Phong shading are discussed.

This unit also discusses about the nature of colour and how to represent it in computer graphics use of various colour spaces were discussed.

3.8 ANSWERS TO SAQs

Please refer the relevant preceding text for all the answers to SAQs.

85

Visual RealismVisual RealismFURTHER READINGS David F. Rogers and J. Alan Adams (2002), Mathematical Elements of Computer Graphics, TMH.

David F. Rogers (2002), Procedural Elements for Computer Graphics, TMH.

Glen. Mullineux (1986), CAD : Computational Concepts, Macmillan Publishing, New York.

Ibrahim. Zied (1991), CAD/CAM Theory and Practice, McGraw-Hill, New York.

M. Groover and P. Mikell (1984), CAD/CAM Computer Aided Design and Manufacturing, Englewood Clifffs : Prentice Hall.

Micheal E. Mortemson (1990), Computational Geometry, Industrial Press, New York.

P. N. Rao (2004), CAD/CAM : Principles and Applications, Tata McGraw-Hill.

Introduction to Computer and Computer Graphics INTRODUCTION TO COMPUTER AND

COMPUTER GRAPHICS In this block, the basics of computer aided design are discussed. This block consists of three units.

Unit 1 deals with the hardware aspects necessary to know for computer aided design. It introduces various displace devices and input/output devices.

After discussing the current computer hardware for CAD in Unit 1, Unit 2 describes the concepts of computer graphics applicable to computer aided design. In the beginning, concept of 2-D transformations have been discussed followed by 3-D transformation. Various types of projections have also been detailed.

Finally, in Unit 3, visual realism fundamentals have been discussed. This unit discusses various hidden lines and surface removal algorithms like z-algorithms, back face detection methods, painter’s algorithms etc. along with their application. Also, various methods of rendering, colouring and shading are elaborated.

86

Visual RealismVisual RealismCOMPUTER AIDED DESIGN This course consists of two blocks and contains seven units.

Block 1 consists of three units which covers topics like display devices along with detailed discussion on raster refresh. Input and output devices are also detailed. Unit 2 covers 2-dimensional transformation including reflection and geometric interpretation of homogeneous coordinates. This is further extended to 3-dimensional transformation and projection. In this block, also visual realism, is devoted to the concepts of hidden line and surface removal algorithms. After this, methods of rendering, colouring and shading are also described.

In Block 2, there are four units. First unit, i.e. Unit 4, describes the geometric modeling of curves. In this unit, curve definition procedure for both explicit and parametric representation are presented. Curve definition techniques also include the use of conic sections, circular arc interpolation, cubic splines, parabolic blending, Bezier curves and curves based on B-splines. In the next unit of this block, i.e. Unit 5, methods to generate surface are discussed. In case the model is existing or it is to be created from the scratch. Surface modeling is followed by solid modeling. Various methods of solid modeling along with the methods to create 3-Dimensional solid models are described. In the last unit, CAD/CAM data exchange concepts are given. Here, different types of interfaces along with the details of UPR architecture are detailed.

87