#include #include void init_shared_data(),gl_init_fn(),draw_fn(); /* Shared data */ float *x,*y,*z; main(int argc,char **argv) { float vx,vy,vz; CAVEConfigure(&argc,argv,NULL); init_shared_data(); vx = (drand48()-0.5)/10.0; vy = (drand48()-0.5)/10.0; vz = (drand48()-0.5)/10.0; CAVEInit(); CAVEInitApplication(gl_init_fn,0); CAVEDisplay(draw_fn,0); while (!CAVEgetbutton(CAVE_ESCKEY)) { /* Bounce the ball around */ if (*x < -5) vx = fabs(vx); else if (*x > 5) vx = -fabs(vx); if (*y < 0) vy = fabs(vy); else if (*y > 10) vy = -fabs(vy); if (*z < -5) vz = fabs(vz); else if (*z > 5) vz = -fabs(vz); *x += vx; *y += vy; *z += vz; sginap(1); /* Make this loop run about 100 iterations/second */ } CAVEExit(); } /* Get a shared arena, amalloc the shared variables, & initialize them */ void init_shared_data() { x = (float *) CAVEMalloc(sizeof(float)); y = (float *) CAVEMalloc(sizeof(float)); z = (float *) CAVEMalloc(sizeof(float)); *x = 0; *y = 5; *z = 0; } static GLUquadricObj *sphereObj; /* initialize the graphics - define the lighting data */ void gl_init_fn() { float light_pos[] = { -5, 10, 5, 0 }; float mat_diffuse[] = { 0.6, 0.5, 0.1, 1 }; glLightfv(GL_LIGHT0, GL_POSITION, light_pos); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse); sphereObj = gluNewQuadric(); } /* draw the scene - draw a ball at (*x,*y,*z), and, if button 1 is pressed, draw a smaller ball in front of the wand */ void draw_fn() { glClearColor(0.5, 1., 1., 0.); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_LIGHTING); glPushMatrix(); glTranslatef(*x,*y,*z); gluSphere(sphereObj, 1.0, 8, 8); glPopMatrix(); if (CAVEBUTTON1) { float wandPos[3],wandFront[3]; CAVEGetPosition(CAVE_WAND,wandPos); CAVEGetVector(CAVE_WAND_FRONT,wandFront); glPushMatrix(); glTranslatef(wandPos[0]+wandFront[0]*2,wandPos[1]+wandFront[1]*2, wandPos[2]+wandFront[2]*2); gluSphere(sphereObj, .25, 8, 8); glPopMatrix(); } glDisable(GL_LIGHTING); }