**Projections,** required for displaying a three-dimensional scene on a two-dimensional plane or screen, were discussed in Sect. 5.8. A projection is a special type of mapping from the three-dimensional space to a plane. In this sense, a projection describes only where a point or an object has to be drawn on the projection plane. The determination of visible surfaces in Chap. 7 also focussed only on the question which objects should be drawn or projected and which ones are hidden from view by others. The information where an object should be drawn on the projection plane, i.e., which pixels are covered by the object, is not at all sufficient for a realistic representation of a three-dimensional scene. Figure 8.1 shows the projections of a grey sphere and a grey cube, both in two variants. The first variant simply assigns the colour of the sphere and the cube directly to the pixels that are occupied by the corresponding object. This leads to geometric shapes with a homogeneous colour losing almost the complete information about the three-dimensional structure. The projection of the sphere is a grey circle, the cube becomes a grey hexagon.

**Taking illumination and light reflections** into account leads to different light effects on the surfaces of the three-dimensional objects and to a non-homogeneous shading of their projections. In this way, even the flat images appear vivid and provide a three-dimensional impression as can be seen in Fig. 8.1 where the effects of illumination were taken into account for the second sphere and the second cube. Shading refers to rendering an object’s surface with illumination and light reflection effects. This topic introduces the necessary background and techniques for illumination and shading in computer graphics.

**From a theoretical point of view**, the computations for shading described in the following sections would have to be carried out for each wavelength of the light individually. Since this is impossible, the computations will always be restricted to the three primary colours red, green and blue in order to determine the RGB-values for the representation.

**Fig. 8.1 Objects with and without illumination and shading effects**

## Light Sources

In addition to information about the objects and the viewer, the description of a three-dimensional scene must also include information about illumination of the scene. A single light source or a number of light sources can contribute to the illumination of a scene. In most cases, light sources will provide white or “grey” light, i.e., white light which does not have the full intensity. But also coloured light coming from a traffic light or the more red or orange light from the sun at dawn can occur in a scene. The colour and intensity of a light source are defined by suitable RGB-values.

**The simplest form of light is ambient light**. Ambient light does not come from a specific light source and has no direction. It represents the light that is more or less everywhere in the scene, originating from multiple reflections of light at various surfaces. In a room with a lamp on a table, it will not be completely dark under the table although the lamp cannot shed its light directly under the table. The light is reflected by the surface of the table, the walls, the ceiling and the floor. Of course, the light under the table will have a lower intensity, but it will still be there with approximately the same intensity everywhere, not coming from a specific direction. Ambient light is a simplification of the computations for illumination. From the theoretical point of view, there is nothing like ambient light. The correct way to take ambient light into account would be to trace the multiple reflections of the light completely. This would increase the computational effort enormously, so that this approach is not (yet) well suited for real-time computer graphics. An approach to compute the ambient light correctly is introduced in Sect. 8.10.

For ambient light it is sufficient to specify its colour. A directional light source has in addition to a colour also a direction. The light rays from a directional light source are parallel. Directional light is used to model light coming from a source in almost infinite distance, for instance sunlight.

**A lamp is modelled as a point light source**. A point light source has a position and the light rays spread in all directions from this position. The intensity of the light decreases with increasing distance. This effect is called attenuation. The following argument shows that the intensity of the light decreases quadratically with the distance to the light source. If a point light source is in the centre of a sphere with radius r, then the full energy of the light will be distributed equally on the inner part of the surface of the sphere. If the sphere is replaced by a bigger sphere with radius R, then the full energy of the light will not change. But it is now distributed to a larger surface. The ratio of the surfaces of the two spheres is

For a ratio of r/R = 1/2 each point on the inner part of the surface of the larger sphere receives therefore only one quarter of the energy of a point on the inner part of the surface of the smaller sphere.

**The theoretical model for attenuation would then** be to multiply the intensity of the light from a point light source by the factor 1 /d2 when it hits the surface of an object at distance d to the light source. The intensity of the light source will decrease very quickly with the distance so that the intensity differences for larger distances will be almost unnoticeable. However, for objects very close to the light source, drastic differences will occur. The intensity could be arbitrarily large and would tend to infinity when a surface is directly in front of the light source. In order to avoid these effects, the decrease of the intensity caused by attenuation is modelled by a general quadratic polynomial in the denominator in the form

where the constants C1, c2, c3 can be chosen individually for each point light source. d is the distance of an object to the light source. This formula guarantees that the intensity can never exceed the value 1. The constants can also be adjusted so that a more moderate attenuation effect occurs than with the simple form 1/d2. The coefficient c2 for the linear term can also be used to model atmospheric attenuation. The quadratic decrease of the light intensity comes from the distribution of the energy of the light onto a larger surface for an increasing distance. In addition, part of the light is absorbed by dust particles in the air causing atmospheric opacity. This leads obviously to a linear decrease of the intensity with increasing distance. The number of dust particles a light ray can hit grows proportionally with distance it covers.

**Another common light source are spotlights**. In contrast to a point light source, a spotlight has a direction in which it spreads its light in the form of a cone. A spotlight is characterised by the colour of its light, its location, the direction in which it shines and an angular limit that describes the extension of the cone of light. Attenuation is computed for a spotlight on the basis of (8.1) in the same way as for point light sources. The quadratic decrease of the intensity with increasing distance can also be deduced from Fig. 8.2 where it can be seen that the full energy of the light from the spotlight is distributed over a circle whose radius growth is linear with the distance. Therefore, the surface grows quadratically with the distance.

