/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* viewport.c - Demonstrates how to separate a window into two drawing * areas using glViewport(). It creates two viewports that divide * the window in half. In each half, a rectangle the size of the * viewing volume is rendered. In one half it also renders a square * grid. Note how the mapping of the viewing volume to the viewport * affects the shape of the grid. * * SPACE key - toggle which viewport the grid is drawn in * Escape Key - exit program */ #include /* includes gl.h, glu.h */ #include /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid keyboard( GLubyte, GLint, GLint ); GLvoid drawScene( GLvoid ); void printHelp( char * ); /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ /* Global Variables */ static GLboolean grid_in_bottom = GL_TRUE; GLvoid main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( width / 4, height / 4 ); glutInitWindowSize( width / 2, height / 2 ); glutInitDisplayMode( GLUT_RGBA ); glutCreateWindow( argv[0] ); initgfx(); glutKeyboardFunc( keyboard ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glMatrixMode( GL_PROJECTION ); glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 ); glMatrixMode( GL_MODELVIEW ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - demonstrates how the viewport controls \n" "what portion of the window will be rendered into\n\n" "SPACE key - toggle which viewport the grid is drawn in\n" "Escape Key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { glClearColor( 1.0, 0.0, 1.0, 1.0 ); /* magenta */ glShadeModel( GL_FLAT ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case ' ': /* toggle grid location */ grid_in_bottom = !grid_in_bottom; glutPostRedisplay(); break; case KEY_ESC: /* Exit when the Escape key is pressed */ exit(0); } } /* Draw a square grid */ GLvoid drawSquareGrid( GLvoid ) { GLfloat x, y; glBegin( GL_LINES ); for ( x = -1.0f; x < 1.1f; x += 0.1f ) { glVertex2f( x, -1.0f ); glVertex2f( x, 1.0f ); } for ( y = -1.0f; y < 1.1f; y += 0.1f ) { glVertex2f( -1.0f, y ); glVertex2f( 1.0f, y ); } glEnd(); } GLvoid drawScene( GLvoid ) { GLsizei width, height; static GLfloat blueColor[] = { 0.0f, 0.0f, 1.0f }; static GLfloat greenColor[] = { 0.0f, 1.0f, 0.0f }; glClear( GL_COLOR_BUFFER_BIT ); /* Get the current window size */ width = glutGet(GLUT_WINDOW_WIDTH); height = glutGet(GLUT_WINDOW_HEIGHT); /* Create the first viewport - the bottom half of the screen */ glViewport( 0, 0, width, height / 2 ); /* Draw a blue rectangle that fills the entire viewing volume */ glColor3fv( blueColor ); glRectf( -2.0f, -2.0f, 2.0f, 2.0f ); if (grid_in_bottom) { glColor3f( 1.0f, 1.0f, 1.0f ); drawSquareGrid(); } /* Create the second viewport - the top half of the screen */ glViewport( 0, height / 2, width, height / 2 ); /* Draw a green rectangle that fills the entire viewing volume */ glColor3fv( greenColor ); glRectf( -2.0f, -2.0f, 2.0f, 2.0f ); if (!grid_in_bottom) { glColor3f( 0.0f, 0.0f, 0.0f ); drawSquareGrid(); } glFlush(); }