/* bounce.c /* An example of a dynamic CAVE environment. Two bouncing balls are /* drawn. The calculations for their movements are performed in the /* main process, and communicated to the drawing processes through /* shared memory. */ #include #include /* The data that will be shared between processes */ struct _balldata { float y; }; void init_gl(void),draw_balls(struct _balldata *); struct _balldata *init_shmem(void); void compute(struct _balldata *); main(int argc,char **argv) { struct _balldata *ball; CAVEConfigure(&argc,argv,NULL); /* Initialize shared memory */ ball = init_shmem(); CAVEInit(); CAVEInitApplication(init_gl,0); /* Give the library a pointer to the drawing function, plus one argument */ CAVEDisplay(draw_balls,1,ball); while (!CAVEgetbutton(CAVE_ESCKEY)) { /* Update the balls' positions */ compute(ball); sginap(1); } CAVEExit(); } /* init_shmem - initializes shared memory. The data is allocated from a shared memory arena, and so will be common to all processes forked after this is called. */ struct _balldata *init_shmem(void) { struct _balldata *ball; ball = CAVEMalloc(2*sizeof(struct _balldata)); bzero(ball,2*sizeof(struct _balldata)); return ball; } /* compute - compute new positions for the balls. The height of the balls is a function of the current CAVE time. */ void compute(struct _balldata *ball) { float t = CAVEGetTime(); ball[0].y = fabs(sin(t)) * 6 + 1; ball[1].y = fabs(sin(t*1.2)) * 4 + 1; } static GLuint redMat, blueMat; static GLUquadricObj *sphereObj; /* init_gl - initialize GL lighting & materials */ void init_gl(void) { float redMaterial[] = { 1, 0, 0, 1 }; float blueMaterial[] = { 0, 0, 1, 1 }; glEnable(GL_LIGHT0); redMat = glGenLists(1); glNewList(redMat, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, redMaterial); glEndList(); blueMat = glGenLists(1); glNewList(blueMat, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blueMaterial); glEndList(); sphereObj = gluNewQuadric(); } /* draw_balls - draw the two balls, using the shared data for their y coordinates */ void draw_balls(struct _balldata *ball) { glClearColor(0., 0., 0., 0.); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_LIGHTING); glCallList(redMat); glPushMatrix(); glTranslatef(2.0, ball[0].y, -5.0); gluSphere(sphereObj, 1.0, 8, 8); glPopMatrix(); glCallList(blueMat); glPushMatrix(); glTranslatef(-2.0, ball[1].y, -5.0); gluSphere(sphereObj, 1.0, 8, 8); glPopMatrix(); glDisable(GL_LIGHTING); }