Wednesday, 5 March 2014

Shading in Graphics

A shading model is used in computer graphics to simulate the effects of light shining on a surface. The intensity that we see on a surface is dependent upon
  • The type of light sources.
  • The surface characteristics (eg. Shining, matte, dull, and opaque or transparent).
Types of illumination
A).Light emitting sources (eg. Light bulb, sun)
1).When the light source is far away from the object => point source (eg.Sun)
2).When the light source is large compared with the object => distributed light source (eg. Neon light)

B).Light reflecting sources (eg. Illuminated surface of an object, walls)
1).Three possible types of reflections ((1), (2), and (3)) to form the shading of a point on a surface.
2).Reflection of ambient light: Multiple reflections of light from nearby objects => ambient light (or background light). Ambient light has a uniform intensity in all directions. It causes a surface to be uniformly illuminated at any viewing position.
3).Reflection of point source light: A point source light emits directional light rays. Normally we consider light rays from a distant light source as being parallel (eg. Sun) and light rays from a close light source as being divergent (eg. Table lamp).
Two types of reflection of point source light:
Due to surface roughness => diffuse reflection
Due to shinny surface => specular reflection
When light is reflected (or refracted) from a surface, it does not reflect all light that enters the surface. Part of the energy will be absorbed by the surface. The reflection coefficient represents the amount of light reflected from an object's surface.
The unit normal vector N represents the orientation of a surface.
The unit vector L represents the direction of a light source at the surface.
The angle of incident q represents the angle between N and L.
In diffuse reflection, we consider surfaces which reflect light with equal intensity in all directions. The amount of light energy that falls on a given area and hence the intensity of the reflection is proportional to the cosine of the angle of incident (ie. cos q). This kind of surfaces are normally dull or matt.
In specular reflection, we consider surfaces which reflect light with higher intensity at a particular direction. This kind of surfaces are normally shiny.

C).Light refraction
Refraction occurs when light travels through transparent or semitransparent objects of different densities (for example, from air to glass).

Types of Shading
  • Constant Intensity Shading (Flag Shading)
  • Gouraud Shading
  • Phong Shading

Gouraud Shading !

Gouraud shading, computes an intensity for each vertex and then interpolates the computed intensities across the polygons. Gouraud shading performs a bi-linear interpolation of the intensities down and then across scan lines. It thus eliminates the sharp changes at polygon boundaries.
The algorithm is as follows:
1).Compute a normal N for each vertex of the polygon.
2).From N compute an intensity I for each vertex of the polygon.
3).From bi-linear interpolation compute an intensity Ii for each pixel.
4).Paint pixel to shade corresponding to Ii.
How do we compute N for a vertex? Let N = the average of the normals of the polygons which include the vertex. Note that 4 sided polygons have 4 neighbors and triangles have 6 neighbors.
We can find the neighbors for a particular vertex by searching the polygons and including any polygons which include the vertex. Now look at performing the bi-linear intensity interpolation. This is the same as the bi-linear interpolation for the z depth of a pixel (used with the z buffer visible surface algorithm).
Gouraud shading gives a much better image than faceted shading (the facets no longer visible). It is not too computationally expensive: one floating point addition (for each color) for each pixel. (the mapping to actual display registers requires a floating point multiplication)
It eliminates creases that you may want to preserve, e.g. in a cube. We can modify data structure to prevent this by storing each physical vertex 3 times, i.e. a different logical vertex for each polygon.

Phong Shading !

Phong Shading overcomes some of the disadvantages of Gouraud Shading and specular reflection can be successfully incorporated in the scheme. The first stage in the process is the same as for the Gouraud Shading - for any polygon we evaluate the vertex normals. For each scan line in the polygon we evaluate by linear intrepolation the normal vectors at the end of each line. These two vectors Na and Nb are then used to interpolate Ns. we thus derive a normal vector for each point or pixel on the polygon that is an approximation to the real normal on the curved surface approximated by the polygon. Ns , the interpolated normal vector, is then used in the intensity calculation. The vector interpolation tends to restore the curvature of the original surface that has been approximated by a polygon mesh.
The algorithm is as follows:
1).Compute a normal N for each vertex of the polygon.
2).From bi-linear interpolation compute a normal, Ni for each pixel.
3).From Ni compute an intensity Ii for each pixel of the polygon.
4).Paint pixel to shade corresponding to Ii.
We have:
These are vector equations that would each be implemented as a set of three equations, one for each of the components of the vectors in world space. This makes the Phong Shading interpolation phase three times as expensive as Gouraud Shading. In addition there is an application of the Phong model intensity equation at every pixel.