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