home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / book / alpha3D.c < prev    next >
C/C++ Source or Header  |  1998-02-14  |  5KB  |  176 lines

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*
  39.  *  alpha3D.c
  40.  *  This program demonstrates how to intermix opaque and
  41.  *  alpha blended polygons in the same scene, by using 
  42.  *  glDepthMask.  Press the 'a' key to animate moving the 
  43.  *  transparent object through the opaque object.  Press 
  44.  *  the 'r' key to reset the scene.
  45.  */
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <GL/glut.h>
  49.  
  50. #define MAXZ 8.0
  51. #define MINZ -8.0
  52. #define ZINC 0.4
  53.  
  54. static float solidZ = MAXZ;
  55. static float transparentZ = MINZ;
  56. static GLuint sphereList, cubeList;
  57.  
  58. static void init(void)
  59. {
  60.    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
  61.    GLfloat mat_shininess[] = { 100.0 };
  62.    GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
  63.  
  64.    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  65.    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  66.    glLightfv(GL_LIGHT0, GL_POSITION, position);
  67.  
  68.    glEnable(GL_LIGHTING);
  69.    glEnable(GL_LIGHT0);
  70.    glEnable(GL_DEPTH_TEST);
  71.  
  72.    sphereList = glGenLists(1);
  73.    glNewList(sphereList, GL_COMPILE);
  74.       glutSolidSphere (0.4, 16, 16);
  75.    glEndList();
  76.  
  77.    cubeList = glGenLists(1);
  78.    glNewList(cubeList, GL_COMPILE);
  79.       glutSolidCube (0.6);
  80.    glEndList();
  81. }
  82.  
  83. void display(void)
  84. {
  85.    GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
  86.    GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
  87.    GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
  88.    GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
  89.  
  90.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  91.  
  92.    glPushMatrix ();
  93.       glTranslatef (-0.15, -0.15, solidZ);
  94.       glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
  95.       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
  96.       glCallList (sphereList);
  97.    glPopMatrix ();
  98.  
  99.    glPushMatrix ();
  100.       glTranslatef (0.15, 0.15, transparentZ);
  101.       glRotatef (15.0, 1.0, 1.0, 0.0);
  102.       glRotatef (30.0, 0.0, 1.0, 0.0);
  103.       glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  104.       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
  105.       glEnable (GL_BLEND);
  106.       glDepthMask (GL_FALSE);
  107.       glBlendFunc (GL_SRC_ALPHA, GL_ONE);
  108.       glCallList (cubeList);
  109.       glDepthMask (GL_TRUE);
  110.       glDisable (GL_BLEND);
  111.    glPopMatrix ();
  112.  
  113.    glutSwapBuffers();
  114. }
  115.  
  116. void reshape(int w, int h)
  117. {
  118.    glViewport(0, 0, (GLint) w, (GLint) h);
  119.    glMatrixMode(GL_PROJECTION);
  120.    glLoadIdentity();
  121.    if (w <= h)
  122.       glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
  123.                1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  124.    else
  125.       glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
  126.                1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
  127.    glMatrixMode(GL_MODELVIEW);
  128.    glLoadIdentity();
  129. }
  130.  
  131. void animate(void)
  132. {
  133.    if (solidZ <= MINZ || transparentZ >= MAXZ)
  134.       glutIdleFunc(NULL);
  135.    else {
  136.       solidZ -= ZINC;
  137.       transparentZ += ZINC;
  138.       glutPostRedisplay();
  139.    }
  140. }
  141.  
  142. /* ARGSUSED1 */
  143. void keyboard(unsigned char key, int x, int y)
  144. {
  145.    switch (key) {
  146.       case 'a':
  147.       case 'A':
  148.          solidZ = MAXZ;
  149.          transparentZ = MINZ;
  150.          glutIdleFunc(animate);
  151.          break;
  152.       case 'r':
  153.       case 'R':
  154.          solidZ = MAXZ;
  155.          transparentZ = MINZ;
  156.          glutPostRedisplay();
  157.          break;
  158.       case 27:
  159.         exit(0);
  160.     }
  161. }
  162.  
  163. int main(int argc, char** argv)
  164. {
  165.    glutInit(&argc, argv);
  166.    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  167.    glutInitWindowSize(500, 500);
  168.    glutCreateWindow(argv[0]);
  169.    init();
  170.    glutReshapeFunc(reshape);
  171.    glutKeyboardFunc(keyboard);
  172.    glutDisplayFunc(display);
  173.    glutMainLoop();
  174.    return 0;
  175. }
  176.