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 / alpha_blending / alpha.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.6 KB  |  348 lines

  1. /*
  2.  * Copyright 1995, 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. /* alpha.c - show how alpha values can be used for transparency
  18.  *
  19.  *  Escape key              - exit the program 
  20.  *  Left Mouse Button        - change incidence and azimuth angles
  21.  *  Middle Mousebutton        - change the twist angle based on
  22.  *                  horizontal mouse movement
  23.  *  Right Mousebutton        - zoom in and out based on vertical
  24.  *                  mouse movement
  25.  *  <b> key            - cycle through different blend functions
  26.  *  <R> key            - reset view
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31. #include <math.h>
  32. #include <stdio.h>    /* for printf */
  33.  
  34. /*  Function Prototypes  */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  drawScene( GLvoid );
  38. GLvoid  reshape( GLsizei, GLsizei );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40. GLvoid  mouse( GLint, GLint, GLint, GLint );
  41. GLvoid  motion( GLint, GLint );
  42. GLvoid  initialize( char * );
  43. GLvoid  blendFuncCycle( GLvoid );
  44.  
  45. void resetView( GLvoid );
  46. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  47. void printHelp( char * );
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. /* Global Variables */
  54.  
  55. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  56. static GLint        action;
  57.  
  58. static GLint        xStart = 0, yStart = 0;
  59.  
  60. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  61.  
  62. static void         *strokeFont;
  63.  
  64. GLvoid
  65. main( int argc, char *argv[] )
  66. {
  67.     GLsizei width, height;
  68.  
  69.     glutInit( &argc, argv );
  70.  
  71.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  72.     height = glutGet( GLUT_SCREEN_HEIGHT );
  73.     glutInitWindowPosition( 0, height / 4 );
  74.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  75.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  76.     glutCreateWindow( argv[0] );
  77.  
  78.     initgfx();
  79.  
  80.     glutMouseFunc( mouse );
  81.     glutMotionFunc( motion );
  82.     glutKeyboardFunc( keyboard );
  83.     glutReshapeFunc( reshape );
  84.     glutDisplayFunc( drawScene ); 
  85.  
  86.     printHelp( argv[0] );
  87.  
  88.     glutMainLoop();
  89. }
  90.  
  91. void
  92. printHelp( char *progname )
  93. {
  94.     fprintf(stdout, "\n%s - demonstrates alpha blending\n\n"
  95.         "Axes: X - red, Y - green, Z - blue\n\n"
  96.         "Left Mousebutton    - move eye position\n"
  97.         "Middle Mousebutton    - change twist angle\n"
  98.         "Right Mousebutton    - move up / down to zoom in / out\n"
  99.         "Escape Key        - exit the program\n"
  100.         "<b> Key        - cycle through 3 blend functions\n"
  101.         "<R> Key        - reset view\n",
  102.         progname);
  103. }
  104.  
  105. GLvoid
  106. initgfx( GLvoid )
  107. {
  108.     GLfloat maxObjectSize = 3.0;
  109.  
  110.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  111.     glShadeModel( GL_FLAT );
  112.  
  113.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  114.     /* and determine the viewing distance (adjust for zooming) */
  115.     near = 1.0;
  116.     far = near + 8*maxObjectSize; 
  117.  
  118.     resetView();
  119.  
  120.     /* set the initial blend function */
  121.     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  122.     fprintf(stdout, 
  123.         "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
  124.  
  125.     strokeFont = GLUT_STROKE_ROMAN;
  126. }
  127.  
  128. GLvoid
  129. reshape( GLsizei width, GLsizei height )
  130. {
  131.     GLdouble            aspect;
  132.  
  133.     glViewport( 0, 0, width, height );
  134.  
  135.     aspect = (GLdouble) width / (GLdouble) height;
  136.  
  137.     glMatrixMode( GL_PROJECTION );
  138.     glLoadIdentity();
  139.     gluPerspective( 45.0, aspect, near, far );
  140.     glMatrixMode( GL_MODELVIEW );
  141. }
  142.  
  143. GLvoid  
  144. blendFuncCycle( GLvoid )
  145. {
  146.     static int whichBlendFunc = 0;
  147.     
  148.     whichBlendFunc = (whichBlendFunc + 1) % 3;
  149.     
  150.     switch (whichBlendFunc)
  151.     {
  152.     case 0:
  153.         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  154.         fprintf(stdout, 
  155.             "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
  156.         break;
  157.     case 1:
  158.         glBlendFunc( GL_SRC_ALPHA, GL_ONE );
  159.         fprintf(stdout, 
  160.             "glBlendFunc( GL_SRC_ALPHA, GL_ONE )\n" );
  161.         break;
  162.     case 2:
  163.         glBlendFunc( GL_ONE, GL_ZERO );
  164.         fprintf(stdout, 
  165.             "glBlendFunc( GL_ONE, GL_ZERO )\n" );
  166.         break;
  167.     default:
  168.         break;
  169.     }
  170. }
  171.  
  172. GLvoid 
  173. keyboard( GLubyte key, GLint x, GLint y )
  174. {
  175.     switch (key) {
  176.     case 'b':    /* cycle through several blend funcs */
  177.         blendFuncCycle();
  178.         glutPostRedisplay();
  179.         break;
  180.     case 'R':
  181.         resetView();
  182.         glutPostRedisplay();
  183.         break;
  184.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  185.         exit(0);
  186.     }
  187. }
  188.  
  189. GLvoid 
  190. mouse( GLint button, GLint state, GLint x, GLint y )
  191. {
  192.     static GLint buttons_down = 0;
  193.  
  194.     if (state == GLUT_DOWN) {
  195.         switch (button) {
  196.         case GLUT_LEFT_BUTTON:
  197.             action = MOVE_EYE;
  198.             break;
  199.         case GLUT_MIDDLE_BUTTON:
  200.             action = TWIST_EYE;
  201.             break;
  202.         case GLUT_RIGHT_BUTTON:
  203.             action = ZOOM;
  204.             break;
  205.         }
  206.  
  207.         /* Update the saved mouse position */
  208.         xStart = x;
  209.         yStart = y;
  210.     } else {
  211.         if (--buttons_down == 0) 
  212.             action = MOVE_NONE;
  213.     }
  214.  
  215. }
  216.  
  217. GLvoid
  218. motion( GLint x, GLint y )
  219. {
  220.     switch (action) {
  221.     case MOVE_EYE:
  222.         /* Adjust the eye position based on the mouse position */
  223.         azimAngle += (GLdouble) (x - xStart);
  224.         incAngle -= (GLdouble) (y - yStart);
  225.         break;
  226.     case TWIST_EYE:
  227.         /* Adjust the eye twist based on the mouse position */
  228.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  229.         break;
  230.     case ZOOM:
  231.         /* Adjust the eye distance based on the mouse position */
  232.         distance -= (GLdouble) (y - yStart)/10.0;
  233.         break;
  234.     default:
  235.         printf("unknown action %d\n", action);
  236.     }
  237.     
  238.     /* Update the stored mouse position for later use */
  239.     xStart = x;
  240.     yStart = y;
  241.  
  242.     glutPostRedisplay();
  243. }
  244.  
  245. void
  246. resetView( GLvoid )
  247. {
  248.     distance = near + (far - near) / 2.0;
  249.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  250.     incAngle = 0.0;
  251.     azimAngle = 0.0;
  252. }
  253.  
  254. void
  255. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  256.             GLfloat twist)
  257. {
  258.     glTranslatef( 0.0, 0.0, -distance);
  259.     glRotatef( -twist, 0.0, 0.0, 1.0);
  260.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  261.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  262. }
  263. GLvoid
  264. renderStrokeString( void *font, char *string )
  265. {
  266.         int i;
  267.         int len = (int) strlen(string);
  268.         for (i = 0; i < len; i++) {
  269.                 glutStrokeCharacter(font, string[i]);
  270.         }
  271. }
  272.  
  273. void
  274. drawFrontRectangle( GLvoid )
  275. {
  276.     static GLfloat red[] = { 1.0, 0.0, 0.0, 0.75 };
  277.  
  278.     glPushMatrix();
  279.         glTranslatef( 0.0, 0.0, 0.1 );
  280.         glColor4fv( red );
  281.         glRectf( 0.0, 0.0, 2.0, 2.0 );
  282.     glPopMatrix();
  283.  
  284.     glPushMatrix();
  285.         glTranslatef( 1.0, 1.5, 0.1 );
  286.         glScalef(0.003, 0.003, 0.003);
  287.         glColor4f( 1.0, 1.0, 1.0, 0.75 );
  288.         renderStrokeString( strokeFont, "Front" );
  289.     glPopMatrix();
  290. }
  291.  
  292. void
  293. drawBackRectangle( GLvoid )
  294. {
  295.     static GLfloat blue[] = { 0.0, 0.0, 1.0, 0.75 };
  296.  
  297.     glColor4fv( blue );
  298.     glRectf( -1.0, -1.0, 1.0, 1.0 );
  299.  
  300.     glPushMatrix();
  301.         glTranslatef( -0.9, -0.9, 0.0 );
  302.         glScalef(0.003, 0.003, 0.003);
  303.         glColor4f( 1.0, 1.0, 1.0, 0.75 );
  304.         renderStrokeString( strokeFont, "Back" );
  305.     glPopMatrix();
  306. }
  307.  
  308.  
  309. GLvoid
  310. drawScene( GLvoid )
  311. {
  312.     glClear( GL_COLOR_BUFFER_BIT );
  313.  
  314.     glPushMatrix();
  315.         polarView( distance, azimAngle, incAngle, twistAngle );
  316.         XYZaxes();
  317.  
  318.         /* enable blending */
  319.         glEnable( GL_BLEND );
  320.  
  321.         /* draw partially overlapping rectangles */
  322.         glPushMatrix ();
  323.             glTranslatef( -3.0, 0.0, 0.0 );
  324.  
  325.             drawBackRectangle();
  326.             drawFrontRectangle();
  327.  
  328.         glPopMatrix ();
  329.  
  330.         /* draw partially overlapping rectangles again,
  331.          * but reverse the drawing order 
  332.          */
  333.         glPushMatrix ();
  334.             glTranslatef( 3.0, 0.0, 0.0 );
  335.  
  336.             drawFrontRectangle();
  337.             drawBackRectangle();
  338.  
  339.         glPopMatrix ();
  340.  
  341.         /* disable blending */
  342.         glDisable( GL_BLEND );
  343.  
  344.     glPopMatrix();
  345.  
  346.     glutSwapBuffers();
  347. }
  348.