[comp.sys.sgi] cmode lighting

andy@BRL.MIL ("Ronald D. Anderson", IBD) (04/19/91)

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?

i will be drawing a 3-d surface of many polygons, and will be rotating
this surface about at least two axes.  the lighting model can be at
infinite distance, or may be local.

send to e-mail "andy@ibd.brl.mil" (128.63.11.2) or to info-iris if the
example is fairly short and of general interest.

						thanks,
							andy

howardl@sgi.com (Howard Look) (04/20/91)

In article <9104191049.aa04848@IBD.BRL.MIL> andy@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@sgi.com   (415) 335-1780
.__   One of these :) after being run over by one of these O-O

Dan Karron@UCBVAX.BERKELEY.EDU (04/20/91)

>From: "Ronald D. Anderson" (IBD) <andy@BRL.MIL>
>To: info-iris@BRL.MIL
>Subject:  cmode lighting
>Message-Id:  <9104191049.aa04848@IBD.BRL.MIL>
>
>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?
>
>i will be drawing a 3-d surface of many polygons, and will be rotating
>this surface about at least two axes.  the lighting model can be at
>infinite distance, or may be local.
>

The use of color map mode are limited, and the manual does not recommend using
this.

From the Graphics LIbrary Programming Guide, 9-24, section 9.8:

There is a way to do lighting in color map mode, but it is designed for
systems without enought bit planes to support RGB mode. On graphics systems
with enough bitplanes, RGB mode lighting is is recommended.

Color map lighting generates a pseudo-intensity which is a function of the
direction to the light source and the direction to the viewpoint. This pseudo-
intensity is mapped to a color map value. A well-chosen range of color map
values givea a reasonable lighting effect. You can represent multiple
materials by creating a color map range for each material (UCK!). Color map
lighting is enabled when both lighting an color map mode are enabled.

----------------------------------------------------------------------------

I have a problem porting a color map based system to the RGB environment
mainly for the purpose of using the advanced lighting subroutines. 

My solution is based on this: Instead of trying to use lighting models 
in color map mode, I have decided to try
to simulate the color map mode in RGB mode. I will make a private color map
and take all of the color calls and change them to RGB calls via a
intermediate subroutine. 

I am doing it right now, and I will post the code that seems to work.

If anyone else has idea, I would like to hear them too.

| karron@nyu.edu (e-mail alias )         Dan Karron, Research Associate      |
| Phone: 212 263 5210 Fax: 212 263 7190  New York University Medical Center  |
| 560 First Avenue                       Digital Pager <1> (212) 397 9330    |
| New York, New York 10016               <2> 10896   <3> <your-number-here>  |