BRender Tutorial Guide:5 Adding Colour

5 Adding Colour

The Program
8-Bit Indexed Colour Mode
A `material' may be explicitly assigned to a model actor or to each face on a model. A material describes the appearance of a surface - its colour and texture, whether it's shiny or dull, smooth or rough, etc.

For each face on a model, BRender looks for an associated material. If none has been specified (or the associated material is not found in the registry), the model actor's material is assumed. If a material has not been assigned to the model actor, it inherits its parent's material. If the parent actor, or a previous ancestor, has not been assigned a material, a flat-shaded grey material is used by default.

The default material has been used with all the models you have displayed so far.

Let's design a material and apply it to the revolving grey cube of BRTUTOR1.C.

The information describing a material is stored in a br_material data structure. Refer to your technical reference manual for details of br_material.

Care should be taken when initialising data structures statically, as only public members of BRender data structures are documented in the technical reference manual.

A custom function, BrFmtScriptMaterialLoad, is provided for loading material descriptions from a script file. A material script file is a text file as in the following example:

sample material script file

# Comment
# Fields may be specified in any order, or omitted
# Where fields are omitted, sensible defaults will be supplied
# Extra white spaces are ignored

material =	[
		identifier = "block";
		flags = [light,prelit,smooth,environment,
		colour = [0,0,255];
		ambient = 0.05;
		diffuse = 0.55;
		specular = 0.4;
		power = 20;
		map_transform = [[1,0], [0,1], [0,0]];
		index_base = 0;
		index_range = 0;
		colour_map = "brick"
		index_shade = ""

The fields in the script file relate directly to br_material fields. Refer to your technical reference manual for futher details. Note that all material flags would never be set at the same time, as they are in the above example. They are shown here to illustrate how material flags are specified in script files.

The script file used to determine the appearance of the revolving cube in BRTUTOR5.C is called cube.mat and is included on your Tutorial Programs disk.


# This material script file describes the appearance
# of the material "BLUE MATERIAL"

material =	[
		identifer = "BLUE MATERIAL"; 
		colour = [0,0,255]; 
		ambient = 0.05;
		diffuse = 0.55;
		specular = 0.4;
		power = 20;
		flags = [light,smooth];

A script file may contain a number of material descriptions. The identifier field allows you to specify a name by which each loaded material is subsequently known. When you want to assign a particular material to a model, you simply instruct BRender to find it by name before completing the assignation.

The material colour is pure blue (both red and green components are 0).

There are a number of material properties, besides colour, that determine how a surface will appear under given lighting conditions - whether it will appear rough or smooth, shiny or dull etc.

The ambient, diffuse and specular fields are used to specify, respectively, the ka, kd and ks members of the br_material data structure. Each ranges between 0 and 1, and the three should sum to 1. An additional field, power, determines how sharp highlights will appear. A more detailed discussion of material properties can be found in your technical reference manual (see br_material).

The most commonly specified flags are light and smooth. Light specifies that lighting effects should be taken into account when rendering. Smooth specifies Gouraud shading. The polygons that make up the surface of a model can be drawn with a single colour (flat shading) or with many colours (smooth or Gouraud shading). With flat shading, the colour of a single vertex is calculated and duplicated across the entire polygon. With smooth shading, the colour at each vertex is computed and colour values for the interior of the polygon interpolated linearly between the vertex colours. In our present example, if we hadn't specified smooth shading, each face on the cube would have been drawn using a single colour. With smooth shading a more realistic effect is achieved through interpolation.

For a demonstration of flat shading, simply remove `smooth' from the flag field in the text file cube.dat and run the program again. This is the major advantage of using script files to define material properties; any of these properties can be changed, and the result viewed, without having to re-compile the program. Simply edit the script file as necessary. This makes it easy to experiment with different colour values, specular/diffuse/ambient properties and shading techniques.

Generated with CERN WebMaker