Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / progs / tests / antialias.c
1
2 /*
3 * Test multisampling and polygon smoothing.
4 *
5 * Brian Paul
6 * 4 November 2002
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/glut.h>
13
14
15 static GLfloat Zrot = 0;
16 static GLboolean Anim = GL_TRUE;
17 static GLboolean HaveMultisample = GL_TRUE;
18 static GLboolean DoMultisample = GL_TRUE;
19
20
21 static void
22 PrintString(const char *s)
23 {
24 while (*s) {
25 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
26 s++;
27 }
28 }
29
30
31 static void
32 Polygon( GLint verts, GLfloat radius, GLfloat z )
33 {
34 int i;
35 for (i = 0; i < verts; i++) {
36 float a = (i * 2.0 * 3.14159) / verts;
37 float x = radius * cos(a);
38 float y = radius * sin(a);
39 glVertex3f(x, y, z);
40 }
41 }
42
43
44 static void
45 DrawObject( void )
46 {
47 glLineWidth(3.0);
48 glColor3f(1, 1, 1);
49 glBegin(GL_LINE_LOOP);
50 Polygon(12, 1.2, 0);
51 glEnd();
52
53 glLineWidth(1.0);
54 glColor3f(1, 1, 1);
55 glBegin(GL_LINE_LOOP);
56 Polygon(12, 1.1, 0);
57 glEnd();
58
59 glColor3f(1, 0, 0);
60 glBegin(GL_POLYGON);
61 Polygon(12, 0.4, 0.3);
62 glEnd();
63
64 glColor3f(0, 1, 0);
65 glBegin(GL_POLYGON);
66 Polygon(12, 0.6, 0.2);
67 glEnd();
68
69 glColor3f(0, 0, 1);
70 glBegin(GL_POLYGON);
71 Polygon(12, 0.8, 0.1);
72 glEnd();
73
74 glColor3f(1, 1, 1);
75 glBegin(GL_POLYGON);
76 Polygon(12, 1.0, 0);
77 glEnd();
78 }
79
80
81 static void
82 Display( void )
83 {
84 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
85
86 glColor3f(1, 1, 1);
87 if (HaveMultisample) {
88 glRasterPos2f(-3.1, -1.6);
89 if (DoMultisample)
90 PrintString("MULTISAMPLE");
91 else
92 PrintString("MULTISAMPLE (off)");
93 }
94 glRasterPos2f(-0.8, -1.6);
95 PrintString("No antialiasing");
96 glRasterPos2f(1.6, -1.6);
97 PrintString("GL_POLYGON_SMOOTH");
98
99 /* multisample */
100 if (HaveMultisample) {
101 glEnable(GL_DEPTH_TEST);
102 if (DoMultisample)
103 glEnable(GL_MULTISAMPLE_ARB);
104 glPushMatrix();
105 glTranslatef(-2.5, 0, 0);
106 glPushMatrix();
107 glRotatef(Zrot, 0, 0, 1);
108 DrawObject();
109 glPopMatrix();
110 glPopMatrix();
111 glDisable(GL_MULTISAMPLE_ARB);
112 glDisable(GL_DEPTH_TEST);
113 }
114
115 /* non-aa */
116 glEnable(GL_DEPTH_TEST);
117 glPushMatrix();
118 glTranslatef(0, 0, 0);
119 glPushMatrix();
120 glRotatef(Zrot, 0, 0, 1);
121 DrawObject();
122 glPopMatrix();
123 glPopMatrix();
124 glDisable(GL_DEPTH_TEST);
125
126 /* polygon smooth */
127 glEnable(GL_POLYGON_SMOOTH);
128 glEnable(GL_LINE_SMOOTH);
129 glEnable(GL_BLEND);
130 glPushMatrix();
131 glTranslatef(2.5, 0, 0);
132 glPushMatrix();
133 glRotatef(Zrot, 0, 0, 1);
134 DrawObject();
135 glPopMatrix();
136 glPopMatrix();
137 glDisable(GL_LINE_SMOOTH);
138 glDisable(GL_POLYGON_SMOOTH);
139 glDisable(GL_BLEND);
140
141 glutSwapBuffers();
142 }
143
144
145 static void
146 Reshape( int width, int height )
147 {
148 GLfloat ar = (float) width / (float) height;
149 glViewport( 0, 0, width, height );
150 glMatrixMode( GL_PROJECTION );
151 glLoadIdentity();
152 glOrtho(-2.0*ar, 2.0*ar, -2.0, 2.0, -1.0, 1.0);
153 glMatrixMode( GL_MODELVIEW );
154 glLoadIdentity();
155 }
156
157
158 static void
159 Idle( void )
160 {
161 Zrot = 0.01 * glutGet(GLUT_ELAPSED_TIME);
162 glutPostRedisplay();
163 }
164
165
166 static void
167 Key( unsigned char key, int x, int y )
168 {
169 const GLfloat step = 1.0;
170 (void) x;
171 (void) y;
172 switch (key) {
173 case 'a':
174 Anim = !Anim;
175 if (Anim)
176 glutIdleFunc(Idle);
177 else
178 glutIdleFunc(NULL);
179 break;
180 case 'm':
181 DoMultisample = !DoMultisample;
182 break;
183 case 'z':
184 Zrot = (int) (Zrot - step);
185 break;
186 case 'Z':
187 Zrot = (int) (Zrot + step);
188 break;
189 case 27:
190 exit(0);
191 break;
192 }
193 glutPostRedisplay();
194 }
195
196
197 static void
198 Init( void )
199 {
200 /* GLUT imposes the four samples/pixel requirement */
201 int s;
202 glGetIntegerv(GL_SAMPLES_ARB, &s);
203 if (!glutExtensionSupported("GL_ARB_multisample") || s < 1) {
204 printf("Warning: multisample antialiasing not supported.\n");
205 HaveMultisample = GL_FALSE;
206 }
207 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
208 printf("GL_SAMPLES_ARB = %d\n", s);
209
210 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
211 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
212 glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
213
214 glGetIntegerv(GL_MULTISAMPLE_ARB, &s);
215 printf("GL_MULTISAMPLE_ARB = %d\n", s);
216 }
217
218
219 int
220 main( int argc, char *argv[] )
221 {
222 glutInit( &argc, argv );
223 glutInitWindowPosition( 0, 0 );
224 glutInitWindowSize( 600, 300 );
225 glutInitDisplayMode( GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE |
226 GLUT_DEPTH | GLUT_MULTISAMPLE );
227 glutCreateWindow(argv[0]);
228 glutReshapeFunc( Reshape );
229 glutKeyboardFunc( Key );
230 glutDisplayFunc( Display );
231 if (Anim)
232 glutIdleFunc( Idle );
233 Init();
234 glutMainLoop();
235 return 0;
236 }