/* spin_dat.c Rotating *.dat with color interpolation */ /* Utah Graphics ASCII data *.dat of *.det */ /* Demonstration of use of homogeneous coordinate */ /* transformations and simple data structure for representing */ /* Colors are assigned to the vertices */ /* Mouse buttons control direction of rotation */ #include #include #include static FILE * infile; static char input_line[255]; static int num_pts; static int num_pys; typedef struct {GLfloat x; GLfloat y; GLfloat z;} dpts; static dpts * data_points; static int debug = 0; static GLuint dataList; static GLfloat size = 1.0; /* auto scale */ GLfloat colors[][3] = {{1.0,0.0,0.0},{0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,1.0,0.0}, {0.0,1.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.5,0.5,0.5}}; static GLfloat theta[] = {0.0,0.0,0.0}; static GLfloat dtheta = 2.0; static GLint axis = 2; void drawDat(void) { int i, j, k, pts; /* be efficient--make data display list */ dataList = glGenLists(1); glNewList(dataList, GL_COMPILE); /* use pys to do surfaces */ for(i=0; i 360.0) theta[axis] = theta[axis] - 360.0; /* display(); */ glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { /* mouse callback, selects an axis about which to rotate */ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) glOrtho(-size, size, -size * (GLfloat)h / (GLfloat)w, size * (GLfloat)h / (GLfloat)w, -5.0*size, 5.0*size); else glOrtho(-size * (GLfloat)w / (GLfloat)h, size * (GLfloat)w / (GLfloat)h, -size, size, -5.0*size, 5.0*size); glMatrixMode(GL_MODELVIEW); } void keyboard (unsigned char key, int x, int y) { switch (key) { case 'S': dtheta = dtheta + 0.5; glutPostRedisplay(); break; case 's': dtheta = dtheta - 0.5; glutPostRedisplay(); break; case 'x': axis = 0; /* same as left mouse button */ glutPostRedisplay(); break; case 'y': axis = 1; /* same as middle mouse button */ glutPostRedisplay(); break; case 'z': axis = 2; /* same as right mouse button */ glutPostRedisplay(); break; case 27: exit(0); break; default: break; } } #undef abs #define abs(x) (((x)<0.0)?(-(x)):(x)) int main(int argc, char *argv[]) { int i; /* get file name of data to render */ if(argc<2) { printf("supply a file name of a Utah data file\n"); exit(0); } else { printf("open %s for reading\n", argv[1]); infile = fopen(argv[1], "r"); if(infile == NULL) { printf("could not open %s for reading\n", argv[1]); exit(0); } fgets(input_line, 254, infile); printf("first line %s \n", input_line); if(!strncmp("data", input_line, 4)) { sscanf(input_line, "data%d %d", &num_pts, &num_pys); } else { sscanf(input_line, "%d %d", &num_pts, &num_pys); } printf("num_pts=%d, num_pys=%d\n", num_pts, num_pys); data_points = (dpts *)malloc(sizeof(dpts)*num_pts); if(data_points == NULL) { printf("can not allocate enough space for points\n"); exit(0); } } for(i=0; isize) size = abs(data_points[i].x); if(abs(data_points[i].y)>size) size = abs(data_points[i].y); if(abs(data_points[i].z)>size) size = abs(data_points[i].z); } printf("scaling to size=%g\n", size); size = size * 2.0; /* for spinning */ glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinDat); /* "spinDat" setup */ glutMouseFunc(mouse); /* "mouse" setup */ glutKeyboardFunc(keyboard); /* "keyboard" setup */ glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ drawDat(); glutMainLoop(); return 0; }