OSDN Git Service

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