Comput. & Graphics Vot. 9. No. 3, pp. 259-265, 1985 Printed in Great Britain,
0097-8493/85 $3.00 + .00 ~ 1985 Pergamon Press Ltd.
Technical Section
ALGORITHMS FOR ANTIALIASED CAST SHADOWS J. CHARLES HOURCADEand A. NICOLAS Institut National de la Communication Audiovisuelle,4, avenue de l'Europe, 94360 Bry sur Marne, France
Abstract--The ability to render the shadows cast by objects in a scene is of prime importance in augmenting the realism of sequencesproduced by image synthesisalgorithms. From a theoretical standpoint, the problem does not present a great number of difficulties: It requires an integration in the process, of the viewpoint from the light source and the utilization of this information when calculating the lighting of the pixels. The purpose of this study is not to present a radically new approach, but to explore in depth the most common problem present in these images, the aliasing,or more generallyspeaking, the sampling problem. This work has implied a specific approach in the hidden surface determination, the introduction of a variation of the already used A-buffersor Z-buffers.as well as an extensive study of the filteringproblem.
I. POSITION OF THE PROBLEM, EXISTING METHODS
The principal requirement for a scan-line software today is to be able to produce high quality images in a minimum of time, in other words, in a much shorter period of time than is required by ray tracing methods. It is thus necessary to have in one's possession when calculating the image all the information relative to shadowing, preprocessed in a minimum of time. Two major approaches have been developed. The first has been analyzed by Crow [ I, 2]. It consists of integrating in the data base the "shadow volumes" obtained by extending the object's silhouette in the opposite direction of the light source, and to treat these resulting polygons as "false polygons," simply indications of the transition between the lighted and the shadowed zones. This approach obviously implies a rendering algorithm capable of processing the intersections of the surfaces (Fig. 1). The major drawback is in the di~culty entailed in processing curved surfaces as bicubic patches, currently used in modeling; in addition the utilization of this approach increases the complexity of the scene, imposes a systematic calculation of the intersections with the shadow volumes and represents a large software investment in comparison to preexisting scan-line algorithms. This is typically the "object space" method, independent of the resolution, but the problems in processing curved primitives render this method very di~cult to utilize. The second has been developed by Williams [3]. The general principle consists of memorizing in a Zbuffer the image of the scene as would be perceived by the light source; this image is thus produced during the first pass of the program, and when processing the second pass, it is possible to test each pixel while calculating its intensity: The point is transformed in the light source space and it's Z is compared to that which is stored in the Z-buffer. The result of this comparison determines the status (whether or not it is lighted) of the point (Fig. 2). This method has one considerable advantage: It is not dependent on the type of primitives used (polygons, patches, fractals . . . . ). In addition, an implementation
is greatly simplified since the same algorithm can be used twice. Nevertheless, there are several drawbacks: • The use of the first pass interjects an additional calculation time, which increases proportionally with the resolution used. • The access to the produced buffer being random, it is necessary to store it in main memory; the memory space used, corresponding to that of a "supplementary frame buffer," is not insignificant and increases as the square of the resolution used. • The precision errors of the Z values introduce some defaults and especially could cause some of the surface zones to self-shadow. • The difference between the sampling modes of the scene, perceived by the observer and by the light source bring about an aliasing phenomena which could be redhibitory. The advantages of this method justify a more in-depth study to reach satisfactory solutions for all of these problems. With regard to the work developed by Williams [3], the studies presented here are exposed in two directions: • Presentation of an original approach for the first pass, minimizing the calculation time and the memory space, and avoiding the quantization errors. • Exhaustive study of the aliasing problems evoked in [3] presentation of two different filtering algorithms and evaluation of their relative performance. 2. FIRST PASS: THE P-BUFFER
Intensities are not calculated in this first pass; thus the only preoccupying problem is that of hidden surfaces. Also, only the contours are used; the uniform zones are of no interest. The ideal solution is then to work with a rather rough definition and to use a good under-resolution inside the pixel. Here, again, we find the general principle of under-sampling masks introduced by Fournier [4] and developed by Carpenter [5], with the concept of A-buffer. Because of the low resolution at the initial step and the absence of intensity calculation, the total calculation time remains low. The quantization errors in the Z calculation are in
259
26O
J. CHARLES HOURCADE and A. NICOLAS
Fig. 1.
fact inevitable: The fact of storing (quantification) a Z value obtained by a first calculation mode (hidden surfaces of the first pass) and of comparing it with a value obtained by another calculation mode (transformation from observer space to light source space) can only generate errors, the most obvious of which is the selfshadowing. The most common solution of the bias contributes only to the displacement of the problem and is not aesthetically very satisfying. In fact, the numerical value of the stored Z does not present in itself any advantage: Only the results of the test carry any important information. The right question to ask is not "at what depth am I in the light source space?," but rather "am I in the foreground?," which is much easier. So, it isn't necessary to store the numerical value; the priority information is sufficient. This implies the utilization of an algorithm accurately sorting the surfaces in priority order, beginning with the foreground and .moving towards the background in the light source space. A scan-line algorithm responding to this criteria has been developed; the segments present on each line are first sorted in X and then in their priority order. The methods of construction of the sorted list are inspired by the work of Sharaps [6]. A complete description of this algorithm would exceed the limits of this article, but it is not necessary to go into an excessive algorithmic devel-
Fig. 2.
opment if any kind of classical Watkins-type algorithm is used [2, 7]. Thanks to this algorithm, it is possible to simply store the "number" of the first surface encountered on each pixel, and that of the second if the first doesn't entirely cover the pixel. The percentage of surface, obtained by using the masks [4] is then memorized as well. These three pieces of information can be stored on 8 bits each, and are sufficient in giving correct information to the second pass (Fig. 3). This structure of priority buffer, that we are calling P-buffer, is probably one of the most compact structures that can be imagined for processing antialiased hidden surfaces.
Remarks • The utilization of an identifying number for the surfaces permits a large degree of flexibility in managing the process of cast-shadow synthesis. It permits, most notably, to define undersets of surface pieces and prohibits self-shadowing; this classification can be made automatically from hierarchical data bases [8]. Certain surfaces can receive shadows but can not cast them (which permits not treating them during the first pass), or the reverse, etc. • A structure of this simplicity has sufficiently revealed itself during the actual practice. Nevertheless, it would be possible to improve upon it, by defining each pixel as a pointer on a linked list of surface fragments [5], which would permit a more accurate processing of the pixels where more than two surfaces contribute to the masking. The amount of memory required would be greater, since a pointer would take up more space than the collection of data relative to a fragment (at least in C on a 32 bit machine). 3. SECOND PASS: SAMPLING AND FILTERING When using the results of the first pass to calculate the intensity of the pixels, one comes across a basic fact: Whatever the resolution utilized during the first pass and whatever the method employed, one is obliged to recognize that the "images" produced in the two cases are difficult to correlate, for they have been produced with entirely different scene samplings. In particular, the sampling of a surface by the observer depends on his orientation: One part could be well oriented for the eye and yet not for the source, while for another part the reverse could be true. Even the term "image," speaking about the result of the first pass, leads to an error; one cannot help but ascertain that the results consist of a "certain number of samplings" of the scene in the light source space.
Fig, 3.
Algorithms for aiatialiased cast shadows It is thus necessary to adopt a strategy similar to that which is utilized in texture mapping: The pixel in the observer space is defined as a surface of squared projection leaning on four corners of the observer's definition grid. These four eorners transformed in the light source space, define a quadrilateral. The dimensions of this quadrilateral brought back to the grid dimensions of the P-buffer, can then be interpreted as the ratios of the sampling frequencies between the two spaces, on the inside of the considered pixel. Two cases could then occur. • Over-sampling of the P-buffer: The transformed pixel is "smaller" than a pixel of the P-buffer. This is the simplest occurrence; the best solution is to compute a bilinear interpolation on the four corners of the Pbuffer pixel, which will give continuous variations for small displacements of the tested points between the P-buffer grid points. • Under-sampling of the P-buffer: The transformed pixel is "larger" than the P-buffer grid (i.e. encompasses several grid points). It is necessary to filter. We have implemented two different algorithms to achieve this operation, corresponding to two opposite approaches in terms of signal analysis: Considering that the sampling frequency during the second pass is less than that of the first pass, one can either raise it to a sufficient value (Nyquist intermediary grid), or work on a low-pass filtered version of the P-buffer (pyramidal filtering). From a practical standpoint, we execute in one case a post-filtering and in the other a prefiltering. Their comparison has proven to be rather interesting.
3. I. Nyquist intermediary grid This idea was developed by Gangnet [9, 10] for texture mapping, and is situated in relation to those presented previously in [11, 12, 13]. Nevertheless, it presents certain theoretical and practical advantages, for it integrates the information on a regular grid. If P0, PI, P2, P3 are the four transformed corners in the P-buffer [Figs. 4(a) and 4(b)], we define
261
Ratiox = max(distance(P0, PI), distance(P2, P3)): Ratiox = max(distance(P0, P2), distance(Pl, P3)); Ratiox, if it is > 1, shows a ratio of under-sampling along the X axis of analysis in the observer's space, which implies that it would be necessary to have along this axis Ratiox times more analysis points. This is also true for Y. We can thus construct inside the pixel in the observer's space, a regular intermediary grid at the appropriate frequency (also called Nyquist grid). The new analysis points can be evaluated in the light source space without any particular treatment, and the results will be integrated to reach the final value of the pixel [Figs. 4(c) and 4(d)].
Remarks • If one neglects the nonlinear distribution of the points on the inside of a straight line due to the perspective projection, it is possible to generate the Nyquist grid in the light source space, by constructing a bilinear mesh between the four transformed corners, which would simplify the algorithm. • 1l is necessary to assure the continuity between the cases of over and under sampling (i.e. when Ratio < 1 and Ratio > l). A simple solution consists of computing the value of each intermediary grid point by bilinear interpolation on the four neighboring Pbuffer pixels. This precaution becomes unnecessary when the Ratio is large, but it assures a complete continuity. 3.2. Prehltering In this case, the P-buffer is low-pass filtered in order to bring it's spatial frequency back to the level of the analysis frequency. It is worthwhile to note that the Ratios X and Y which will give an estimation of the necessary low-pass filtering level, are not evaluated in the same way as in the previous method: Ratiox = max(Ix0 - xl[, Ix0 - x2[, ]xl - x31, Ix2 - x3]); Ratioy
I I
P
Pol
Ps
= max(]y0 - yll, [y0 - y2[, lyl - y3], ]y2 - y3l);
IPl "
~
Ps
Pol
IP'I
lUlliffllii
muiip ||I~QII~ )m
li~(~dili iil~mlllmi llm~lliil Fig. 4.
CAG 9 : 3 - F
mlU
because this analysis is made in the light source space. without any reference to the original observer space. To avoid having to integrate numerous values on each pixel in critical under-sampling cases, it is natural to think about some prefihering of the P-buffer, similar to that which was introduced by Williams [ 14] for the filtering of textures or diverse numerical values (displacements, highlights, normals . . . . ). However, it is important to note the necessity of separately prefiltering according to the axes X and Y, for the compression ratios in X and Y are not correlated: what is more, the "'shape" information contained in the P-buffer is at a very high frequency (maximum for the level of resolution chosen) and would not with-
262
J. CHARLESHOURCADEand A. NICOLAS
stand without notable aliasing, an unadapted sampling on one of the axes (see image 3: A calculation with a simple diagonal pyramid produces aliasing along one of the axes). A simple pyramidal filtering would be insufficient; it is necessary to construct a pyramid according to two axes, as Gangnet indicated for texture mapping in [15]. The choice of filters (shape and dimension), the resolution of the problems around the edges, and the diverse theoretical consequences of this method are accurately depicted by Burt and Adelson in [16]. A complete pyramid is visualized in image 4. It is clear that the P-buffer thus filtered occupies four times as much space as the nonfiltered buffer and three times more space than a simple pyramid (according to the only diagonal). On each axis, we find ourselves generally between two levels of filtering, and the final result is obtained by a bilinear interpolation of the four intermediary values obtained by the four selected under-images.
Remarks • To obtain a continuous result, it is preferable to compute a bilinear interpolation for every selected image at each corner of the transformed pixel, rather than compute one single value at the center of the pixel. • Precautions must be taken in the averaging operations, when calculating the prefiltered pyramid of the P-buffer, relative to its particular structure. This buffer occurs as a source of information divided into two hierarchical levels, where the one governs the other. The main information is that of the surface identifier(s), and the surface percentage information is used only under two conditions: If the "first" surface doesn't entirely cover the pixel, and if the current surface (the shading of which is being currently computed) is the "second" surface. 4. EVALUATIONAND PERFORMANCES The developments and the tests have been made in C language under UNIX,'t on a Perkin Elmer 3210. On the test images, presented in the photos l, 3, 5, the cast shadow presents a compression ratio of about 1 along one axis and of about 4 on the other. Image l was generated without filtering and image 3 with a simple diagonal pyramid prefiltering. The defects are apparent when contrasted with image 5, correctly filtered.
4.1. Comparison of the two methods A shadowing calculation is executed for every pixel of the floor and the table. This leads to a significant amount of calculation.
4.1.1. Intermediary grid filtering • execution time for the first pass: 40 sec, • cumulative time for the shadow calculations of the second pass: 130 sec, 5"Unix is a Trademark of Bell Laboratories.
• idem, but for a compression 3 times greater (distance of observer 3 times greater): 41 sec, • amount of extra memory needed (resolution of the P-buffer 256 × 256): 256 ko. 4.1.2. Prefiltering (pyramid along 2 axis) • execution time for the first pass: 40 sec, • time consumed during the prefiltering: 58 sec, • cumulative time for the shadow calculations of the second pass: 161 sec, • idem, but for a compression 3 times greater (distance of observer 3 times greater): 27 sec, • amount of extra memory needed (resolution of the P-buffer 256 × 256): 1 Mo. The images obtained by the two methods are both very satisfactory; the differences are difficult to distinguish on a photographic print, it is possible to see then, that for images presenting compression ratios in reasonable proportions, the prefiltering method can entail more time than the local over-sampling method. We are also obliged to compute the filtered P-buffer for each image of an animation, contrary to the case of texture mapping, and this initial "handicap" can not be recovered except in the case of critical under-sampling, where the first method can lead to a significant augmentation of computation time. Actually, the prefiltering saves some computation time in the most critical cases, is inefficient in the common cases, and occupies four times as much space as the first method. We can clearly conclude a preference for the first method in practical application. 4.2. Picture quality considerations Under certain conditions, it is possible to find a slight deterioration in the picture quality, when it is computed with a prefiltering method. In fact, the utilization of a prefiltering pyramid on 2 axes, even if it presents an obvious amelioration on the diagonal pyramid, is not always completely satisfying. Indeed, it favors the two axes X and Y, which are not the principal axes of the transformed pixel in the buffer. In the most unfavorable condition of a transformed pixel elongated along a diagonal, one would be obliged to take into account in the integration some parasite grid points situated in the corner of the rectangle including the quadrilateral (cf. [16] for the study of the recursive propagation of the domains of integration in a prefiltering pyramid). The first method does not exhibit this drawback. 5. CONCLUSION Thanks to the concept of P-buffer, this approach has permitted a resolution of the problems concerning precision in the correlation of the "images" of a particular scene, as seen by both the observer and the light source. The computing of shadowing must integrate the notions of under and over sampling, as indicated in this article. The combination of these techniques permits a rapid integration of cast shadows in a scanline algorithm for image synthesis and thus a considerable improvement of the resulting images, without any major increase in computation time.
Fig. 6. Zoom ×6.
Fig. 8. Two axis prefiltering pyramid.
Fig. 5. Nonfillered shadows.
Fig. 7. Simple pyramid filtering.
D
>
O
g~
O
Fig. 9. Filtered shadows (nyquist grid).
Fig. I 1. Cast shadows in a complex scene.
Fig. 10. Z o o m ×6.
Z N or "
e~
,-e o
.w > ~O
L...
Algorithms for antialiased cast shadows
Acknowledgements--We would like to thank E. Beeker, D. Borenstein, G. Soirant and B. Tezenas for the multifarious assistance that they have provided during the course of this work. as well as M. Gangnet for his advice in the domain of filtering and signal analysis.
REFERENCES
1. F. C. Crow, Shadow Algorithms for Computer Graphics. Siggraph Proc. 11, 242-248 (1977). 2. I. E. Sutherland, R. F. Sproull and R. G. Schumaker, A Characterization of ten Hidden Surface Algorithms. Comput. Surv. 6, 1-55 (1974). 3. L. Williams, Casting Curved Shadows on Curved Surfaces. Siggraph Proc. 12, 270-281 (1978). 4. A. Fournier, E. Fiume and L. Rudolph, A Parallel Scan Conversion Algorithm with Antialiasing for a GeneralPurpose Ultracomputer. ACM Cornput. Graphics 17, 141-150 (1983). 5. L. Carpenter, The A-buffer, an Anti-aliased Hidden Surface Method. ACM Comput. Graphics 18, 103-108 (1984). 6. M. I. Shamos, Computational Geometry. Ph.D. Thesis, Yale University, May 1978.
265
7. J. M. Lane, L. C. Carpenier, T. Whitted and J. F. Blinn, Scanline Methods for Displaying Parametrically Defined Surfaces. CACM 23, 23-34 (1980). 8. J. H. Clark, Hierarchical Geometric Models for Visible Surface Algorithms. CACM 19, 547-554 (1976). 9. M. Gangnet and P. Coueignoux, Perspective Mapping of Planar Textures. Eurographics 1982. 10. J.C. Hourcade and A. Nicolas, Inverse Perspective Mapping in Scanline Order. Eurographics 309-320 1983. 11. J. Blinn and M. Newell, Texture and Reflection in Computer Generated Images. CACM, 19, 286-292 (1976). 12. E. Catmull and A. R. Smith, 3-D Transformations of Images in Scanline Order. ACM Comput. Graphics 14, 279-285 (1980). 13. E. Feibush, M. Levoy and R. Cook. Synthetic Texturing Using Digital Filters. ACM Comput. Graphics 14, 294301 (1980). 14. L. Williams, Pyramidal Parametrics. ACM Comput. Graphics 17, 1-11 (1983). 15. M. Gangnet and D. Ghazanfarpour, Techniques for Perspective Mapping of Planar Textures. Colloque Image de Biarritz, CESTA, pp. 29-34.May 1984. 16. P. J. Burt and E. H. Adelson, A Multiresolution Spline with Application to Image Mosaics. ACM Trans. Graphics 2, 217-236 (1983).