blob: 79b5ab75c5751e09c77675ea3b19da3d2293eece [file] [log] [blame]
Brian Paulc13a0552003-03-29 16:42:57 +00001/* $Id: antialias.c,v 1.2 2003/03/29 16:42:57 brianp Exp $ */
Brian Paulaa80f052002-11-08 18:30:26 +00002
3/*
4 * Test multisampling and polygon smoothing.
5 *
6 * Brian Paul
7 * 4 November 2002
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <GL/glut.h>
14
15
16static GLfloat Zrot = 0;
17static GLboolean Anim = GL_TRUE;
18static GLboolean HaveMultisample = GL_TRUE;
19
20
21static void
22PrintString(const char *s)
23{
24 while (*s) {
25 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
26 s++;
27 }
28}
29
30
31static void
32Polygon( 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
44static void
45DrawObject( 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
81static void
82Display( 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 PrintString("MULTISAMPLE");
90 }
91 glRasterPos2f(-0.8, -1.6);
92 PrintString("No antialiasing");
93 glRasterPos2f(1.6, -1.6);
94 PrintString("GL_POLYGON_SMOOTH");
95
96 /* multisample */
97 if (HaveMultisample) {
98 glEnable(GL_DEPTH_TEST);
99 glEnable(GL_MULTISAMPLE_ARB);
100 glPushMatrix();
101 glTranslatef(-2.5, 0, 0);
102 glPushMatrix();
103 glRotatef(Zrot, 0, 0, 1);
104 DrawObject();
105 glPopMatrix();
106 glPopMatrix();
107 glDisable(GL_MULTISAMPLE_ARB);
108 glDisable(GL_DEPTH_TEST);
109 }
110
111 /* non-aa */
112 glEnable(GL_DEPTH_TEST);
113 glPushMatrix();
114 glTranslatef(0, 0, 0);
115 glPushMatrix();
116 glRotatef(Zrot, 0, 0, 1);
117 DrawObject();
118 glPopMatrix();
119 glPopMatrix();
120 glDisable(GL_DEPTH_TEST);
121
122 /* polygon smooth */
123 glEnable(GL_POLYGON_SMOOTH);
124 glEnable(GL_LINE_SMOOTH);
125 glEnable(GL_BLEND);
126 glPushMatrix();
127 glTranslatef(2.5, 0, 0);
128 glPushMatrix();
129 glRotatef(Zrot, 0, 0, 1);
130 DrawObject();
131 glPopMatrix();
132 glPopMatrix();
133 glDisable(GL_LINE_SMOOTH);
134 glDisable(GL_POLYGON_SMOOTH);
135 glDisable(GL_BLEND);
136
137 glutSwapBuffers();
138}
139
140
141static void
142Reshape( int width, int height )
143{
144 GLfloat ar = (float) width / (float) height;
145 glViewport( 0, 0, width, height );
146 glMatrixMode( GL_PROJECTION );
147 glLoadIdentity();
148 glOrtho(-2.0*ar, 2.0*ar, -2.0, 2.0, -1.0, 1.0);
149 glMatrixMode( GL_MODELVIEW );
150 glLoadIdentity();
151}
152
153
154static void
155Idle( void )
156{
157 Zrot = 0.01 * glutGet(GLUT_ELAPSED_TIME);
158 glutPostRedisplay();
159}
160
161
162static void
163Key( unsigned char key, int x, int y )
164{
165 const GLfloat step = 1.0;
166 (void) x;
167 (void) y;
168 switch (key) {
169 case 'a':
170 Anim = !Anim;
171 if (Anim)
172 glutIdleFunc(Idle);
173 else
174 glutIdleFunc(NULL);
175 break;
176 case 'z':
177 Zrot = (int) (Zrot - step);
178 break;
179 case 'Z':
180 Zrot = (int) (Zrot + step);
181 break;
182 case 27:
183 exit(0);
184 break;
185 }
186 glutPostRedisplay();
187}
188
189
190static void
191Init( void )
192{
193 /* GLUT imposes the four samples/pixel requirement */
194 int s;
195 glGetIntegerv(GL_SAMPLES_ARB, &s);
196 if (!glutExtensionSupported("GL_ARB_multisample") || s < 1) {
197 printf("Warning: multisample antialiasing not supported.\n");
198 HaveMultisample = GL_FALSE;
199 }
200 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
201 printf("GL_SAMPLES_ARB = %d\n", s);
202
203 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
204 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
205 glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
Brian Paulc13a0552003-03-29 16:42:57 +0000206
207 glGetIntegerv(GL_MULTISAMPLE_ARB, &s);
208 printf("GL_MULTISAMPLE_ARB = %d\n", s);
Brian Paulaa80f052002-11-08 18:30:26 +0000209}
210
211
212int
213main( int argc, char *argv[] )
214{
215 glutInit( &argc, argv );
216 glutInitWindowPosition( 0, 0 );
217 glutInitWindowSize( 600, 300 );
218 glutInitDisplayMode( GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE |
219 GLUT_DEPTH | GLUT_MULTISAMPLE );
220 glutCreateWindow(argv[0]);
221 glutReshapeFunc( Reshape );
222 glutKeyboardFunc( Key );
223 glutDisplayFunc( Display );
224 if (Anim)
225 glutIdleFunc( Idle );
226 Init();
227 glutMainLoop();
228 return 0;
229}