**For a more realistic model of a spotlight,** it should be taken into account that the intensity of the light is smaller close to the boundary of the cone of light than at the centre. In the Warn model [9], a parameter p is used to control how fast the intensity of the light decreases from the centre of the cone to its boundary. Consider a point on a surface that is illuminated by a spotlight.

**Fig. 8.2 Cone of light from a spotlight**

**Fig. 8.3 The Warn model for a spotlight**

**Let l be a vector that points from** the point where the spotlight is located to a point on the surface of the illuminated object, and let ls be the axis of the cone pointing in the direction of the light. Then the intensity of light at the point on the surface coming from the spotlight is computed in the Warn model by

**Is is the intensity of the spotlight,** fatt is the distance-dependent factor for attenuation as in (8.1) and γ is the angle between l and ls. The value p controls how much the spotlight is focussed. For p = 0 the spotlight behaves in the same way as a point light source. The larger p is chosen, the more the light concentrated around the axis of the cone and the smaller is the intensity at the boundary of the cone. The cosine in (8.2) can be computed as the dot product of the vectors l and ls if they are normalised, i.e., if both of them have the length one. Figure 8.3 illustrates the situation in the Warn model.

Figure 8.4 shows the effect of the parameter p. The function (cos γ)ρ is drawn for the values p = 1, 2, 8, 64. For p = 1, the rightmost curve, the intensity drops slowly to zero with increasing angle γ. For higher values of p, even small deviations from the axis of the cone, i.e., small angles γ, lead already to very small intensities, since (cos γ)p is almost zero.

**It should be noted that the intensity Is in (8.2)** of the Warn model can have different values for each colour, or at least for each of the primary colours red, green and blue.

**Fig. 8.4 The functions (cosγ)64, (cosγ)8, (cosγ)2, cos γ**

## Light Sources in Java 3D

Java 3D provides classes for all light sources which have been introduced in the previous section. As mentioned in Sect. 5.5, most of the programs here will have a separate branch in the scenegraph for the lights. In the same way as for objects in the scene, an instance bgLight of the class BranchGroup has to be created first. All lights will be entered into this branch group with the method addChild. The branch group bgLight itself will be included in the scene by adding it to the SimpleUniverse with the method addBranchGraph.

For each light source, a colour must be defined by three float-values between zero and one for the RGB-intensities.

**Color3f lightColour = new Color3f(r,g,b);**

Each light source can have a different colour. In the same way as for interpolators in Sect. 5.7, a bounding region must be assigned to each light source. For this purpose, one can generate an instance bs of the class BoundingSphere as was described on page 120.

**Ambient light is then created with the class AmbientLight in the following way:**

The colour of ambient light will usually be a grey one, i.e., the intensities for red, green and blue will be chosen equally, but definitely lower than one. The methods setInfluencingBounds and addChild have to be applied in the same way for all other light sources to specify the bounding region of the light source and to add it to BranchGroup bgLight.

**A directional light source is generated with the class DirectionalLight. In addition to the colour, the direction of the parallel light rays must be specified in the form of a vector (x,y,z)T consisting of three float-values. An instance of the class Vector3f is used for this purpose:**

**For a point light source,** a colour must be defined and two instances location and attenuation of the class Point3f that are determined by three float-values in the same way as Vector3f.

**The instance location determines the** position of the point light source. Thethree components of attenuation specify the coefficients in (8.1) for attenuation.

The class SpotLight models spotlights.

**The same parameters as for point light sources are required,** i.e., colour, position and attenuation. In addition, the direction in which the spotlight shines is defined by the Vector3f direction. The float-value angle defines the angular limit corresponding to half of the opening angle of the cone of light. The float-value concentration between 0 and 120 determines how much the spotlight is focussed to the centre axis. For the value zero, the light will have the same intensity at the axis of the cone and at the boundary. The intensity drops abruptly to zero at the boundary of the cone. The value 120 defines a spotlight whose light is strongly focussed on the axis of the cone and very weak at the boundary.

**The BranchGroup for the light can contain an arbitrary** number of light sources of the same or of different type. If the light sources are directly assigned to the BranchGroup, they remain static where they were positioned. Moving light sources can be implemented in the BranchGroup bgLight in the same way as moving objects in the BranchGroup theScene as was described in Sect. 5.7. Instead of assigning a light source directly to the BranchGroup bgLight, a transformation group with an interpolator can be defined to which the light source is added. Then this transformation group is assigned to the BranchGroup bgLight. It is also possible to include light sources in the BranchGroup theScene or in a transformation group within theScene when a light source is directly connected with an object. In this way, the object and the light source will carry out the same movement.

As an example, the class MovingLight.java implements a moving directional light source. Other example programs where different light sources are used will be described in Sect. 8.4.

**Light sources themselves are invisible**. They only send out light which is reflected by objects in the scene. For instance, if a scene contains a point light source, the viewer cannot see it, even if he look in the direction of the light source. When there are no objects in the scene, the scene will remain completely dark. If a light source should be visible, it is necessary to include a corresponding object in the scene, for instance a light bulb or a torch. It is also necessary to assign the corresponding bright colour of the light to the object or the part of the object that is intended to emit the light. How surfaces or objects can be defined which emit light themselves will be explained in Sect. 8.4.