// Viewport --- Demonstration of using OpenGL viewports and // rendering text. // // Tom Kelliher, Feb. 5, 2001. #define ESC 0x1b #include #include #include void display(void); void reshape(int w, int h); void idle(void); void mouse(int btn, int action, int x, int y); void keyboard(unsigned char key, int x, int y); int gww = 200; // Window width. int gwh = 200; // Window height. int redraw = 1; // Flag for idle func. GLuint list; // Display list for polygons. int maintainAspectRatio = 0; int realtimeRedraw = 1; int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(gww, gwh); glutCreateWindow("Viewport Experiment"); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); list = glGenLists(1); // Get display list ID. glNewList(list, GL_COMPILE); // Create display list. glBegin(GL_POLYGON); glVertex2d(0.0, 0.0); glVertex2d(0.0, 5.0); glVertex2d(2.0, 5.0); glVertex2d(2.0, 0.0); glEnd(); glBegin(GL_POLYGON); glVertex2d(8.0, 8.0); glVertex2d(8.0, 10.0); glVertex2d(10.0, 10.0); glVertex2d(10.0, 8.0); glEnd(); glEndList(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(idle); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } // display --- Generate the polygons using different // viewports. Note the distortion when the aspect ratios // between the clipping rectangle and the viewport differ. void display(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3d(1.0, 1.0, 1.0); glViewport(0, 0, gww, gwh); glCallList(list); glColor3d(1.0, 0.0, 0.0); glViewport(0, 0, gww / 2, gwh / 2); glCallList(list); glColor3d(0.0, 1.0, 0.0); glViewport(gww / 2, gwh / 2, gww / 2, gwh / 2); glCallList(list); glColor3d(0.0, 0.0, 1.0); glViewport(0, gwh / 2, gww / 4, gwh / 2); glCallList(list); glColor3d(1.0, 1.0, 0.0); glViewport(gww / 2, 0, gww / 2, gwh / 4); glCallList(list); glFlush(); redraw = 1; // Tell idle to redraw the text. } // reshape --- Record the new window size and call display. void reshape(int w, int h) { if (maintainAspectRatio) gww = gwh = (w < h) ? w : h; else { gww = w; gwh = h; } if (realtimeRedraw) display(); else glutPostRedisplay(); } // idle --- Just a stupid demonstration showing how to draw // text. void idle(void) { static int count = 0; if (!redraw) return; printf("Redrawing text: %d.\n", ++count); redraw = 0; glColor3d(1.0, 1.0, 1.0); glViewport(0, 0, gww, gwh); glRasterPos2d(7.5, 5); // World coordinates. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'T'); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'o'); glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'm'); glFlush(); } void mouse(int btn, int action, int x, int y) { if (btn == GLUT_LEFT_BUTTON && action == GLUT_DOWN) maintainAspectRatio = (maintainAspectRatio + 1) % 2; if (btn == GLUT_RIGHT_BUTTON && action == GLUT_DOWN) realtimeRedraw = (realtimeRedraw + 1) % 2; } void keyboard(unsigned char key, int x, int y) { switch (key) { case ESC: exit(0); break; } }