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 / demos / shear.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.5 KB  |  159 lines

  1. /*
  2.  * Copyright 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. /* shear.c - open a window and clear the background.
  19.  *   glutMainLoop() calls drawScene whenever the window needs
  20.  *   to be updated.
  21.  */
  22. #include <GL/gl.h>
  23. #include <GL/glu.h>
  24. #include <GL/glut.h>
  25.  
  26. /* Function Prototypes */
  27. GLvoid initgfx( );
  28. GLvoid reshape( GLsizei, GLsizei );
  29. GLvoid drawScene( GLvoid );
  30. GLvoid keyboard( GLubyte, GLint, GLint );
  31.  
  32. /* Global Definitions */
  33.  
  34. #define KEY_ESC    27    /* ascii value for the escape key */
  35.  
  36. /* Global Variables */
  37.  
  38. GLboolean shearFlag = GL_FALSE;
  39.  
  40. void
  41. main( int argc, char *argv[] )
  42. {
  43.     GLsizei width, height;
  44.     glutInit(&argc, argv);
  45.  
  46.     width = glutGet(GLUT_SCREEN_WIDTH); 
  47.     height = glutGet(GLUT_SCREEN_HEIGHT);
  48.     glutInitWindowPosition( width / 4, height / 4); 
  49.     glutInitWindowSize( width / 2, height / 2 );
  50.     glutInitDisplayMode( GLUT_RGBA );
  51.     glutCreateWindow( argv[0] );
  52.  
  53.     initgfx( );
  54.  
  55.     glutReshapeFunc( reshape );
  56.     glutKeyboardFunc( keyboard );
  57.     glutDisplayFunc( drawScene ); 
  58.     glutMainLoop();
  59. }
  60.  
  61. GLvoid
  62. initgfx()
  63. {
  64.     /* set clear color to magenta */
  65.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  66. }
  67.  
  68. GLvoid 
  69. reshape( GLsizei w, GLsizei h )
  70. {
  71.     GLfloat aspect = (GLfloat)w / (GLfloat)h;
  72.     glViewport(0, 0, w, h);
  73.  
  74.     glMatrixMode(GL_PROJECTION);
  75.     glLoadIdentity();
  76.     gluOrtho2D( -3.0*aspect, 3.0*aspect, -3.0, 3.0);
  77.     glMatrixMode(GL_MODELVIEW);
  78. }
  79.     
  80. GLvoid
  81. renderBitmapString( void *font, char *string )
  82. {
  83.         int i;
  84.         int len = (int) strlen(string);
  85.         for (i = 0; i < len; i++) {
  86.                 glutBitmapCharacter(font, string[i]);
  87.         }
  88. }
  89.  
  90.     
  91. GLvoid 
  92. keyboard( GLubyte key, GLint x, GLint y )
  93. {
  94.     switch (key) {
  95.     case 's':    /* toggle shear mode */
  96.         shearFlag = !shearFlag;
  97.         glutPostRedisplay();
  98.         break;
  99.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  100.         exit(0);
  101.     }
  102. }
  103.  
  104. GLvoid
  105. drawScene( GLvoid )
  106. {
  107.     glClear( GL_COLOR_BUFFER_BIT );
  108.  
  109.     glPushMatrix(); {
  110.  
  111.         GLfloat shearMatrix[] = { 1, 0, 0, 0,
  112.                       1.5, 1, 0, 0,
  113.                       0, 0, 1, 0,
  114.                       0, 0, 0, 1 };
  115.  
  116.         glTranslatef(1.0, -0.5, 0.0);
  117.         XYaxes();
  118.  
  119.         if (shearFlag)
  120.             glMultMatrixf(shearMatrix);
  121.  
  122.         glColor3f(1.0, 0.5, 0.0);
  123.         glRectf(-1.0, -1.0, 1.0, 1.0);
  124.  
  125.     } glPopMatrix();
  126.  
  127.     glMatrixMode(GL_PROJECTION);
  128.     glPushMatrix(); {
  129.         GLsizei width, height;
  130.         char str[100];
  131.  
  132.         glLoadIdentity();
  133.         width = glutGet(GLUT_WINDOW_WIDTH); 
  134.         height = glutGet(GLUT_WINDOW_HEIGHT);
  135.         gluOrtho2D( 0.0, (GLfloat)width, 0.0, (GLfloat)height);
  136.  
  137.         glMatrixMode(GL_MODELVIEW);
  138.         glPushMatrix(); {
  139.             glLoadIdentity();
  140.             glColor3f(0.5, 0.5, 0.5);
  141.             glRasterPos2f(5.0, 25.0);
  142.             renderBitmapString(GLUT_BITMAP_8_BY_13, 
  143.                 "Hit 's' to toggle shear");
  144.             
  145.             if (shearFlag)
  146.                 sprintf(str, "Shearing...");
  147.             else
  148.                 sprintf(str, "Not shearing...");
  149.             glRasterPos2f(5.0, 5.0);
  150.             renderBitmapString(GLUT_BITMAP_8_BY_13, str);
  151.         } glPopMatrix();
  152.  
  153.         glMatrixMode(GL_PROJECTION);
  154.     } glPopMatrix();
  155.         
  156.     checkError( "drawScene" );
  157.     glFlush();
  158. }
  159.