2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
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.
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.
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.
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
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.
48 #include "glut_wrap.h"
54 static float solidZ
= MAXZ
;
55 static float transparentZ
= MINZ
;
56 static GLuint sphereList
, cubeList
;
58 static void init(void)
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 };
64 glMaterialfv(GL_FRONT
, GL_SPECULAR
, mat_specular
);
65 glMaterialfv(GL_FRONT
, GL_SHININESS
, mat_shininess
);
66 glLightfv(GL_LIGHT0
, GL_POSITION
, position
);
68 glEnable(GL_LIGHTING
);
70 glEnable(GL_DEPTH_TEST
);
72 sphereList
= glGenLists(1);
73 glNewList(sphereList
, GL_COMPILE
);
74 glutSolidSphere (0.4, 16, 16);
77 cubeList
= glGenLists(1);
78 glNewList(cubeList
, GL_COMPILE
);
83 static void display(void)
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 };
90 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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
);
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
);
106 glDepthMask (GL_FALSE
);
107 glBlendFunc (GL_SRC_ALPHA
, GL_ONE
);
108 glCallList (cubeList
);
109 glDepthMask (GL_TRUE
);
110 glDisable (GL_BLEND
);
116 static void reshape(int w
, int h
)
118 glViewport(0, 0, (GLint
) w
, (GLint
) h
);
119 glMatrixMode(GL_PROJECTION
);
122 glOrtho (-1.5, 1.5, -1.5*(GLfloat
)h
/(GLfloat
)w
,
123 1.5*(GLfloat
)h
/(GLfloat
)w
, -10.0, 10.0);
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
);
131 static void animate(void)
133 static double t0
= -1.;
134 if (solidZ
<= MINZ
|| transparentZ
>= MAXZ
)
141 t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.;
147 transparentZ
+= ZINC
*dt
;
153 static void keyboard(unsigned char key
, int x
, int y
)
160 glutIdleFunc(animate
);
173 int main(int argc
, char** argv
)
175 glutInit(&argc
, argv
);
176 glutInitDisplayMode (GLUT_DOUBLE
| GLUT_RGB
| GLUT_DEPTH
);
177 glutInitWindowSize(500, 500);
178 glutCreateWindow(argv
[0]);
180 glutReshapeFunc(reshape
);
181 glutKeyboardFunc(keyboard
);
182 glutDisplayFunc(display
);