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