BRender Tutorial Guide:6 Texture Mapping:Assigning the Material

Assigning the Material

The following code assigns the material earth_map to the planet actor:

	planet->material = BrMaterialFind("earth_map");
'earth_map' is the name of the material described in earth.mat.

Let's take a look at the material script file earth.mat.


# This material script file describes the appearance
# of the material "earth_map"

material =	[
		identifier = "earth_map";
		ambient = 0.05;
		diffuse = 0.55;
		specular = 0.4;
		power = 20;
		flags = [];
		map_transform = [[1,0], [0,1], [0,0]];
		colour_map = "earth";

'earth_map' is the name by which this material is known and registered.


 * Copyright (c) 1996 Argonaut Technologies Limited. All rights reserved.
 * Program to Display a Texture Mapped Sphere (15-bit colour).

#include <stddef.h>
#include <stdio.h>
#include "brender.h"
#include "dosio.h"

int main(int argc, char **argv)
	br_pixelmap *screen_buffer, *back_buffer, *depth_buffer, *palette;
	br_actor *world, *observer, *planet;
	br_material *planet_material;
	int i;		

/*********** Initialise BRender and Graphics Hardware ***********/


	 * Initialise screen buffer and set up CLUT (ignored in true colour) 
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
	BrZbBegin(screen_buffer->type, BR_PMT_DEPTH_16);

	back_buffer = BrPixelmapMatch(screen_buffer,BR_PMMATCH_OFFSCREEN);
	depth_buffer = BrPixelmapMatch(screen_buffer,BR_PMMATCH_DEPTH_16);

/*************** Build the World Database **********************/

	world = BrActorAllocate(BR_ACTOR_NONE,NULL);
	observer = BrActorAdd(world,BrActorAllocate(BR_ACTOR_CAMERA,NULL));
	observer->t.type = BR_TRANSFORM_MATRIX34;  

	 * Load and Position Planet Actor

	planet = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
	planet->model = BrModelLoad("sph32.dat");
	planet->t.type = BR_TRANSFORM_MATRIX34;
	 * Load and Register "earth" Texture


	 * Load and Apply Earth Material

	planet_material = BrFmtScriptMaterialLoad("earth.mat");
	planet->material = BrMaterialFind("earth_map");

/********************** Animation Loop ***********************/

	for(i=0; i < 200; i++) {


	 * Close down 
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
	return 0;


Note that, depending on your platform and the version of BRender installed, lighting calculations may not be performed for texture mapped models in true-colour modes. In addition smooth shading may not be supported. Light and smooth shading may need to be turned off. Refer to your installation guide for details. Note flags = [] disables all rendering flags.

map_transform is a matrix representing a general texture map transformation. The transform is applied to texture coordinates. This enables textures to be translated, scaled and rotated. [[1,0], [0,1], [0,0]] represents the identity matrix (remember texture maps are two-dimensional).

[[1,0], [0,1], [0.5,0]] encodes a translation 0.5 units in x.

Scaling transformations can be used to produce a tiling effect. For example
[[3,0], [0,3], [0,0]] generates a new texture map made up of 9 (3*3) copies of the original map (each reduced in size by a factor of 9).

[[0.707,-0.707], [0.707,0.707], [0,0]] is a rotation of approximately +45xb0 .

See Computer Graphics - Principles and Practice, by James D. Foley et al., Chapter 5, for a comprehensive overview of 2D transformations.

colour_map = "earth" specifies the name of the texture map to be retrieved from the registry. If you don't know, or can't remember, what a texture map is called, use the TEXCONV utility to retrieve its name from the relevant .pix file. TEXCONV is BRender's texture import and conversion utility, command line options are detailed in Chapter 8.

The texture map used in our example, called 'earth', was loaded from earth15.pix. To verify this name, enter the following command line:

	texconv earth15.pix
The following will be displayed,

TEXCONV 1.5 Copyright (C) 1994 by Argonaut Technologies Limited
Loaded `earth' as BR_PMT_RGB_555 (256,256)
confirming that the texture map is called 'earth'. BR_PMT_RGB_555 tells us that the pixel map format is RGB, 5 bits per pixel(15-bit true colour).

A number of texture maps are supplied with BRender to help get you started (all have .pix extensions). However, you will want to define and apply your own textures - perhaps scanned from a photograph, or created using a paint package such as Photoshop. TEXCONV is used for importing and converting textures to be used in BRender applications.

To convert the sample pixel map bluebels.bmp, included on your Tutorial Programs disk, to BRender 15-bit RGB format, enter the following command line:

	texconv bluebels.bmp -n bluebells -c BR_PMT_RGB_555 -o bbell15.pix
The input file, bluebels.bmp, is in 8-bit, 256 indexed colour format. TEXCONV creates an output file, bbell15.pix, in 15-bit RGB (BRender .pix) format. The -n option assigns the name 'bluebells' to the texture map stored in bbell15.pix. This newly created texture can now be used in BRender applications. You might want to try substituting it for 'earth' in the above program. Remember that you can experiment with material properties without having to re-compile the program every time you make a change - simply edit the material script file as required.

Generated with CERN WebMaker