Fun GL program

Bruce Karsh karsh at trifolium.esd.sgi.com
Fri Jun 1 20:36:22 AEST 1990


Here's a fun little graphics program.  Run it when you feel that bugs are
getting you down.

When you've had enough, you can kill it by selecting the plain root from the
Windows chest.

------------------------( Cut Here multibug.c )--------------------------------
#include "gl.h"
#include "gl/device.h"
#include "math.h"

#define PI 3.1415926535

#define NBUGS	12
#define MAXBUGS 500

int phase[MAXBUGS];
Coord bugx[MAXBUGS],bugy[MAXBUGS];
Angle bugt[MAXBUGS];
int bugc[MAXBUGS];
int nbugs;

main(argc,argv)
int argc;
char *argv[];
{
	short dev,data;
	int i;
	imakebackground();
	winopen("background");
	shademodel(FLAT);

	if(argc == 2)
		nbugs=atoi(argv[1]);
	else
		nbugs=NBUGS;
	if(nbugs > MAXBUGS)nbugs=MAXBUGS;

	for(i=0;i<nbugs;i++)bugc[i]=i+1;
	repaint();
	while(1) {
		if(qtest()) {
			dev=qread(&data);
			if(dev == REDRAW) {
				repaint();
			}
		} else {
			/* Move the bugs. */
			for(i=0;i<nbugs;i++) {
				bugx[i] += 2*cos(PI/180*bugt[i]/10.0);
				bugy[i] += 2*sin(PI/180*bugt[i]/10.0);
				if(bugx[i]>1300)bugx[i] = -35;
				if(bugx[i]< -35)bugx[i] = 1300;
				if(bugy[i] > 1050)bugy[i] = -35;
				if(bugy[i] < -35)bugy[i] = 1050;
				bugt[i] += rand() % 200 - 100;
			}
			newbug();
			sginap(2);
		}
	}
}
repaint()
{
	color(14);
	clear();
	newbug();
}
newbug()
{
	int i;
	/* Draw the new bugs. */
	for(i=0;i<nbugs;i++) {
		bug(bugx[i],bugy[i],bugt[i],phase[i],bugc[i]);
	}
}
bug(x,y,t,p,c)
Coord x,y;
Angle t;
int p;
{
	float x0,y0;
	pushmatrix();
	translate(x,y,0);
	rotate(t,'Z');
	/* Draw the body. */
	color(c);
	circf(0,0,15);
	color(BLACK);
	circ(0,0,15);
	color(c);
	/* Draw the head. */
	circf(15+6+1,0,6);
	color(BLACK);
	circ(15+6+1,0,6);
	color(c);
	/* Draw the left foreleg. */
	x0=15*cos(60*PI/180.);
	y0=15*sin(60*PI/180.);
	color(BLACK);
	move2(x0,y0);
	draw2(x0,y0+5);
	/* Draw the right foreleg. */
	move2(x0,-y0);
	draw2(x0,-y0-5);
	/* Draw the left middle leg. */
	x0=0;
	y0=15;
	move2(x0,y0);
	draw2(x0,y0+5);
	/* Draw the right middle leg. */
	move2(x0,-y0);
	draw2(x0,-y0-5);
	/* Draw the left hindleg. */
	x0=15*cos(120*PI/180.);
	y0=15*sin(120*PI/180.);
	color(BLACK);
	move2(x0,y0);
	draw2(x0,y0+5);
	/* Draw the right hindleg. */
	move2(x0,-y0);
	draw2(x0,-y0-5);
	popmatrix();
}

			Bruce Karsh
			karsh at sgi.com



More information about the Comp.sys.sgi mailing list