/* read_file3.c This is a basic CAVE programing, that will read * in a set of data points, as well as a type field. It will display * each datatype differently. */ /* parts of this code originally came from Dave Pape, EVL, UIC. */ #include #include #include /* This time, we put in a value for the data point type. */ typedef struct { float x; float y; float z; int type; } 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); /* Make sure a file name has been specified, then read in the data file */ 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 %d", &dataPoints[i].x, &dataPoints[i].y, &dataPoints[i].z, &dataPoints[i].type)) { 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'); } static GLuint floorList; void init_gl(void) { float sphere1[] = { 1, 1, 1, 1 }; /* white */ float sphere2[] = { 0, 0, 0, 1 }; /* black */ float sphere3[] = { 0.5, 0.5, 1, 1 }; /* light blue */ float sphere4[] = { 1, 0, 0, 1 }; /* red */ float sphere5[] = { 0, 1, 0, 1 }; /* green */ float sphere6[] = { 0.5, 0.5, 0.5, 1 }; /* grey */ GLfloat shininess[] = { 80.0 }; GLfloat specular[] = { 1, 1, 1, 1 }; static float blueMaterial[] = { 0, 0, 1, 1 }; static float whiteMaterial[] = { 1, 1, 1, 1 }; int i, j; GLUquadricObj *sphereObj; /* Create several display lists to holds spheres of different * size and shapes. The different spheres will represent the * different data types. */ sphereObj = gluNewQuadric(); glNewList(1, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphere1); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); gluSphere(sphereObj, 0.3, 10, 10); glEndList(); glNewList(2, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphere2); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); gluSphere(sphereObj, 0.5, 10, 10); glEndList(); glNewList(3, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphere3); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); gluSphere(sphereObj, 0.5, 10, 10); glEndList(); glNewList(4, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphere4); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); gluSphere(sphereObj, 0.6, 10, 10); glEndList(); glNewList(5, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphere5); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); gluSphere(sphereObj, 0.5, 10, 10); glEndList(); glNewList(6, GL_COMPILE); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sphere6); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); gluSphere(sphereObj, 0.8, 10, 10); glEndList(); /* Create a display list to hold 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(dataPoints[i].type); glPopMatrix(); } glPopMatrix(); glDisable(GL_LIGHTING); }