CAVELib Sample Program 1
Previous  Top  Next


This application draws a red sphere at (0,4,-4).

#include <cave_ogl.h>
#include <GL/glu.h>



void init_gl(void);
void draw(void);



GLUquadricObj *sphereObj;

int
main(int argc,char **argv) {

/* Initialize the CAVE */  
CAVEConfigure(&argc,argv,NULL);  
 
 
/* Give the library a pointer to the GL initialization function */  
CAVEInitApplication(init_gl,0);  
 
 
/* Give the library a pointer to the drawing function */  
CAVEDisplay(draw,0);  
 
 
/* Create the multiple processes/threads and  
start the display loop */  
CAVEInit();  
 
 
/* Wait for the escape key to be hit */  
while (!CAVEgetbutton(CAVE_ESCKEY)) {  
 
/* Nap so that this busy loop doesn't waste CPU time  
reset timeval struct every time for linux compatibility  
*/  
CAVEUSleep(10);  
 
}  
 
/* Clean up & exit */  
CAVEExit();  
return 0;  
}

/* init_gl - GL initialization function. This function will be called
exactly once by each of the drawing processes, at the beginning of
the next frame after the pointer to it is passed to CAVEInitApplication.
It defines and binds the light and material data for the rendering,
and creates a quadric object to use when drawing the sphere. 
*/
void init_gl(void) {

float redMaterial[] = { 1, 0, 0, 1 };  
 
/* Enable light source 0 */  
glEnable(GL_LIGHT0);  
 
/* Set material to color both front and back face to a diffuse red */  
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redMaterial);  
 
/* Create a glu quadric object */  
sphereObj = gluNewQuadric();  
 
}

/* draw - the display function. This function is called by the
CAVE library in the rendering processes' display loop. It draws a
sphere 1 foot in radius, 4 feet off the floor, and 1 foot in front
of the front wall (assuming a 10' CAVE).
*/
void draw(void) {

/* Set clear color to black and clear both the screen and the zbuffer */  
glClearColor(0., 0., 0., 0.);  
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);  
 
/* Turn lighting on */  
glEnable(GL_LIGHTING);  
 
/* Draw a sphere */  
glPushMatrix();  
glTranslatef(0.0, 4.0, -4.0);  
 
/* Draw a sphere using GLU quadric object.  
Radius = 1  
Slices = 8  
Stacks = 8  
*/  
gluSphere(sphereObj, 1.0, 8, 8);  
glPopMatrix();  
 
/* Turn lighting off */  
glDisable(GL_LIGHTING);  
 
}