Personal Iris lighting problems

Howard Look howardl at sgi.com
Wed Mar 13 16:29:45 AEST 1991


In article <13917 at life.ai.mit.edu> ringrose at fibula.ai.mit.edu (Robert Ringrose) writes:
>
>I am running into a problem with lighting on the Personal Iris 4D/25.
>(working with C and the gl libraries)
>
>It works fine until I put in a call to a routine with the
>following code, part of an attempt to get the current
>projection and modelview matrices:
>
>
>mmode(MPROJECTION);
>mmode(MVIEWING);
>
>...
>
>Specifically, the problem is that the one infinite light I use,
>bound to LIGHT1, is rotated and the rotation is increased every
>time those two lines are executed.  I have tried re-defining and 
>re-binding the light, without change.

Using mmode(MPROJECTION) is not a usual thing to do. As a matter of
fact, for most applications, you really only need to go into mmode(MVIEWING)
once in your initialize routine. Projection calls (perspective, window,
ortho, ortho2) will replace the projection matrix anyway.

Here is an example of a rotating infinite light source. The light is
made to rotate by binding while a rotation is part of the current matrix.
The light is only bound once.

Hope this helps.

-- howard.

/*
 * light_motion.c
 * Compile with
 * cc light_motion.c -o light_motion -lsphere -lgl_s -lm
 */

#include <gl/gl.h>
#include <gl/device.h>
#include <gl/sphere.h>

/* macro to count the number of properties in an array */
#define NP(array) ((sizeof(array)/(sizeof(array[0]))))

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

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

/* different light positions */
#define EYE 1
#define WORLD 2
#define MOVING 3

int light_position = MOVING;
int moving_eye = FALSE;

float light_model[] =
{
	AMBIENT, 0.1, 0.1, 0.1,
	ATTENUATION, 1.0, 0.0,
	LOCALVIEWER, 0.0,
	LMNULL
};

float white_light[] =
{
	AMBIENT, 0.2, 0.2, 0.2,
	POSITION, 0.0, 0.0, 10.0, 0.0,
	LCOLOR, 1.0, 1.0, 1.0,
	LMNULL
};

float green_plastic[] =
{
	DIFFUSE, 0.1, 1.0, 0.1,
	SPECULAR, 1.0, 1.0, 1.0,
	SHININESS, 10.0,
	LMNULL
};
	
float yellow_plastic[] =
{
	DIFFUSE, 1.0, 1.0, 0.1,
	SPECULAR, 1.0, 1.0, 1.0,
	SHININESS, 10.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();
	    	}
	    	else if (dev == INPUTCHANGE)
			{
				attached = value;	
			}   /*  end while qtest or not attached  */
		}
	}   /*  end while (exitflag == FALSE)  */
	exit(0);
}   /*  end main()  */


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 ("light1");
    minsize (xmax/10, ymax/10);
    keepaspect (xmax, ymax);
    winconstraints();

	RGBmode();
	doublebuffer();
    gconfig ();

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

	/* double matrix mode since using lighting */
	mmode(MVIEWING);

	/* define the light model, light source, and material */
	lmdef(DEFLMODEL, 1, NP(light_model), light_model);
	lmdef(DEFLIGHT, 1, NP(white_light), white_light);
	lmdef(DEFMATERIAL, 1, NP(green_plastic), green_plastic);
	lmdef(DEFMATERIAL, 2, NP(yellow_plastic), yellow_plastic);

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


void
drawscene(void)
{
	static int angle = 0;
	static float unit[] = {0.0,0.0,0.0,1.0};


    czclear(0x0, getgdesc(GD_ZMAX));

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

	/* Bound here, the light is positioned relative to the eye */
	if (light_position == EYE)
		lmbind(LIGHT0, 1);

    pushmatrix();
		if (moving_eye)
			polarview(5.0, angle, angle, 0);
		else
			polarview(5.0, 0, 0, 0);

		/* Bound here, the light is positioned relative to the world */
		if (light_position == WORLD)
			lmbind(LIGHT0, 1);

		if (light_position == MOVING)
		{
			pushmatrix();
				rotate(angle,'x');
				
				/* Bound here, the light rotates around the world x axis */
				lmbind(LIGHT0, 1);
			popmatrix();
		}
			
		pushmatrix();
			translate(-1.0, 0.0, 0.0);
			
			lmbind(MATERIAL,1);
			sphdraw(unit);
		popmatrix();
		
		pushmatrix();
			translate(1.0, 0.0, 0.0);
			
			lmbind(MATERIAL,2);
			sphdraw(unit);

			/* turn off material so later objects will not be lit */
			lmbind(MATERIAL,0);

		popmatrix();
	   
    popmatrix();
	
    swapbuffers();
	
    angle = angle + 20;
}



--
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