home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / samples / speed.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  9KB  |  425 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #define _HPUX_SOURCE
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include "gltk.h"
  32.  
  33. #ifdef __unix
  34. #include <sys/times.h>
  35. #include <sys/param.h>
  36. #endif
  37. #ifdef __bsdi
  38. #include <time.h>
  39. #endif
  40.  
  41.  
  42. #define GAP 10
  43. #define ROWS 1
  44. #define COLS 4
  45.  
  46.  
  47. GLenum rgb, doubleBuffer, directRender, windType;
  48. GLint windW, windH;
  49.  
  50. GLint boxW, boxH;
  51.  
  52. GLenum antialiasing = GL_FALSE;
  53. GLenum depthTesting = GL_FALSE;
  54. GLenum fogging = GL_FALSE, niceFogging = GL_FALSE;
  55. GLenum lighting = GL_FALSE;
  56. GLenum shading = GL_FALSE;
  57. GLenum texturing = GL_FALSE;
  58.  
  59. GLint repeatCount = 1000;
  60. GLint loopCount = 100;
  61.  
  62. GLubyte texture[4*3] = {
  63.     0xFF, 0, 0, 0, 0, 0,
  64.     0, 0, 0, 0, 0xFF, 0,
  65. };
  66.  
  67.  
  68. static void SetWindSize(int width, int height)
  69. {
  70.  
  71.     windW = (GLint)width;
  72.     windH = (GLint)height;
  73. }
  74.  
  75. static GLenum Key(int key, GLenum mask)
  76. {
  77.  
  78.     switch (key) {
  79.       case TK_ESCAPE:
  80.     tkQuit();
  81.       case TK_a:
  82.     antialiasing = !antialiasing;
  83.     break;
  84.       case TK_d:
  85.     depthTesting = !depthTesting;
  86.     break;
  87.       case TK_f:
  88.     fogging = !fogging;
  89.     break;
  90.       case TK_F:
  91.     niceFogging = !niceFogging;
  92.     break;
  93.       case TK_s:
  94.     shading = !shading;
  95.     break;
  96.       case TK_t:
  97.     texturing = !texturing;
  98.     break;
  99.       default:
  100.     return GL_FALSE;
  101.     }
  102.     return GL_TRUE;
  103. }
  104.  
  105. static void Viewport(GLint row, GLint column)
  106. {
  107.     GLint x, y;
  108.  
  109.     boxW = (windW - (COLS + 1) * GAP) / COLS;
  110.     boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  111.  
  112.     x = GAP + column * (boxW + GAP);
  113.     y = GAP + row * (boxH + GAP);
  114.  
  115.     glViewport(x, y, boxW, boxH);
  116.  
  117.     glMatrixMode(GL_PROJECTION);
  118.     glLoadIdentity();
  119.     gluOrtho2D(-boxW/2, boxW/2, -boxH/2, boxH/2);
  120.     glMatrixMode(GL_MODELVIEW);
  121.  
  122.    /* glEnable(GL_SCISSOR_TEST);*/
  123.     glScissor(x, y, boxW, boxH);
  124. }
  125.  
  126. static double Now(void)
  127. {
  128. #ifdef __unix
  129.     struct tms tm;
  130. #ifdef __MACHTEN__
  131.  
  132.     times(&tm);
  133.  
  134.     return (double)tm.tms_utime / (double)CLK_TCK;
  135. #else
  136.     clock_t clk;
  137.  
  138.     clk = times(&tm);
  139.  
  140. #ifdef CLK_TCK
  141.     return (double)clk / (double)CLK_TCK;
  142. #else
  143.     return (double)clk / (double)HZ;
  144. #endif
  145. #endif
  146.     return 0;
  147. #endif
  148. }
  149.  
  150. static void Report(const char *msg, float elapsed)
  151. {
  152.  
  153.     if (elapsed == 0.0) {
  154.     printf("%s per second: Unknown, elapsed time is zero\n", msg);
  155.     } else {
  156.     printf("%s per second: %g\n", msg, repeatCount*loopCount/elapsed);
  157.     }
  158. }
  159.  
  160. static void Points(void)
  161. {
  162.     GLint i, j;
  163.     float v1[3];
  164.     double start;
  165.  
  166.     start = Now();
  167.     for (i = 0; i < repeatCount; i++) {
  168.     v1[0] = 10;
  169.     v1[1] = 10;
  170.     v1[2] = 10;
  171.     glBegin(GL_POINTS);
  172.         for (j = 0; j < loopCount; j++) {
  173.         glVertex2fv(v1);
  174.         }
  175.     glEnd();
  176.     }
  177.     glFinish();
  178.     Report("Points", Now()-start);
  179. }
  180.  
  181. static void Lines(void)
  182. {
  183.     GLint i, j;
  184.     float v1[3], v2[3];
  185.     double start;
  186.  
  187.     start = Now();
  188.     for (i = 0; i < repeatCount; i++) {
  189.     v1[0] = 10;
  190.     v1[1] = 10;
  191.     v1[2] = 10;
  192.     v2[0] = 20;
  193.     v2[1] = 20;
  194.     v2[2] = 10;
  195.     glBegin(GL_LINES);
  196.         for (j = 0; j < loopCount; j++) {
  197.         glVertex2fv(v1);
  198.         glVertex2fv(v2);
  199.         }
  200.     glEnd();
  201.     }
  202.     glFinish();
  203.     Report("Lines", Now()-start);
  204. }
  205.  
  206. static void Triangles(void)
  207. {
  208.     GLint i, j;
  209.     float v1[3], v2[3], v3[3], t1[2], t2[2], t3[2];
  210.     double start;
  211.  
  212.     start = Now();
  213.  
  214.     v1[0] = 10;
  215.     v1[1] = 10;
  216.     v1[2] = 10;
  217.     v2[0] = 20;
  218.     v2[1] = 20;
  219.     v2[2] = 10;
  220.     v3[0] = 10;
  221.     v3[1] = 20;
  222.     v3[2] = 10;
  223.  
  224.     t1[0] = 0;
  225.     t1[1] = 0;
  226.     t2[0] = 1;
  227.     t2[1] = 1;
  228.     t3[0] = 0;
  229.     t3[1] = 1;
  230.  
  231.     for (i = 0; i < repeatCount; i++) {
  232.     glBegin(GL_TRIANGLES);
  233.         for (j = 0; j < loopCount; j++) {
  234.         if (texturing) {
  235.             glTexCoord2fv(t1);
  236.         }
  237.         glVertex2fv(v1);
  238.         if (texturing) {
  239.             glTexCoord2fv(t2);
  240.         }
  241.         glVertex2fv(v2);
  242.         if (texturing) {
  243.             glTexCoord2fv(t3);
  244.         }
  245.         glVertex2fv(v3);
  246.         }
  247.     glEnd();
  248.     }
  249.     glFinish();
  250.     Report("Triangles", Now()-start);
  251. }
  252.  
  253. static void Rects(void)
  254. {
  255.     GLint i, j;
  256.     float v1[2], v2[2];
  257.     double start;
  258.  
  259.     start = Now();
  260.     for (i = 0; i < repeatCount; i++) {
  261.     v1[0] = 10;
  262.     v1[1] = 10;
  263.     v2[0] = 20;
  264.     v2[1] = 20;
  265.     for (j = 0; j < loopCount; j++) {
  266.         glRectfv(v1, v2);
  267.     }
  268.     }
  269.     glFinish();
  270.     Report("Rects", Now()-start);
  271. }
  272.  
  273. static void Draw(void)
  274. {
  275.  
  276.     glClearColor(0.0, 0.0, 0.0, 0.0);
  277.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  278.  
  279.     TK_SETCOLOR(windType, TK_YELLOW);
  280.  
  281.     if (antialiasing) {
  282.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  283.     glEnable(GL_BLEND);
  284.  
  285.     glEnable(GL_POINT_SMOOTH);
  286.     glEnable(GL_LINE_SMOOTH);
  287.     glEnable(GL_POLYGON_SMOOTH);
  288.     printf("antialiasing: on\n");
  289.     }
  290.     else {
  291.        glDisable(GL_BLEND);
  292.        glDisable(GL_POINT_SMOOTH);
  293.        glDisable(GL_LINE_SMOOTH);
  294.        glDisable(GL_POLYGON_SMOOTH);
  295.        printf("antialiasing: off\n");
  296.     }
  297.     if (depthTesting) {
  298.     glEnable(GL_DEPTH_TEST);
  299.     printf("depthtest: on\n");
  300.     }
  301.     else {
  302.        glDisable(GL_DEPTH_TEST);
  303.        printf("depthtest: off\n");
  304.     }
  305.     if (fogging) {
  306.     glEnable(GL_FOG);
  307.     glHint(GL_FOG_HINT, (niceFogging) ? GL_NICEST : GL_FASTEST);
  308.     printf("fog: on\n");
  309.     }
  310.     else {
  311.        glDisable(GL_FOG);
  312.        printf("fog: off\n");
  313.     }
  314.     if (lighting) {
  315.     static GLfloat ambient[4] = {1, 0.5, 0.5, 0};
  316.  
  317.     glEnable(GL_NORMALIZE);
  318.     glNormal3f(1.0, 1.0, 1.0);
  319.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  320.     glEnable(GL_LIGHTING);
  321.     glEnable(GL_LIGHT0);
  322.     printf("lighting: on\n");
  323.     }
  324.     else {
  325.        glDisable( GL_LIGHTING );
  326.        printf("lighting: off\n");
  327.      }
  328.     if (shading) {
  329.        glShadeModel(GL_SMOOTH);
  330.        printf("shading: smooth\n");
  331.     }
  332.     else {
  333.        glShadeModel(GL_FLAT);
  334.        printf("shading: flat\n");
  335.     }
  336.     if (texturing) {
  337.     static GLfloat modulate[1] = {GL_DECAL};
  338.     static GLfloat clamp[1] = {GL_CLAMP};
  339.     static GLfloat linear[1] = {GL_LINEAR};
  340.  
  341.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  342.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE,
  343.              (GLvoid *)texture);
  344.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modulate);
  345.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
  346.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
  347.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear);
  348.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear);
  349.     glEnable(GL_TEXTURE_2D);
  350.     printf("texturing: on\n");
  351.     }
  352.     else {
  353.        glDisable( GL_TEXTURE_2D );
  354.        printf("texturing: off\n");
  355.      }
  356.  
  357.     Viewport(0, 0); Points();
  358.     Viewport(0, 1); Lines();
  359.     Viewport(0, 2); Triangles();
  360.     Viewport(0, 3); Rects();
  361.  
  362.     glFlush();
  363.  
  364.     if (doubleBuffer) {
  365.     tkSwapBuffers();
  366.     }
  367. }
  368.  
  369. static GLenum Args(int argc, char **argv)
  370. {
  371.     GLint i;
  372.  
  373.     rgb = GL_TRUE;
  374.     doubleBuffer = GL_FALSE;
  375.     directRender = GL_TRUE;
  376.  
  377.     for (i = 1; i < argc; i++) {
  378.     if (strcmp(argv[i], "-ci") == 0) {
  379.         rgb = GL_FALSE;
  380.     } else if (strcmp(argv[i], "-rgb") == 0) {
  381.         rgb = GL_TRUE;
  382.     } else if (strcmp(argv[i], "-sb") == 0) {
  383.         doubleBuffer = GL_FALSE;
  384.     } else if (strcmp(argv[i], "-db") == 0) {
  385.         doubleBuffer = GL_TRUE;
  386.     } else if (strcmp(argv[i], "-dr") == 0) {
  387.         directRender = GL_TRUE;
  388.     } else if (strcmp(argv[i], "-ir") == 0) {
  389.         directRender = GL_FALSE;
  390.     } else {
  391.         printf("%s (Bad option).\n", argv[i]);
  392.         return GL_FALSE;
  393.     }
  394.     }
  395.     return GL_TRUE;
  396. }
  397.  
  398. void main(int argc, char **argv)
  399. {
  400.  
  401.     if (Args(argc, argv) == GL_FALSE) {
  402.     tkQuit();
  403.     }
  404.  
  405.     windW = 600;
  406.     windH = 300;
  407.     tkInitPosition(0, 0, windW, windH);
  408.  
  409.     windType = TK_ALPHA | TK_DEPTH;
  410.     windType |= (rgb) ? TK_RGB : TK_INDEX;
  411.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  412.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  413.     tkInitDisplayMode(windType);
  414.  
  415.     if (tkInitWindow("Speed Test") == GL_FALSE) {
  416.     tkQuit();
  417.     }
  418.  
  419.     tkExposeFunc(SetWindSize);
  420.     tkReshapeFunc(SetWindSize);
  421.     tkKeyDownFunc(Key);
  422.     tkDisplayFunc(Draw);
  423.     tkExec();
  424. }
  425.