home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / gdemos / trispd.c < prev   
Encoding:
C/C++ Source or Header  |  1998-01-31  |  4.1 KB  |  183 lines

  1. /* trispd.c */
  2.  
  3. /*
  4.  * Simple GLUT program to measure triangle strip rendering speed.
  5.  * Brian Paul  February 15, 1997
  6.  */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. #include <sys/times.h>
  14. #include <sys/param.h>
  15.  
  16.  
  17. static float MinPeriod = 2.0;   /* 2 seconds */
  18. static float Width = 400.0;
  19. static float Height = 400.0;
  20. static int Loops = 1;
  21. static int Size = 50;
  22.  
  23.  
  24.  
  25. static void Idle( void )
  26. {
  27.    glutPostRedisplay();
  28. }
  29.  
  30.  
  31. static void Display( void )
  32. {
  33.    float x, y;
  34.    float xStep;
  35.    float yStep;
  36.    double t0, t1;
  37.    double triRate;
  38.    double pixelRate;
  39.    int triCount;
  40.    int i;
  41.    float red[3] = { 1.0, 0.0, 0.0 };
  42.    float blue[3] = { 0.0, 0.0, 1.0 };
  43.  
  44.    xStep = yStep = sqrt( 2.0 * Size );
  45.  
  46.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  47.  
  48.    triCount = 0;
  49.    t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  50.    for (i=0; i<Loops; i++) {
  51.       for (y=1.0; y<Height-yStep; y+=yStep) {
  52.          glBegin(GL_TRIANGLE_STRIP);
  53.          for (x=1.0; x<Width; x+=xStep) {
  54.             glColor3fv(red);
  55.             glVertex2f(x, y);
  56.             glColor3fv(blue);
  57.             glVertex2f(x, y+yStep);
  58.             triCount += 2;
  59.          }
  60.          glEnd();
  61.          triCount -= 2;
  62.       }
  63.    }
  64.    t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  65.  
  66.    if (t1-t0 < MinPeriod) {
  67.       /* Next time draw more triangles to get longer elapsed time */
  68.       Loops *= 2;
  69.       return;
  70.    }
  71.  
  72.    triRate = triCount / (t1-t0);
  73.    pixelRate = triRate * Size;
  74.    printf("Rate: %d tri in %gs = %g tri/s  %d pixels/s\n",
  75.           triCount, t1-t0, triRate, (int)pixelRate);
  76.  
  77.    glutSwapBuffers();
  78. }
  79.  
  80.  
  81. static void Reshape( int width, int height )
  82. {
  83.    Width = width;
  84.    Height = height;
  85.    glViewport( 0, 0, width, height );
  86.    glMatrixMode( GL_PROJECTION );
  87.    glLoadIdentity();
  88.    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
  89.    glMatrixMode( GL_MODELVIEW );
  90.    glLoadIdentity();
  91. }
  92.  
  93.  
  94. static void Key( unsigned char key, int x, int y )
  95. {
  96.    switch (key) {
  97.       case 27:
  98.          exit(0);
  99.          break;
  100.    }
  101.    glutPostRedisplay();
  102. }
  103.  
  104.  
  105. static void Init( int argc, char *argv[] )
  106. {
  107.    GLint shade;
  108.    GLint rBits, gBits, bBits;
  109.  
  110.    int i;
  111.    for (i=1; i<argc; i++) {
  112.       if (strcmp(argv[i],"-dither")==0)
  113.          glDisable(GL_DITHER);
  114.       else if (strcmp(argv[i],"+dither")==0)
  115.          glEnable(GL_DITHER);
  116.       else if (strcmp(argv[i],"+smooth")==0)
  117.          glShadeModel(GL_SMOOTH);
  118.       else if (strcmp(argv[i],"+flat")==0)
  119.          glShadeModel(GL_FLAT);
  120.       else if (strcmp(argv[i],"+depth")==0)
  121.          glEnable(GL_DEPTH_TEST);
  122.       else if (strcmp(argv[i],"-depth")==0)
  123.          glDisable(GL_DEPTH_TEST);
  124.       else if (strcmp(argv[i],"-size")==0) {
  125.          Size = atoi(argv[i+1]);
  126.          i++;
  127.       }
  128.       else
  129.          printf("Unknown option: %s\n", argv[i]);
  130.    }
  131.  
  132.    glGetIntegerv(GL_SHADE_MODEL, &shade);
  133.  
  134.    printf("Dither: %s\n", glIsEnabled(GL_DITHER) ? "on" : "off");
  135.    printf("ShadeModel: %s\n", (shade==GL_FLAT) ? "flat" : "smooth");
  136.    printf("DepthTest: %s\n", glIsEnabled(GL_DEPTH_TEST) ? "on" : "off");
  137.    printf("Size: %d pixels\n", Size);
  138.  
  139.    glGetIntegerv(GL_RED_BITS, &rBits);
  140.    glGetIntegerv(GL_GREEN_BITS, &gBits);
  141.    glGetIntegerv(GL_BLUE_BITS, &bBits);
  142.    printf("RedBits: %d  GreenBits: %d  BlueBits: %d\n", rBits, gBits, bBits);
  143. }
  144.  
  145.  
  146. static void Help( const char *program )
  147. {
  148.    printf("%s options:\n", program);
  149.    printf("  +/-dither      enable/disable dithering\n");
  150.    printf("  +/-depth       enable/disable depth test\n");
  151.    printf("  +flat          flat shading\n");
  152.    printf("  +smooth        smooth shading\n");
  153.    printf("  -size pixels   specify pixels/triangle\n");
  154. }
  155.  
  156.  
  157. int main( int argc, char *argv[] )
  158. {
  159.    printf("For options:  %s -help\n", argv[0]);
  160.    glutInit( &argc, argv );
  161.    glutInitWindowSize( (int) Width, (int) Height );
  162.    glutInitWindowPosition( 0, 0 );
  163.  
  164.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  165.  
  166.    glutCreateWindow( argv[0] );
  167.  
  168.    if (argc==2 && strcmp(argv[1],"-help")==0) {
  169.       Help(argv[0]);
  170.       return 0;
  171.    }
  172.  
  173.    Init( argc, argv );
  174.  
  175.    glutReshapeFunc( Reshape );
  176.    glutKeyboardFunc( Key );
  177.    glutDisplayFunc( Display );
  178.    glutIdleFunc( Idle );
  179.  
  180.    glutMainLoop();
  181.    return 0;
  182. }
  183.