/* 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 #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 (!getbutton(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; } /* init_gl - initialize GL lighting & materials */ void init_gl(void) { float redMaterial[] = { DIFFUSE, 1, 0, 0, LMNULL }; float blueMaterial[] = { DIFFUSE, 0, 0, 1, LMNULL }; lmdef(DEFLMODEL,1,0,NULL); lmbind(LMODEL,1); lmdef(DEFLIGHT,1,0,NULL); lmbind(LIGHT0,1); lmdef(DEFMATERIAL,1,0,redMaterial); lmdef(DEFMATERIAL,2,0,blueMaterial); } /* draw_balls - draw the two balls, using the shared data for their y coordinates */ void draw_balls(struct _balldata *ball) { float sphereParam0[] = { 2, 4, -5, 1}; float sphereParam1[] = { -2, 4, -5, 1}; czclear(0,getgdesc(GD_ZMAX)); lmbind(MATERIAL,0); cpack(0xff088018); pushmatrix(); rot(-90.0,'x'); rectf(-100.0,-100.0,100.0,100.0); popmatrix(); lmbind(MATERIAL,1); sphereParam0[1] = ball[0].y; sphdraw(sphereParam0); lmbind(MATERIAL,2); sphereParam1[1] = ball[1].y; sphdraw(sphereParam1); }