home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / irisgl_vs_opengl / polygonMode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.6 KB  |  161 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. /* polygonMode.c  - open a window, clear the background, and render
  19.  *     a polygon using different polygon modes
  20.  *
  21.  *    Escape key    - exit the program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <math.h>
  28. #include <stdio.h>
  29.  
  30. /* Function Prototypes */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  reshape( GLsizei, GLsizei );
  35. GLvoid  keyboard( GLubyte, GLint, GLint );
  36.  
  37. void printHelp( char * );
  38.  
  39. /* Global Definitions */
  40.  
  41. #define KEY_ESC    27    /* ascii value for the escape key */
  42.  
  43. void
  44. main( int argc, char *argv[] )
  45. {
  46.     GLsizei     width, height;
  47.  
  48.     glutInit( &argc, argv );
  49.  
  50.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  51.     height = glutGet( GLUT_SCREEN_HEIGHT );
  52.     glutInitWindowPosition( width/4, height/4 ); 
  53.     glutInitWindowSize( width/2, height/2 );
  54.     glutInitDisplayMode( GLUT_RGBA );
  55.     glutCreateWindow( argv[0] );
  56.     
  57.     initgfx();
  58.  
  59.     glutKeyboardFunc( keyboard );
  60.     glutReshapeFunc( reshape );
  61.     glutDisplayFunc( drawScene ); 
  62.  
  63.     printHelp( argv[0] );
  64.  
  65.     glutMainLoop();
  66. }
  67.  
  68. GLvoid
  69. printHelp( char *progname )
  70. {
  71.     fprintf(stdout, "\n%s - demonstrates polygon modes\n\n"
  72.         "Escape key        - exit the program\n\n",
  73.         progname
  74.     );
  75. }
  76.  
  77. GLvoid
  78. initgfx( GLvoid )
  79. {
  80.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  81.  
  82.     /* make points and lines really big 
  83.      * so that they will be easy to see 
  84.      */
  85.     glLineWidth( 3.5 );
  86.     glPointSize( 8.5 );
  87. }
  88.  
  89. GLvoid 
  90. keyboard( GLubyte key, GLint x, GLint y )
  91. {
  92.     switch (key) {
  93.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  94.         exit(0);
  95.     }
  96. }
  97.  
  98. GLvoid
  99. reshape( GLsizei width, GLsizei height )
  100. {
  101.     GLdouble    aspect;
  102.  
  103.     glViewport( 0, 0, width, height );
  104.  
  105.     aspect = (GLdouble) width / (GLdouble) height;
  106.  
  107.     glMatrixMode( GL_PROJECTION );
  108.     glLoadIdentity();
  109.         glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  110.     glMatrixMode( GL_MODELVIEW );
  111.     glLoadIdentity();
  112. }
  113.  
  114. GLvoid
  115. drawScene( GLvoid )
  116. {
  117.     static GLfloat purpleColor[] = { 1.0, 0.0, 1.0 };
  118.     static GLfloat v[] = { 0.5, 0.1 };
  119.  
  120.     glClear( GL_COLOR_BUFFER_BIT );
  121.  
  122.     /* Draw a purple filled polygon */
  123.     glColor3fv( purpleColor );
  124.     glBegin( GL_POLYGON );
  125.       glVertex2fv( v );
  126.       glVertex2f( 1.0, 0.4 );
  127.       glVertex2f( 0.9, 1.0 );
  128.       glVertex2f( 0.3, 0.8 );
  129.       glVertex2f( 0.1, 0.5 );
  130.     glEnd();
  131.  
  132.     /* draw the back of this polygon (vertices specified in 
  133.      * clockwise order) using line mode */
  134.     glPolygonMode( GL_BACK, GL_LINE );
  135.         glColor3f( 1.0, 1.0, 1.0 );
  136.     glBegin( GL_POLYGON );
  137.       glVertex2f( 0.1, 0.5 );
  138.       glVertex2f( 0.3, 0.8 );
  139.       glVertex2f( 0.9, 1.0 );
  140.       glVertex2f( 1.0, 0.4 );
  141.       glVertex2fv( v );
  142.     glEnd();
  143.  
  144.     /* change to clockwise, front-facing polygons;
  145.      * draw front facing polygons as points */
  146.     glFrontFace( GL_CW );
  147.     glPolygonMode( GL_FRONT, GL_POINT );
  148.     glBegin( GL_POLYGON );
  149.       glVertex2f( -1.0, 1.0 );
  150.       glVertex2f( 1.0, 1.0 );
  151.       glVertex2f( 1.0, -1.0 );
  152.       glVertex2f( -1.0, -1.0 );
  153.     glEnd();
  154.  
  155.     /* return to default modes */
  156.     glFrontFace( GL_CCW );
  157.     glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  158.  
  159.     glFlush();
  160. }
  161.