Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

antialias.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* $Id: antialias.c,v 1.2 2003/03/29 16:42:57 brianp Exp $ */
  2. /*
  3. * Test multisampling and polygon smoothing.
  4. *
  5. * Brian Paul
  6. * 4 November 2002
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <GL/glut.h>
  12. static GLfloat Zrot = 0;
  13. static GLboolean Anim = GL_TRUE;
  14. static GLboolean HaveMultisample = GL_TRUE;
  15. static void
  16. PrintString(const char *s)
  17. {
  18. while (*s) {
  19. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  20. s++;
  21. }
  22. }
  23. static void
  24. Polygon( GLint verts, GLfloat radius, GLfloat z )
  25. {
  26. int i;
  27. for (i = 0; i < verts; i++) {
  28. float a = (i * 2.0 * 3.14159) / verts;
  29. float x = radius * cos(a);
  30. float y = radius * sin(a);
  31. glVertex3f(x, y, z);
  32. }
  33. }
  34. static void
  35. DrawObject( void )
  36. {
  37. glLineWidth(3.0);
  38. glColor3f(1, 1, 1);
  39. glBegin(GL_LINE_LOOP);
  40. Polygon(12, 1.2, 0);
  41. glEnd();
  42. glLineWidth(1.0);
  43. glColor3f(1, 1, 1);
  44. glBegin(GL_LINE_LOOP);
  45. Polygon(12, 1.1, 0);
  46. glEnd();
  47. glColor3f(1, 0, 0);
  48. glBegin(GL_POLYGON);
  49. Polygon(12, 0.4, 0.3);
  50. glEnd();
  51. glColor3f(0, 1, 0);
  52. glBegin(GL_POLYGON);
  53. Polygon(12, 0.6, 0.2);
  54. glEnd();
  55. glColor3f(0, 0, 1);
  56. glBegin(GL_POLYGON);
  57. Polygon(12, 0.8, 0.1);
  58. glEnd();
  59. glColor3f(1, 1, 1);
  60. glBegin(GL_POLYGON);
  61. Polygon(12, 1.0, 0);
  62. glEnd();
  63. }
  64. static void
  65. Display( void )
  66. {
  67. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  68. glColor3f(1, 1, 1);
  69. if (HaveMultisample) {
  70. glRasterPos2f(-3.1, -1.6);
  71. PrintString("MULTISAMPLE");
  72. }
  73. glRasterPos2f(-0.8, -1.6);
  74. PrintString("No antialiasing");
  75. glRasterPos2f(1.6, -1.6);
  76. PrintString("GL_POLYGON_SMOOTH");
  77. /* multisample */
  78. if (HaveMultisample) {
  79. glEnable(GL_DEPTH_TEST);
  80. glEnable(GL_MULTISAMPLE_ARB);
  81. glPushMatrix();
  82. glTranslatef(-2.5, 0, 0);
  83. glPushMatrix();
  84. glRotatef(Zrot, 0, 0, 1);
  85. DrawObject();
  86. glPopMatrix();
  87. glPopMatrix();
  88. glDisable(GL_MULTISAMPLE_ARB);
  89. glDisable(GL_DEPTH_TEST);
  90. }
  91. /* non-aa */
  92. glEnable(GL_DEPTH_TEST);
  93. glPushMatrix();
  94. glTranslatef(0, 0, 0);
  95. glPushMatrix();
  96. glRotatef(Zrot, 0, 0, 1);
  97. DrawObject();
  98. glPopMatrix();
  99. glPopMatrix();
  100. glDisable(GL_DEPTH_TEST);
  101. /* polygon smooth */
  102. glEnable(GL_POLYGON_SMOOTH);
  103. glEnable(GL_LINE_SMOOTH);
  104. glEnable(GL_BLEND);
  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_LINE_SMOOTH);
  113. glDisable(GL_POLYGON_SMOOTH);
  114. glDisable(GL_BLEND);
  115. glutSwapBuffers();
  116. }
  117. static void
  118. Reshape( int width, int height )
  119. {
  120. GLfloat ar = (float) width / (float) height;
  121. glViewport( 0, 0, width, height );
  122. glMatrixMode( GL_PROJECTION );
  123. glLoadIdentity();
  124. glOrtho(-2.0*ar, 2.0*ar, -2.0, 2.0, -1.0, 1.0);
  125. glMatrixMode( GL_MODELVIEW );
  126. glLoadIdentity();
  127. }
  128. static void
  129. Idle( void )
  130. {
  131. Zrot = 0.01 * glutGet(GLUT_ELAPSED_TIME);
  132. glutPostRedisplay();
  133. }
  134. static void
  135. Key( unsigned char key, int x, int y )
  136. {
  137. const GLfloat step = 1.0;
  138. (void) x;
  139. (void) y;
  140. switch (key) {
  141. case 'a':
  142. Anim = !Anim;
  143. if (Anim)
  144. glutIdleFunc(Idle);
  145. else
  146. glutIdleFunc(NULL);
  147. break;
  148. case 'z':
  149. Zrot = (int) (Zrot - step);
  150. break;
  151. case 'Z':
  152. Zrot = (int) (Zrot + step);
  153. break;
  154. case 27:
  155. exit(0);
  156. break;
  157. }
  158. glutPostRedisplay();
  159. }
  160. static void
  161. Init( void )
  162. {
  163. /* GLUT imposes the four samples/pixel requirement */
  164. int s;
  165. glGetIntegerv(GL_SAMPLES_ARB, &s);
  166. if (!glutExtensionSupported("GL_ARB_multisample") || s < 1) {
  167. printf("Warning: multisample antialiasing not supported.\n");
  168. HaveMultisample = GL_FALSE;
  169. }
  170. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  171. printf("GL_SAMPLES_ARB = %d\n", s);
  172. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  173. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  174. glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
  175. glGetIntegerv(GL_MULTISAMPLE_ARB, &s);
  176. printf("GL_MULTISAMPLE_ARB = %d\n", s);
  177. }
  178. int
  179. main( int argc, char *argv[] )
  180. {
  181. glutInit( &argc, argv );
  182. glutInitWindowPosition( 0, 0 );
  183. glutInitWindowSize( 600, 300 );
  184. glutInitDisplayMode( GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE |
  185. GLUT_DEPTH | GLUT_MULTISAMPLE );
  186. glutCreateWindow(argv[0]);
  187. glutReshapeFunc( Reshape );
  188. glutKeyboardFunc( Key );
  189. glutDisplayFunc( Display );
  190. if (Anim)
  191. glutIdleFunc( Idle );
  192. Init();
  193. glutMainLoop();
  194. return 0;
  195. }