Guest User

asch

a guest
Nov 30th, 2009
245
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Simple benchmark program to measure texturing performance of OpenGL.
  3.  *  Complie:
  4.  *  gcc glfwTexture.c -lpthread -lXrandr -lm -lGL -lGLU -lglfw -lX11
  5.  *
  6.  *  Requires packages (on Ubuntu Karmic default install):
  7.  *  libglfw-dev freeglut-dev libxrandr-dev
  8.  */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <GL/glfw.h>
  13. // Size of the textures drawn on the screen
  14. #define WIDTH 50
  15. #define HEIGHT 140
  16. // Size of the becnhmark application screen
  17. #define SCREEN_WIDTH 1024
  18. #define SCREEN_HEIGHT 768
  19. // Number of rectangles drawn
  20. #define N 1000
  21. // Number of textures used on rectangles. The same texture is never used twice in a row
  22. #define K 1000
  23.  
  24.  
  25. int screenWidth;
  26. int screenHeight;
  27.  
  28. GLuint * textures;
  29. /**
  30.  * Create a GL texture and fill with random values.
  31.  * The texture's size is controlled by the WIDTH and HEIGHT constants.
  32.  */
  33. GLuint createTexture()
  34. {
  35.     GLuint handle;
  36.     int l=WIDTH*HEIGHT*4;
  37.     GLubyte * ptr=malloc(l);
  38.     GLubyte * p=ptr;
  39.     int i=0;
  40.     for(i=0;i<l;++i)
  41.     {
  42.         *(ptr+i)=(GLubyte)(random()%256);
  43.     }
  44.     glGenTextures(1, &handle);
  45.     printf("Create texture: %d\n", handle);
  46.     glBindTexture(GL_TEXTURE_2D, handle);
  47.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  48.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  49.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  50.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  51.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH, HEIGHT, 0, GL_RGBA,
  52.         GL_UNSIGNED_BYTE, ptr);
  53.     free(ptr);
  54.     return handle;
  55. }
  56.  
  57. /**
  58.  * Set the projection matrices. Use orthogonal projection to emulate 2D.
  59.  */
  60. static void
  61. Init(void)
  62. {
  63.     glMatrixMode(GL_PROJECTION);
  64.     glLoadIdentity();
  65.     glOrtho(-screenWidth/2, screenWidth/2, -screenHeight/2, screenHeight/2, -1, 1);
  66.     glMatrixMode(GL_MODELVIEW);
  67.     glLoadIdentity();
  68. }
  69.  
  70. int lastPrint;
  71. int ctr;
  72. int nPixel;
  73. int renderTime;
  74. /**
  75.  * Separate method is created so that we see time consumed in profiler.
  76.  */
  77. void myBindTexture(int i)
  78. {
  79.     glBindTexture(GL_TEXTURE_2D, textures[i%K]);
  80. }
  81. /**
  82.  * Draw the i. textured rectangle.
  83.  * The rectangle will be textured with texture number i%K
  84.  * The rectangles are positioned on the screen a way that they fill the whole area
  85.  *
  86.  */
  87. void drawRectangle(int i)
  88. {
  89.     int width=50;
  90.     int height=140;
  91.     int x=(i*50)%(screenWidth-width);
  92.     int y=(i*63)%(screenHeight-height);
  93.     x-=screenWidth/2;
  94.     y=(screenHeight/2-y);
  95.     // activate the image texture
  96.     myBindTexture(i);
  97.     glBegin(GL_QUADS);
  98.         glTexCoord2f(0, 0);
  99.         glVertex3f(x, y, 0.0);
  100.         glTexCoord2f(1, 0);
  101.         glVertex3f(x+width, y, 0.0);
  102.         glTexCoord2f(1, 1);
  103.         glVertex3f(x+width, y-height, 0.0);
  104.         glTexCoord2f(0, 1);
  105.         glVertex3f(x, y-height, 0.0);
  106.     glEnd();
  107.     nPixel+=width*height;
  108. }
  109. /**
  110.  * Render a single frame of the benchmark.
  111.  */
  112. static void
  113. Draw(void)
  114. {
  115.     nPixel=0;
  116.     glClearColor(0.0, 0.0, 0.0, 1.0);
  117.     glClear(GL_COLOR_BUFFER_BIT);
  118.     int i=0;
  119.     Init();
  120.     glEnable(GL_TEXTURE_2D);
  121.     glColor4f(1, 1, 1, 1); // no color
  122.     glDisable(GL_LIGHTING); // no lighting
  123.     glDisable(GL_DEPTH_TEST); // no depth test
  124.     glEnable(GL_BLEND); // enable transparency
  125.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  126.     for(i=0;i<N;++i)
  127.     {
  128.         drawRectangle(i);
  129.     }
  130.     int time;
  131.     time = (int)(glfwGetTime( )*1000);
  132.     ctr++;
  133.     if(time>lastPrint+1000)
  134.     {
  135.         printf("\nPerformance counters:\n current time: %d\nframes in last second: %d\npixels drawn: %d \ncurrent screen size: %dX%d.\nglFinish() time per frame(millis): %lf \n",
  136.             time, ctr, nPixel,
  137.             screenWidth, screenHeight, ((double)renderTime)/ ((double)ctr));
  138.         ctr=0;
  139.         lastPrint=lastPrint+1000;
  140.         renderTime=0;
  141.     }
  142.     glFlush();
  143.     glFinish();
  144.     int afterFinish=(int)(glfwGetTime()*1000);
  145.     renderTime+=afterFinish-time;
  146. }
  147. int
  148. main(int argc, char **argv)
  149. {
  150.     int running = GL_TRUE;
  151.     // Initialize GLFW
  152.     srandom(0);
  153.     glfwInit();
  154.     renderTime=0;
  155.     lastPrint=0;
  156.     ctr=0;
  157.     screenWidth=SCREEN_WIDTH;
  158.     screenHeight=SCREEN_HEIGHT;
  159.     // Open an OpenGL window
  160.     if( !glfwOpenWindow( screenWidth, screenHeight, 0,0,0,0,0,0, GLFW_WINDOW ) )
  161.     {
  162.         glfwTerminate();
  163.         return 0;
  164.     }
  165.     int i;
  166.     textures=calloc(sizeof(GLuint), N);
  167.     for(i=0;i<K;++i)
  168.     {
  169.         textures[i]=createTexture();
  170.     }
  171.     glfwSetWindowTitle( "GLFW Textures performance test" );
  172. //  glfwSwapInterval(1);
  173.     while( running )
  174.     {
  175.         // OpenGL rendering goes here...
  176.         glClear( GL_COLOR_BUFFER_BIT );
  177.         Init();
  178.         Draw();
  179.         // Swap front and back rendering buffers
  180.         glfwSwapBuffers();
  181.         // Check if ESC key was pressed or window was closed
  182.         running = !glfwGetKey( GLFW_KEY_ESC ) &&
  183.             glfwGetWindowParam( GLFW_OPENED );
  184.     }
  185.     // Close window and terminate GLFW
  186.     glfwTerminate();
  187.     return 0;
  188. }
  189.  
  190.  
RAW Paste Data