BRender Tutorial Guide:5 Adding Colour:8-Bit Indexed Colour Mode
Next|Prev|Up

8-Bit Indexed Colour Mode


As previously noted, the complexities of palette management make 8-bit indexed colour more complex to work with than true colour. You can build your own palettes using the supplied utility MKRANGES. For now we will use the palette supplied with BRender (std.pal) when working in 8-bit mode.

The 256 colours in std.pal are divided into seven ranges, or `colour ramps'. The first 64 colours represent shades of grey ranging from very dark grey (black) to very light grey (white). Colours 64 to 95 are various shades of blue.

BRTUTOR5.C

/*
 * Copyright (c) 1996 Argonaut Technologies Limited. All rights reserved.
 * Program to Display a Revolving Illuminated Blue Cube.
 */
#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, *cube;
	br_material *cube_material;
	int i;

/*********** Initialise BRender and Graphics Hardware ***********/
	BrBegin();
	/*
	 * 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 *****************/
	/* 
	 * Load Root Actor. Load and Enable Default Light Source
	 */
	world = BrActorAllocate(BR_ACTOR_NONE,NULL);
	BrLightEnable(BrActorAdd(world,BrActorAllocate(BR_ACTOR_LIGHT,NULL)));
	/*
	 * Load and Position Camera
	 */
	observer = BrActorAdd(world,BrActorAllocate(BR_ACTOR_CAMERA,NULL));
	observer->t.type = BR_TRANSFORM_MATRIX34;  
	BrMatrix34Translate(&observer->t.t.mat,BR_SCALAR(0.0),BR_SCALAR(0.0), 
                                                         BR_SCALAR(5.0));
	/*
	 * Load and Position Cube Model
	 */
	cube = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
	cube->t.type = BR_TRANSFORM_MATRIX34;
	BrMatrix34RotateY(&cube->t.t.mat,BR_ANGLE_DEG(30)); 
	/*
	 * Load and Apply Cube's Material
	 */
	cube_material = BrFmtScriptMaterialLoad("cube.mat");
	BrMaterialAdd(cube_material);
	cube->material = BrMaterialFind("BLUE MATERIAL");

 /********************** Animation Loop ******************/
	for(i=0; i < 200; i++)  {
					BrPixelmapFill(back_buffer,0);
					BrPixelmapFill(depth_buffer,0xFFFFFFFF);
					BrZbSceneRender(world,observer,back_buffer,depth_buffer);
					BrPixelmapDoubleBuffer(screen_buffer,back_buffer);
					BrMatrix34PostRotateX(&cube->t.t.mat,BR_ANGLE_DEG(2.0));
	}
	/*
	 * Close down 
	 */
	BrPixelmapFree(depth_buffer);
	BrPixelmapFree(back_buffer);
	BrZbEnd();
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
	BrEnd();
	return 0;
}
BRTUTOR5.C

So colour information in 8-bit colour mode specifies an index into a colour palette. A material script file describing a blue material (using std.pal) is given below.

cube.mat

# Material for Cube - 8-bit
# A plain blue texture

material =	[
		identifier = "BLUE MATERIAL"; 
		ambient = 0.05;
		diffuse = 0.55;
		specular = 0.4;
		power = 10;
		flags = [light,smooth];
		index_base = 64;
		index_range = 30;
	];

Colour number 64 indexes the start of the blue colour ramp. The index_range value defines the range of colours available for rendering this material. BRender uses lighting calculations to determine `how blue' a model with this material should appear at any point. If no light is shining on the model, colour number 64 will be selected. If a bright light is shining directly onto the model, values closer to 90 will be selected.

Only two lines need to be changed in BRTUTOR5.C to display a revolving blue cube in 8-bit mode. One is highlighted in BRTUTR5b.C below. The other specifies 8-bit mode in the hardware initialisation code (refer to the program listing on your Tutorial Programs disk). If you are running BRender under DOS, the line:

	screen_buffer = DOSGfxBegin(NULL);
replaces:

	screen_buffer = DOSGfxBegin("VESA,W:320,H:200,B:15");
BRTUTR5B.C uses the material script file cube8.mat.

BRTUTR5B.C


/* 
 * Copyright (c) 1996 Argonaut Technologies Limited. All rights reserved.
 * Program to Display a Revolving Illuminated Blue Cube (8-bit mode)
 */

#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, *cube;
 	br_material *cube_material;
	int i;		

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

	BrBegin();
	/*
	 * 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);
	BrLightEnable(BrActorAdd(world,BrActorAllocate(BR_ACTOR_LIGHT,NULL)));

	observer = BrActorAdd(world,BrActorAllocate(BR_ACTOR_CAMERA,NULL));
	observer->t.type = BR_TRANSFORM_MATRIX34;  
	BrMatrix34Translate(&observer->t.t.mat,BR_SCALAR(0.0),BR_SCALAR(0.0), 
                                                         BR_SCALAR(5.0));

	/*
	 * Load and Position Cube Model
	 */
	cube = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
	cube->t.type = BR_TRANSFORM_MATRIX34;
	BrMatrix34RotateY(&cube->t.t.mat,BR_ANGLE_DEG(30)); 
 
	/*
	 * Load and Apply Cube's Material
	 */
	cube_material = BrFmtScriptMaterialLoad("cube8.mat");
	BrMaterialAdd(cube_material);
	cube->material = BrMaterialFind("BLUE MATERIAL");

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

	for(i=0; i < 200; i++) {
				BrPixelmapFill(back_buffer,0);
				BrPixelmapFill(depth_buffer,0xFFFFFFFF);
				BrZbSceneRender(world,observer,back_buffer,depth_buffer);
				BrPixelmapDoubleBuffer(screen_buffer,back_buffer);
				BrMatrix34PostRotateX(&cube->t.t.mat,BR_ANGLE_DEG(2.0));
	}
	BrPixelmapFree(depth_buffer);/*Close down*/
	BrPixelmapFree(back_buffer);
	BrZbEnd();
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
.      .      .
	BrEnd();
	return 0;
}

BRTUTR5B.C



Generated with CERN WebMaker