home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / demos / drawpix.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  8KB  |  336 lines

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