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 "glut_wrap.h" 14 15 16static GLfloat Zrot = 0; 17static GLboolean Anim = GL_TRUE; 18static GLboolean HaveMultisample = GL_TRUE; 19static GLboolean DoMultisample = GL_TRUE; 20 21 22static void 23PrintString(const char *s) 24{ 25 while (*s) { 26 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); 27 s++; 28 } 29} 30 31 32static void 33doPolygon( GLenum mode, GLint verts, GLfloat radius, GLfloat z ) 34{ 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); 42 } 43 glEnd(); 44} 45 46 47static void 48DrawObject( void ) 49{ 50 glLineWidth(3.0); 51 glColor3f(1, 1, 1); 52 doPolygon(GL_LINE_LOOP, 12, 1.2, 0); 53 54 glLineWidth(1.0); 55 glColor3f(1, 1, 1); 56 doPolygon(GL_LINE_LOOP, 12, 1.1, 0); 57 58 glColor3f(1, 0, 0); 59 doPolygon(GL_POLYGON, 12, 0.4, 0.3); 60 61 glColor3f(0, 1, 0); 62 doPolygon(GL_POLYGON, 12, 0.6, 0.2); 63 64 glColor3f(0, 0, 1); 65 doPolygon(GL_POLYGON, 12, 0.8, 0.1); 66 67 glColor3f(1, 1, 1); 68 doPolygon(GL_POLYGON, 12, 1.0, 0); 69} 70 71 72static void 73Display( void ) 74{ 75 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 76 77 glColor3f(1, 1, 1); 78 79 glRasterPos2f(-3.1, -1.6); 80 PrintString("No antialiasing"); 81 82 glRasterPos2f(-0.8, -1.6); 83 if (HaveMultisample) { 84 if (DoMultisample) 85 PrintString(" MULTISAMPLE"); 86 else 87 PrintString("MULTISAMPLE (off)"); 88 } 89 else 90 PrintString("MULTISAMPLE (N/A)"); 91 92 glRasterPos2f(1.6, -1.6); 93 PrintString("GL_POLYGON_SMOOTH"); 94 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); 105 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); 119 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); 134 135 glutSwapBuffers(); 136} 137 138 139static void 140Reshape( int width, int height ) 141{ 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(); 149} 150 151 152static void 153Idle( void ) 154{ 155 Zrot = 0.01 * glutGet(GLUT_ELAPSED_TIME); 156 glutPostRedisplay(); 157} 158 159 160static void 161Key( unsigned char key, int x, int y ) 162{ 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; 186 } 187 glutPostRedisplay(); 188} 189 190 191static void 192Init( void ) 193{ 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; 200 } 201 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 202 printf("GL_SAMPLES_ARB = %d\n", s); 203 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); 207 208 glGetIntegerv(GL_MULTISAMPLE_ARB, &s); 209 printf("GL_MULTISAMPLE_ARB = %d\n", s); 210} 211 212 213int 214main( int argc, char *argv[] ) 215{ 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; 231} 232