Merge branch '7.8'
[mesa.git] / progs / redbook / multisamp.c
1 /*
2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright
8 * notice and this permission notice appear in supporting documentation,
9 * and that the name of Silicon Graphics, Inc. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF
22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
24 * OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
31 * in the FAR or the DOD or NASA FAR Supplement. Unpublished - rights
32 * reserved under the copyright laws of the United States.
33 *
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
39 *
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 */
42
43 /*
44 * multisamp.c
45 * This program draws shows how to use multisampling to
46 * draw anti-aliased geometric primitives. The same
47 * display list, a pinwheel of triangles and lines of
48 * varying widths, is rendered twice. Multisampling is
49 * enabled when the left side is drawn. Multisampling is
50 * disabled when the right side is drawn.
51 *
52 * Pressing the 'b' key toggles drawing of the checkerboard
53 * background. Antialiasing is sometimes easier to see
54 * when objects are rendered over a contrasting background.
55 */
56 #include <GL/glut.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59
60 static int bgtoggle = 1;
61
62 /*
63 * Print out state values related to multisampling.
64 * Create display list with "pinwheel" of lines and
65 * triangles.
66 */
67 static void init(void)
68 {
69 static GLint buf[1], sbuf[1];
70 int i, j;
71
72 glClearColor(0.0, 0.0, 0.0, 0.0);
73 glGetIntegerv (GL_SAMPLE_BUFFERS_ARB, buf);
74 printf ("number of sample buffers is %d\n", buf[0]);
75 glGetIntegerv (GL_SAMPLES_ARB, sbuf);
76 printf ("number of samples is %d\n", sbuf[0]);
77
78 glNewList (1, GL_COMPILE);
79 for (i = 0; i < 19; i++) {
80 glPushMatrix();
81 glRotatef(360.0*(float)i/19.0, 0.0, 0.0, 1.0);
82 glColor3f (1.0, 1.0, 1.0);
83 glLineWidth((i%3)+1.0);
84 glBegin (GL_LINES);
85 glVertex2f (0.25, 0.05);
86 glVertex2f (0.9, 0.2);
87 glEnd ();
88 glColor3f (0.0, 1.0, 1.0);
89 glBegin (GL_TRIANGLES);
90 glVertex2f (0.25, 0.0);
91 glVertex2f (0.9, 0.0);
92 glVertex2f (0.875, 0.10);
93 glEnd ();
94 glPopMatrix();
95 }
96 glEndList ();
97
98 glNewList (2, GL_COMPILE);
99 glColor3f (1.0, 0.5, 0.0);
100 glBegin (GL_QUADS);
101 for (i = 0; i < 16; i++) {
102 for (j = 0; j < 16; j++) {
103 if (((i + j) % 2) == 0) {
104 glVertex2f (-2.0 + (i * 0.25), -2.0 + (j * 0.25));
105 glVertex2f (-2.0 + (i * 0.25), -1.75 + (j * 0.25));
106 glVertex2f (-1.75 + (i * 0.25), -1.75 + (j * 0.25));
107 glVertex2f (-1.75 + (i * 0.25), -2.0 + (j * 0.25));
108 }
109 }
110 }
111 glEnd ();
112 glEndList ();
113 }
114
115 /* Draw two sets of primitives, so that you can
116 * compare the user of multisampling against its absence.
117 *
118 * This code enables antialiasing and draws one display list
119 * and disables and draws the other display list
120 */
121 static void display(void)
122 {
123 glClear(GL_COLOR_BUFFER_BIT);
124
125 if (bgtoggle)
126 glCallList (2);
127
128 glEnable (GL_MULTISAMPLE_ARB);
129 glPushMatrix();
130 glTranslatef (-1.0, 0.0, 0.0);
131 glCallList (1);
132 glPopMatrix();
133
134 glDisable (GL_MULTISAMPLE_ARB);
135 glPushMatrix();
136 glTranslatef (1.0, 0.0, 0.0);
137 glCallList (1);
138 glPopMatrix();
139 glutSwapBuffers();
140 }
141
142 static void reshape(int w, int h)
143 {
144 glViewport(0, 0, w, h);
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 if (w <= (2 * h))
148 gluOrtho2D (-2.0, 2.0,
149 -2.0*(GLfloat)h/(GLfloat)w, 2.0*(GLfloat)h/(GLfloat)w);
150 else
151 gluOrtho2D (-2.0*(GLfloat)w/(GLfloat)h,
152 2.0*(GLfloat)w/(GLfloat)h, -2.0, 2.0);
153 glMatrixMode(GL_MODELVIEW);
154 glLoadIdentity();
155 }
156
157 static void keyboard(unsigned char key, int x, int y)
158 {
159 switch (key) {
160 case 'b':
161 case 'B':
162 bgtoggle = !bgtoggle;
163 glutPostRedisplay();
164 break;
165 case 27: /* Escape Key */
166 exit(0);
167 default:
168 break;
169 }
170 }
171
172 /* Main Loop
173 * Open window with initial window size, title bar,
174 * RGBA display mode, and handle input events.
175 */
176 int main(int argc, char** argv)
177 {
178 glutInit(&argc, argv);
179 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE);
180 glutInitWindowSize (600, 300);
181 glutCreateWindow (argv[0]);
182 init();
183 glutReshapeFunc (reshape);
184 glutKeyboardFunc (keyboard);
185 glutDisplayFunc (display);
186 glutMainLoop();
187 return 0;
188 }