home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / newopg.zip / SPEED.C < prev    next >
Text File  |  1995-03-04  |  7KB  |  371 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. //#include <unistd.h>
  4. #include <stdlib.h>
  5. #include "tk.h"
  6.  
  7. float tkRGBMap[8][3] = {
  8.     {
  9.     0, 0, 0
  10.     },
  11.     {
  12.     1, 0, 0
  13.     },
  14.     {
  15.     0, 1, 0
  16.     },
  17.     {
  18.     1, 1, 0
  19.     },
  20.     {
  21.     0, 0, 1
  22.     },
  23.     {
  24.     1, 0, 1
  25.     },
  26.     {
  27.     0, 1, 1
  28.     },
  29.     {
  30.     1, 1, 1
  31.     }
  32. };
  33.  
  34. #ifdef __unix
  35. #include <sys/times.h>
  36. #include <sys/param.h>
  37. #endif
  38.  
  39.  
  40. #define GAP 10
  41. #define ROWS 1
  42. #define COLS 4
  43.  
  44.  
  45. GLenum rgb, doubleBuffer, directRender, windType;
  46. GLint windW, windH;
  47.  
  48. GLint boxW, boxH;
  49. GLint x, y;
  50.  
  51. GLenum antialiasing = GL_FALSE;
  52. GLenum depthTesting = GL_FALSE;
  53. GLenum fogging = GL_FALSE, niceFogging = GL_FALSE;
  54. GLenum lighting = GL_FALSE;
  55. GLenum shading = GL_FALSE;
  56. GLenum texturing = GL_FALSE;
  57.  
  58. GLint repeatCount = 100;
  59. GLint loopCount = 10;
  60.  
  61. GLubyte texture[4*3] = {
  62.     0xFF, 0, 0, 0, 0, 0,
  63.     0, 0, 0, 0, 0xFF, 0,
  64. };
  65.  
  66.  
  67. static void SetWindSize(int width, int height)
  68. {
  69.  
  70.     windW = (GLint)width;
  71.     windH = (GLint)height;
  72. }
  73.  
  74. static GLenum Key(int key, GLenum mask)
  75. {
  76.  
  77.     switch (key) {
  78.       case TK_ESCAPE:
  79.     tkQuit();
  80.       case TK_a:
  81.     antialiasing = !antialiasing;
  82.       case TK_d:
  83.     depthTesting = !depthTesting;
  84.     break;
  85.       case TK_f:
  86.     fogging = !fogging;
  87.       case TK_F:
  88.     niceFogging = !niceFogging;
  89.       case TK_s:
  90.     shading = !shading;
  91.       case TK_t:
  92.     texturing = !texturing;
  93.       default:
  94.     return GL_FALSE;
  95.     }
  96.     return GL_TRUE;
  97. }
  98.  
  99. static void Viewport(GLint row, GLint column)
  100. {
  101.     boxW = (windW - (COLS + 1) * GAP) / COLS;
  102.     boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  103.  
  104.     x = GAP + column * (boxW + GAP);
  105.     y = GAP + row * (boxH + GAP);
  106.  
  107.     glViewport(x, y, boxW, boxH);
  108.  
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     gluOrtho2D((float) -boxW/2, (float) boxW/2,(float)  -boxH/2, (float) boxH/2);
  112.     glMatrixMode(GL_MODELVIEW);
  113.  
  114.     glEnable(GL_SCISSOR_TEST);
  115.     glScissor(x, y, boxW, boxH);
  116. }
  117.  
  118. static double Now(void)
  119. {
  120. #ifdef __unix
  121.     struct tms tm;
  122.     clock_t clk;
  123.  
  124.     clk = times(&tm);
  125.     return (double)clk / (double)HZ;
  126. #else
  127.     return 0;
  128. #endif
  129. }
  130.  
  131. static void Report(const char *msg, float elapsed)
  132. {
  133.  
  134.     if (elapsed == 0.0) {
  135.     printf("%s per second: Unknown, elapsed time is zero\n", msg);
  136.     } else {
  137.     printf("%s per second: %g\n", msg, repeatCount*loopCount/elapsed);
  138.     }
  139. }
  140.  
  141. static void Points(void)
  142. {
  143.     GLint i, j;
  144.     float v1[3];
  145.     double start;
  146.  
  147.     start = Now();
  148.     for (i = 0; i < repeatCount; i++) {
  149.     v1[0] = 10;
  150.     v1[1] = 10;
  151.     v1[2] = 10;
  152.     glBegin(GL_POINTS);
  153.         for (j = 0; j < loopCount; j++) {
  154.         glVertex2fv(v1);
  155.         }
  156.     glEnd();
  157.     }
  158.     glFinish();
  159.     Report("Points", Now()-start);
  160. }
  161.  
  162. static void Lines(void)
  163. {
  164.     GLint i, j;
  165.     float v1[3], v2[3];
  166.     double start;
  167.  
  168.     start = Now();
  169.     for (i = 0; i < repeatCount; i++) {
  170.     v1[0] = 10;
  171.     v1[1] = 10;
  172.     v1[2] = 10;
  173.     v2[0] = 20;
  174.     v2[1] = 20;
  175.     v2[2] = 10;
  176.     glBegin(GL_LINES);
  177.         for (j = 0; j < loopCount; j++) {
  178.         glVertex2fv(v1);
  179.         glVertex2fv(v2);
  180.         }
  181.     glEnd();
  182.     }
  183.     glFinish();
  184.     Report("Lines", Now()-start);
  185. }
  186.  
  187. static void Triangles(void)
  188. {
  189.     GLint i, j;
  190.     float v1[3], v2[3], v3[3], t1[2], t2[2], t3[2];
  191.     double start;
  192.  
  193.     start = Now();
  194.  
  195.     v1[0] = 10;
  196.     v1[1] = 10;
  197.     v1[2] = 10;
  198.     v2[0] = 20;
  199.     v2[1] = 20;
  200.     v2[2] = 10;
  201.     v3[0] = 10;
  202.     v3[1] = 20;
  203.     v3[2] = 10;
  204.  
  205.     t1[0] = 0;
  206.     t1[1] = 0;
  207.     t2[0] = 1;
  208.     t2[1] = 1;
  209.     t3[0] = 0;
  210.     t3[1] = 1;
  211.  
  212.     for (i = 0; i < repeatCount; i++) {
  213.     glBegin(GL_TRIANGLES);
  214.         for (j = 0; j < loopCount; j++) {
  215.         if (texturing) {
  216.             glTexCoord2fv(t1);
  217.         }
  218.         glVertex2fv(v1);
  219.         if (texturing) {
  220.             glTexCoord2fv(t2);
  221.         }
  222.         glVertex2fv(v2);
  223.         if (texturing) {
  224.             glTexCoord2fv(t3);
  225.         }
  226.         glVertex2fv(v3);
  227.         }
  228.     glEnd();
  229.     }
  230.     glFinish();
  231.     Report("Triangles", Now()-start);
  232. }
  233.  
  234. static void Rects(void)
  235. {
  236.     GLint i, j;
  237.     float v1[2], v2[2];
  238.     double start;
  239.  
  240.     start = Now();
  241.     for (i = 0; i < repeatCount; i++) {
  242.     v1[0] = 10;
  243.     v1[1] = 10;
  244.     v2[0] = 20;
  245.     v2[1] = 20;
  246.     for (j = 0; j < loopCount; j++) {
  247.         glRectfv(v1, v2);
  248.     }
  249.     }
  250.     glFinish();
  251.     Report("Rects", Now()-start);
  252. }
  253.  
  254. static void Draw(void)
  255. {
  256.  
  257.     glClearColor(0.0, 0.0, 0.0, 0.0);
  258.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  259.  
  260.     TK_SETCOLOR(windType, TK_YELLOW);
  261.  
  262.     if (antialiasing) {
  263.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  264.     glEnable(GL_BLEND);
  265.  
  266.     glEnable(GL_POINT_SMOOTH);
  267.     glEnable(GL_LINE_SMOOTH);
  268.     glEnable(GL_POLYGON_SMOOTH);
  269.     }
  270.     if (depthTesting) {
  271.     glEnable(GL_DEPTH_TEST);
  272.     }
  273.     if (fogging) {
  274.     glEnable(GL_FOG);
  275.     glHint(GL_FOG_HINT, (niceFogging) ? GL_NICEST : GL_FASTEST);
  276.     }
  277.     if (lighting) {
  278.     static GLfloat ambient[4] = {1, 0.5, 0.5, 0};
  279.  
  280.     glEnable(GL_NORMALIZE);
  281.     glNormal3f(1.0, 1.0, 1.0);
  282.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  283.     glEnable(GL_LIGHTING);
  284.     glEnable(GL_LIGHT0);
  285.     }
  286.     (shading) ? glShadeModel(GL_SMOOTH) : glShadeModel(GL_FLAT);
  287.     if (texturing) {
  288.     static GLfloat modulate[1] = {GL_DECAL};
  289.     static GLfloat clamp[1] = {GL_CLAMP};
  290.     static GLfloat linear[1] = {GL_LINEAR};
  291.  
  292.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  293.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE,
  294.              (GLvoid *)texture);
  295.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modulate);
  296.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
  297.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
  298.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear);
  299.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear);
  300.     glEnable(GL_TEXTURE_2D);
  301.     }
  302.  
  303.     Viewport(0, 0); Points();
  304.     Viewport(0, 1); Lines();
  305.     Viewport(0, 2); Triangles();
  306.     Viewport(0, 3); Rects();
  307.  
  308.     glFlush();
  309.  
  310.     if (doubleBuffer) {
  311.     tkSwapBuffers();
  312.     }
  313. }
  314.  
  315. static GLenum Args(int argc, char **argv)
  316. {
  317.     GLint i;
  318.  
  319.     rgb = GL_TRUE;
  320.     doubleBuffer = GL_FALSE;
  321.     directRender = GL_TRUE;
  322.  
  323.     for (i = 1; i < argc; i++) {
  324.     if (strcmp(argv[i], "-ci") == 0) {
  325.         rgb = GL_FALSE;
  326.     } else if (strcmp(argv[i], "-rgb") == 0) {
  327.         rgb = GL_TRUE;
  328.     } else if (strcmp(argv[i], "-sb") == 0) {
  329.         doubleBuffer = GL_FALSE;
  330.     } else if (strcmp(argv[i], "-db") == 0) {
  331.         doubleBuffer = GL_TRUE;
  332.     } else if (strcmp(argv[i], "-dr") == 0) {
  333.         directRender = GL_TRUE;
  334.     } else if (strcmp(argv[i], "-ir") == 0) {
  335.         directRender = GL_FALSE;
  336.     } else {
  337.         printf("%s (Bad option).\n", argv[i]);
  338.         return GL_FALSE;
  339.     }
  340.     }
  341.     return GL_TRUE;
  342. }
  343.  
  344. void main(int argc, char **argv)
  345. {
  346.  
  347.     if (Args(argc, argv) == GL_FALSE) {
  348.     tkQuit();
  349.     }
  350.  
  351.     windW = 600;
  352.     windH = 300;
  353.     tkInitPosition(0, 0, windW, windH);
  354.  
  355.     windType = TK_ALPHA | TK_DEPTH;
  356.     windType |= (rgb) ? TK_RGB : TK_INDEX;
  357.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  358.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  359.     tkInitDisplayMode(windType);
  360.  
  361.     if (tkInitWindow("Speed Test") == GL_FALSE) {
  362.     tkQuit();
  363.     }
  364.  
  365.     tkExposeFunc(SetWindSize);
  366.     tkReshapeFunc(SetWindSize);
  367.     tkKeyDownFunc(Key);
  368.     tkDisplayFunc(Draw);
  369.     tkExec();
  370. }
  371.