/* grid12.c same as grid11 + handle rotation around z draw the boundary of the grid on screen; can move left/right/up/down via instant calls to glTranslatef(); can rotate via instant calls to glRotate() Laura Toma */ #include #include #include #include #ifdef __APPLE__ #include #else #include #endif //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); //not necessary in 2D /* callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); /***** INITIALIZE DISPLAY ***********/ //clear the screen, need t oclear also the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //clear the matrix /* Transform to GL coordinates: 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(2.0/ncols, 2.0/nrows, 1.0); //this makes it square, we don't want that. We want to keep the //aspect ratio of the grid //pick the maximum of (nrows, ncols) and scale with it //glScalef(2.0/nrows, 2.0/nrows, 1.0); //this stretches it to the whole window glScalef(1.5/nrows, 1.5/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, 0); /* event handler */ glutMainLoop(); return 0; } void display(void) { //clear the screen, need to clear also the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /************** DRAW *****************/ /* draw grid boundary polygon. we are rendering in object space, which is [0,0] x [ncols, nrows] */ //this is where you write code to draw a grid. Start with the //boundary. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glBegin(GL_POLYGON); glVertex2f(0,0); glVertex2f(0,nrows); glVertex2f(ncols, nrows); glVertex2f(ncols, 0); glEnd(); /* 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] -= 5; //move left 5 cols glTranslatef(-5,0,0 ); glutPostRedisplay(); break; case 'r': //move right //pos[0] += 5; //move right 5 cols glTranslatef(5,0,0 ); glutPostRedisplay(); break; case 'u': //move up //pos[1] += 5; //move up 5 rows glTranslatef(0,5,0 ); glutPostRedisplay(); break; case 'd': //move down //pos[1] -= 5; //move down 5 rows glTranslatef(0,-5,0); glutPostRedisplay(); break; case 'z': //rotate around z /* 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 glTranslatef(ncols/2, nrows/2, 0); glRotatef(5, 0,0,-1); glTranslatef(-ncols/2, -nrows/2, 0); glutPostRedisplay(); break; } }