Computer Graphics and Visualisation
Lighting and Shading Student Notes Developed by J Irwin W T Hewitt T L J Howard
Computer Graphics Unit Manchester Computing University of Manchester Department of Computer Science University of Manchester
Produced by the ITTI Gravigs Project Computer Graphics Unit Manchester Computing MANCHESTER M13 9PL First published by UCoSDA in October 1995 Copyright The University of Manchester 1995 The moral right of John Irwin, William Terence Hewitt and Toby Leslie John Howard to be identified as the authors of this work is asserted by them in accordance with the Copyright, Designs and Patents Act (1988). ISBN 1 85889 064 0 The training materials, software and documentation in this module may be copied within the purchasing institution for the purpose of training their students and staff. For all other purposes, such as the running of courses for profit, please contact the copyright holders. For further information on this and other modules please contact: The ITTI Gravigs Project, Computer Graphics Unit, Manchester Computing. Tel: 0161 275 6095 Email:
[email protected] To order further copies of this or other modules please contact: Mrs Jean Burgan, UCoSDA. Tel: 0114 272 5248 Email:
[email protected] These materials have been produced as part of the Information Technology Training Initiative, funded by the Information Systems Committee of the Higher Education Funding Councils. The authors would like to thank Neil Gatenby for his assistance in providing the source of some material used in this document. Printed by the Reprographics Department at Manchester Computing from PostScript source supplied by the authors.
Table of Contents 1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 2 Local reflection models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 2.1 Light sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.1.1 Light emitters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.1.2 Light reflectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.2 Types of reflection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.3 Light source geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.4 Ambient illumination. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.5 Diffuse reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.6 The position of the light source . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.7 Specular reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.7.1 The Phong model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.8 Multiple light sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.9 Colour. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3 Shading surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 3.1 Constant shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.1.1 Mach banding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.2 Intensity interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.2.1 Problems with intensity interpolation . . . . . . . . . . . . 20 3.3 Normal vector interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.4 Dot-product interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4 Texture and transparency . . . . . . . . . . . . . . . . . . . . . . . . . . .23 4.1 Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.1.1 Object space mapping . . . . . . . . . . . . . . . . . . . . . . . 23
The University of Manchester
i
Lighting and Shading
4.1.2 Parametric space mapping . . . . . . . . . . . . . . . . . . . . 25 4.1.3 Environment mapping . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.2 Roughness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 4.2.1 Macroscopic roughness . . . . . . . . . . . . . . . . . . . . . . . 26 4.2.2 Microscopic roughness . . . . . . . . . . . . . . . . . . . . . . . . 28 4.2.3 Phong vs Cook-Torrance . . . . . . . . . . . . . . . . . . . . . . . 29 4.3 The optics of transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 4.4 Modelling transparency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5 Ray-tracing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5.1 Basic algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 5.2 A simple viewing system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 5.3 Reflection and refraction vectors. . . . . . . . . . . . . . . . . . . . . . . . 38 5.4 Shadows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.5 The ray-tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.6 Object intersections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.6.1 Sphere intersection. . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5.6.2 Polygon intersection. . . . . . . . . . . . . . . . . . . . . . . . . . . 45 5.7 Acceleration techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.7.1 Hierarchical bounding volumes . . . . . . . . . . . . . . . . . 47 5.7.2 3D spatial subdivision . . . . . . . . . . . . . . . . . . . . . . . . . . 48 5.8 Image aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 5.8.1 Anti-aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 5.8.2 Adaptive supersampling . . . . . . . . . . . . . . . . . . . . . . . 53 5.9 Example images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
6 Classical radiosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.1 The radiosity equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6.2 Form-factor calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 6.2.1 Reciprocity relation . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
ii
Computer Graphics and Visualisation
6.3 Solving the radiosity equation . . . . . . . . . . . . . . . . . . . . . . . . . . 63 6.4 The hemi-cube algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 6.4.1 Calculating the delta factors . . . . . . . . . . . . . . . . . . 66 6.5 Rendering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 6.5.1 Light and shadow leaks . . . . . . . . . . . . . . . . . . . . . . . 69 6.6 Progressive refinement radiosity . . . . . . . . . . . . . . . . . . . . . . . . 69 6.6.1 Formulation of the method . . . . . . . . . . . . . . . . . . . . 70 6.6.2 Ambient contribution . . . . . . . . . . . . . . . . . . . . . . . . . 71 6.7 Improving the radiosity solution. . . . . . . . . . . . . . . . . . . . . . . . . 72 6.7.1 Substructuring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 6.7.2 Adaptive subdivision . . . . . . . . . . . . . . . . . . . . . . . . . 73 6.8 Example images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
A Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 A.1 What is a vector? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 A.2 Vector addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 A.3 Vector multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 A.4 Determining the surface normal. . . . . . . . . . . . . . . . . . . . . . . . 82
B Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 B.1 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 B.2 Research literature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 B.3 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The University of Manchester
iii
Lighting and Shading
iv
Computer Graphics and Visualisation
1 Introduction We see objects only by virtue of the light they reflect into our eyes. Additionally, the way in which the light is reflected from objects gives us important information about their three-dimensional shape. One of the first things painters learn is the power of light and shade to make pictures look realistic. Similarly, in the quest for realism in computer graphics, we must consider the effects of illumination on the objects we model. Ultimately, we draw pictures by displaying collections of pixels with the appropriate colours and intensities. We need methods for determining exactly what these values should be. A previous module in this series, Colour in Computer Graphics, considered how to determine the colour of a light source from its emission spectrum. We also looked at the colour of light reflected from an object, considering the emission spectrum of the light and the reflectance spectrum of the object. The illuminant was shown to be important – table lamps illuminate their surroundings quite differently from fluorescent strip-lamps, for example. This was sufficient to define the intrinsic colour of a single, isolated object. When rendering a scene, however, we must consider many other factors, including: • The geometrical arrangement of the light, the object and the viewer. Experience tells us that at certain angles, we see reflected glare from the light source and this is not the same colour as the object. So the reflectance spectrum is geometry dependent. Also, surfaces directly facing a light source will appear brighter than surfaces inclined at angles. • The geometry of the illuminant - is it a point or an area, how far away is it from each object. • The physical nature of the surface – is it shiny, matte, coloured, patterned, transparent, smooth or bumpy? • The effect of other surfaces in the scene. • The medium through which the light passes – is it smoky, foggy, underwater or raining for example. In this module we will look at techniques for quantifying these rather vague ideas, so we can compute how to draw realistic pictures. One thing to bear in mind right at the start is that there is no single method which gives the 'right' result all of the time. Rather, it is a case of making a model of the effect of light on objects. The closeness of fit of this model to physical reality depends on what phenomena we choose to model and how much effort we are prepared to expend for a particular effect. The trick is to use a model that fits well enough for the particular purpose. The amount of effort that this requires varies greatly. A simple 3D previewer requires considerably less computational resources than an architectural lighting simulation, for example.
The University of Manchester
1
Lighting and Shading
Figure 1 illustrates that image synthesis is a combination of first defining what to put in the picture, and then how to display it. These two stages are often known respectively as modelling and rendering.
Define the geometry
Polygons Curves Surfaces etc.
Hidden surface removal
Render
Lights Patterns Texture etc.
Pictures
Figure 1: The rendering pipeline. To a large extent, the two steps are quite independent. Given a particular geometrical description, we can render an image of it in many ways. These notes concentrate on illumination modelling and rendering; geometric modelling is only discussed where it affects rendering. During the process of rendering we must consider the following distinctions: • The illumination of objects by light sources (of whatever origin) • The reflection of light from objects • The shading of objects (for display on a graphics screen) Concerning the first point, we have to model the geometry of the light source, both its physical dimensions as well as its intensity distribution, together with its spectral distribution, i.e. what colour it is. For the second point, we need to consider how incident light is modified by a particular surface, especially the colour of the light. We do this by defining a light reflection model and there are many ways to do this. The third point is necessary since in most cases we only compute the light leaving a few specific points on a surface and use various interpolation techniques to find the light leaving the surface at other locations. There are two ways of computing illumination effects: local illumination models and global illumination models. Local models consider each object in a scene in isolation, without reference to other objects in the scene. Scenes rendered in this way therefore look artificial and
2
Computer Graphics and Visualisation
Introduction
contain no shadows or reflection effects. Because they are simple, and the time to render one object does not depend on the overall scene complexity, local models are fast. Also, this simplicity allows local models to be implemented in hardware on graphics workstations which gives further speed benefits. Local models are a suitable choice when the purpose of rendering is to give basic information about the 3D shape of an object. An example would be the display of a molecule made of thousands of coloured spheres. All that is needed is an indication of shape, and it is important that rendering is fast so that the molecule may be rotated and examined in real time. In contrast, global models consider the relationships between objects in a scene and can produce more realistic images incorporating shadows, reflection, and refraction. Global models are used when a realistic portrayal of some aspect of physical reality is desired, and when this realism is more important than the speed of rendering. In general, the more realistic the lighting model, the greater the computational cost. In the next chapter we will develop a simple local reflection model which takes into account the following: • Illumination from a point light source • Illumination from ambient light • Diffusely reflected light • Specularly reflected light In subsequent chapters we will see how this model can be used to produce shaded images of objects and how the model can be refined to include effects due to surface finish, such as texturing and bump mapping, and also transparency. Following this we will turn our attention to the problem of modelling the global reflection of light. This will cover a basic discussion of two of the most popular techniques known as ray-tracing and radiosity, which actually model two quite different aspects of light reflection.
The University of Manchester
3
Lighting and Shading
4
Computer Graphics and Visualisation
2 Local reflection models 2.1 Light sources There are two basic kinds of illumination source: emitters and reflectors.
2.1.1 Light emitters Anything that gives off light, such as the sun, a light bulb, or a fluorescent tube is an emitter. Characteristics of emitters include the overall intensity of the light and its spectrum, which determines the colour of the light. The spectra of some common light sources are shown in Figure 2.
300.0 Illuminant A (incandescent lightbulb) Illuminant D65 (sunlight)
Relative intensity
200.0
100.0
0.0 300.0
400.0
500.0 600.0 Wavelength (nm)
700.0
800.0
Figure 2: Spectra of sunlight and a light bulb. We can further classify light emitters into two types. When the light source is sufficiently far away from the surface, we can consider it to be an infinite point source, and all the light rays effectively strike a surface at the same angle. But, when the light source is near to the surface rays will arrive at different points on the surface at different angles, as illustrated in Figure 3.
The University of Manchester
5
Lighting and Shading
Area source nearby Point source at infinity
Figure 3: Distant and nearby light sources. We may also wish to take the geometry of the light source into account. In particular, we can classify emitter geometry into two types: point sources and area sources. With area light sources, a single point on an object can be illuminated from several different angles from the same light source, as shown in Figure 4.
Area source
Point source
Figure 4: Point and area light sources. As any physical light source must take up a certain amount of space, all light sources are area sources! However, considering some small area sources to be point sources gives a significant reduction in complexity and is an acceptable compromise in many instances.
2.1.2 Light reflectors Turn a spotlight on in one corner of a room, and parts of the room not directly lit by the spotlight will still be illuminated by light reflected off walls, ceiling, carpets, people, and so on. So light reflectors are all the other objects in a scene that are not emitters. Although clearly an emitter can also reflect light, the amount reflected is so small in comparison with the emitted light that the reflected portion is frequently ignored. The colour of a light reflector depends on the spectrum of the incident light and the reflectance spectrum of the reflective object.
6
Computer Graphics and Visualisation
Local reflection models
In addition to these direct reflections, there will be indirect ones - reflections of reflections, and so on. These multiple reflections in a scene produce an overall illumination called ambient light, which is discussed further below.
2.2 Types of reflection We need to develop a model to enable us to calculate how much light is reflected from different objects in a scene. There are two ways in which reflection occurs: • Diffuse reflection – light is scattered uniformly from the surface of the object (Figure 5a), and the surface looks dull. The surface is effectively absorbing the illumination and re-radiating it only at preferred wavelengths. For example, a green object looks green when illuminated with white light because it absorbs all wavelengths except green, which it reflects. • Specular reflection – a high proportion of the incident light is reflected over a fairly narrow range of angles, and the object looks shiny (Figure 5b). To a first approximation at least, the colour of the surface does not affect the colour of the specularly reflected light. In practice, many materials exhibit both of these effects to different degrees, and the total reflection is of the kind shown in Figure 5c.
(b)
(a)
(c) Figure 5: Diffuse, specular and combined reflections
2.3 Light source geometry Consider a single light source, modelled as a point infinitely distant from the object being lit. Clearly, the amount of light reflected from a surface will depend
The University of Manchester
7
Lighting and Shading
on its orientation to the source of light. We describe this quantitatively with two vectors, shown in Figure 6 (refer to Appendix A if you need a refresher on vectors) • The normal vector N - this is a vector normal (at right angles to) the surface. For example, for a surface lying in the xz-plane, its normal vector will be parallel to the y-axis. • The light vector L – this points towards the light source.
Normal vector N L To light source
θ
Figure 6: Describing a surface and its orientation. Since only the directions of these vectors are required to orient the surface with respect to the light source, it is convenient (and conventional) to use unit vectors, so we will assume that N and L have been normalised. The angle θ between N and L is called the angle of incidence.
2.4 Ambient illumination Multiple reflections of light in a scene together produce a uniform illumination called ambient light. Actually, this is an over-simplification. For many purposes, we can assume a constant level of ambient light in a scene. In reality, however, ambient light effects are much more subtle. The intensity I of diffuse reflection at a point on an object illuminated only by ambient light of intensity Ia is: I = kd Ia
(2-1)
where kd is called the diffuse reflection coefficient and is a constant in the range 0 to 1. Its value varies from surface to surface according to the surface properties of the material in question. A highly reflective surface will have kd approaching 1, and a surface that absorbs most of the light falling onto it will have kd near 0. Note that some researchers refer to this term as ka. We could use Equation 2-1 to compute the illumination of an object, but each point on the object would be assigned the same intensity. This is not realistic, as we know that in practice the shading of objects is not constant. If this were the case we would not be able to perceive the 'three-dimensionality' of the object. In Figure 7 we illustrate how a cube would appear if it was illuminated solely by ambient lighting. We achieve a more realistic effect by considering localised light sources
8
Computer Graphics and Visualisation
Local reflection models
(such as points sources) and the orientation of surfaces with respect to light sources.
Figure 7: Object illuminated only by ambient light
2.5 Diffuse reflection The amount of light reflected by the surface depends on the amount received from the source of illumination, which in turn depends on the intensity of the illumination, and the orientation of the surface with respect to it. If N and L are in the same direction, (where θ = 0°) reflection will be at a maximum. If they are at right angles (where θ = 90°), reflection will be at a minimum. The precise relationship is expressed by Lambert’s Cosine Law, which states that the effective intensity Ie incident on a surface when illuminated by a light of intensity Ip incident at θ is: I e = I p cos θ Expressing the diffuse reflectivity of the surface by kd, the amount of light I diffusely reflected by a surface illuminated by a light of intensity Ip incident at θ is: I = k d I p cos θ
(2-2)
Since N and L are normalised, their dot product gives the angle between them: cos θ = N • L so we can rewrite Equation 2-2 entirely in terms of vectors as: I = kd Ip ( N • L)
(2-3)
We can now combine the effects of ambient illumination and a point light source to give the illumination model: I = ambient term + diffuse term or, I = kd Ia + kd Ip ( N • L)
(2-4)
The University of Manchester
9
Lighting and Shading
Shown in Figure 8 are four spheres whose surfaces have been shaded using the diffuse reflection model given by Equation 2-3. From left to right and then top to bottom, kd = 0.1, 0.4, 0.7 and 0.95.
Figure 8: Spheres shaded using diffuse reflection model Shown in Figure 9 are four spheres again, but this time the surfaces are shaded according to Equation 2-4 which includes an ambient lighting term. Each sphere has kd = 0.4, but Ia = 0%, 20%, 40% and 60% of Ip respectively as we move from left to right and then from top to bottom.
Figure 9: Spheres shaded using ambient and diffuse reflection
10
Computer Graphics and Visualisation
Local reflection models
Notice that in Equation 2-4 the intensity of light reflected from a surface is independent of the distance of the point source from the object. This means if two objects have parallel surfaces, and seen from the viewpoint the surfaces overlap, they will be shaded the same and will merge into one. Also, note that neither the colour of the illuminating light nor that of the surface is taken into account in this model. We will come on to this shortly.
2.6 The position of the light source This can be modelled by observing that the intensity of light falls off in inverse proportion to the square of the distance the light has travelled. If light from a source of intensity Ip travels a distance d, its effective intensity is Ip/4πd2. We can incorporate this into Equation 2-4 to give: kd Ip ( N • L) I = k d I a + ---------------------------------2 4πd (Note the ambient term kdIa is not dependent on distance, since by definition the source is everywhere). However this model does not give realistic results in practice. In typical modelling applications, we will either have a point light source at infinity, so that d is infinite, or we have a light source at a finite position. Either way, we will typically have values of d that differ little between objects, and this, in conjunction with the finite values available to assign to pixels, may mean that the effect of the d2 term in the model may be too great. Experiments have shown that a better model is to use a linear approximation for fall-off of intensity with distance, using: kd Ip ( N • L) I = k d I a + ---------------------------------( d + d0)
(2-5)
where d is the actual distance from the light source to the object, and d0 is a constant determined experimentally. (There are lot of 'constants determined experimentally' in computer graphics.)
2.7 Specular reflection An object modelled using only diffuse reflection appears dull and matte. However many real surfaces are shiny. At certain viewing angles, a shiny surface reflects a high proportion of the incident light – this is called specular reflection. When this happens you see a bright spot on the object the same colour as the illuminating light. First we consider a perfect reflector such as a highly-polished mirror. In Figure 10, the unit vector R represents the direction of specular reflection. V is the direction from which the surface is being viewed. For a perfect reflector, the angle of incidence equals the angle of reflection, so the specular reflection can only be observed when V and R coincide, such that φ = 0°. Real materials, however, behave less exactly. Light arriving at an angle of incidence of θ is reflected in a range of angles centred on θ, where the intensity is at a maximum. The intensity of the specular reflection falls off rapidly as the angle φ
The University of Manchester
11
Lighting and Shading
between the direction of maximum ideal reflection R, and the viewing direction V, increases. We need to somehow incorporate this effect into our illumination model.
Reflection vector R
Normal vector N
To light source L
V View vector φ
θ
Figure 10: The four reference vectors.
2.7.1 The Phong model Phong developed an empirically based model where the intensity of specular reflection is proportional to cosnφ. Figure 11 graphs cosnφ for -π/2 ≤ φ ≤ π/2. We use n to represent the surface properties, and is typically in the range 1 to 200. For a perfect reflector, n = ∞; for a very poor specular reflector, like foam, n = 1.
1.0 n=1
n=5
n = 50
−π/2
0
π/2
φ
n Figure 11: The behaviour of cos φ.
For real materials, the amount of light specularly reflected also depends on the angle of incidence θ. In general, the intensity of the specular reflection increases as the angle of incidence increases. Exactly how the two are related depends on individual surface properties, and we represent this with the specular reflection function W(θ) which has a value in the range 0 to 1. The precise behaviour of W(θ) will vary from surface to surface. Figure 12 shows how reflection depends on the wavelength of incident light and the angle of incidence for several materials. For glass, for example, the specular reflection increases from almost nothing to full intensity as the angle of incidence increases from 0° to 90°. Other materials show
12
Computer Graphics and Visualisation
Local reflection models
much less variation, and W(θ) is often replaced by a constant value ks selected experimentally. This constant, ks, is called the specular reflection coefficient.
100
100
Reflectance (%)
Al
Silver
Ag Steel 50
50
Gold
Dielectric 0 2
4 6 Wavelength, λ (x10-3nm)
8
0
30 60 Angle of incidence, θ°
90
Figure 12: Reflection curves. We can now incorporate specular reflection into our illumination model of Equation 2-5, by adding the specular reflection component to the diffuse and ambient reflections: I = ambient term + diffuse term + specular term or, Ip I = k d I a + ---------------- k d ( N • L ) + k s cos nφ d + d0
(2-6)
Since R and V are normalised, cos φ = R • V and we can rewrite Equation 2-6 entirely in terms of vectors: Ip I = k d I a + ---------------- k d ( N • L ) + k s ( R • V ) n d + d0
(2-7)
Figure 13: Spheres shaded using Phong’s model
The University of Manchester
13
Lighting and Shading
Figure 13 shows a sphere illuminated using Equation 2-7 with different values of ks and n. In each case kd = 0.4, Ia = 20% of Ip. The top row has ks = 0.25 while the bottom has ks = 0.5. Moving from left to right n = 3, 10 and 100 respectively.
2.8 Multiple light sources Extending Equation 2-7 to include more than one source of illumination is straightforward. Each source will cause diffuse and specular reflection at the surface, so we need to sum these effects. If there are n different point sources, our model becomes: n
I = ambient term +
∑ i=1
n
diffuse term i +
∑
specular term i
i=1
This summation, however, can cause problems with overflow in the value of I, although the problem can also occur with a single light source. For display purposes we require the range of I to be limited, usually 0 to 1 in normalised form. There are a number of ways to deal with overflow. First we could avoid the situation arising in the first place by adjusting the reflection parameters or the relative light sources strengths. If we decide this is not such a good idea then adjusting the computed value of I is necessary. In this case we can either • adjust the value locally by, for example, clamping the value to the required range, or • adjust the value globally by considering the full range of intensities calculated for the image and scaling everything at the same time so that no values are out of range. The second method is generally preferred which avoids limit errors leading to Mach banding in the image.
2.9 Colour So far we have mainly assumed a monochromatic light source, and have ignored the fact that objects are coloured. We express colour using a colour model, which gives us a way of assigning numerical values to colours. A common simple model is the Red-Green-Blue (RGB) model, where a colour is represented by a mixture of the three primary colours red, green and blue corresponding to the colours of the monitor phosphors. This is illustrated in Figure 14. To consider the effect of coloured illumination and coloured objects, we must apply our illumination model to each component of the colour model we are using. For example, if we are using the RGB colour model, we would treat each of the red, green and blue components of the colour separately We can take the surface (diffuse) colour of objects into consideration by expressing the diffuse reflection coefficient as a colour dependent parameter (kdR, kdG, kdB). We also have colour components for the illumination; the red component of Ip is IpR, and so on. So, for the red component our model becomes: I pR I R = k dR I aR + ---------------- k dR ( N • L ) + k s ( R • V ) n d + d0
14
Computer Graphics and Visualisation
Local reflection models
G
Green (0, 1, 0)
Yellow (1, 1, 0)
White (1, 1, 1)
Cyan (0, 1, 1)
Shades of grey along diagonal
R Black (0, 0, 0)
Blue (0, 0, 1)
Red (1, 0, 0)
Magenta (1, 0, 1)
B Figure 14: The RGB colour model. and similarly for green and blue. We are assuming that the specular highlight will be the same colour as the light source, hence ks is not colour dependent. Once we have the three intensities IR, IG and IB, and each component is within the display range, we can use these to directly drive the RGB monitor to display the required colour. Out of range values are treated, in ways similar to those mentioned before, by clamping or global scaling. We could also locally scale the colour by dividing each component by the value of the largest component, a method which preserves the hue and saturation of the colour but modifies its lightness. Although using the RGB colour model often provides reasonable results, it is a gross over-simplification of the interactions between light and objects. For proper results, it is necessary to apply the illumination model with many more than three samples across the spectrum. Instead of restricting ourselves to a threecomponent colour model, a more general form of the illumination model may be written: I pλ I λ = k dλ I aλ + ---------------- k dλ ( N • L ) + k s ( R • V ) n d + d0 where any terms which are wavelength-dependent are indicated with the subscript λ. But how do we use these spectral samples to specify an RGB triplet, which we must have to be able to display the colour on a graphics screen? The straightforward way of doing this is to convert the samples to the CIE XYZ colour space and then to the monitor RGB space, which requires knowledge of the chromaticities of the monitor phosphors. Further details can be found in the accompanying module, Colour in Computer Graphics. Although this spectral sampling technique is physically more correct, we must remember that the reflection models discussed thus far are mainly based on empirical observation: they are constructed such that the results look reasonable.
The University of Manchester
15
Lighting and Shading
So in general it would seem inappropriate to use such a method. However, when we come to discuss global models of light reflection we will see that these models are more physically based and would benefit from a more considered use of spectral sampling. Unfortunately, while this is true, it is still the custom of the majority of practitioners in the field to use the RGB colour model in their rendering systems.
16
Computer Graphics and Visualisation
3 Shading surfaces The illumination models we have developed so far can calculate for us the intensity and colour of the light leaving a particular surface at one point. How do we compute intensities across a whole object? Curved surfaces and complex objects are often approximated using meshes of polygons, as illustrated by Figure 15.
Figure 15: A polygonal model. We can apply our illumination model to each polygonal facet, but there are several ways to do this: we could compute a single intensity for each polygon, based, for example, on the centre point of the polygon. Or we could apply the shading model to each point in a polygon, so that the intensity varies across the polygon. The latter approach will give a highly realistic effect, but it is very expensive. Instead we could compute the intensity at a few key points on the polygon, and determine the intensity of all the other points using interpolation methods. We now examine each of these methods.
The University of Manchester
17
Lighting and Shading
3.1 Constant shading Constant shading appears unrealistic when two or more polygons share a common boundary, and are each shaded differently. For some applications, such as finite element modelling, this effect may be required. But for realistic modelling, the harsh change of illumination at the boundary is distracting and unrealistic. Additionally, with constant shading each polygon approximating the surface is visible as an entity in its own right, although it is possible to reduce this effect by using more, smaller, polygons to approximate an area.
3.1.1 Mach banding The human visual system is especially good at detecting edges – even when they are not there. Abrupt differences in shading between two facets are perceived to be even greater than they actually are, a phenomenon called the Mach band effect. In another module in this series, Colour in Computer Graphics, we saw that this is due to local contrast enhancement by the horizontal cells in the retina of the eye. Figure 16 shows (greatly exaggerated for illustration) how the perceived intensity differs from the actual measured intensity across the surface of an object.
Actual intensity
Intensity
Perceived intensity
Distance along surface
Figure 16: The Mach band effect. To reduce the Mach band effect, and eliminate the obvious intensity changes between facets, we need to smooth out the changes of intensity, so that one facet anticipates the shading of the next. We shall look at three techniques: intensity interpolation, normal-vector interpolation and dot-product interpolation.
18
Computer Graphics and Visualisation
Shading surfaces
3.2 Intensity interpolation In this method, intensity values are computed for the vertices of a polygon, and these are then linearly interpolated across the polygon to determine the intensity values to be used for the intervening points. The method is also called Gouraud shading, after its inventor Henri Gouraud, who developed the technique in 1971.
N N1
N4
N2 N3 Figure 17: Computing vertex normals. First we must determine the intensities at the vertices of a polygon. To compute the intensity at a vertex using our illumination model, we need the vertex normal. Just using the normal for the polygon is no good, because we need to consider the orientations of adjacent polygons to avoid discontinuities at the shared edges. What we really need is the surface normal at that point for the underlying surface being approximated with polygons. We can approximate the surface normal at a vertex by averaging the true surface normals of all the polygons which share the vertex (Figure 17). Consider the polygon shown in Figure 18. The intensity IQ at Q is interpolated from the intensities IA at A and IB at B using: I Q = uI B + ( 1 – u ) I A
AQ where u = --------AB
I R = wI B + ( 1 – w ) I C
CR where w = --------CB
similarly for point R,
Now we can interpolate between IQ and IR to obtain the intensity at P: I P = vI R + ( 1 – v ) I Q
QP where v = --------QR
The University of Manchester
19
Lighting and Shading
C
A P
R
Q
B
Figure 18: Intensity Interpolation. For each scanline, we can speed things up considerably if we use incremental calculations. For two pixels p1and p2 at v1 and v2 respectively on the scanline, we have Ip = v2 IR + ( 1 – v2) IQ 2
and Ip = v1 IR + ( 1 – v1) IQ 1
Subtracting I p from I p gives 1
2
Ip = Ip + ( IR – IQ) ( v2 – v1) 2
1
Writing (IR - IQ) as ∆I and (v2 - v1) as ∆v, we have I p = I p + ∆I∆v 2
1
We need only compute ∆I and ∆v once per scanline, reducing the computation per pixel to one addition. We will no longer have a shading discontinuity for adjacent polygons. The intensity arrived at by interpolation at one edge will be the starting point for the interpolation across the next polygon.
3.2.1 Problems with intensity interpolation But what if we really want an edge to be visible? The method described above 'averages out' edges so they disappear. The answer is to take special action when a true edge is desired: we compute two vertex normals at the boundary, one for either side, each found by averaging the surface normals of the polygons on the appropriate side of the edge.
20
Computer Graphics and Visualisation
Shading surfaces
Another problem is shown by the corrugated surface of Figure 19a.
na (a)
Vertex normals
nc
nb
Surface normals
Vertex normals
(b)
Figure 19: Intensity interpolation problems Here the arrangement of adjacent facets is such that the averaged vertex normals na, nb and nc are all parallel. So each facet will have the same shading, and the surface will incorrectly appear to be flat across the facets. One solution is to introduce extra polygons, as shown in Figure 19b. Intensity interpolation doesn’t eliminate Mach bands completely, but the results are far more realistic than constant shading. The interpolation technique can also distort highlights due to specular reflection. Another problem arises when a specular highlight lies inside a single polygon – it may be smoothed out and not appear.
3.3 Normal vector interpolation This technique uses a similar interpolation method, except that the quantity interpolated across the polygon is the surface normal vector, instead of the computed intensity itself. (This method is also known as Phong interpolation.) The vertex normals are computed just as in the intensity interpolation method. Referring to Figure 18 again, and representing the normal vector at a point P as nP, we have
The University of Manchester
21
Lighting and Shading
n Q = un B + ( 1 – u ) n A n R = wn B + ( 1 – w ) n C n P = vn R + ( 1 – v ) n Q where u, v and w are the same as before. The normal vector can also be determined incrementally. The result is much more realistic than the intensity interpolation method, especially when specular reflection is considered – the highlights are rendered more faithfully, and Mach banding is greatly reduced (but spheres and cylinders are still prone to Mach band effects). The drawback of this method is that it requires more computations to process each interpolated point in a polygon; at each point the complete shading calculation must be performed using the interpolated normal at that point.
3.4 Dot-product interpolation This method is a compromise between intensity interpolation and normal vector interpolation. Here, the quantities interpolated from one end of the scan line to the other are the dot products N • L and ( R • V )n . This method is sometimes called 'cheap Phong' interpolation.
3.5 Summary We have derived a simple local illumination model that takes into account the following: • Direct diffuse reflections from point sources. • Direct specular reflections from point sources. Not taken into account are: • Area light sources • Indirect reflections: -
Specular to specular
-
Specular to diffuse
-
Diffuse to specular
-
Diffuse to diffuse
Instead, indirect diffuse reflections are modelled as ambient light. Our model can cope with polygonal geometries, interpolating the surface normals to simulate the appearance of a curved surface. Wavelength dependencies are modelled by taking three wavelength samples, one each in the red, green and blue areas of the spectrum, and mapping the intensities directly to the drive signals for a colour monitor. Now, we will look at ways of improving this model to simulate particular effects such as transparency, or to more realistically model some of the factors that were simplified or omitted in the local model.
22
Computer Graphics and Visualisation
4 Texture and transparency The shading methods we have discussed so far render smooth surfaces lit by one or more light sources. We now consider how to model surfaces which are not smooth or simply coloured. There are two kinds of surface texture to consider. • Patterns, or colour detail – We superimpose a pattern on a smooth surface, but the surface remains smooth. For example, we could simply model a planet by wrapping a map around a sphere, and rendering the composite object. Adding patterns to a surface is essentially a mapping function and the process is called pattern mapping. • Roughness – Most natural objects are not smooth. Their surfaces have some kind of micro-structure. For example, we could model an orange using a sphere of the appropriate colour. Even with a realistic shading model, it won’t look much like a real orange; the reason is that the surface of an orange is not smooth. In modelling terms, we must alter the uniformity of a surface using a perturbation function that effectively changes the geometry of the surface.
4.1 Patterns 4.1.1 Object space mapping To provide colour detail we can superimpose a pattern, stored as an image file on the computer, onto a smooth surface. Both the pattern and the object surface are defined in their particular local coordinate system and texturing is defined by a mapping function between these two systems. Usually the pattern space is specified using (u, v) coordinates and if the object space coordinates are (θ, φ) then we determine the mapping function as u = f ( θ, φ )
v = g ( θ, φ )
(4-1)
Once this is done we can compute the (u, v) coordinates of a point in pattern space corresponding to a point on the object’s surface we wish to shade. The colour corresponding to these coordinates is the colour we apply to the surface. To illustrate the determination of the mapping function we will now derive the mapping for a sphere. The determination requires knowledge of the normal vector N at the point of interest on the sphere’s surface and the process is equivalent to finding the latitude and longnitude. The local coordinate system attached to the sphere (origin at sphere’s centre) is defined by two unit vectors, Tp and Te , which point from the origin to the north pole and from the origin to a reference point on the equator, respectively. We define the mapping (see Figure 20) such that the u-coordinate varies from 0 to 1 along the equator and the v-coordinate varies also from 0 to 1 but from the south pole to the north pole. We put u equal to 0 at the poles.
The University of Manchester
23
Lighting and Shading
Tp N
1.0 v
Te
v
u
0.0
u
1.0
Figure 20: Mapping function for a sphere With these definitions we obtain the v-coordinate as φ v = --π
(4-2)
where φ is a latitudinal parameter given by φ = cos – 1 ( – T p • N )
(4-3)
If v is equal to 0 or 1 then u is defined to be zero. If not, then the u-coordinate is calculated as follows cos – 1 [ ( T e • N ) ⁄ sin φ ] u = -------------------------------------------------------------2π
(4-4)
( Tp × Te) • N
(4-5)
However, if the quantity
is less than zero we must use u → u − 1. This test is needed to decide which side of Te the point lies on. That is all there is to the calculation. With the u- and v-coordinates we pick out the colour from the image file containing the pattern and apply this to the appropriate point on the sphere. In Figure 21 we can see an example of pattern mapping onto a sphere. The pattern consists of a regular array of squares of random lightness and the mapping onto the surface of a sphere produces the distortions shown on the right of the figure. Note the concentration around the pole and the stretching around the equator.
24
Computer Graphics and Visualisation
Texture and transparency
Figure 21: Mapping a regular pattern onto a sphere
4.1.2 Parametric space mapping This is similar to object space mapping in that we need to determine a mapping from pattern space to surface space, but the surface in this case is defined parametrically (see the module on Curves and Surfaces).
4.1.3 Environment mapping This mapping technique is an efficient method of simulating the mirror-like reflection of an environment in an object. It is popularly seen in feature films, such as The Abyss or Terminator 2, which have mixed 3D computer graphics with live camera footage. The environment mapping technique relies upon the existence of an image, or of images, which depict the view of the scene surrounding the object. To see how this mapping works, imagine placing a cube around the object and photographing the scene through each of its six faces. These six images constitute the environment map. During rendering, when we come to colour the object’s surface at a particular point, we construct a reflection vector at this point relative to the corresponding view vector (see Figure 10). We then find where on the surface of the cube the reflection intersects and thence where in the image maps this vector points to. The colour in the image map corresponding to this intersection is the colour we apply to the surface of the object. The image maps are, in this case, just perspective projections of the scene onto the six faces of the cube. However, since each view is 90° away from its nearest neighbours, significant distortions occur in those parts of the image maps corresponding to each edge of the cube, which can appear un-natural in a rendering of the object’s surface. In most cases, this limitation can be tolerated, especially for objects with a complex shape.
The University of Manchester
25
Lighting and Shading
4.2 Roughness There are various scales of roughness, each producing a different effect. • Macroscopic roughness may be so large that it affects the geometry, and the surface is visibly not flat. Examples of this scale of roughness include the texture of an orange, the tread of a tyre, and so on. • Much smaller micro scale roughness gives a surface that appears smooth, but does not function as a perfect mirror. Examples include common materials such as paper, painted walls, smooth wood, and so on. While small in geometric terms, the roughness is still large compared to the wavelength of the incident light. • Finally, nano scale roughness is of a similar magnitude to the wavelength of light and produces a range of interference colours. These effects are rarely modelled in computer graphics.
4.2.1 Macroscopic roughness Most natural objects are not smooth; their surfaces have some kind of micro-structure. For example, we could model an orange using a sphere of the appropriate colour. Even with a realistic shading model, it won’t look much like a real orange; the reason is that the surface of an orange is not smooth. One approach is to model the geometry in a more complex way. In some cases, this is acceptable. For example, to model a sphere with four large spikes, the geometric form would be altered.To generate a realistic orange, however, with thousands of bumps and pits, would be very complicated. The resulting object would also be very slow to render. An alternative that is often chosen is to simulate a more complex geometry at the rendering stage, by altering the uniformity of a surface using a perturbation function that effectively changes the geometry of the surface. A common technique for modelling surfaces which are not smooth is to perturb the normal vector at each point on the surface, using a wrinkle function, before computing the intensity of the point using an illumination model.
Figure 22: Bump mapping
26
Computer Graphics and Visualisation
Texture and transparency
A rough surface results if a small random component is added. More regular features can be produced if the perturbation of the normal vector is a function of its position on the surface. For example, a surface can be made 'wavy' by perturbing the surface normal using a sine function. This is also called bump mapping and is shown in Figure 22. For a surface Q defined parametrically as Q(u, v) we can define a new (rough) surface Q'(u, v) by adding a wrinkle function f(u, v) to Q at each point in the direction of the original normal at that point. So for a point q on the surface, the new perturbed point q' is: q' = q + f ( q ) n q where nq is the unit normal vector at q. Figure 23 illustrates a torus whose surface has been made bumpy with this technique. The perturbation function can be defined as an analytic function, or a set of values stored in a lookup table. Defining values for an entire surface would require a very large table, so a common approach is to store a small number of quite widely spaced values, and to interpolate between them for the intermediate points. Note that this technique avoids explicitly modelling the geometry of the new, roughened, surface. This has an important visible effect, if you look at the silhouette of a surface roughened in this way, it is still smooth. The roughening only becomes apparent when the shading model is applied.
Figure 23: Example of bump mapping
The University of Manchester
27
Lighting and Shading
4.2.2 Microscopic roughness The Cook-Torrance model was developed by illumination engineers and describes surfaces whose microscopic roughness is large compared to the wavelength of the incident light. A reflecting surface is modelled as a collection of randomly oriented microscopic facets, each of which is a perfect reflector. Figure 24 shows a collection of microfacets, and we are only concerned with those facets which reflect light to the viewer. In the figure, these are facets a, c and e.
To viewer
f
N
a Actual surface
b
c
d
e
Figure 24: Modelling a surface with microfacets The model accounts for three situations: • A facet reflects directly towards the viewer – this is illustrated by facet a in the diagram. • A facet is in the shadow of a nearby facet, so it does not receive the full amount of illumination – facet c is partially shadowed by facet b, shown by the dotted line. • A facet reflects, but the reflected rays are re-reflected by another facet and do not reach the viewer – rays reflected by facet e hit facet f. These multiple reflections contribute to the diffuse reflection of the surface. The Cook-Torrance model takes these effects into account, and gives the amount of specular reflection of the overall surface ks as DGF k s = -----------------------------------------------π ( N • V) ( N • L) D is the distribution function describing the directions of each facet comprising the surface (usually a Gaussian probability function), and G is a factor which describes the amounts by which the facets shadow and mask each other. F is the
28
Computer Graphics and Visualisation
Texture and transparency
Fresnel factor, which gives the fraction of light incident on a facet that is reflected rather than being absorbed. It is defined as 2
2
1 sin ( φ – θ ) tan ( φ – θ ) F = --- ------------------------------- + -------------------------------2 sin 2 ( φ + θ ) tan 2 ( φ + θ )
4.2.3 Phong vs Cook-Torrance The two models give different results. In particular • The location and intensity of highlights produced by both models are similar for small values of θ, but differ considerably as θ increases towards grazing angles. • The Phong model assumes that highlights are the same colour as the illumination. In reality this is not true, since the reflectance of materials depends on wavelength. The Cook-Torrance model incorporates this effect (via the Fresnel function) and gives more realistic results. • The Cook-Torrance model is computational more expensive than Phong and for some purposes the extra cost is not justified.
4.3 The optics of transparency Not all materials are opaque. Some materials allow light to pass through them, and light passing through a surface is called transmitted, or refracted light. There are two kinds of refraction: • Diffuse refraction – This occurs when a surface is translucent but not truly transparent. Light passing through it is jumbled up and scattered by internal and surface irregularities. When a scene is viewed through a surface with diffuse refraction, it appears blurred to a greater or lesser extent. • Specular refraction – This occurs when light passes through a transparent object. A scene viewed through a specular refractor will be clearly visible, although in general the light rays will have been bent as they passed through the material (Figure 25).
Figure 25: Refraction. How light behaves when moving from one medium into another is described by Snell’s Law. With reference to Figure 26
The University of Manchester
29
Lighting and Shading
η 1λ sin θ = η 2λ sin θ'
(4-6)
Here η1λ and η2λ are called the refractive indices of the media, and because η depends on the wavelength of the incident light, different colours will be bent by different amounts. We commonly use an averaged value for η of a material, ignoring wavelength dependence. This means that glass prisms, for example, do not produce rainbows. They do bend the light, however - otherwise they would be invisible! The effect of Snell’s Law is significant: a beam of white light passing from air into heavy glass (η ≈ 1.5) at an angle of 30° will be bent by about 11°. On leaving a sheet of glass the light will be deviated again, and the effect is that the incident and refracted beams are parallel, but shifted.
Normal Incident ray
Reflected ray
θ
η1
Interface
η2 θ'
Transmitted (refracted) ray
Figure 26: Snell’s law. Note that refraction can have significant visible effects. In Figure 27, we have two refracting objects A and B, both with η greater that the surrounding medium, and two opaque objects C and D. If we look from viewpoint v1, ray r1 will undergo refraction by A and we will see C. If there were no refraction, we would see D. Similarly, looking from v2, we see (after refraction) D – again, without refraction we would have seen C. In practice good results can be obtained by simply shifting the path of the incident light by a constant (small) amount. This avoids the computational overhead of the trigonometry. For very thin objects, the path shift is so small it can be safely ignored. We will ignore the path shift caused by refraction, and concentrate on the drop in intensity as light passes through a transparent material.
30
Computer Graphics and Visualisation
Texture and transparency
r1
v1
A
C
r2
v2
D
B
Figure 27: Refraction effects (highly exaggerated).
4.4 Modelling transparency When a refracting object is interposed between the viewpoint and a scene, the scene behind is visible through the object, but the object itself is visible too. Figure 28 shows a scene viewed from above. Polygon pf is in the foreground of the scene, and pb in the background. To model the refraction due to pf, we add some proportion of the intensity of the point b on the background object pb visible through pf.
(y-axis out of page) x
b pb
pf f
z
Figure 28: Modelling transparency.
The University of Manchester
31
Lighting and Shading
We introduce a transmission coefficient, t, which is a measure of the transparency of an object. For a completely opaque object, t = 1, and for a perfectly transparent object, t = 0. Then the observed intensity I is I = tI f + ( 1 – t ) I b This approach also allows us to model non-uniform surfaces: we can make t a function of position on the surface. For example, if we view a scene through a glass vase, the apparent transparency of the silhouette edges of the vase will be less than the surface directly facing the viewer. Figure 29a shows how light travelling through the edges of the object has to penetrate more of the material than light travelling directly front to back. Figure 29b shows a top view of a scene where an object B is being viewed through a transparent glass object A. We want to have less transparency as we move round the surface from p1 through p2 to p3, and we can use the z-component of the surface normal, nz, at each point p to determine this. A simple linear relationship for the transparency is given by t = t min + ( t max – t min ) n z where tmin and tmax are the object’s minimum and maximum transparencies. The point p1 has nz = 0 hence t = tmin as expected. Similarly, at point p3, nz = 1 and t = tmax. Point p2 has a transparency somewhere between tmin and tmax according to its value of nz. An alternative relationship has also been proposed which is non-linear: t = t min + ( t max – t min ) 1 – ( 1 – n z )
m
where m is a power factor of the order 2 or 3.
S (y-axis out of page) x
R B p1
A p2
p3
z Ray R travels through more material than ray S
(a)
View
(b) Figure 29: Non-linear transparency.
32
Computer Graphics and Visualisation
5 Ray-tracing All the illumination models we have seen so far are local, so they suffer from one omission: it is not possible to model the reflections between objects in a scene. Effectively they consider each surface to be suspended in space, independent of other surfaces, and illuminated from one or more light sources. We now consider global methods which are able to model the object-to-object interactions in a complex scene. The first global illumination algorithm to appear was called recursive ray-tracing which produces very realistic results by applying the laws of optics. This method follows the transport of light energy along rays through a scene, which means in practice that only specularly reflected or transmitted light can be accounted for. In reality, of course, there is a complex mixture of diffuse and specular light within an environment, particularly diffuse which to a large extent defines the body colour of a surface. In practice, ray-tracing methods include an element which applies a local illumination calculation to simulate diffuse effects. Without this, ray-tracing would only produce images of objects with black, shiny surfaces! Another feature of ray-tracing is that its technique can be used on many different kinds of surfaces, not just polygons but also simple primitives such as spheres, cones and tori, as well as parametrically-defined surfaces. Another powerful and popular method used for the calculation of global illumination is called radiosity which, in contrast to ray-tracing, is particularly suited to environments with only diffusely reflecting and transmitting surfaces. Images produced by the radiosity method are highly realistic, some say more so than raytracing. However, the radiosity method is mainly limited to the rendering of objects defined by meshes of polygons. We shall discuss radiosity in the next chapter. Bare in mind that the motivation behind the development of global methods is the goal of image realism. Here attempts are made to model the physical nature of light energy and its interaction with a material environment to produce images which are close to being photographic and indistinguishable from a real scene. The quest for realism in our images, however, has its drawbacks in terms of computational cost, which arise simply because we wish to model phenomena which are considerably more complex than that considered in a local illumination model.
5.1 Basic algorithm The basic idea of ray-tracing is that an observer sees a point on a surface as a result of that surface interacting with rays of light emanating from elsewhere in the environment. When we discussed local illumination models only the interaction of the surface directly from the light sources was considered. In general, a light ray may reach the surface indirectly via reflection from or transmission through other surfaces. The ray-tracing method calculates that part of the global illumination, provided by this object-to-object interaction, which is distributed by specular light. As such, it is view-dependent since, for example, the position of the
The University of Manchester
33
Lighting and Shading
reflection of one object in the surface of another changes when the position of the view-point changes. In principle the ray-tracing algorithm is simple: we trace the path of each light ray emitted from each light source, follow all of its reflections and refractions as it interacts with the environment, and if it eventually reaches the view-point, use its intensity to render one part of the image (see Figure 30). In practice this can’t be done. Each light source emits a infinite number of rays, the majority of which never reach the view-point anyway. The answer is to start at the view-point and follow rays back into the scene.
Light source
A
B
C
Viewing screen
Eye Figure 30: Ray tracing from the light sources. As an aid to projecting rays out into the scene we define both a view-point and view-plane in object-space coordinates (a simple viewing system is described in more detail below). The view-plane is then divided up into a large number of imaginary pixels which will be mapped onto the real pixels on a graphics display. One ray per pixel is traced from the view-point through the view-plane (this ray is called the primary- or eye-ray) and the nearest object with which it intersects is found. The object thus found will represent the surface which is visible through that particular pixel. (Notice how primary ray-tracing is itself a form of hiddensurface removal.) Mathematically, an arbitrary ray is defined in parametric form as r ( t ) = O + Dt
34
Computer Graphics and Visualisation
(5-1)
Ray-tracing
where r is the position vector of a point on the ray’s path with parameter t, O is the position of the origin of the ray and D is a unit vector in the direction of the ray. The parameter t is a real-valued number and represents the distance (in whatever units are in use) along the ray from its origin and for all primary rays the origin lies at the view-point The next stage in the algorithm is to calculate the intensity of light leaving the surface of the object at the point of intersection (see Figure 31). This intensity will be due to a local component similar in form to that in Equation 2-7 plus components due to globally reflected and transmitted light. We can write this, for m light sources, as follows m
I = kd Ia + kd
∑
I p i ( N • L i ) + k s ( R • V )n + k r I r + k t I t
(5-2)
i=1
where • kd, ks, Ia, n, N, R and V are as previously defined • I p i and Li are the intensity and direction of light source i • kr is the global specular reflection coefficient • Ir is the intensity of the light coming from the direction R • kt is global specular transmission coefficient • It is the intensity of the light coming from the direction T An expression such as that given in Equation 5-2 exists for each colour band we are using.
Ir
I N R V θ1
L
η1 η2 θ2
T -N It Figure 31: Ray-tracing geometry The calculation of the last two terms in Equation 5-2 is accomplished by spawning new rays at the intersection point in the direction of specular reflection and refraction (along R and T respectively). Of course, if kr is zero, the surface is perfectly diffuse and no global specular reflection occurs, so there is no need to spawn a new reflected ray. A similar point can be made regarding the refraction of light,
The University of Manchester
35
Lighting and Shading
where if kt is zero then the surface can be considered perfectly opaque, so no light energy is transmitted through the surface. These new rays, called secondary rays, are traced through the scene exactly like the primary rays were, until they intersect another object surface. Then we can apply Equation 5-2 again at the new intersection point to give us the values of Ir and It for the primary ray. As you can see this process can be repeated again and again, in a recursive manner, creating new secondary rays at each intersection point and returning the values Ir and It for each previous ray-trace. The recursive nature of ray-tracing is illustrated in Figure 32 where there are four surfaces S1, S2, S3 and S4 in the scene, all of which have some degree of transparency except S4, which is opaque.
R4
Reflected ray
S4
Transmitted ray
T1
T3 T2
R1 S1
S3
S2
R
R3
R2 p
Viewing screen
Pixel
Eyepoint
Figure 32: Ray tracing a particular pixel. We trace the primary ray R in the view direction through pixel p. It first hits S1, producing two secondary rays R1 and T1. There are no other surfaces for T1 to hit, but R1 intersects S2 and produces to new rays R2 and T2. Ray R2 heads off into the background while T2 intersects surface S3 giving rise to rays R3 and T3. Ray R3 leaves the scene but ray T3 hits surface S4. Since S4 is opaque only a reflected ray R4 is produced. We can see that rays T1, R2, R3 and R4 won’t make a contribution to the intensity seen through pixel p because they are rays which could never arise from the light sources.
36
Computer Graphics and Visualisation
Ray-tracing
5.2 A simple viewing system The purpose of the viewing system is to aid the computation of eye-rays through the image pixels. In this section we will describe the layout and use of a simple but effective system which can produce perspective views of our scene. The system is shown in Figure 33.
(1, 1)
V H Y
G (0, 0)
U φ
θ E X Figure 33: Viewing system The eye is located at E with position vector E and is looking in the direction of G. The vector G can be directly calculated from the eye and look point positions. The two viewing angles 2θ and 2φ are the horizontal and vertical fields of view of the view-plane (and hence the image) respectively and indicate the amount of perspective in the picture. If it is assumed that the pixels in the image are square, then the two angles are not independent. Only one angle needs to be specified (2θ), the other can be computed from knowledge of the image resolution. One more piece of information is required before the view-plane is uniquely defined. An 'up' vector (U in the diagram) is needed to indicate the orientation of the view-plane about the look direction. Next a coordinate system is set-up on the view-plane such that the bottom lefthand corner is the origin and the upper right-hand corner is at (1, 1). Now construct two vectors X and Y from X = G×U Y = X×G These vectors are orthogonal to each other and parallel to the view-plane. From the geometry it can be seen that the vectors H and V can be written H = d tan θX V = d tan φY if X and Y are normalised and d is the distance of the view-plane from the eye. Using H and V a point on the view-plane, say S, can be expressed as S = E + dG + ( 2x' – 1 ) H + ( 2y' – 1 ) V
The University of Manchester
37
Lighting and Shading
where G has now been normalised and (x', y') is the position of S in the view-plane coordinate system. If the point S represents the centre of a pixel then in terms of its screen position (i, j) i + 0.5 x' = ---------------Rx j + 0.5 y' = ---------------Ry where Rx and Ry are the image resolution. Now it is a simple matter to calculate the direction of the ray through S as P = S – E = dG + ( 2x' – 1 ) H + ( 2y' – 1 ) V We now have all the parameters we need to define the ray originating at the eye and passing through the pixel at point S: once P has been normalised we equate this vector with D in Equation 5-1, and set O equal to E.
5.3 Reflection and refraction vectors In order to trace a ray’s path we must be able to calculate the unit vectors R and T shown in Figure 31. Referring to Figure 34, the reflected vector is computed by noting that the vectors R, V and N lie in the same plane and using similar triangles. Firstly, we find the vector N' by multiplying the normal N by cosθ1. Since cosθ1 is just the dot product of N and V, then N' = N ( N • V )
(5-3)
This vector is called the cosine vector of V since its length is just cosθ1.
N
S
S N'
V
θ1 θ1
R
Figure 34: Computing the reflected vector From this we can calculate the corresponding sine vector S as S = N' – V
(5-4)
By definition the length of the sine vector is sinθ1 since the vectors V, N' and S form a right-angled triangle. We can now see that the reflected vector R is given by R = N' + S = 2N' – V
38
Computer Graphics and Visualisation
Ray-tracing
or R = 2N ( N • V ) – V
(5-5)
The direction of the transmitted ray T is found by applying Snell’s law given in Equation 4-6 and noting that T is in the same plane as V and N (see Figure 35). We compute T by finding the sine vector (S) of the incident vector, scaling it using Snell’s law to get the sine vector (S') of the transmitted vector, computing a cosine vector (N'') for the transmitted ray and then adding the sine and cosine vectors to give the transmitted vector. The sine vectors, S and S', are related by Snell’s law η1 S' = ------ S η2
(5-6)
The length of the cosine vector is given by N'' =
1 – S'
2
since cos2θ+sin2θ = 1, hence N'' = – N N'' = – N 1 – S'
2
N S
V
θ1
N' η1 η2 θ2
N''
T
S' Figure 35: Computing the transmitted vector Therefore, the transmitted vector is given by η1 2 T = N'' + S' = ------ S – N 1 – S' η2 This equation can be written entirely in terms of the original vectors N and V using Equation 5-6, Equation 5-4 and Equation 5-3. After a little simplifying algebra we arrive at the following result η1 η 2 2 T = ------ [ N ( N • V ) – V ] – N 1 – -----1- 1 – ( N • V ) η2 η2
The University of Manchester
(5-7)
39
Lighting and Shading
If we take a look at Equation 5-6 again, we can see that it is possible for the length of S' to be greater than one when η1 > η2. This situation will arise when the light is moving from one medium to a less dense medium, such as from glass to air. However, by definition the length of a sine vector is less than or equal to one and anything else is physically impossible. The significance of this condition is that it will alert us to the fact that total internal reflection has occurred. Hence there is no transmitted vector and the internally reflected vector can be calculated by flipping the normal N and applying Equation 5-5.
5.4 Shadows Returning briefly now to the calculation of the intensity of light leaving a surface (see Equation 5-2 and Figure 31), another computation is usually made at the same time to determine whether the intersection point is in shadow with respect to the light source. Testing for shadows is very simple in theory. We first construct a ray whose origin is at the point of intersection and whose direction is towards the light source, vector L in Figure 31. This ray, called the shadow ray or shadow feeler, is then tested for an intersection with all the objects in the scene, as in the case with finding the original intersection. If a hit is found and the intersection is nearer than the light source then we can say the surface point is in shadow for that light source. If there are multiple sources of light, shadow rays are traced to each one to determine which will cast a shadow on the surface. Only those which do not block the light contribute local illumination. The light from the rest should not be included in the calculation of the intensity leaving the surface in Equation 5-2. Usually the object whose surface we are shading is included in the list of objects we are testing the shadow ray against. However, we may come across a problem which has been termed ray-tracing acne. This is caused by inaccuracies in the calculated position of the intersection point which causes it to fall a very short but not insignificant distance away from the object’s surface. If the point should fall in the space behind the surface with respect to the light source, then an intersection of the shadow ray with its originating surface will be detected. This will produce a dark spot on the surface in the image of the object. Clearly this is an undesirable artefact which can be compensated for in a number of ways. We can introduce a small tolerance value, say 10-5, and stipulate that only intersection distances along the shadow ray greater than this value are genuine. Of course, the tolerance value must be greater than any inaccuracies which we think the intersection calculations produce but still small enough not to miss any real object intersections. Alternatively, we can use this tolerance value to move the shadow ray origin towards the light source before performing the intersection tests. Another way, applicable only if we know that the originating surface cannot 'see' itself, i.e. is convex, is to exclude this surface in the intersection tests.
5.5 The ray-tree We can visualise the recursion process used in ray-tracing using a diagram called a ray-tree which depicts the complete pattern of surface intersections that need to be found in order to find the intensity and colour of the light received by one par-
40
Computer Graphics and Visualisation
Ray-tracing
ticular pixel. The ray-tree corresponding to the situation in Figure 32 is shown in Figure 36. When implementing a ray-tracer it is necessary to set-up a maximum depth to which rays are traced, otherwise we may find we are spending a great deal of time computing rays which contribute little to the final image. However, if the ray-tree is not allowed to grow far enough, artefacts may be evident in the resulting picture. There are three ways in which the depth of the ray-tree may be limited. The first, which may happen as a matter of course, occurs when a particular ray shoots off without intersecting any objects, thus terminating this branch of the tree. In our example tree this occurs for rays T1, R2, R3, and R4. The colour returning from these directions is usually a user-defined background value.
Eye ray
Shadow ray(s) S1 R1
T1
Shadow ray(s) S2 T2
R2 Shadow ray(s)
S3 T3
S4
R3
Shadow ray(s) R4 Figure 36: An example ray-tree
Another method is to explicitly set the absolute maximum depth for the ray-tree so that rays are not spawned deeper than this level. For example, if we define rays from the eye as depth 0, then a maximum ray depth of 2 will prevent rays T3, R3 and R4 contributing to the intensity received by the eye-ray. Now, experience has pointed out that only a small percentage of a typical scene consists of highly reflective or transparent surfaces, so it would be inefficient to trace every ray to the maximum depth. What we need is an adaptive depth control
The University of Manchester
41
Lighting and Shading
which can take account of the properties of materials the rays interact with. This is our third method. Rays are attenuated by the global specular reflection and transmission coefficients. So, as an example, the contribution of ray T3 to the final colour at the top level is attenuated by the product of the coefficients k t 3 k r2 k r1 . If this value is below some threshold there will be no point in tracing back further than ray T3.
5.6 Object intersections In this section we will discuss the problem of finding the intersection point of an arbitrary ray with a particular surface. To determine this we must test the ray against every surface defined in the scene and if an intersection is found that it is the nearest to the ray’s origin, that is we need to find the minimum positive value for the parameter t in Equation 5-1. As an example of the computation of intersections, we will demonstrate the procedure on two common types of surface: a sphere and a polygon.
5.6.1 Sphere intersection The sphere has one of the simplest kinds of surface and the computation of the intersection point of an arbitrary ray is quite straightforward. We define a sphere using two parameters: its centre by the position vector C, and its radius R. Points on the sphere’s surface with position vector r then satisfy the following equation: ( r – C) • ( r – C) = R
2
(5-8)
This is easy to understand: on the left-hand side r - C is a vector originating at the sphere’s centre and terminating on its surface, so the length of this vector is equal to the radius of the sphere. The dot product of this vector with itself gives the length squared, hence Equation 5-8. To find the intersection point we substitute Equation 5-1 into Equation 5-8 and solve the resulting equation for the parameter t. After some vector algebra we arrive at the following quadratic equation t 2 – 2tµ + λ – R
2
= 0
(5-9)
where we have defined µ = D•T λ = T•T T = C–O
(5-10)
We can solve this quadratic equation using the standard formula, giving t = µ± γ 2
where γ = µ 2 – λ + R . Depending on the sign of γ there exists one of three situations: • If γ < 0: the ray misses the sphere • If γ = 0: one intersection which grazes the surface
42
Computer Graphics and Visualisation
(5-11)
Ray-tracing
• If γ > 0: two distinct intersections with t = t1 = µ + γ and t = t2 = µ - γ Usually the first two conditions are grouped together since a grazing intersection is not meaningful. At this point we can see that there is quite a lot of calculation to be done before we decide an intersection occurs. Often this can be avoided by using intermediate results to cut short the computation if we find that an intersection cannot take place. Hence we avoid calculations until there are needed. We will now show how this is done. First of all, we need to know if the ray’s origin lies inside the sphere. To do this calculate the vector T given in Equation 5-10. The length of this vector is just the distance from the ray’s origin to the centre of the sphere, hence we can compare this 2 with the sphere’s radius in the following way: if T • T < R the ray originates inside the sphere and must intersect the surface. Otherwise, the origin is on or outside the sphere and an intersection may or may not occur. These two cases are illustrated in Figure 37. We also define here that a ray whose origin lies exactly on the surface is considered not to intersect the sphere at the ray’s origin.
Origin outside the sphere
D
D O
Origin inside the sphere
R T
C
O
R T C
Figure 37: The ray origin in relation to the sphere The next step is to find the value of the parameter t for the point on the ray closest to the centre of the sphere (see Figure 38 which shows the case where the ray origin is outside the sphere). This distance is given by the projection of T along the ray, i.e. D • T , which as we have seen is equal to µ (we now have a geometric interpretation for the parameter µ). If µ ≤ 0 the intersections, if any, lie behind the ray origin and if the origin is outside the surface, the ray cannot intersect the sphere (Figure 38a). Hence we can stop the calculation here. If µ > 0 and the origin is outside the sphere (Figure 38b), we still cannot say if an intersection occurs, so we proceed to the next stage of the algorithm.
The University of Manchester
43
Lighting and Shading
(a)
(b)
µ µ
T
T
Figure 38: The point of closest approach (t = µ) The distance along the ray from the point of closest approach to the sphere’s surface is determined next. We have two right-angled triangles to help us, as shown in Figure 39. The point Q is the point of closest approach, at a distance d from the sphere’s centre, and l is the distance we require.
Q l d
R
O T
C
Figure 39: Determining the intersection points Using Pythagoras’s theorem, 2
2
l = R –d
2
and 2
d = λ –µ
2
since OQ2 = µ2 and OC2 = |T|2 = T • T = λ, hence 2
2
2
l = R +µ –λ = γ
44
Computer Graphics and Visualisation
Ray-tracing
We can now see how Equation 5-11 works: µ is the distance to the point of closest approach, while the square root of γ is the (signed) distance from here to the intersection points, if they exist. We can then test the value of γ to find whether intersections occur and if so where the forward one is located. If the ray’s origin is outside the sphere, we need the value of t which is least, which is t = µ– γ If the ray’s origin is on or inside the surface, we have t = µ+ γ Once we know a relevant value of t, we can plug this number into the ray equation (Equation 5-1) and determine the actual position of the intersection point.
5.6.2 Polygon intersection The ray-polygon intersection calculation is composed of two stages: • Find where the ray intersects the polygon’s embedding plane and • if an intersection exists, find if this point is inside the polygon. The embedding plane of a polygon is just the infinite plane in which it lies. This plane is defined by the equation N•P+d = 0
(5-12)
where N is the unit normal, d is the plane constant and P is the position vector of any point in the plane. The normal vector is equal to the polygon’s normal and can be computed from ( V1 – V0) × ( V2 – V0) N = ---------------------------------------------------------------( V1 – V0) × ( V2 – V0) where V0, V1 and V2 are the position vectors of three of the polygon’s vertices. The plane constant is given by d = –V0 • N From Equation 5-1 and Equation 5-12 we can evaluate the parameter t corresponding to the intersection point as d+N•O t = – ------------------------N•D
(5-13)
Note first that if N • D = 0 then the ray and the plane (and hence the polygon) are parallel and no intersection is possible. Secondly, if t ≤ 0 the intersection lies behind the ray origin and is of no interest since we only need forward intersections. Thirdly, if t is greater than the current nearest intersection distance then the present intersection cannot be the nearest and is rejected. Only if all these tests are proved false do we proceed to the second stage.
The University of Manchester
45
Lighting and Shading
2 intersections = outside
1 intersection = inside 3 intersections = inside 4 intersections = outside
Figure 40: Jordan curve theorem We now determine whether the intersection point lies within the boundary of the polygon. To do this we use an idea known as the Jordan curve theorem which is illustrated in Figure 40. Shown here is a complex two-dimensional polygon and a number of points (in black) dotted around it. From each point a line is drawn in an arbitrary direction and the number of crossings with the polygon boundary is noted (points in grey). The theorem states that if the number of crossings is odd, the point is inside the polygon; otherwise it is outside. z
N
y (v)
x (u) Figure 41: Projecting the intersected polygon To use this theorem we project the polygon onto one of the coordinate planes (xyplane, xz-plane or yz-plane) making sure, for accuracy reasons, that the projected area is the greatest. We can determine which plane by examining the components of the normal vector N: if the magnitude of the z-component is the largest then
46
Computer Graphics and Visualisation
Ray-tracing
project onto the xy-plane and similarly for the other components. Projection onto the coordinate planes simply involves ignoring a corresponding component. For example, if |Nz| is the largest component, projecting the point (x, y, z) = (a, b, c) onto the xy-plane gives the point (u, v) = (a, b); see Figure 41. We also perform the same operation on the coordinates of the intersection point. Then we end up with a situation which may look something like that shown in Figure 42a. The plane coordinates of the intersection point are (ui, vi).
v
v
(ui,vi)
u
u (a)
(b)
Figure 42: Point in polygon test We can now perform the polygon inside-outside test by translating the polygon so that the intersection point is at the origin (see Figure 42b). This is simply achieved by subtracting the point’s coordinates from each vertex. Now use the positive u-axis and test for an intersection with each edge of the polygon. If the total count of crossings is odd, then from the Jordan curve theorem we conclude the intersection point is inside the polygon. What happens if the positive u-axis passes exactly through a vertex? This special case can be avoided in the following way: the u-axis divides the plane into two parts and we declare that vertices that lie on this line are on the +v side. In effect we have redefined the u-axis to be infinitesimally close to the original axis, but not to pass through any points. Thus the special cases do not occur.
5.7 Acceleration techniques Testing a ray against every surface for an intersection is a time-consuming operation which gets worse the greater the number of objects in the scene. The operation is also more expensive the greater the image resolution since more eye-rays are generated. It has been estimated that over 90% of the computation time is spent comparing rays with objects. Therefore, it is advantageous to investigate ways of cutting down on the time taken to find the nearest intersection and so speed-up the calculation of an image. A number of schemes are available which either limit the number of tests that have to be made, or delay a costly test until a cheaper test has confirmed its necessity.
5.7.1 Hierarchical bounding volumes In the early days of the development of ray-tracing a technique was developed which used the concept of bounding volumes. The bounding volume of an object is
The University of Manchester
47
Lighting and Shading
a simple primitive (e.g. a sphere or box) which has the smallest volume enclosing the object. During the ray intersection test on an object, a test is first performed on the bounding volume, which is usually easier and faster. Only if the bounding volume is intersected is the test applied to the object itself. The use of a sphere as the bounding volume is the most popular and the raysphere intersection is particularly efficient considering we only need to figure out if an intersection takes place without actually calculating the intersection point. However, the calculation of the parameters (centre and radius) describing a particular bounding sphere is not that easy. In the ideal case, the sphere with the smallest volume is determined by simultaneously varying both the centre and the radius and looking for the minimum radius which encloses the whole object. In practice, a good approximation is made by calculating the centroid of the object, this will be the sphere’s centre, and finding the distance to the point furthest from the centroid, this is equated to the radius. Bounding volumes can also be used in a hierarchical structure, whereby clusters of bounding volumes are themselves enclosed in a larger volume. The ray intersection testing would proceed by first detecting a hit with the outer bounding volume and then decide whether it needs to continue into this volume and perform intersection tests on the inner ones. Of course, we can have volumes within volumes within volumes, if we so wish, to any number of levels. This idea would work well in scenes containing objects which are not uniformly distributed, so that a particular ray doesn’t need to do much work in areas of the scene which it is not 'looking' at. Such a hierarchical bounding volume scheme would result in a considerable speed-up in the rendering time.
5.7.2 3D spatial subdivision Other acceleration schemes have also been developed which are more powerful, but no so easy to implement. These methods are based upon the idea of voxels, which are axis-aligned rectangular prisms acting like bounding volumes except they fill all of the space occupied by the scene and are non-overlapping. There are two varieties to this method: uniform and non-uniform. In a uniform arrangement we surround the whole scene with a bounding cuboid and then split this into L×M×N smaller cuboids, which are the voxels. For each voxel we keep a list of the objects which encroach into its space. Then, when we trace a ray through the scene, we need only look at objects which pertain to the voxels the ray passes through. Figure 43 shows a 2D example of uniform spatial subdivision of a simple scene containing 8 objects and a ray passing through it. The voxels which are shaded correspond to those which the ray passes through and we see that a number of objects are intersected by the ray. We follow the ray voxel by voxel. The first voxel has one object associated with it, but we find that the surface is not intersected. We move onto the next voxel which contains two objects, one of which has already been tested and was found not to have been intersected. This information would have been retained from the time the first intersection test was performed. The ray intersection tests will show that the polygon has been hit, but the intersection point lies outside the current voxel, so we cannot be sure that this is the nearest intersection. We can see from the diagram that this is definitely not the nearest intersection, but the computer has still to discover this! We move on but keep this information to avoid recalculation. The next voxel contains a triangle as well as the previous polygon and we find that the
48
Computer Graphics and Visualisation
Ray-tracing
triangle is hit first, and the intersection point is in the current voxel. Thus we have found the nearest intersection. We can see that the ray passes through another object further 'down-stream' (in fact there could easily been many more objects) but we didn’t have to perform any intersection test for this object, which would have been the case without this acceleration method. In our example scene we needed only to perform 3 out of 8 possible intersection tests. In a more realistic 3D scene, containing maybe hundreds of thousands of objects, being able to restrict intersection testing to objects in the neighbourhood of the ray’s trajectory can significantly reduce the rendering time.
Figure 43: Uniform spatial sub-division (2D analogy) In the non-uniform method of 3D space subdivision, the aim is to produce a partitioning which emphasises the distribution of the objects throughout the scene. The structure used in this case is called an octree, which is a hierachical tree of non-overlapping voxels of various sizes. The octree is constructed by first surrounding the whole scene with a bounding cuboid. This volume is then divided into eight equal-sized sub-volumes or voxels and for each voxel a list of objects associated with it is found. Now, depending on whether a specified maximum treedepth has been reached, each voxel is sub-divided again into eight if the number objects in that voxel exceeds a pre-set maximum. In this way the sub-division into smaller voxels only occurs where there are lots of objects. In regions of the scene with few or no objects the voxels are large.
Figure 44: Non-uniform spatial sub-division (2D analogy)
The University of Manchester
49
Lighting and Shading
We will demonstrate the use of an octree by analogy with the 2D case, in which the sub-division produces a structure called a quadtree (see Figure 44). Again the shaded voxels indicate those traversed by the ray and sub-division is continued until there is at most only one object per voxel. The procedure for finding the nearest intersection is the same as in the uniform case: we process in order only those voxels which the ray passes through until we find an intersection point in the current voxel. In the example shown 6 voxels are considered before an intersection is found and only 3 distinct intersection tests are made. We can see that large areas of the scene are empty, indicated by the large voxels, where the ray passes through very quickly. Only where there are higher concentrations of objects does the ray start to do any useful work in finding an intersection. In any kind of 3D spatial sub-division, there is usually a trade-off between how finely we sub-divide the scene (influenced by the distribution of objects in the scene and the maximum number of objects per voxel) and the time spent hopping from one voxel to another along the ray. These features can normally be controlled by the user and adjusted for optimum efficiency. When this is done significant savings in rendering time can be accomplished, especially for complex scenes containing huge numbers of objects. As an indication of what can be achieved, it was once quoted by the developers of the uniform sub-division method that a scene consisting of 10,584 objects, which was ray-traced in 15 minutes using their technique, took an estimated 40 days to render using the traditional method! This is indeed a very important and powerful technique.
5.8 Image aliasing As we have seen, when we use ray-tracing to generate a synthetic image we pass an eye-ray through each pixel in the view-plane. What we have done, of course, is sample the view-plane in a number of discrete directions and this sampling process will introduce into our image an artefact called aliasing. Aliasing is a direct consequence of trying to represent a continuous signal (the distribution of light intensity across the view-plane) in terms of a sequence of discrete samples: if we attempt to reconstruct the signal from our samples (for example, by displaying the samples on a graphics screen), distortions will appear and the reconstruction will not be exactly the same as the original. It is a well known fact from the theory of signal processing, that if we wish to sample a signal without any loss or distortion, then we must sample at a rate which is at least two times the highest frequency present in the original signal (this is the Nyquist theorem and the minimum sampling frequency is called the Nyquist limit). However, since the signal representing our scene has an infinite range of spatial frequencies, no finite sampling frequency will be sufficiently high to prevent aliasing occurring. Aliasing causes frequencies higher than the Nyquist limit to appear in the reconstructed signal at lower frequencies: in effect high frequencies are masquerading as low frequencies. Hence the term aliasing.
50
Computer Graphics and Visualisation
Ray-tracing
5.8.1 Anti-aliasing In practical terms, aliasing in a ray-traced image causes smooth edges (along surface boundaries, for example) to appear jagged. This effect is illustrated in Figure 45 where in (a) we show a part of an image and the pixel boundaries, and the projection of part of an object onto the image plane, while in (b) we show the result of sampling the projection at each pixel centre.
(a) reality
(b) image Figure 45: Image aliasing In simple terms we can see that the cause of image aliasing lies in the representation of an finite area of the image (the pixel coverage) by a single point. We can also see that it may be possible to reduce the affects of aliasing if we could reduce size of this area. This is in fact true and one way of reducing the pixel area is to increase the resolution of the image, thus increasing the sampling rate. In practice, this anti-aliasing technique, is implemented by the equivalent process of sending multiple rays through each pixel, i.e. we supersample the image. There are two methods available for determining how the samples are distributed within the pixel. These perform uniform and jittered sampling.
(a) Uniform
(b) Jittered
Figure 46: Supersampling distributions
The University of Manchester
51
Lighting and Shading
In uniform supersampling, each pixel is subdivided into a regular array of subpixels and the image is sampled at the centre of each sub-pixel, while in the jittered scheme, the sample points in the uniform sampling picture are displaced by small random amounts from their original position. In Figure 46 we show an example of both types of supersampling: in (a) a pixel is uniformly supersampled using 16 sub-pixels and in (b) a pixel is supersampled using 16 jittered samples obtained by jittering the grid of points in (a). Once we have sampled our pixel and traced rays through the sampling points, we must combine the intensities returned by each sample to arrive at a representative value for the whole pixel. This processing of the samples is called filtering and there is more than one way of doing this. We could simply average the samples (box filtering), which works quite well in most cases. More realistic filters could be used, such as a gaussian filter, which weighs the values of the samples in the averaging according to their distance from the centre of the original pixel. This is in fact a good filter to use for images which will be displayed on a CRT graphics screen because it mimics closely the intensity variation across a pixel produced by the monitor electron beam.
Pixel values 0
1/9
5/9
7/9
1
1
Figure 47: Anti-aliasing using uniform supersampling Shown in Figure 47 is an example of uniform supersampling applied to the image in Figure 45. We supersample at 9 samples per pixel and use a box filter to calculate the pixel values. In the top of the diagram an enlargement of 6 pixels near the centre of the image is shown with the sub-pixel boundaries indicated by dashed lines. The numbers to the side of this are the pixel values obtained by sampling the centre of each sub-pixel and averaging the results. The effect of our simple anti-aliasing technique is shown in the lower part of Figure 47 and we can see how the edge has become less jagged: there is more of a smooth transition from pixels which see only the object to pixels which do not see the object at all. A problem with uniform supersampling, especially if the sampling density is still too low, is that aliasing artefacts can still remain, though at a reduced perceptual level. The appearance of jagged edges may still be detectable, particularly so with edges which have small or large gradients relative the image raster. The use of jittered sampling improves this situation because the aliasing frequencies are replaced by noise in the image, which the human visual system is more tolerant of.
52
Computer Graphics and Visualisation
Ray-tracing
5.8.2 Adaptive supersampling So far in this discussion of aliasing we have assumed that an anti-aliasing technique is applied to every pixel in the image regardless of what each pixel actually sees. However, it may not always be the case that a particular pixel needs to be supersampled since not all parts of the image will have an edge, or similar high contrast boundary, passing through it. This means we will be doing more work than is necessary: many of the pixels will be getting, say 9, samples when only one is needed. To remedy this, we need to adopt a method of anti-aliasing which concentrates most of its effort in the areas of the image that need attention: these methods are called adaptive supersampling. In adaptive anti-aliasing, we attempt to locate those pixels which need special attention. We can do this by first shooting rays through the corners of the pixels, rather than their centres, and for each pixel examine the difference in the intensities between these 4 samples. Should the variation across the pixel be too large, then the pixel is split into 4 sub-pixels and the process repeated to as many levels as desired. The following simple heuristic could be used to decide whether the contrast between samples in a pixel or subpixel warrants further sub-division, I max – I min C = ------------------------------I max + I min where Imax is the largest sample returned and Imin is the smallest. Values of C will need to be determined for each colour band, usually R, G and B, and then if any of these exceed pre-set values another level of supersampling is performed. Typical values for the R, G and B thresholds are 0.25, 0.20 and 0.4 respectively. An adaptive anti-aliasing scheme is illustrated in Figure 48 which shows three levels of supersampling.
(b)
(a)
(c) Figure 48: Adaptive anti-aliasing First note that in moving from one level of supersampling to the next, we can reuse many of the samples already calculated. Also, it is obvious that far fewer samples are needed in an adaptive, than a global sampling scheme. In the exam-
The University of Manchester
53
Lighting and Shading
ple shown, if we were to supersample all 6 pixels to the same level, we would have to trace 117 eye-rays compared to 62 in the adaptive method. This amounts to a substantial saving in the rendering time.
5.9 Example images In this final section, we will take a look at some synthetic images which were computed using the techniques outlined in this chapter1. The first image we will discuss is shown in Figure 49 which depicts a view of a stylized scene containing a large smooth ball sitting between two mirrors, one mirror hanging from a wall and the other on a stand. The standing mirror cannot be observed directly since it is behind the view-point but its reflections in the other mirror can be seen clearly.
Figure 49: Ray-traced scene containing highly specular surfaces The whole scene contains 9 spheres (one large ball and 4 smaller balls at the corners of each mirror), 9 cylinders (the mirror frames and the leg of the mirror stand), 2 polygons (the mirrors themselves), a plane (the wall) and 2 discs (the
1.All of the images were originally computed as full, 24-bit colour pictures, but for printing in this document, each one was converted to grey-scale (maximum of 256 levels) and the printing used a halftone screen to reproduce these grey levels. In consequence, the image quality has diminished greatly and does not compare favourably with the originals. This point should be kept in mind during this discussion. However, the original image files are available via FTP as part of the on-line distribution of this module.
54
Computer Graphics and Visualisation
Ray-tracing
floor and the base of the mirror stand). Notice how some of the surfaces have been textured: the floor has a checker board pattern, the mirror frames have a wood texture and the wall is blotchy. All surfaces have been defined as purely diffuse except for the large sphere, which also has a local specular component of 0.9, and the mirrors, which only have a global specular reflectivity of 0.9. The fact that the reflectivity of the mirrors is not unity produces a darkening of the image in the direction of the multiple reflections. There are two point light sources of unit intensity and an ambient source with an intensity of 0.3. Observe the shadows on the wall and the floor which would have been rendered very dark without any ambient illumination. The ray-tree was descended to a depth of 12 to capture the multiple reflections between the mirrors and the image was rendered at a resolution of 500x500 pixels using adaptive supersampling at 16 jittered samples per pixel. A gaussian filter was used to reconstruct the pixel values. Shown in Figure 50 is another example image which illustrates the ability of raytracing to render transparent media. The object in the foreground has been modelled as a spherical glass shell of refractive index 1.3 and a global specular transmissivity of 0.99. The surface of the glass also has a local specular reflection coefficient of 0.9 which gives rise to the specular highlights both on the outside and the inside of the shell. The refraction of light is clearly seen in this image. The scene contains only one point light source plus ambient at a level similar to the previous image. Eye-rays were traced to a depth of 5 and rendered at 750x500 pixels using adaptive supersampling at 9 jittered samples per pixel.
Figure 50: Ray-traced scene containing transparent surfaces One important feature to notice about this image is the appearance of the shadow behind the glass object. What we see is not at all realistic for the following reasons. First of all, the refraction of light through the glass has not been taken into account. The intensity of light in the shadow has been calculated by a method similar to that described in Section 4.4 which only takes into account the attenuation
The University of Manchester
55
Lighting and Shading
of the light on a straight line path through the glass (see how the inner region of the shadow is darker than the periphery where it travels a longer path through the glass). It is in fact extremely difficult to calculate realistic shadows behind transparent objects using conventional ray-tracing. These sorts of shadows, called caustics, are in reality a complicated pattern of light and shade caused by the focusing effect of the glass. We will now use the image displayed in Figure 49 to illustrate the effect of prematurely truncating the depth of the ray-tree and also show how aliasing affects the image quality. Figure 51 contains four images of the same scene ray-traced to various raydepths. The first image has a depth of 0 indicating that only eye-rays are followed: effectively only hidden surface removal has been performed. Notice how the mirror on the wall is black since no reflected rays have been spawned. The rest of the image looks unchanged since it is composed only of diffuse surfaces. In the next image the depth is 1, so we get one reflection from the mirror on the wall but nothing from the other mirror. Increasing the depth increases the number of interreflections spawned by the eye-ray, increasing the realism in the image.
Figure 51: Ray-tracing to depths 0, 1, 2 and 3
56
Computer Graphics and Visualisation
Ray-tracing
To illustrate the effect of aliasing, we will render a small part of the image of Figure 49 with and without anti-aliasing. We will also investigate the effect of different supersampling methods. In Figure 52 three images are shown which were calculated using uniform supersampling. The first image used only one eye-ray per pixel and aliasing is clearly seen in this enlargement along high contrast boundaries. The second and third images are anti-aliased using 4 and 9 samples per pixel, respectively. Notice the gradual improvement in the image quality, though at the expense of a slight blurring around the edges. Using 9 samples per pixel would seem adequate in this example image. If you look carefully, there is still a hint of aliasing apparent in this image, but remember we’re looking at an image under magnification; the aliasing will not be noticable in the full image. In all of these images a very low contrast threshold has been set to force supersampling along all edges to illustrate this point. Now compare this situation with jittered supersampling: see Figure 53. Again 1, 4 and 9 samples per pixel are used to calculate the three images. Even with 1 sample we can use jittering, but the result is not particularly pleasant! Again an improvement in the quality of the image results when we use more samples per pixel. In addition, all traces of aliasing are removed in each case when we use jittered sampling; the aliasing is replaced with noise in the image. But only when enough samples per pixel are used is the noise level reduced to an acceptable level. The result of using 9 jittered samples will be virtually indistinguishable from using 9 uniform samples in the full image.
The University of Manchester
57
Lighting and Shading
Figure 52: Uniform supersampling at 1, 4 and 9 samples per pixel
58
Computer Graphics and Visualisation
Ray-tracing
Figure 53: Jittered supersampling at 1, 4 and 9 samples per pixel
The University of Manchester
59
Lighting and Shading
60
Computer Graphics and Visualisation
6 Classical radiosity The radiosity of a surface is a measure of the rate at which energy leaves that surface. As well as describing a physical flux, the term radiosity is also used in computer graphics to describe algorithms which calculate the distribution of light in an enclosed environment based upon the principle of the conservation of energy. Radiosity methods attempt to accurately model the effects of ambient light. Before, we considered the ambient component in our illumination model to be Iakd, where Ia is constant for the scene. In practice this is not the case, so the radiosity model was developed in an attempt to express the ambient term exactly.
6.1 The radiosity equation When applying the radiosity method to the problem of rendering an image of an enclosure, the main assumption that is made is that all surfaces are ideal diffuse reflectors or emitters. This assumption, whilst limiting us to environments which are diffuse (making it impossible to render metallic or mirrored surfaces) does leave us with a view-independent rendering technique. Once the distribution of light throughout the environment has been calculated, the only step required to render different views of the scene is to apply hidden-surface removal techniques for each of the required views. The radiosity method also assumes that the scene is comprised of a number of discrete patches, each of which has a uniform radiosity. Then considering the equilibrium of energy in the scene we can write n
Ai Bi = Ai Ei + ρi
∑
B j F ji A j
(6-1)
j=1
where • Bi is the radiosity of a particular patch i, measured in energy/unit time/ unit area (e.g. Wm-2) and is the total amount of energy (emitted and reflected) leaving a unit area of the patch in unit time • Ai is the area of the patch • Ei is the light emitted from the patch (also in Wm-2) • ρi is the diffuse reflectivity of the patch (which serves the same purpose as kd) which is the fraction of the energy impinging on the surface which is reflected off it (hence ρi is in the range [0, 1]) • Fji is the form-factor of the jth patch relative to the ith, and represents the fraction of the energy leaving patch j which arrives at patch i without interaction with other patches (Fji is in the range [0, 1]) Looking at Equation 6-1 we can see that the term on the left-hand side is the total energy per unit time (i.e. the power) leaving the ith patch. This is then equated to the sum of two terms, the first representing the total emitted power (if any) and the second the total power reflected. This last term is easy to understand since
The University of Manchester
61
Lighting and Shading
BjAj is the power emitted by patch j and of this only the fraction Fji directly reaches patch i. Hence the sum of all such expressions gives the total power arriving at patch i from all other patches and then only the fraction ρi is actually reflected (the rest is absorbed). Note that all the patches in the enclosure are treated as sources of light, with either zero or positive values for Ei. Thus images with arbitrarily shaped light sources can now be rendered by simply setting Ei to some non-zero value.
6.2 Form-factor calculation In order to solve Equation 6-1 for Bi it is first necessary to calculate the form-factor between every pair of patches in the scene. As has already been mentioned, the form-factor Fji is equal to the fraction of the energy that leaves the jth patch and arrives at the ith patch without interacting with any other patches in the environment. The form-factor makes no attempt to account for light that arrives at patch i after having emanated from patch j after being reflected off (say) patch k - this is accounted for by Bk and Fki in Equation 61 above.
dAj
φj
Nj
Aj
Ni φi
R
dAi Ai
Figure 54: Form factor geometry. Figure 54 shows two patches i and j, which are split into a number of elemental areas dAi and dAj (only one shown for each patch for clarity). The elemental areas are separated by a distance R and the normals to the areas are inclined to the line between them by angles φi and φj. It can be shown that form-factor between these two elemental areas is exactly given by cos φ i cos φ j F dA dA = ----------------------------dA i j i πR 2
(6-2)
With the assumption already made about the radiosity being constant across each patch, the patch to patch form-factor is simply the area average of Equation 6-2,
62
Computer Graphics and Visualisation
Classical radiosity
1 F ji = ----Aj
∫∫ Aj
cos φ i cos φ j ---------------------------- dA i dA j πR 2 Ai
(6-3)
Note that the limitation caused by this assumption can be circumvented by splitting any patches which do not have non-zero radiosity gradients across their surface into smaller patches across which this gradient is negligible. Equation 6-3 assumes that every part of patch i can be seen from every part of patch j, which is not generally the case. To account for occlusion by other patches a function Hji is introduced which has a value 1 if a straight line from dAi to dAj does not pass through any other patch and 0 otherwise. Hence Equation 6-3 becomes 1 F ji = ----Aj
∫∫ Aj
cos φ i cos φ j ----------------------------H ji dA i dA j πR 2 Ai
(6-4)
6.2.1 Reciprocity relation There is a relationship between the form-factor Fji and its converse Fij (the formfactor of the ith patch relative to the jth) which states that (6-5)
A i F ij = A j F ji
This is called the reciprocity relation for form-factors. This relation is very useful when implementing an algorithm that evaluates the form-factors for an enclosure, since it is clear that we need only to calculate the form-factors Fij for j > i. The n form-factors Fii will all be zero for a scene made up of flat patches and the remaining n(n-1)/2 form-factors can be found from Equation 6-5. Another useful relation that can be applied to the calculation of form-factors is the following n
∑
F ij = 1
(6-6)
j=1
which is derived from the definition of the form-factor as being the fraction of the energy that leaves patch i and impinges directly on patch j. Since we are assuming that the environment is closed then Equation 6-6 is an equivalent statement of the principle of the conservation of energy. Equation 6-6 is usually employed as a check of the accuracy of the method which calculates the form-factors.
6.3 Solving the radiosity equation Having established the form-factor between all pairs of patches in the enclosure, it should now be possible to solve Equation 6-1 for the radiosity values Bi.This task is made simpler by applying the reciprocity relation in Equation 6-5 to Equation 6-1. This enables us to write n
Bi = Ei + ρi
∑
B j F ij
(6-7)
j=1
The University of Manchester
63
Lighting and Shading
To understand how to solve these equations, we will consider a simple situation with three patches: i = 1, 2 and 3. Applying Equation 6-7, we can derive expressions for the radiosities of each surface B 1 = E 1 + ρ 1 ( F 11 B 1 + F 12 B 2 + F 13 B 3 ) B 2 = E 2 + ρ 2 ( F 21 B 1 + F 22 B 2 + F 23 B 3 )
(6-8)
B 3 = E 3 + ρ 3 ( F 31 B 1 + F 32 B 2 + F 33 B 3 ) We now have three simultaneous equations in the three unknowns B1, B2 and B3. We can compute all the form-factors from the geometry of the scene, and we know E for each surface – if it is a light emitter we know its intensity; if it is not, E = 0. We can rearrange the equations into matrix form 1 – ρ 1 F 11 – ρ 1 F 12
– ρ 1 F 13
– ρ 2 F 21 1 – ρ 2 F 22 – ρ 2 F 23 – ρ 3 F 31
E1
B1
B2 = E2
– ρ 3 F 32 1 – ρ 3 F 33 B 3
(6-9)
E3
and solve this system for the Bi terms using a standard technique such as Gaussian elimination. Note that the equations of the form shown in Equation 6-9 need to be set-up and solved for each wavelength of light of interest. But typically these equations are solved for wavelengths which correspond to red, green and blue light so that the Bi can easily be rendered.
6.4 The hemi-cube algorithm The hemi-cube method was one of the first numerical techniques for evaluating form-factors in complex multi-object environments. This algorithm is an approximation to the integral in Equation 6-4 in which an assumption is made that the separation distance between patches is large compared to their size. This is frequently true, so the inner integral in Equation 6-4 is approximately constant no matter where the elemental area dAj is situated on patch j. We can thus write F ji ≈
=
∫ ∫
cos φ i cos φ j 1 ----------------------------H ----ji dA i A 2 πR j Ai
∫
Aj
dA j (6-10)
cos φ i cos φ j ----------------------------H ji dA i πR 2 Ai
which effectively is the form-factor between the elemental area dAj and the finite area Ai. To evaluate the integral in Equation 6-10 an imaginary hemi-cube is centred about patch j, that is to say a cube of edge length 2 which has two of its faces parallel to the patch. Only half of the cube which is 'in front' of the patch is relevant, hence the term hemi-cube. The situation is illustrated in Figure 55. The faces of the hemi-cube are divided into pixels (not to be confused with pixels on a graphics screen) of equal size. The number and size of these pixels are implementation dependent - higher densities giving greater accuracy in the value of the
64
Computer Graphics and Visualisation
Classical radiosity
form-factor. Typically resolutions for the top-face range from 50x50 to many hundred squared.
Nj
Patch i Patch j
Figure 55: The projection of a patch onto a hemi-cube The next step in the algorithm is to project every patch in the enclosure onto the hemi-cube and determine which pixels the projection covers. Two patches which project to the same pixel can have their depth compared and the more distant one rejected, since it cannot be seen from the receiving patch. This operation accounts for the Hji term in Equation 6-10. The hemi-cube faces are therefore effectively acting like a Z-buffer, except no intensity information is considered, only the identity of the patch involved. The form-factor associated with each hemi-cube pixel, known as the delta formfactor, is pre-calculated and referenced from a look-up table at run-time. Once the delta form-factors have been found we can calculate the form-factor of a particular patch simply by summing the delta factors of all the pixels the patch projects to, i.e. F ji =
∑ ∆Fq q
where ∆Fq is the delta form-factor of pixel q. Having found the n form-factors, the imaginary hemi-cube can now be centred about another patch in the enclosure and the process repeated until all form-factors have been found.
The University of Manchester
65
Lighting and Shading
6.4.1 Calculating the delta factors Referring back to Equation 6-2 we can see that the delta form-factor of a hemicube pixel q, is given by cos φ i cos φ q ∆F q = ------------------------------ ∆A πr 2
(6-11)
where • φi is the angle between the normal to patch i and the line joining the centre of the patch to pixel q • φq is the angle between the normal to the face containing pixel q and the line joining the centre of patch i to pixel q • r is the distance of hemi-cube pixel q from the centre of patch i • ∆A is the area of the hemi-cube pixel q Remember that in the hemi-cube algorithm we are modelling patch i as an elemental area so that the result of Equation 6-10 can be used, and approximating the hemi-cube pixel also as an elemental area. Hence we can use Equation 6-2. For a pixel on the top-face of the hemi-cube (see Figure 56) 1 cos φ i = cos φ q = --r where r =
x2 + y2 + 1
and the position of the hemi-cube pixel q is given by (x, y, z) in a local coordinate system centred on the patch. Thus from Equation 6-11 we get 1 ∆F q = ----------------------------------------2- ∆A 2 π x + y2 + 1
Z
pixel q
φq
φi
Y
1 x y
X
Figure 56: Delta form-factor for a pixel on the top face
66
Computer Graphics and Visualisation
Classical radiosity
Z
φq φi
pixel q
z
r Y
x
1
X
Figure 57: Delta form-factor for a pixel on a side face For a side-face (see Figure 57) z cos φ i = -r
and
1 cos φ q = --r
where r =
x2 + z2 + 1
hence z ∆F q = ----------------------------------------2- ∆A π x2 + z2 + 1 For a side-face perpendicular to the X-axis, substitute y for x in these expressions. Because of the hemi-cube’s symmetry, it is only ever necessary to store 1/8 of the delta factors associated with the top-face and 1/4 of those on a side-face.
6.5 Rendering At this point we have applied the hemi-cube algorithm at every patch in the enclosure and derived all of the entries in the form-factor matrix. We can now solve Equation 6-7 for each colour band of interest and then render a view of the scene by converting the value of Bi into (R, G, B) values and using a conventional hidden-surface removal algorithm. However, the result will be a picture made up of different coloured facets - an unsatisfactory situation which is improved upon by extrapolating the patch radiosities to the patch vertices and rendering an image using Gouraud interpolation across each patch.
The University of Manchester
67
Lighting and Shading
a a
b
B1
b
c B1
X
B2 d
c
d
f
e B3
B2
B4
g
h
i
e
f (b)
(a)
Figure 58: Extrapolation of the patch radiosities to patch vertices Figure 58 shows two different situations that can arise when trying to extrapolate patch radiosities to patch vertices. Figure 58a shows the most common case. If a vertex is interior to a surface, for example e, the radiosity assigned to it is the average of the radiosities of the patches that share it, B1 + B3 + B3 + B4 B e = -----------------------------------------------4 If it is on the edge, for example b, we find the nearest interior vertex (vertex e again) and note that the midpoint X between b and e can have its radiosity expressed in two ways, so Bb + Be B1 + B2 -------------------- = -------------------2 2 giving Bb = B1 + B2 – Be In order to establish the radiosity of a vertex such as a, we again find its nearest interior vertex, e, and note that the average of Ba and Be should be the value of B1, hence B a = 2B 1 – B e In the case illustrated in Figure 58b none of the results above can be applied as there are no interior points. Although it is always possible to extrapolate to the vertices in this case, it is non-trivial to recognise the very many different scenarios that can arise and there seems to no general formula that covers all cases.
68
Computer Graphics and Visualisation
Classical radiosity
6.5.1 Light and shadow leaks A problem is often encountered in radiosity images which results when two surfaces meet but their respective mesh of elements do not coincide. To explain how this occurs consider the schematic diagram in Figure 59 showing 6 elements of a surface and another surface perpendicular to it at an arbitrary angle. Note also that light is illuminating the surfaces from top-right.
0
1 1/4
0
3/4
0 0
0
1
1 1/4
0
0
Figure 59: Light and shadow leaks Each element will receive a certain level of illumination from the light source and surrounding surfaces, calculated using a hemi-cube situated at the centre of the element. The numbers shown at the centres of the elements indicate whether they have received any radiosity directly from the illuminating sources. When we come to render the scene we must extrapolate the radiosities to the element vertices so that, for example, the middle element will get radiosities proportional to the numbers shown. Hence, the element will be rendered with a gradation of light from bottom-left to top-right, even though most of it should be dark. The visual result of this is that light will appear to leak onto parts of the surface which physically it can’t reach. The converse will also occur where the shadow leaks in the opposite direction, into a region where no shadows should occur. Usually, only one type of leaking is apparent, depending on the contrast of radiosity between the element’s vertices. In the example discussed above the light leak will predominate with the effect that the object whose surface is causing the shadowing will appear to float above other surface. We will see real examples of this artefact later. We will also see how this artefact can be reduced using adaptive refinement of patches.
6.6 Progressive refinement radiosity The basic radiosity method described thus far gives very realistic results for diffuse environments, but it can take an enormous amount of time to compute the picture as well as take up substantial amounts of storage. Before an image can be rendered it is first necessary to calculate and store the values of nxn form-factors. This takes of the order n2 time to do. It is also necessary to solve for the patch radiosities in each colour band, taking up another long period of time.
The University of Manchester
69
Lighting and Shading
The progressive refinement approach attempts to overcome these problems by making a subtle change to the existing radiosity algorithm. The new technique provides storage costs of the order n as well as ensuring that an initial rendering of the scene is available after a time of the order of n. The aim of progressive refinement radiosity is to combine image realism with user interactivity. This is accomplished by quickly finding an approximation to Equation 6-1 and then render an image. This approximation is then iteratively improved upon and the image updated every time an improvement is made. This progression can be halted by the user as soon as they think the image to be of suitable quality.
6.6.1 Formulation of the method The reformulation of the radiosity algorithm is this: Consider a single term from the summation in Equation 6-1 - this states that the portion of Bi which is due to Bj is Aj B i ( due to B j ) = ρ i B j F ji -----Ai
(6-12)
So by placing a single hemi-cube at patch j it is possible to calculate all n form-factors Fji. However, if Equation 6-12 is to be usefully applied in some graceful iterative process, it would be better to calculate the increase in Bi since the last time Bj contributed to it. We can do this by writing Aj B i ( due to ∆B j ) = ρ i ∆B j F ji -----Ai
(6-13)
where ∆Bj is the difference between the current estimate for Bj and the value that was used last time patch j was used to shoot light from. The term ∆Bj is effectively the unshot radiosity from the jth patch. So now, after having only found n form-factors (by placing one hemi-cube at patch j) it is possible to determine how much of the radiosities of all patches i will increase due to the contribution from Bj. This process is described as shooting the light out from patch j to the rest of the enclosure. In particular, if patch j is chosen to be the patch with the greatest contribution still to make (∆BjAj is largest) then an illuminated image can be rendered after only one hemi-cube calculation. If the patch is chosen randomly then it will typically be the case that the scene is still dark (apart from the light sources) after several such calculations. Sorting of the shooting patches in terms of their unshot radiosity greatly increases the speed with which the solution converges. After one iteration the rendered image will not be 'correct' as the values used for ∆Bj in Equation 6-13 are all underestimates. The algorithm continues to iterate until the increase in all patch radiosities is deemed negligible. It should be noted however, that the picture is invariably deemed to be of a suitable standard within one iteration if sorting is utilised.
70
Computer Graphics and Visualisation
Classical radiosity
6.6.2 Ambient contribution The early stages of the progressive refinement radiosity procedure generally produce poor images, particularly in those areas of the enclosure that do not receive direct illumination. To compensate for this downfall (caused by the fact that the algorithm is not calculating radiosities simultaneously), an ambient term, used for display purposes only, is employed. The ambient term is based on the average reflectivity of the environment and also on the current estimate of the radiosities of all the patches. This means that it 'gracefully' lessens as the real solution converges. In order to calculate the ambient contribution, we first assert that a reasonable guess for the form-factor from any patch j to a given patch i can be found from est
F ji
Ai = ------------------------n ∑ Ak k=1
which is the fraction of the area of patch i to the total surface area of the enclosure. The average reflectivity for the enclosure is taken to be the area weighted average of the patch reflectivities, i.e. n
∑j = 1 ρj Aj
ρ av = ---------------------------n ∑ Aj
(6-14)
j=1
Given any energy being emitted into the enclosure, a fraction ρav will, on average, be reflected back into the enclosure, and ρav of that will, on average, be reflected back, and so on. So the overall inter-reflection coefficient for that enclosure is given by 1 + ρ av + ρ 2av + ρ 3av + ...
(6-15)
Since 0 ≤ ρj ≤ 1, then from Equation 6-14 the same inequality holds for ρav, so Equation 6-15 can be written, using the binomial theorem, as 1 1 + ρ av + ρ 2av + ρ 3av + ... = -----------------– 1 ρ av The ambient radiosity term, is then taken to be the product of the inter-reflection coefficient with the area-weighted average of all the radiosity which has yet to be shot via a form-factor computation. So 1 B amb = -----------------1 – ρ av
n
∑
est
∆B i F ji
i=1
All that remains now, in order to more realistically render the kth patch, is to add ρkBamb to whatever value was returned by the algorithm for Bk. This revised value plays no part in further calculations and is included purely for display purposes. Note also, that as the solution converges, ∆Bi → 0 and so Bamb → 0, thus the ambient term gracefully vanishes.
The University of Manchester
71
Lighting and Shading
6.7 Improving the radiosity solution Having shaded a scene it is often the case that shadows and other areas where the radiosity changes rapidly are poorly represented. This is because the sampling density for working out the radiosity at a given point is only as fine or coarse as the patch density: the more patches, the greater the number of points with known radiosity values and the better the solution. One way to overcome this problem is simply to increase the number of patches in the scene. However, this will also increase the number of form-factors to calculate: for example, doubling the number patches will create a four-fold increase in the number of form-factors. More importantly, the computation time will also increase.
6.7.1 Substructuring A better solution is to use a technique called substructuring which works as follows. First we need some new terminology that distinguishes a patch (as described earlier) from an element, which is some fraction of a patch that has been obtained by a binary subdivision of a patch or another element. Elements are the smallest units for which a radiosity value is calculated. Also, elements are indexed using the letter q, while patches use i and j. Now assume that the environment has been split into n patches as before and that some or all of these patches have been split into a number of elements, so that there are a total of m elements in the scene. We can now start to view the situation in terms of the element radiosities. From Equation 6-7 we know that the radiosity of element q can be written as n
Bq = Eq + ρq
∑
B j F qj
(6-16)
j=1
Suppose that patch i has been divided into t elements. Then the radiosity of patch i can be represented as an area-weighted average over the patch of the t element radiosities 1 B i = -----Ai
t
∑
Bq Aq
q=1
which on substituting Equation 6-16 gives 1 B i = -----Ai
t
∑
n
Eq + ρq
q=1
∑
B j F qj A q
(6-17)
j=1
We have already assumed that the global radiosity does not vary across a patch. If we also assume that neither ρq nor Eq can vary across a patch then Equation 6-17 can be simplified to n
Bi = Ei + ρi
∑ j=1
1 B j -----Ai
t
∑
F qj A q
q=1
Comparing this equation with Equation 6-7 we see that
72
Computer Graphics and Visualisation
(6-18)
Classical radiosity
1 F ij = -----Ai
t
∑
F qj A q
(6-19)
q=1
which is a better approximation to the assumption that the inner integral in Equation 6-4 is constant across the whole patch i (the assumption now is that only the integral is constant across each element). Because of this improvement, the resulting patch-to-patch form-factors are more accurate. Therefore, substructuring allows us to obtain accurate values for the element radiosities using the radiosity values already obtained from the coarse-patch solution of the environment by simply using Equation 6-16. We illustrate this process in Figure 60.
(a)
(b)
(c) Figure 60: Capturing radiosity gradients Shown in Figure 60a is the true variation of the radiosity across a patch plotted as a two-dimensional surface, while in Figure 60b is the coarse-patch representation which is clearly a poor approximation. However, when the patch is split into 16 elements and a radiosity computed for each one, the resulting representation is much more faithful (Figure 60c).
6.7.2 Adaptive subdivision Adaptive subdivision is a technique whereby only those patches (or elements) that have a radiosity gradient across their surface exceeding some predefined level are subject to subdivision. By making an initial guest to which parts of the picture should be subdivided a 'first attempt' solution can be obtained exactly as described in Section 6.3. The patches and elements can now be examined and checked for excessive radiosity
The University of Manchester
73
Lighting and Shading
gradients and if necessary a further subdivision into elements is undertaken. Then a second solution is computed and the process repeated until none of the radiosity gradients are deemed too steep. The resulting image is one which may well contain less elements than an acceptable image computed using simple substructuring, but whose shadow definition and shading are far superior, as the element density at critical points in the image will be much higher.
(a)
(b) Figure 61: Adaptive subdivision Figure 61 shows two cases in which a patch, which has a shadow boundary across its surface, has been subdivided in an effort to accurately account for the steep radiosity gradient caused by the shadow. In both cases the patch was split (with an initial guess) into four elements. Figure 61a shows the case where the elements have been split using adaptive subdivision and Figure 61b shows the case where the same resolution as in Figure 61a has been attained, but this time using simple substructuring. Note that the same image should result from both approaches, but the structure represented in Figure 61a will require far less storage space and computation than for that represented in Figure 61b. We can use this adaptive subdivision technique within the progressive radiosity solution method in a similar way. As in the full matrix algorithm, patches are split into elements and these elements act as receivers of light from patches, not other elements. So a hemi-cube is placed at every patch in the scene and elements are projected onto its surface in order to find the m patch-to-element form-factors Fiq. In effect, patches shoot light out to elements. Other aspects of the adaptive subdivision of patches are exactly the same as for the full matrix situation.
74
Computer Graphics and Visualisation
Classical radiosity
6.8 Example images To illustrate the power of the radiosity method, we will show in this section a few example images which were computed using the progressive refinement technique. In particular we will show how radiosity solutions are adept at capturing natural features, such as soft shadows and colour bleeding1, which ray-tracing would find hard to simulate. Our first example image is shown in Figure 62 depicting 5 views of a simple closed environment. The scene is composed of a large room with an area light source in the centre of the ceiling. The walls of the room are coloured red, blue, light-gray and black. Inside the room there are two other objects sitting on the floor: a small yellow box and a taller box with mid-gray sides and light-gray top. The walls, floor and ceiling have each been divided uniformly into 100 elements (52 patches), the yellow and gray boxes into 20 elements (5 patches) each and the light source is represented by 1 element (1 patch). The calculation required 1506 iterations to converge: the solution was deemed converged when the total unshot energy in the environment was less than 0.1% of that input by the light source. Of course, the views shown in Figure 62 only require one solution, each view being quickly calculated using this single solution. These images illustrate clearly how radiosity can render realistic shadows. An area light source, like the one used in this demonstration, always casts soft-edged shadows with umbra and penumbra. However, notice how the boxes appear to be floating above the floor. This is caused by a combination of light and shadow leaks which we discussed earlier. Also note the strong colour bleeding between surfaces, particularly the effect of the red wall on the side of the yellow box and of the blue wall on the sides of the gray box. Figure 63 shows the progressive refinement technique in action on a single view of the environment described above. The sequence of images represents the state of the refinement after 1, 2, 20 and 100 iterations with 100%, 45%, 34% and 21% unshot energy remaining, respectively. No estimated ambient contribution was used in the rendering of these views. After 1 iteration only the energy from the light source has been distributed amongst the other patches and we can see that only surfaces in the line of sight of the light source have received (and reflected) any light. The rest of the scene is black. After 2 iterations the patch with the largest unshot energy, actually a patch on top of the tall box, has shot its energy to the environment, most noticeably to the section of the ceiling immediately above it, which brightens accordingly. After each iteration the amount of unshot energy decreases, resulting in a gradual brightening of the scene. Even after 100 iterations the image looks almost the same as the fully converged solution and we would be tempted to stop the calculation here. However, it is important to continue since many more iterations are needed to show subtle detail in the shadows and in the inter-reflection of light between the various surfaces. The sequence of images shown in Figure 64 uses the results of the progressive refinement calculation shown in Figure 63, but uses an ambient contribution (estimated from the total unshot energy) when rendering each image.
1.Again it is emphasised that the quality of these black & white prints does not match the quality of the original fullcolour images. Refer to the image files distributed with this document if necessary, especially for the colour bleeding effects mentioned in the text.
The University of Manchester
75
Lighting and Shading
Figure 62: Five views of a simple scene computed using the radiosity method
76
Computer Graphics and Visualisation
Classical radiosity
Figure 63: Progressive refinement without ambient display term
Figure 64: Progressive refinement with ambient display term
The University of Manchester
77
Lighting and Shading
In the next example image we will illustrate the adaptive refinement technique for patch subdivision and show how the method is used to increase the accuracy of the radiosity solution. Figure 65 shows another simple environment, similar to the one we saw earlier, and the results of a complete radiosity solution. Also shown is the distribution of patches and elements into which each surface was divided. There are notably two regions of the scene which have been heavily subdivided: the area where the ceiling meets the walls, and around the edge of the box and its shadow. The region around the shadow will have a particularly high radiosity contrast which has triggered the refinement, resulting in the better shadow definition rendered in this image. However, adaptive refinement can never be perfect and we can see some rendering artefacts which cause the shadow boundary to break up in places. Also notice that the refinement has eliminated most of the shadow leak around the edge of the box, so that this time the box looks to be firmly planted on the floor! Finally, shown in Figure 66 is a rendering of a scene which is much more complex than any we have considered so far.
78
Computer Graphics and Visualisation
Classical radiosity
Figure 65: A radiosity simulation with adaptive patch refinement (Courtesy of Fraunhofer-IGD, 26483 Darmstadt, Germany)
The University of Manchester
79
Lighting and Shading
Figure 66: A radiosity simulation of a complex scene with adaptive patch refinement (Courtesy of Fraunhofer-IGD, 64283 Darmstadt, Germany)
80
Computer Graphics and Visualisation
A Vectors A.1 What is a vector? A vector is a quantity with magnitude and direction. It can be defined as a directed line segment in some coordinate system. Figure 67a shows a vector with endpoints (x1, y1, z1) and (x2, y2, z2). We can shift the vector so that one of its endpoints is at the origin of the coordinate system (Figure 67b). This will not affect either its magnitude or direction. Now we can describe the vector by the single endpoint (x', y', z'). The vector now represents a point in the coordinate system, and x', y' and z' are called the vector’s components. The magnitude (or length) of a vector v is written |v|, and can be computed from Pythagoras’s theorem v =
x2 + y2 + z2
If the magnitude of a vector is 1, it is said to be normalised, and is called a unit vector. We will use the notation v to indicate that a vector v is a unit vector. Sometimes a vector is expressed in terms of three unit vectors parallel to the x-, yand z-axes, called i, j and k respectively. So a vector v can also be written v = xi + yj + zk
y
y
(x2, y2, z2)
(x', y', z')
(x1, y1, z1) x z
x z
(b)
(a)
Figure 67: Vectors in a coordinate system.
A.2 Vector addition Figure 68a shows two vectors v1 = (x1, y1, z1) and v2 = (x2, y2, z2). We define the sum of two vectors by adding their x-, y- and z-components respectively v 1 + v 2 = ( x 1 + x 2, y 1 + y 2, z 1 + z 2 )
The University of Manchester
81
Lighting and Shading
The resulting vector is shown in Figure 68b.
y
y
v1+ v2 v1
v1
v2
v2 x
x z
z
(b)
(a)
Figure 68: Adding vectors v1 and v2.
A.3 Vector multiplication There are two ways to multiply vectors: one method gives a scalar result, and the other a new vector. The first method, called the dot product (or scalar product) is defined as v1 • v2 = x1 x2 + y1 y2 + z1 z2 or alternatively, using the magnitude of the vectors and the angle θ between them v 1 • v 2 = v 1 v 2 cos θ The second method of multiplying vectors is called the cross product (or vector product). This is defined as v 1 × v 2 = v 1 v 2 sin θu where u is a unit vector perpendicular to both v1 and v2.
A.4 Determining the surface normal In rendering, we will often need to know the surface normal vector for a surface, since this is used to measure the orientation of the surface. There are several techniques for determining the normal vector. If we have an analytical definition of the surface, we can derive the normal vector for any point on the surface directly. For a planar polygon whose plane equation is ax + by + cz + d = 0 Its surface normal N is given by N = ai + bj + ck where i, j and k are the unit vectors in the x-, y- and z-directions respectively.
82
Computer Graphics and Visualisation
If the plane equation is not available (as will often be the case), the normal for a vertex of a polygon can be derived from two edges that terminate at the vertex. If the two edges are E1 and E2, the normal vector N is given by the cross product of the two edge vectors N = E1 × E2 This is illustrated in Figure 69.
N
E1
E2 Figure 69: Determining the surface normal N from adjacent edges E1 and E2.
The University of Manchester
83
Lighting and Shading
84
Computer Graphics and Visualisation
B Resources B.1 Books Watt A., Fundamentals of Three-Dimensional Computer Graphics, Addison Wesley 1989. Foley J.D., van Dam A., Feiner S.K. and Hughes J.F., Computer Graphics: Principles and Practice, 2nd edition, Addison Wesley 1990. Glassner A. (Ed.), An Introduction to Ray Tracing, Academic Press 1989. Ashdown I., Radiosity: A Programmer’s Perspective, John Wiley 1994. Cohen M.F. and Wallace J.R., Radiosity and Realistic Image Synthesis, Academic Press Professional 1993. Sillion F.X. and Puech C., Radiosity and Global Illumination, Morgan Kaufmann 1994. Glassner A., Principles of Digital Image Synthesis, (2 vols.), Morgan Kaufmann 1995. Hall R., Illumination and Color in Computer Generated Imagery, Springer-Verlag 1989.
B.2 Research literature If you are interested in pursuing topics in state-of-the-art computer graphics there are several very good sources of research literature. Of relevance here are: • IEEE Computer Graphics & Applications • Computer Graphics Forum • ACM Transactions on Graphics • Eurographics Conference Proceedings (annual), published as special edition of Computer Graphics Forum • Eurographics Workshop on Rendering (annual) • ACM SIGGRAPH Conference Proceedings (annual), published as a special edition of ACM Computer Graphics
B.3 Software The example images included in the chapter on ray-tracing were generated using a package called Rayshade, created by Craig Kolb of Princeton University. This ray-tracing program is freely available on the internet and you can download the source code from: • WWW at http://www-graphics.stanford.edu/~cek/rayshade/
The University of Manchester
85
Lighting and Shading
• anonymous FTP at graphics.stanford.edu (36.22.0.146) in directory /pub/rayshade/ The software runs on a variety of UNIX platforms; there is also versions for MSDOS, Macintosh, Amiga and OS2. There are several other ray-tracing programs available for downloading. A good starting point for looking at these is the ray-tracing WWW home page at • http://arachnid.cs.cf.ac.uk/Ray.Tracing/ which also contains pointers to other ray-tracing resources on the internet. The example images included in the chapter on radiosity were generated using a package called Helios, created by Ian Ashdown of Ledalite Architectural Products Inc. Although this program is not freely available (the source code is distributed on a diskette which you can buy in conjunction with his book), an almost fullyfunctioning demonstration version is. You can find the program on the WWW at • http://www.ledalite.com/library/rrt.html The demo is a ready compiled executable for running under MS-Windows. This site also contains other sources of information relevant to lighting and illumination. You can find this information at • http://www.ledalite.com/library/ledapub.html For a more advanced radiosity system, you may wish to take a look at a program called RAD written by Philippe Bekaert of the University of Leuven, the source code of which is available from • http://www.cs.kuleuven.ac.be/~philippe/rad.html and, at the moment, only appears to run on Silicon Graphics machines.
86
Computer Graphics and Visualisation
These materials have been produced as part of the Information Technology Training Initiative, funded by the Information Systems Committee of the Higher Education Funding Councils. For further information on this and other modules please contact: The ITTI Gravigs Project, Computer Graphics Unit, Manchester Computing. Tel: 0161 275 6095 Email:
[email protected] To order additional copies of this or other modules please contact: Mrs Jean Burgan, UCoSDA. Tel:0114 272 5248 Email:
[email protected] ISBN 1 85889 064 0