/* grid1.c draws a white filled rectangle on a black background Laura Toma */ #include #include #include #include #ifdef __APPLE__ #include #else #include #endif int nrows = 472; int ncols = 391; float pos[3] = {0,0,0}; /* 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 */ /* callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); glEnable(GL_DEPTH_TEST); //not necessary in 2D /* event handler */ glutMainLoop(); return 0; } void display(void) { //clear the screen, need t oclear also teh depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //clear the matrix /* the default GL window is [-1,1]x[-1,1] with the origin in the center the points are in the range (0,0) to (ncols,nrows), so they need to be mapped to [-1,1]x [-1,1] */ //this make sit square, and we don;t want that //glScalef(2.0/ncols, 2.0/nrows, 1.0); //pick the maximum of (nrows, ncols) and scale with it //glScalef(2.0/nrows, 2.0/nrows, 1.0); glScalef(1.5/nrows, 1.5/nrows, 1.0); /* our grid is in upper right quadrant, so we need to shift it to get it centered */ glTranslatef(-ncols/2.0, -nrows/2.0, 0); //user translation glTranslatef(pos[0], pos[1], pos[2]); /* draw grid boundary polygon. we are rendering in the space [0,0] x [ncols, nrows] */ 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': pos[0] -= 5; //move left 5 cols glutPostRedisplay(); break; case 'r': pos[0] += 5; //move right 5 cols glutPostRedisplay(); break; case 'u': pos[1] += 5; //move up 5 rows glutPostRedisplay(); break; case 'd': pos[1] -= 5; //move down 5 rows glutPostRedisplay(); break; } }