/* read_file2.c This is a basic CAVE programing, that will read * in a set of data points, and then display them as blue spheres. */ /* parts of this code originally came from Dave Pape, EVL, UIC. */ #include #include #include /* Create a simple data structure to hold the point data */ typedef struct { float x; float y; float z; } point; void init_gl(void); void draw(void); void navigate(void); void readData(char *file); /* The shared data */ point *dataPoints; int *pointCount; main(int argc,char **argv) { CAVEConfigure(&argc,argv,NULL); if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } else { readData(argv[1]); } CAVEInit(); CAVEInitApplication(init_gl,0); CAVEDisplay(draw,0); while (!CAVEgetbutton(CAVE_ESCKEY)) { navigate(); sginap(1); } CAVEExit(); } void readData(char *file) { FILE *fp; int i, number_of_points; if((fp = fopen(file, "r"))==NULL) { printf("Unable to open data file %s\n", file); exit(1); } if (!fscanf(fp, "%d", &number_of_points)) { fprintf(stderr, "Error Reading data file\n"); exit(1); } /* This is where the shared memory variables get initialized.*/ pointCount = (int*)CAVEMalloc(sizeof(int)); *pointCount = number_of_points; dataPoints = (point*)CAVEMalloc(sizeof(point) * number_of_points); for(i = 0; i < number_of_points; i++) { if (!fscanf(fp, "%f %f %f", &dataPoints[i].x, &dataPoints[i].y, &dataPoints[i].z)) { fprintf(stderr, "Error Reading data file\n"); exit(1); } } fclose(fp); } #define SPEED 5.0f 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, 0, wandFront[2]*jy*SPEED*dt); } if (fabs(jx)>0.2) CAVENavRot(-jx*90.0f*dt,'y'); } /* This holds the value for the display lists. One for the * floor, and one for a blue sphere. */ static GLuint sphereList; static GLuint floorList; void init_gl(void) { float sphereMaterial[] = { 0.5, 0.5, 1, 1 }; static float blueMaterial[] = { 0, 0, 1, 1 }; static float whiteMaterial[] = { 1, 1, 1, 1 }; int i, j; GLUquadricObj *sphereObj; /* Create a sphere to draw later. A display list holds instructions * on how to draw something. In this case, we are drawing a light * blue sphere. */ sphereObj = gluNewQuadric(); sphereList = glGenLists(1); glNewList(sphereList, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphereMaterial); gluSphere(sphereObj, 0.5, 8, 8); glEndList(); /* Create a display list for the floor drawing commands */ floorList = glGenLists(1); glNewList(floorList, GL_COMPILE); glBegin(GL_QUADS); glNormal3f(0, 1, 0); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, whiteMaterial); glVertex3f(-100, 0, -100); glVertex3f(100, 0, -100); glVertex3f(100, 0, 100); glVertex3f(-100, 0, 100); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blueMaterial); for (i = -100; i <100; i+=20) { for(j = -100; j <100; j+=20) { glVertex3f(i, 0.1, j); glVertex3f(i+10, 0.1, j); glVertex3f(i+10, 0.1, j+10); glVertex3f(i, 0.1, j+10); } } for (i = -90; i < 100; i+=20) { for(j = -90; j < 100; j+=20) { glVertex3f(i, 0.1, j); glVertex3f(i+10, 0.1, j); glVertex3f(i+10, 0.1, j+10); glVertex3f(i, 0.1, j+10); } } glEnd(); glEndList(); glEnable(GL_LIGHT0); } /* draw the objects. */ void draw(void) { int i; glClearColor(0., 0., 0., 1.); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_LIGHTING); CAVENavTransform(); /* draw the floor */ glCallList(floorList); /* This just puts the data out in front of us, so we can see it better. */ glPushMatrix(); glTranslatef(0, 5, -8); /* Draw the data. */ for (i = 0; i < *pointCount; i ++) { glPushMatrix(); glTranslatef(dataPoints[i].x, dataPoints[i].y, dataPoints[i].z); glCallList(sphereList); glPopMatrix(); } glPopMatrix(); glDisable(GL_LIGHTING); }