cmode lighting

Howard Look howardl at sgi.com
Sat Apr 20 05:03:20 AEST 1991


In article <9104191049.aa04848 at IBD.BRL.MIL> andy at BRL.MIL ("Ronald D. Anderson", IBD) writes:
>am using a 4D/70G system, and want to use a lighting model while in
>color-map mode.  anyone out there have a (preferably simple) example?

Here is a sample program that uses color map lighting. It is taken
from our "GL Programming 2" course. You'll need to replace th call
to ftorus() with your favorite filled object, or maybe a sphere library
sphere.

Hope this helps,
Howard.


/*
 * cmap_light.c
 * This program demonstrates the use of the GL lighting model
 * in color map mode.
 * A torus is drawn using a green plastic material
 * characteristic. A single light source illuminates the
 * object.
 *
 * Copyright 1991, Silicon Graphics, Inc.  All Rights Reserved.
 * Author:   Technical Education Course Developers
 */

#include <gl/gl.h>
#include <gl/device.h>
#include "shapes.h"

void initialize(void);
void drawscene(void);

/* size of the window */
int xmax, ymax;

/*
 * Define the lighting model properties array.
 * only LOCALVIEWER, which must be set to infinite 
 * viewpoint (0.0), can be specified in colormap mode
 */
float light_model[] =
{
	LOCALVIEWER, 0.0,
	LMNULL
};

/* 
 * Define the light source properties array.
 * This is a white, infinite light source, shining from
 * the top.
 * Only POSITION, which must be infinite, is
 * supported in color map mode.
 */
float white_light[] =
{
	POSITION, 0.0, 1.0, 1.0, 0.0,
	LMNULL
};

/* 
 * Define the material properties array.
 * Only COLORINDEXES and SHININESS are supported in
 * color map mode.
 * The indexes are the ambient, diffuse, and specular
 * locations in the color ramp defined in
 * the function init_material_ramp.
 */
float green_plastic[] =
{
	COLORINDEXES, 512, 640, 767,
	SHININESS, 20.0,
	LMNULL
};
	
void
main ()
{
	Boolean exitflag = FALSE;
	short attached=0; /* attached to window */
	short value;                  
	int dev; /* input device */
	
	initialize();

	while (exitflag == FALSE)
	{
		drawscene();
		
		while ((exitflag == FALSE) && (qtest() || !attached))
		{
			dev = qread (&value);
			if ((dev == ESCKEY) && (value == 0))
			{
				exitflag = TRUE;
			}
			else if (dev == REDRAW)
			{
				reshapeviewport();
				drawscene();
			}
			else if (dev == INPUTCHANGE)
			{
				attached = value;	
			} /*  end while qtest or not attached  */
		}
	}   /*  end while (exitflag == FALSE)  */
	exit(0);
}   /*  end main()  */


/*
 * Initialize a color ramp for a green plastic material.
 * The ramp goes from grey ambient to green
 * diffuse to white specular.
 */
void
init_material_ramp()
{
	int i;
	short r,g,b;

	/* ambient grey to diffuse green */
	for (i = 512; i < 640; i++)
	{
		/* red and blue go from 32 to 0 */
		r = b = 32 - 32*(i - 512)/(640 - 512);
		
		/* green goes from 32 to 255 */
		g = 32 + (255 - 32)*(i - 512)/(640 - 512);
		
		mapcolor( i, r, g, b);
	}

	/* diffuse green to specular white */
	for (i = 640; i < 768; i++)
	{
		/* red and blue go from 0 to 255 */
		r = b = 255*(i - 640)/(768 - 640);

		/* green stays at 255 */
		g = 255;
	
		mapcolor( i, r, g, b);
	};
}		

void
initialize(void)
{
	int gid;
	
	xmax = getgdesc(GD_XPMAX);
	ymax = getgdesc(GD_YPMAX);
	prefposition( xmax/4, xmax*3/4, ymax/4, ymax*3/4 );
	gid = winopen ("cmap_light");
	minsize (xmax/10, ymax/10);
	keepaspect (xmax, ymax);
	winconstraints();

	/* color map mode */
	doublebuffer();
	gconfig ();

	zbuffer(TRUE);
	
	qdevice(ESCKEY);
	qenter(REDRAW,gid);

	/* initialize the color map for the material */
	init_material_ramp();
	
	/* double matrix mode since using lighting */
	mmode(MVIEWING);

	/* define the light model, light source, and material */
	lmdef(DEFLMODEL, 1, 3, light_model);
	lmdef(DEFLIGHT, 1, 6, white_light);
	lmdef(DEFMATERIAL, 1, 7, green_plastic);

	/* bind the light model */
	lmbind(LMODEL, 1);
}


void
drawscene(void)
{
	static short angle = 0;

	czclear(0x0, getgdesc(GD_ZMAX));

	/* projection transformation */
	perspective(450, (float)xmax/(float)ymax, 1.0, 10.0);

	pushmatrix();

		/* viewing transformation */
		polarview(5.0, 0, 0, 0);

		/* Bind (turn on) the light */
		lmbind(LIGHT0, 1);

		/* draw the green torus */
		pushmatrix();
		
			rotate(angle, 'y');
			
			/* Bind (turn on) the material */
			lmbind(MATERIAL,1);
			
			/*	ftorus draws a shape with one unit normal
				per vertex
			*/
			ftorus();
			
			/* Bind (turn off) the material to deactivate lighting */
			lmbind(MATERIAL,0);
			
		popmatrix();

		
	popmatrix();
	
	swapbuffers();
	
	angle = angle + 20;
	if (angle > 3600)
		angle = angle - 3600;
}


--
Howard Look   Silicon Graphics   howardl at sgi.com   (415) 335-1780
.__   One of these :) after being run over by one of these O-O



More information about the Comp.sys.sgi mailing list