/* simple cave program*/ #include #include #include #include #define SPEED 5 /* Max navigation speed in feet per second */ //**************** Declare Global Variables ***************** static GLuint greenMat, blueMat; static GLUquadricObj *sphereObj; float ball1_y, ball2_y; // *************** Declare Functions ********************** void navigate(void); void init_gl(void); void draw_balls(void); void update(void); // *************** Main ********************** void main(int argc, char **argv) { CAVEConfigure(&argc, argv, NULL); CAVEFar = 500.; // these funcs are called in the draw process CAVEInitApplication((CAVECALLBACK) init_gl, 0); CAVEFrameFunction((CAVECALLBACK) update, 0 ); CAVEDisplay((CAVECALLBACK) draw_balls, 0 ); CAVEInit(); // this loop is in the parent process while( ! CAVEgetbutton(CAVE_ESCKEY) ) { navigate(); //usleep is in micro-seconds usleep (10000); } CAVEExit(); } // *************** Define Functions ********************** /* navigate - perform the navigation calculations. This checks the joystick state and uses that to move and rotate. The Y position of the joystick determines the speed of motion in the direction of the wand. The X position of the joystick determines the speed of rotation about the CAVE's Y axis. Joystick values in the range -.2 to .2 are ignored; this provides a dead zone to eliminate noise. The motion is scaled by dt, the time passed since the last call to navigate(), in order to maintain a smooth speed. */ void navigate(void) { float jx=CAVE_JOYSTICK_X,jy=CAVE_JOYSTICK_Y,dt,t; static float prevtime = 0; t = CAVEGetTime(); dt = t - prevtime; prevtime = t; if (fabs(jy)>0.2) { float wandFront[3]; CAVEGetVector(CAVE_WAND_FRONT,wandFront); CAVENavTranslate(wandFront[0]*jy*SPEED*dt, wandFront[1]*jy*SPEED*dt, wandFront[2]*jy*SPEED*dt); } if (fabs(jx)>0.2) CAVENavRot(-jx*90.0f*dt,'y'); } /* init_gl - initialize GL lighting & materials */ void init_gl(void) { float greenMaterial[] = { 0, 1, 0, 1 }; float blueMaterial[] = { 0, 0, 1, 1 }; glEnable(GL_LIGHT0); greenMat = glGenLists(1); glNewList(greenMat, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, greenMaterial); glEndList(); blueMat = glGenLists(1); glNewList(blueMat, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blueMaterial); glEndList(); sphereObj = gluNewQuadric(); } void draw_balls(void) { glClearColor(0., 0., 0., 0.); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_LIGHTING); /* Apply the navigation transformation */ CAVENavTransform(); glCallList(greenMat); glPushMatrix(); glTranslatef(2.0, ball1_y, -5.0); gluSphere(sphereObj, 1.0, 8, 8); glPopMatrix(); glCallList(blueMat); glPushMatrix(); glTranslatef(-2.0, ball2_y, -5.0); gluSphere(sphereObj, 1.0, 8, 8); glPopMatrix(); glDisable(GL_LIGHTING); } void update(void) { float t = CAVEGetTime(); ball1_y = fabs(sin(t)) * 6 + 1; ball2_y = fabs(sin(t*1.2)) * 4 + 1; } ///////////////////////////////////////////////////////////////////////////////////////////