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