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 "glut_wrap.h"
49
50#define MAXZ 8.0
51#define MINZ -8.0
52#define ZINC 4.
53
54static float solidZ = MAXZ;
55static float transparentZ = MINZ;
56static GLuint sphereList, cubeList;
57
58static 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
83static 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
116static 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
131static void animate(void)
132{
133   static double t0 = -1.;
134   if (solidZ <= MINZ || transparentZ >= MAXZ)
135   {
136      glutIdleFunc(NULL);
137      t0 = -1.;
138   }
139   else {
140      double t, dt;
141      t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
142      if (t0 < 0.)
143         t0 = t;
144      dt = t - t0;
145      t0 = t;
146      solidZ -= ZINC*dt;
147      transparentZ += ZINC*dt;
148      glutPostRedisplay();
149   }
150}
151
152/* ARGSUSED1 */
153static void keyboard(unsigned char key, int x, int y)
154{
155   switch (key) {
156      case 'a':
157      case 'A':
158         solidZ = MAXZ;
159         transparentZ = MINZ;
160         glutIdleFunc(animate);
161         break;
162      case 'r':
163      case 'R':
164         solidZ = MAXZ;
165         transparentZ = MINZ;
166         glutPostRedisplay();
167         break;
168      case 27:
169        exit(0);
170    }
171}
172
173int main(int argc, char** argv)
174{
175   glutInit(&argc, argv);
176   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
177   glutInitWindowSize(500, 500);
178   glutCreateWindow(argv[0]);
179   init();
180   glutReshapeFunc(reshape);
181   glutKeyboardFunc(keyboard);
182   glutDisplayFunc(display);
183   glutMainLoop();
184   return 0;
185}
186