Merge branch '7.8'
[mesa.git] / progs / redbook / aapoly.c
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 * aapoly.c
40 * This program draws filled polygons with antialiased
41 * edges. The special GL_SRC_ALPHA_SATURATE blending
42 * function is used.
43 * Pressing the 't' key turns the antialiasing on and off.
44 */
45 #include <GL/glut.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48
49 GLboolean polySmooth = GL_TRUE;
50
51 static void init(void)
52 {
53 glCullFace (GL_BACK);
54 glEnable (GL_CULL_FACE);
55 glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
56 glClearColor (0.0, 0.0, 0.0, 0.0);
57 }
58
59 #define NFACE 6
60 #define NVERT 8
61 static void drawCube(GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1,
62 GLdouble z0, GLdouble z1)
63 {
64 static GLfloat v[8][3];
65 static GLfloat c[8][4] = {
66 {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
67 {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
68 {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
69 {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
70 };
71
72 /* indices of front, top, left, bottom, right, back faces */
73 static GLubyte indices[NFACE][4] = {
74 {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
75 {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
76 };
77
78 v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
79 v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
80 v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
81 v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
82 v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
83 v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
84
85 #ifdef GL_VERSION_1_1
86 glEnableClientState (GL_VERTEX_ARRAY);
87 glEnableClientState (GL_COLOR_ARRAY);
88 glVertexPointer (3, GL_FLOAT, 0, v);
89 glColorPointer (4, GL_FLOAT, 0, c);
90 glDrawElements (GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
91 glDisableClientState (GL_VERTEX_ARRAY);
92 glDisableClientState (GL_COLOR_ARRAY);
93 #else
94 printf ("If this is GL Version 1.0, ");
95 printf ("vertex arrays are not supported.\n");
96 exit(1);
97 #endif
98 }
99
100 /* Note: polygons must be drawn from front to back
101 * for proper blending.
102 */
103 static void display(void)
104 {
105 if (polySmooth) {
106 glClear (GL_COLOR_BUFFER_BIT);
107 glEnable (GL_BLEND);
108 glEnable (GL_POLYGON_SMOOTH);
109 glDisable (GL_DEPTH_TEST);
110 }
111 else {
112 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
113 glDisable (GL_BLEND);
114 glDisable (GL_POLYGON_SMOOTH);
115 glEnable (GL_DEPTH_TEST);
116 }
117
118 glPushMatrix ();
119 glTranslatef (0.0, 0.0, -8.0);
120 glRotatef (30.0, 1.0, 0.0, 0.0);
121 glRotatef (60.0, 0.0, 1.0, 0.0);
122 drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
123 glPopMatrix ();
124
125 glFlush ();
126 }
127
128 static void reshape(int w, int h)
129 {
130 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
131 glMatrixMode(GL_PROJECTION);
132 glLoadIdentity();
133 gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
134 glMatrixMode(GL_MODELVIEW);
135 glLoadIdentity();
136 }
137
138 /* ARGSUSED1 */
139 static void keyboard(unsigned char key, int x, int y)
140 {
141 switch (key) {
142 case 't':
143 case 'T':
144 polySmooth = !polySmooth;
145 glutPostRedisplay();
146 break;
147 case 27:
148 exit(0); /* Escape key */
149 break;
150 default:
151 break;
152 }
153 }
154
155 /* Main Loop
156 */
157 int main(int argc, char** argv)
158 {
159 glutInit(&argc, argv);
160 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
161 | GLUT_ALPHA | GLUT_DEPTH);
162 glutInitWindowSize(200, 200);
163 glutCreateWindow(argv[0]);
164 init ();
165 glutReshapeFunc (reshape);
166 glutKeyboardFunc (keyboard);
167 glutDisplayFunc (display);
168 glutMainLoop();
169 return 0;
170 }
171