Illumination and Shading (Introduction to Computer Graphics Using Java 2D and 3D) Part 3

Shading in Java 3D

In Java 3D, an Appearance is assigned to each object to characterise the properties of the object’s surface. The class Appearance was already used in Sect. 6.4 to display objects as wire frame models. A very important attribute of Appearance is the Material. The properties of the material and therefore the surface are specified in the following constructor:

tmpc009-451_thumb[2][2]

The first four parameters are colours that are defined by instances of the class Color3f which was introduced in Sect. 5.4. The colour ambientColour defines how much ambient light is reflected by the material. If ambient light is added to the scene by an instance of the class AmbientLight as was described in Sect. 8.2, then the contribution of ambient light reflection for a surface of the corresponding material is computed in the following way. Given the RGB-values (ra,ga,ba) of ambientColour for the material and the RGB-values (ri,gi,b{) for ambient light in the scene, the intensities for the reflection of ambient light are determined by (ra · rl,ga · gl,ba · bl). To these intensities the values for self-emitting light specified in emissiveColour and the calculated values for diffuse and specular reflection have to be added. How much light is reflected for diffuse and specular reflection is defined by diffuseColour and specularColour. These colours correspond to the intensities kd and ksr, respectively, in (8.6). They are multiplied with the corresponding values coming from light sources of the classes DirectionalLight, PointLight and SpotLight. The last parameter shininessValue is a float-value between 1 and 128 for the shininess of the material or surface. The larger the value, the more shiny the surface will be. The value shininessValue is the specular reflection exponent in the Phong illumination model.


Once a Material ma has been generated in this way, an Appearance having the reflection properties ma can be created with the method setMaterial.

Appearance app = new Appearance(); app.setMaterial(ma);

This Appearance app can be used for objects to let the object’s surface appear with the corresponding colour and reflection properties of the Material ma.

Although it is possible to use four different colours in the constructor for materials, it is not recommended to do so. Most objects do not emit light themselves so that emissiveColour will be black in most cases. The colours for diffuse reflection and for the reflection of ambient light should be identical or ambientColour could be chosen slightly less intense than diffuseColour. The colour for specular reflection is very often white, a light grey or the same colour as for diffuse reflection, but with higher intensity.

The program LightingExample.java displays two objects, a shiny sphere and a dull cube, that are illuminated by four light sources: ambient light, a directional light source, a point light source and a spotlight. For a better understanding of illumination and shading effects, it is recommended to the reader to modify the parameters of the instances of the class Material and to observe the changes. It is also possible to switch off light sources, simply by declaring the line where the corresponding light source is added to the BranchGroup bgLight with the method addChild as a comment line.

Theprogram LightingExample2.java demonstrates undesired effects that can occur when the four colours in the constructor of Material are chosen completely different. The program uses a moving light source. The program MovingSpotLight.java illustrates how a spotlight with abruptly dropping intensity at the boundary of its cone of light can also lead to unrealistic effects. If the last parameter in the constructor SpotLight is changed from 0 to 1 in the program, the undesired effect vanishes.

Shading

For the computation of light reflections on a surface in Sect. 8.3, it was assumed that the normal vector to the surface is known in each point. In order to compute the correct colour of a pixel on the projection plane, it is not only necessary to determine which object surface is visible at the pixel, but also in which point the projector through the pixel meets the surface. For cubic freeform surfaces this would mean that a system of equations had to be solved whose variables occur in the power of three, leading to unacceptable computational effort per pixel. Therefore, light reflections are not calculated directly for the freeform surfaces, but for their approximations with polygons. What does this mean for the normal vectors to the surface?

A sphere in different tesselations rendered with flat shading

Fig. 8.10 A sphere in different tesselations rendered with flat shading

A very simple approach would ignore the original normal vectors to the freeform surfaces and use the normal vectors to the plane polygons instead.

Constant or flat shading simplifies this idea even further. For a polygon, the colour is determined only for a single pixel based on one normal vector. All other pixels resulting from the projection of the polygon obtain the same colour, leading to a homogeneous colour for the projection of the polygon. This approach is correct under the following assumptions:

•    The light source is in infinite distance so that nT · l is constant. This applies only to directional light sources.

•    The viewer is in infinite distance so that nT · v is constant. This is true for parallel projections.

•    The polygon represents the real surface of the object and is not just an approximation of a curved surface.

•    No specular reflection occurs.

With these assumptions, shading can be computed in a fast and simple way, but will not lead to realistic images. Figure 8.10 shows the same sphere as in Fig. 6.12 on page 137, also in different tesselations. However, flat shading was applied in Fig. 8.10. Even the rightmost refined approximation by triangles still shows clear faces on the surface of the sphere whereas they are almost invisible already for a medium resolution in the figure on page 137.

An extremely refined resolution is needed for flat shading to avoid the effects of visible faces. One reason for this problem is also the preprocessing carried out automatically in the human vision system which intensifies contrast, i.e., edges, so that even small edges are already noticed.

Therefore, instead of flat shading interpolated shading is preferred. Interpolated shading requires the definition of normal vectors in the vertices of a polygon or triangle. Here only the case of triangles will be considered. The normal vectors in the three vertices of a triangle can be different for interpolated shading when the triangle is supposed to approximate a part of a curved surface. If the triangles are not derived from a freeform surface, but where specified manually, different normal vectors in the vertices of the triangle can still be computed. In each vertex, the standard normal vectors to the triangles that share the vertex are interpolated. This technique has been described in Sect. 6.9.1.

When a curved surface is approximated by triangles and suitable normal vectors are specified in the vertices of the triangles, Gouraud shading [5] computes the colour in each of the three vertices based on the corresponding normal vectors. The shading of the other points in the triangle is based on colour interpolation derived from the three vertices. This leads to a linear colour gradient over the triangle. Figure 8.11 illustrates the colour intensity as a function over the triangle.

The colour intensity as a function over a triangle for Gouraud shading

Fig. 8.11 The colour intensity as a function over a triangle for Gouraud shading

An efficient scheme for the computation of the intensities in the triangle uses a scan line technique. For a scan line ys the intensities Ia and Ib on the edges of the triangles are calculated where the scan line intersects the triangle. These values are obtained by weighted interpolation between the vertices of the corresponding edges of the triangle. The intensity changes in a linear way along the scan line with initial value Ia and final value Ib. Figure 8.12 illustrates this principle. The intensities are computed based on the following equations:

tmpc009-454_thumb[2][2]

The colour intensities to be calculated are integer values between 0 and 255. Usually, the intensities on a single triangle will not differ strongly so that the absolute slope of the linear intensity curve along the scan line will be small. In this case, the midpoint algorithm could be applied to determine the discrete intensity values.

The undesired effect of visible faces and edges is already amended by Gouraud shading. Nevertheless, because of the linear interpolation scheme for Gouraud shading, the minimum and maximum intensity on a triangle will always be in one of the vertices. This might still lead to the effect of visible protruding edges or vertices. Phong shading [7] is also based on interpolation as Gouraud shading. However, instead of interpolating the computed colour intensities in the vertices for shading the triangle, the normal vectors in the vertices are interpolated for the calculation of the colour intensities of the other points. In this way, it is also possible that the minimum and maximum colour intensity can occur inside the triangle depending on the configuration of the normal vectors in the vertices and on the direction from which the light comes.

Scan line technique for the computation of Gouraud shading

Fig. 8.12 Scan line technique for the computation of Gouraud shading

Interpolated normal vectors for Phong shading

Fig. 8.13 Interpolated normal vectors for Phong shading

Figure 8.13 shows a curved surface and a triangle which approximates a part of the surface. The normal vectors in the vertices of the triangle are the normal vectors to the surface in these points. Inside the triangle, the normal vectors are convex combinations of these normal vectors.

From the computational point of view, Phong shading requires much more time than Gouraud shading. For the latter technique, the complex computations for illumination involving light sources and reflections need to be carried out only for the three vertices of a triangle. The remaining triangle is shaded by a simple scan line technique with simple calculations. For Phong shading, after interpolating the normal vectors, the full computations for illumination have to be carried out still for each pixel.

Gouraud and Phong shading provide good approximations for shading of the curved surface. From the theoretical point of view, it would even be better to derive the normal vector for a point in the triangle directly from the corresponding point on the curved surface in order to determine the colour of the corresponding pixel. This would mean that it is not sufficient to store only predefined normal vectors of selected points—the vertices—but that the information about the original curved surface is still necessary for shading. This would be unacceptable from the computational point of view.

Constant and Gouraud Shading in Java 3D

Gouraud shading is the default shading technique in Java 3D. One can also switch to constant shading for an object by changing its Appearance. First an instance of the class ColoringAttributes has to be generated and then the shading mode can be set with the method setShadeModel.

tmpc009-457_thumb[2][2]

app is an instance of the class Appearance. The method ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD); switches back to the default shading in Java 3D. Figure 8.10 on page 192 was generated by the program ShadingExample.java using constant shading.

Next post:

Previous post: