home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / viewports / aspect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.3 KB  |  148 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* aspect.c - draw a grid, using glutReshapeFunc() to handle
  19.  *    window resizes (taking into account the aspect ratio).
  20.  *
  21.  *    Escape Key    - exit program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. /*  Function Prototypes  */
  30.  
  31. GLvoid  initgfx( GLvoid );
  32. GLvoid  keyboard( GLubyte, GLint, GLint );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  reshape( GLsizei, GLsizei );
  35.  
  36. void printHelp( char * );
  37.  
  38. /* Global Definitions */
  39.  
  40. #define KEY_ESC    27    /* ascii value for the escape key */
  41.  
  42. GLvoid
  43. main( int argc, char *argv[] )
  44. {
  45.     GLsizei width, height;
  46.  
  47.     glutInit( &argc, argv );
  48.  
  49.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  50.     height = glutGet( GLUT_SCREEN_HEIGHT );
  51.     glutInitWindowPosition( width / 4, height / 4 );
  52.     glutInitWindowSize( width / 2, height / 2 );
  53.     glutInitDisplayMode( GLUT_RGBA );
  54.     glutCreateWindow( argv[0] );
  55.  
  56.     initgfx();
  57.  
  58.     glutReshapeFunc( reshape );
  59.     glutKeyboardFunc( keyboard );
  60.     glutDisplayFunc( drawScene ); 
  61.  
  62.     printHelp( argv[0] );
  63.  
  64.     glutMainLoop();
  65. }
  66.  
  67. void
  68. printHelp( char *progname )
  69. {
  70.     fprintf(stdout, "\n%s - demonstrates one way to make the aspect "
  71.         "ratio of the viewport\nand the viewing volume match\n\n"
  72.         "Escape Key        - exit the program\n\n",
  73.         progname);
  74. }
  75.  
  76. GLvoid
  77. initgfx( GLvoid )
  78. {
  79.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  80.     glShadeModel( GL_FLAT );
  81. }
  82.  
  83. GLvoid
  84. reshape( GLsizei width, GLsizei height )
  85. {
  86.     GLdouble    aspect, left, right, bottom, top;
  87.  
  88.     glViewport( 0, 0, width, height );
  89.  
  90.     /* compute aspect ratio */
  91.     aspect = (GLdouble) width / (GLdouble) height;
  92.  
  93.     /* make sure the window goes from [-2.0, 2.0] in the
  94.        smallest dimension */
  95.     if ( aspect < 1.0 ) {
  96.         left = -2.0;
  97.         right = 2.0;
  98.         bottom = -2.0 * ( 1.0 / aspect );
  99.         top = 2.0 * ( 1.0 / aspect );
  100.     } else {
  101.         left = -2.0 * aspect;
  102.         right = 2.0 * aspect;
  103.         bottom = -2.0;
  104.         top = 2.0;
  105.     }
  106.  
  107.     glMatrixMode( GL_PROJECTION );
  108.     /* Reset world coordinates first ... */
  109.     glLoadIdentity();
  110.  
  111.     /* Then set them to what we want based on the new aspect ratio */
  112.     glOrtho( left, right, bottom, top, -1.0, 1.0 );
  113.     glMatrixMode( GL_MODELVIEW );
  114. }
  115.  
  116. GLvoid 
  117. keyboard( GLubyte key, GLint x, GLint y )
  118. {
  119.     switch (key) {
  120.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  121.         exit(0);
  122.     }
  123. }
  124.  
  125. GLvoid
  126. drawScene( GLvoid )
  127. {
  128.     GLfloat     x, y;
  129.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  130.  
  131.     glClear( GL_COLOR_BUFFER_BIT );
  132.  
  133.     glColor3fv( whiteColor );    
  134.  
  135.     glBegin( GL_LINES );
  136.     for ( x = -1.0; x < 1.1; x += 0.1 ) {
  137.         glVertex2f( x, -1.0 );
  138.         glVertex2f( x, 1.0 );
  139.     }
  140.     for ( y = -1.0; y < 1.1; y += 0.1 ) {
  141.         glVertex2f( -1.0, y );
  142.         glVertex2f( 1.0, y );
  143.     }
  144.     glEnd();
  145.  
  146.     glFlush();
  147. }
  148.