/* visual.c */ /* visualizes a grid from a file */ #include #include #include #include #include #include "grid.h" #ifdef __APPLE__ #include #else #include #endif #include "io.h" void linein() { char buf[BUFSIZ]; int n; n = asyncGetLine(buf, BUFSIZ); if(n > 0) { fprintf(stderr, "I got a line: %s\n", buf); fprintf(stderr, "calling keypress(%c)\n", buf[0]); } if(n < 0) { exit(0); } } GLfloat red[3] = {1.0, 0.0, 0.0}; GLfloat green[3] = {0.0, 1.0, 0.0}; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat black[3] = {0.0, 0.0, 0.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLfloat yellow[3] = {1.0, 1.0, 0.0}; GLfloat magenta[3] = {1.0, 0.0, 1.0}; GLfloat cyan[3] = {0.0, 1.0, 1.0}; int glOpen = 0; struct { int fillmode; int log; int rotA[3]; Grid alt; Grid col; float xres; float yres; GLfloat *nodatacolor; int colorsetting; int logcolor; float max_side; float max_depth; float rot[3]; float pos[3]; float scale; float mult; } glSettings; /* forward declarations of functions */ void display(void); void idle(void); void idleCmd(void); void eleColor(int,float); void keypress(unsigned char key, int x, int y); void main_menu(int value); void visualize (Grid, Grid, int, float); void initialize (); void visualize (Grid alt, Grid col, int log, float mult) { glSettings.alt = alt; glSettings.col = col; glSettings.log = log; glSettings.mult = mult; glSettings.fillmode = 0; glSettings.rotA[0] = 0; glSettings.rotA[1] = 0; glSettings.rotA[2] = 0; glSettings.nodatacolor = black; glSettings.colorsetting = 0; glSettings.rot[0] = glSettings.rot[1] = glSettings.rot[2]; glSettings.pos[0] = glSettings.pos[1] = glSettings.pos[2]; glSettings.scale = 1.0; glSettings.max_side = glSettings.alt.ncols; if (glSettings.alt.nrows > glSettings.max_side) glSettings.max_side = glSettings.alt.nrows; if (glSettings.alt.maxval - glSettings.alt.minval > glSettings.max_side) glSettings.max_side = glSettings.alt.maxval - glSettings.alt.minval; glSettings.max_depth = (float)(glSettings.alt.maxval - glSettings.alt.minval) * 1.2;; glSettings.max_side *= 1.2; glSettings.xres = glSettings.alt.ncols / 100; if (glSettings.xres < 1) glSettings.xres = 1; glSettings.yres = glSettings.alt.nrows / 100; if (glSettings.yres < 1) glSettings.yres = 1; if (!glOpen) initialize(); } void initialize() { glOpen = 1; /* open a window */ //glutInit(NULL,NULL); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(750,750); glutInitWindowPosition(100,100); glutCreateWindow("Visual"); /* OpenGL init */ glClearColor(0, 0, 0, 0); /* set background color black*/ //glEnable(GL_DEPTH_TEST); /* callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); glutIdleFunc(idle); //glutIdleFunc(linein); glutCreateMenu(main_menu); glutAddMenuEntry("Fill/Outline", 1); glutAddMenuEntry("Full Resolution", 2); glutAddMenuEntry("Change color", 3); glutAddMenuEntry("Logarithmic color", 4); glutAddMenuEntry("Quit", 0); glutAttachMenu(GLUT_RIGHT_BUTTON); /* event handler */ glutMainLoop(); } void drawGrid(){ Grid alt = glSettings.alt, col = glSettings.col; int i, j, xres = glSettings.xres, yres = glSettings.yres; float colinc = 1.0 / (float)(col.maxval - col.minval); if (glSettings.fillmode) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } else { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } int xcoord = -1 * (alt.ncols - 1)/2, ycoord = alt.nrows / 2; for (i=0; i < alt.nrows - 1; i += yres) { xcoord = -1 * (alt.ncols - 1) / 2; if (i + yres >= alt.nrows) yres = alt.nrows - i - 1; glBegin(GL_TRIANGLE_STRIP); for (j=0; j < alt.ncols; j += xres) { eleColor(col.tbl[i][j], colinc); if (alt.tbl[i][j] != alt.nodataval) glVertex3f(xcoord, ycoord, (float)alt.tbl[i][j] * glSettings.mult); else glVertex3f(xcoord, ycoord, (float)alt.minval * glSettings.mult); eleColor(col.tbl[i+yres][j], colinc); if (alt.tbl[i+yres][j] != alt.nodataval) glVertex3f(xcoord, ycoord - yres, (float)alt.tbl[i+yres][j] * glSettings.mult); else glVertex3f(xcoord, ycoord - yres, (float)alt.minval * glSettings.mult); xcoord += xres; } glEnd(); ycoord -= yres; } } void eleColor(int ele, float colorstep) { if (ele == glSettings.col.nodataval) { glColor3fv(glSettings.nodatacolor); return; } //printf("eleinc in grid: %f\n",colorstep); int minval = glSettings.col.minval; float elevation = (float)(ele - minval) * colorstep, color[3] = {0,0,0}; if (glSettings.log) { elevation = log(10000. * elevation + 1.) / 9.211; } switch(glSettings.colorsetting) { default: glSettings.colorsetting = 0; case 0: color[0] = elevation * 2.; if (color[0] > 1.0) color[0] = 1.0; color[1] = 2. - elevation * 2.; if (color[1] > 1.0) color[1] = 1.0; break; case 1: color[1] = elevation * 2.; if (color[1] > 1.0) color[1] = 1.0; color[0] = 2. - elevation * 2.; if (color[0] > 1.0) color[0] = 1.0; break; case 2: color[0] = elevation * 2.; if (color[0] > 1.0) color[0] = 1.0; color[2] = 2. - elevation * 2.; if (color[2] > 1.0) color[2] = 1.0; break; case 3: color[1] = color[2] = elevation * 2.; if (color[1] > 1.0) color[1] = color[2] = 1.0; color[0] = 2. - elevation * 2.; if (color[0] > 1.0) color[0] = 1.0; break; case 4: color[0] = color[1] = color[2] = elevation; break; case 5: color[0] = color[1] = color[2] = 1.0 - elevation; break; } glColor3fv(color); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3fv(blue); /* set draw color blue */ glLoadIdentity(); float translation = (float)(glSettings.alt.maxval + glSettings.alt.minval)*glSettings.mult / 2., maxo2 = glSettings.max_side / 2., maxho2 = ((float)(glSettings.alt.maxval - glSettings.alt.minval) / 2); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho(- maxo2, maxo2, - maxo2, maxo2, maxo2, - 2 * maxo2 * glSettings.scale); glMatrixMode (GL_MODELVIEW); glTranslatef(glSettings.pos[0],glSettings.pos[1],glSettings.pos[2]); glRotatef(glSettings.rot[0],-1,0,0); glRotatef(glSettings.rot[2],0,0,-1); glRotatef(glSettings.rot[1],0,-1,0); glTranslatef(0,0, - translation); glScalef(glSettings.scale,glSettings.scale,glSettings.scale); drawGrid(); /* execute the drawing commands */ glFlush(); } void idle(void) { int repaint = 0; if (glSettings.rotA[0] != 0) { repaint = 1; if (glSettings.rot[0] < 0) glSettings.rot[0] = 360; if (glSettings.rot[0] > 360) glSettings.rot[0] = 0; glSettings.rot[0] -= 5.0 * (float)glSettings.rotA[0]; } if (glSettings.rotA[1] != 0) { repaint = 1; if (glSettings.rot[1] < 0) glSettings.rot[1] = 360; if (glSettings.rot[1] > 360) glSettings.rot[1] = 0; glSettings.rot[1] -= 5.0 * (float)glSettings.rotA[1]; } if (glSettings.rotA[2] != 0) { repaint = 1; if (glSettings.rot[2] < 0) glSettings.rot[2] = 360; if (glSettings.rot[2] > 360) glSettings.rot[2] = 0; glSettings.rot[2] -= 5.0 * (float)glSettings.rotA[2]; } if (repaint) glutPostRedisplay(); } void idleCmd() { char line[100]; printf("going"); if (asyncGetLine(line, 100) != 0) linecommand(line); } void keypress(unsigned char key, int x, int y) { switch(key) { case 'u': exit(0); break; case 'f': glSettings.fillmode = !glSettings.fillmode; break; case '+': if (glSettings.xres == 1 && glSettings.yres == 1) return; glSettings.xres = (float)glSettings.xres / 1.1; if (glSettings.xres < 1) glSettings.xres = 1; glSettings.yres = (float)glSettings.yres / 1.1; if (glSettings.yres < 1) glSettings.yres = 1; break; case '-': if (glSettings.xres < glSettings.alt.ncols) glSettings.xres = (float)glSettings.xres * 1.1; if (glSettings.yres < glSettings.alt.nrows) glSettings.yres = (float)glSettings.yres * 1.1; break; case 'l': glSettings.colorsetting++; break; case '0': glSettings.scale = 1.0; break; case '1': glSettings.scale -= .05; break; case '2': glSettings.pos[1] -= (float)glSettings.max_side / 50.; break; case '3': glSettings.pos[2] += (float)glSettings.max_side / 50.; break; case '4': glSettings.pos[0] -= (float)glSettings.max_side / 50.; break; case '5': glSettings.pos[0] = glSettings.pos[1] = glSettings.pos[2] = 0; break; case '6': glSettings.pos[0] += (float)glSettings.max_side / 50.; break; case '7': glSettings.scale += .05; break; case '8': glSettings.pos[1] += (float)glSettings.max_side / 50.; break; case '9': glSettings.pos[2] -= (float)glSettings.max_side / 50.; break; case 'z': glSettings.rotA[0] = 0; if (glSettings.rot[0] <= 0) glSettings.rot[0] = 360; glSettings.rot[0] -= 5.0; break; case 'Z': glSettings.rotA[0] = 1; break; case 'x': glSettings.rotA[0] = 0; glSettings.rot[0] = 0; break; case 'c': glSettings.rotA[0] = 0; if (glSettings.rot[0] >= 360) glSettings.rot[0] = 0; glSettings.rot[0] += 5.0; break; case 'C': glSettings.rotA[0] = -1; break; case 'a': glSettings.rotA[1] = 0; if (glSettings.rot[1] <= 0) glSettings.rot[1] = 360; glSettings.rot[1] -= 5.0; break; case 'A': glSettings.rotA[1] = 1; break; case 's': glSettings.rotA[1] = 0; glSettings.rot[1] = 0; break; case 'd': glSettings.rotA[1] = 0; if (glSettings.rot[1] >= 360) glSettings.rot[1] = 0; glSettings.rot[1] += 5.0; break; case 'D': glSettings.rotA[1] = -1; break; case 'q': glSettings.rotA[2] = 0; if (glSettings.rot[2] <= 0) glSettings.rot[2] = 360; glSettings.rot[2] -= 5.0; break; case 'Q': glSettings.rotA[2] = 1; break; case 'w': glSettings.rot[2] = 0; glSettings.rotA[2] = 0; break; case 'e': glSettings.rotA[2] = 0; if (glSettings.rot[2] >= 360) glSettings.rot[2] = 0; glSettings.rot[2] += 5.0; break; case 'E': glSettings.rotA[2] = -1; break; case 't': linecommand(); break; case 'g': glSettings.log = !glSettings.log; break; case 'm': glSettings.mult *= 1.10; break; case 'n': if (glSettings.mult / 1.10 > 0.0) glSettings.mult /= 1.10; break; case 'p': printf("Current Settings:\n"); printf("pos : (%f,%f,%f)\n", glSettings.pos[0], glSettings.pos[1], glSettings.pos[2]); printf("rot : (%f,%f,%f)\n", glSettings.rot[0], glSettings.rot[1], glSettings.rot[2]); printf("mult : %f\nscale : %f\n", glSettings.mult, glSettings.scale); return; default: return; break; } glutPostRedisplay(); } void main_menu(int value) { switch (value){ case 1: /* toggle outline/fill */ glSettings.fillmode = !glSettings.fillmode; break; case 2: glSettings.xres = 1; glSettings.yres = 1; break; case 3: glSettings.colorsetting++; break; case 4: glSettings.log = !glSettings.log; break; case 0: exit(0); default: return; } glutPostRedisplay(); }