/* grid2.c render grid boundary (as 2 triangles) in 3D with a perspective projection can move left/right/up/down and rotate on key press Laura Toma */ #include #include #include #include #ifdef __APPLE__ #include #else #include #endif 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}; //keep track of global translation and rotation GLfloat pos[3] = {0,0,0}; GLfloat theta[3] = {0,0,0}; //these shoudl be read from grid file. hardcode them for now int nrows = 472; int ncols = 391; /* forward declarations of functions */ void display(void); void keypress(unsigned char key, int x, int y); int main(int argc, char** argv) { /* open a window */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); /* OpenGL init */ glClearColor(0, 0, 0, 0); /* set background color black*/ glColor3f(1.0,1.0,1.0); /* set draw color white */ glEnable(GL_DEPTH_TEST); //set up projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, 1 /* aspect */, 1, 10.0); /* the frustrum is from z=-1 to z=-10 */ /* camera is at (0,0,0) looking along negative y axis */ /* callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); /* event handler */ glutMainLoop(); return 0; } //render points are in object space [0, cols) x [0, ncols) and z in //[0,1]; careful with NODATA void draw_terrain() { //this is where you write code to draw a grid. Start with the //boundary. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_TRIANGLES); glColor3fv(red); glVertex3f(0,0, 10); glColor3fv(yellow); glVertex3f(0,nrows, 5); glColor3fv(green); glVertex3f(ncols, nrows, 7); glEnd(); glBegin(GL_TRIANGLES); glColor3fv(red); glVertex3f(0,0,10); glColor3fv(green); glVertex3f(ncols, nrows, 7); glColor3fv(blue); glVertex3f(ncols, 0, 9); glEnd(); } void display(void) { //clear the screen, need to clear also the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //clear the screen, need to clear also the depth buffer glLoadIdentity(); //clear the matrix //look from above (z=2) and back (y=-2) gluLookAt(0,-2,2, /* eye position */ 0,0,0, /* position of reference point indicating center of the scene*/ 0,0,1 /* direction of up vector */); //user modeling glTranslatef(pos[0], pos[1], pos[2]); glRotatef(theta[0], 1,0,0); glRotatef(theta[1], 0,1,0); glRotatef(theta[2], 0,0,1); //now draw the grid /* the GL window is [-1,1]x[-1,1] with the origin in the center. Our grid points are in the range (0,0) to (ncols,nrows), so they need to be mapped to [-1,1]x [-1,1] */ glScalef(1.8/nrows, 1.8/nrows, 1.0); /* our grid is in upper right quadrant, we need to shift it to get it centered in the middle of the window*/ glTranslatef(-ncols/2.0, -nrows/2.0, -2); /************** DRAW *****************/ //render points in object space [0, ncols) x [0, nrows) and z in [-1,1]; draw_terrain(); /* execute the drawing commands */ glFlush(); } void keypress(unsigned char key, int x, int y) { switch(key) { case 'q': exit(0); break; case 'l': //move left pos[0] -= .1; //move left 5 cols //glTranslatef(-5,0,0 ); glutPostRedisplay(); break; case 'r': //move right pos[0] += .1; //move right 5 cols // glTranslatef(5,0,0 ); glutPostRedisplay(); break; case 'u': //move up pos[1] += .1; //move up 5 rows //glTranslatef(0,5,0 ); glutPostRedisplay(); break; case 'd': //move down pos[1] -= .1; //move down 5 rows //glTranslatef(0,-5,0); glutPostRedisplay(); break; case 'f': pos[2] += .1; glutPostRedisplay(); break; case 'b': pos[2] -= .1; glutPostRedisplay(); break; case 'x': // glRotatef(5, 1,0,0); theta[0] +=5; glutPostRedisplay(); break; case 'X': theta[0] -= 5; glRotatef(-5, 1,0,0); glutPostRedisplay(); break; case 'y': theta[1] += 5; //glRotatef(5, 0,1,0); glutPostRedisplay(); break; case 'Y': theta[1] -= 5; //glRotatef(-5, 0,1,0); glutPostRedisplay(); break; case 'z': //rotate around z theta[2] += 5; /* we are in object system of coordinates; the default rotation is wrt the origin (ie lower left corner of the grid), but we want to rotate wrt the middle of the grid */ //glTranslatef(ncols/2, nrows/2, 0); //glRotatef(5, 0,0,1); //glTranslatef(-ncols/2, -nrows/2, 0); glutPostRedisplay(); break; case 'Z': //rotate around z theta[2] -= 5; //glTranslatef(ncols/2, nrows/2, 0); //glRotatef(5, 0,0,-1); //glTranslatef(-ncols/2, -nrows/2, 0); glutPostRedisplay(); break; } }