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

  1. /* drawpix.c */
  2.  
  3. /*
  4.  * glDrawPixels demo/test/benchmark
  5.  * 
  6.  * Brian Paul   September 25, 1997
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13.  
  14. #include "../util/readtex.c"  /* a hack, I know */
  15.  
  16.  
  17. #define IMAGE "girl.rgb"
  18.  
  19. static int ImgWidth, ImgHeight;
  20. static GLenum ImgFormat;
  21. static GLubyte *Image = NULL;
  22.  
  23. static int Xpos, Ypos;
  24. static int SkipPixels, SkipRows;
  25. static int DrawWidth, DrawHeight;
  26. static int Scissor = 0;
  27. static float Xzoom, Yzoom;
  28.  
  29.  
  30.  
  31. static void Reset()
  32. {
  33.    Xpos = Ypos = 20;
  34.    DrawWidth = ImgWidth;
  35.    DrawHeight = ImgHeight;
  36.    SkipPixels = SkipRows = 0;
  37.    Scissor = 0;
  38.    Xzoom = Yzoom = 1.0;
  39. }
  40.  
  41.  
  42. static void Display( void )
  43. {
  44.    glClear( GL_COLOR_BUFFER_BIT );
  45.  
  46. #if 0
  47.    glRasterPos2i(Xpos, Ypos);
  48. #else
  49.    /* This allows negative raster positions: */
  50.    glRasterPos2i(0, 0);
  51.    glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL);
  52. #endif
  53.  
  54.    glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  55.    glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  56.  
  57.    glPixelZoom( Xzoom, Yzoom );
  58.  
  59.    if (Scissor)
  60.       glEnable(GL_SCISSOR_TEST);
  61.  
  62.    glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  63.  
  64.    glDisable(GL_SCISSOR_TEST);
  65.  
  66.    glutSwapBuffers();
  67. }
  68.  
  69.  
  70. static void Benchmark( void )
  71. {
  72.    int startTime, endTime;
  73.    int draws = 500;
  74.    double seconds, pixelsPerSecond;
  75.  
  76.    printf("Benchmarking...\n");
  77.    /* GL set-up */
  78.    glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels);
  79.    glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows);
  80.    glPixelZoom( Xzoom, Yzoom );
  81.    if (Scissor)
  82.       glEnable(GL_SCISSOR_TEST);
  83.  
  84.    /* Run timing test */
  85.    draws = 0;
  86.    startTime = glutGet(GLUT_ELAPSED_TIME);
  87.    do {
  88.       glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  89.       draws++;
  90.       endTime = glutGet(GLUT_ELAPSED_TIME);
  91.    } while (endTime - startTime < 4000);   /* 4 seconds */
  92.  
  93.    /* GL clean-up */
  94.    glDisable(GL_SCISSOR_TEST);
  95.  
  96.    /* Results */
  97.    seconds = (double) (endTime - startTime) / 1000.0;
  98.    pixelsPerSecond = draws * DrawWidth * DrawHeight / seconds;
  99.    printf("Result:  %d draws in %f seconds = %f pixels/sec\n",
  100.           draws, seconds, pixelsPerSecond);
  101. }
  102.  
  103.  
  104. static void Reshape( int width, int height )
  105. {
  106.    glViewport( 0, 0, width, height );
  107.    glMatrixMode( GL_PROJECTION );
  108.    glLoadIdentity();
  109.    glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  110.    glMatrixMode( GL_MODELVIEW );
  111.    glLoadIdentity();
  112.  
  113.    glScissor(width/4, height/4, width/2, height/2);
  114. }
  115.  
  116.  
  117. static void Key( unsigned char key, int x, int y )
  118. {
  119.    switch (key) {
  120.       case ' ':
  121.          Reset();
  122.          break;
  123.       case 'w':
  124.          if (DrawWidth > 0)
  125.             DrawWidth--;
  126.          break;
  127.       case 'W':
  128.          DrawWidth++;
  129.          break;
  130.       case 'h':
  131.          if (DrawHeight > 0)
  132.             DrawHeight--;
  133.          break;
  134.       case 'H':
  135.          DrawHeight++;
  136.          break;
  137.       case 'p':
  138.          if (SkipPixels > 0)
  139.              SkipPixels--;
  140.          break;
  141.       case 'P':
  142.          SkipPixels++;
  143.          break;
  144.       case 'r':
  145.          if (SkipRows > 0)
  146.              SkipRows--;
  147.          break;
  148.       case 'R':
  149.          SkipRows++;
  150.          break;
  151.       case 's':
  152.          Scissor = !Scissor;
  153.          break;
  154.       case 'x':
  155.          Xzoom -= 0.1;
  156.          break;
  157.       case 'X':
  158.          Xzoom += 0.1;
  159.          break;
  160.       case 'y':
  161.          Yzoom -= 0.1;
  162.          break;
  163.       case 'Y':
  164.          Yzoom += 0.1;
  165.          break;
  166.       case 'b':
  167.          Benchmark();
  168.          break;
  169.       case 27:
  170.          exit(0);
  171.          break;
  172.    }
  173.    glutPostRedisplay();
  174. }
  175.  
  176.  
  177. static void SpecialKey( int key, int x, int y )
  178. {
  179.    switch (key) {
  180.       case GLUT_KEY_UP:
  181.          Ypos += 1;
  182.          break;
  183.       case GLUT_KEY_DOWN:
  184.          Ypos -= 1;
  185.          break;
  186.       case GLUT_KEY_LEFT:
  187.          Xpos -= 1;
  188.          break;
  189.       case GLUT_KEY_RIGHT:
  190.          Xpos += 1;
  191.          break;
  192.    }
  193.    glutPostRedisplay();
  194. }
  195.  
  196.  
  197. static void Init( GLboolean ciMode )
  198. {
  199.    printf("GL_VERSION = %s\n", glGetString(GL_VERSION));
  200.    printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER));
  201.  
  202.    Image = LoadRGBImage( IMAGE, &ImgWidth, &ImgHeight, &ImgFormat );
  203.    if (!Image) {
  204.       printf("Couldn't read %s\n", IMAGE);
  205.       exit(0);
  206.    }
  207.  
  208.    if (ciMode) {
  209.       /* Convert RGB image to grayscale */
  210.       GLubyte *indexImage = malloc( ImgWidth * ImgHeight );
  211.       GLint i;
  212.       for (i=0; i<ImgWidth*ImgHeight; i++) {
  213.          int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  214.          indexImage[i] = gray / 3;
  215.       }
  216.       free(Image);
  217.       Image = indexImage;
  218.       ImgFormat = GL_COLOR_INDEX;
  219.  
  220.       for (i=0;i<255;i++) {
  221.          float g = i / 255.0;
  222.          glutSetColor(i, g, g, g);
  223.       }
  224.    }
  225.  
  226.    printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  227.  
  228.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  229.    glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  230.  
  231.    Reset();
  232. }
  233.  
  234.  
  235. static void Usage(void)
  236. {
  237.    printf("Keys:\n");
  238.    printf("       SPACE  Reset\n");
  239.    printf("     Up/Down  Move image up/down\n");
  240.    printf("  Left/Right  Move image left/right\n");
  241.    printf("           w  Decrease glDrawPixels width\n");
  242.    printf("           W  Increase glDrawPixels width\n");
  243.    printf("           h  Decrease glDrawPixels height\n");
  244.    printf("           H  Increase glDrawPixels height\n");
  245.    printf("           p  Decrease GL_UNPACK_SKIP_PIXELS\n");
  246.    printf("           P  Increase GL_UNPACK_SKIP_PIXELS\n");
  247.    printf("           r  Decrease GL_UNPACK_SKIP_ROWS\n");
  248.    printf("           R  Increase GL_UNPACK_SKIP_ROWS\n");
  249.    printf("           s  Toggle GL_SCISSOR_TEST\n");
  250.    printf("           b  Benchmark test\n");
  251.    printf("         ESC  Exit\n");
  252. }
  253.  
  254.  
  255. int main( int argc, char *argv[] )
  256. {
  257.    GLboolean ciMode = GL_FALSE;
  258.  
  259.    if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  260.       ciMode = GL_TRUE;
  261.    }
  262.  
  263.    glutInit( &argc, argv );
  264.    glutInitWindowPosition( 0, 0 );
  265.    glutInitWindowSize( 500, 400 );
  266.  
  267.    if (ciMode)
  268.       glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  269.    else
  270.       glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  271.  
  272.    glutCreateWindow(argv[0]);
  273.  
  274.    Init(ciMode);
  275.    Usage();
  276.  
  277.    glutReshapeFunc( Reshape );
  278.    glutKeyboardFunc( Key );
  279.    glutSpecialFunc( SpecialKey );
  280.    glutDisplayFunc( Display );
  281.  
  282.    glutMainLoop();
  283.    return 0;
  284. }
  285.