home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / samples / speed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  8.8 KB  |  430 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. #ifdef AMIGAWARP
  41. #include <time.h>
  42. #endif
  43.  
  44. #define GAP 10
  45. #define ROWS 1
  46. #define COLS 4
  47.  
  48.  
  49. GLenum rgb, doubleBuffer, directRender, windType;
  50. GLint windW, windH;
  51.  
  52. GLint boxW, boxH;
  53.  
  54. GLenum antialiasing = GL_FALSE;
  55. GLenum depthTesting = GL_FALSE;
  56. GLenum fogging = GL_FALSE, niceFogging = GL_FALSE;
  57. GLenum lighting = GL_FALSE;
  58. GLenum shading = GL_FALSE;
  59. GLenum texturing = GL_FALSE;
  60.  
  61. GLint repeatCount = 1000;
  62. GLint loopCount = 100;
  63.  
  64. GLubyte texture[4*3] = {
  65.     0xFF, 0, 0, 0, 0, 0,
  66.     0, 0, 0, 0, 0xFF, 0,
  67. };
  68.  
  69.  
  70. static void SetWindSize(int width, int height)
  71. {
  72.  
  73.     windW = (GLint)width;
  74.     windH = (GLint)height;
  75. }
  76.  
  77. static GLenum Key(int key, GLenum mask)
  78. {
  79.  
  80.     switch (key) {
  81.       case TK_ESCAPE:
  82.     tkQuit();
  83.       case TK_a:
  84.     antialiasing = !antialiasing;
  85.     break;
  86.       case TK_d:
  87.     depthTesting = !depthTesting;
  88.     break;
  89.       case TK_f:
  90.     fogging = !fogging;
  91.     break;
  92.       case TK_F:
  93.     niceFogging = !niceFogging;
  94.     break;
  95.       case TK_s:
  96.     shading = !shading;
  97.     break;
  98.       case TK_t:
  99.     texturing = !texturing;
  100.     break;
  101.       default:
  102.     return GL_FALSE;
  103.     }
  104.     return GL_TRUE;
  105. }
  106.  
  107. static void Viewport(GLint row, GLint column)
  108. {
  109.     GLint x, y;
  110.  
  111.     boxW = (windW - (COLS + 1) * GAP) / COLS;
  112.     boxH = (windH - (ROWS + 1) * GAP) / ROWS;
  113.  
  114.     x = GAP + column * (boxW + GAP);
  115.     y = GAP + row * (boxH + GAP);
  116.  
  117.     glViewport(x, y, boxW, boxH);
  118.  
  119.     glMatrixMode(GL_PROJECTION);
  120.     glLoadIdentity();
  121.     gluOrtho2D(-boxW/2, boxW/2, -boxH/2, boxH/2);
  122.     glMatrixMode(GL_MODELVIEW);
  123.  
  124.    /* glEnable(GL_SCISSOR_TEST);*/
  125.     glScissor(x, y, boxW, boxH);
  126. }
  127.  
  128. static double Now(void)
  129. {
  130. #ifdef AMIGAWARP
  131.     return ((double)clock())/CLOCKS_PER_SEC;
  132. #endif
  133.  
  134. #ifdef __unix
  135.     struct tms tm;
  136. #ifdef __MACHTEN__
  137.  
  138.     times(&tm);
  139.  
  140.     return (double)tm.tms_utime / (double)CLK_TCK;
  141. #else
  142.     clock_t clk;
  143.  
  144.     clk = times(&tm);
  145.  
  146. #ifdef CLK_TCK
  147.     return (double)clk / (double)CLK_TCK;
  148. #else
  149.     return (double)clk / (double)HZ;
  150. #endif
  151. #endif
  152.     return 0;
  153. #endif
  154. }
  155.  
  156. static void Report(const char *msg, float elapsed)
  157. {
  158.  
  159.     if (elapsed == 0.0) {
  160.     printf("%s per second: Unknown, elapsed time is zero\n", msg);
  161.     } else {
  162.     printf("%s per second: %g\n", msg, repeatCount*loopCount/elapsed);
  163.     }
  164. }
  165.  
  166. static void Points(void)
  167. {
  168.     GLint i, j;
  169.     float v1[3];
  170.     double start;
  171.  
  172.     start = Now();
  173.     for (i = 0; i < repeatCount; i++) {
  174.     v1[0] = 10;
  175.     v1[1] = 10;
  176.     v1[2] = 10;
  177.     glBegin(GL_POINTS);
  178.         for (j = 0; j < loopCount; j++) {
  179.         glVertex2fv(v1);
  180.         }
  181.     glEnd();
  182.     }
  183.     glFinish();
  184.     Report("Points", Now()-start);
  185. }
  186.  
  187. static void Lines(void)
  188. {
  189.     GLint i, j;
  190.     float v1[3], v2[3];
  191.     double start;
  192.  
  193.     start = Now();
  194.     for (i = 0; i < repeatCount; i++) {
  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.     glBegin(GL_LINES);
  202.         for (j = 0; j < loopCount; j++) {
  203.         glVertex2fv(v1);
  204.         glVertex2fv(v2);
  205.         }
  206.     glEnd();
  207.     }
  208.     glFinish();
  209.     Report("Lines", Now()-start);
  210. }
  211.  
  212. static void Triangles(void)
  213. {
  214.     GLint i, j;
  215.     float v1[3], v2[3], v3[3], t1[2], t2[2], t3[2];
  216.     double start;
  217.  
  218.     start = Now();
  219.  
  220.     v1[0] = 10;
  221.     v1[1] = 10;
  222.     v1[2] = 10;
  223.     v2[0] = 20;
  224.     v2[1] = 20;
  225.     v2[2] = 10;
  226.     v3[0] = 10;
  227.     v3[1] = 20;
  228.     v3[2] = 10;
  229.  
  230.     t1[0] = 0;
  231.     t1[1] = 0;
  232.     t2[0] = 1;
  233.     t2[1] = 1;
  234.     t3[0] = 0;
  235.     t3[1] = 1;
  236.  
  237.     for (i = 0; i < repeatCount; i++) {
  238.     glBegin(GL_TRIANGLES);
  239.         for (j = 0; j < loopCount; j++) {
  240.         if (texturing) {
  241.             glTexCoord2fv(t1);
  242.         }
  243.         glVertex2fv(v1);
  244.         if (texturing) {
  245.             glTexCoord2fv(t2);
  246.         }
  247.         glVertex2fv(v2);
  248.         if (texturing) {
  249.             glTexCoord2fv(t3);
  250.         }
  251.         glVertex2fv(v3);
  252.         }
  253.     glEnd();
  254.     }
  255.     glFinish();
  256.     Report("Triangles", Now()-start);
  257. }
  258.  
  259. static void Rects(void)
  260. {
  261.     GLint i, j;
  262.     float v1[2], v2[2];
  263.     double start;
  264.  
  265.     start = Now();
  266.     for (i = 0; i < repeatCount; i++) {
  267.     v1[0] = 10;
  268.     v1[1] = 10;
  269.     v2[0] = 20;
  270.     v2[1] = 20;
  271.     for (j = 0; j < loopCount; j++) {
  272.         glRectfv(v1, v2);
  273.     }
  274.     }
  275.     glFinish();
  276.     Report("Rects", Now()-start);
  277. }
  278.  
  279. static void Draw(void)
  280. {
  281.  
  282.     glClearColor(0.0, 0.0, 0.0, 0.0);
  283.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  284.  
  285.     TK_SETCOLOR(windType, (GLfloat)(GLint)TK_YELLOW);
  286.  
  287.     if (antialiasing) {
  288.     glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
  289.     glEnable(GL_BLEND);
  290.  
  291.     glEnable(GL_POINT_SMOOTH);
  292.     glEnable(GL_LINE_SMOOTH);
  293.     glEnable(GL_POLYGON_SMOOTH);
  294.     printf("antialiasing: on\n");
  295.     }
  296.     else {
  297.        glDisable(GL_BLEND);
  298.        glDisable(GL_POINT_SMOOTH);
  299.        glDisable(GL_LINE_SMOOTH);
  300.        glDisable(GL_POLYGON_SMOOTH);
  301.        printf("antialiasing: off\n");
  302.     }
  303.     if (depthTesting) {
  304.     glEnable(GL_DEPTH_TEST);
  305.     printf("depthtest: on\n");
  306.     }
  307.     else {
  308.        glDisable(GL_DEPTH_TEST);
  309.        printf("depthtest: off\n");
  310.     }
  311.     if (fogging) {
  312.     glEnable(GL_FOG);
  313.     glHint(GL_FOG_HINT, (niceFogging) ? GL_NICEST : GL_FASTEST);
  314.     printf("fog: on\n");
  315.     }
  316.     else {
  317.        glDisable(GL_FOG);
  318.        printf("fog: off\n");
  319.     }
  320.     if (lighting) {
  321.     static GLfloat ambient[4] = {1, 0.5, 0.5, 0};
  322.  
  323.     glEnable(GL_NORMALIZE);
  324.     glNormal3f(1.0, 1.0, 1.0);
  325.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
  326.     glEnable(GL_LIGHTING);
  327.     glEnable(GL_LIGHT0);
  328.     printf("lighting: on\n");
  329.     }
  330.     else {
  331.        glDisable( GL_LIGHTING );
  332.        printf("lighting: off\n");
  333.      }
  334.     if (shading) {
  335.        glShadeModel(GL_SMOOTH);
  336.        printf("shading: smooth\n");
  337.     }
  338.     else {
  339.        glShadeModel(GL_FLAT);
  340.        printf("shading: flat\n");
  341.     }
  342.     if (texturing) {
  343.     static GLfloat modulate[1] = {(GLfloat)(GLint)GL_DECAL};
  344.     static GLfloat clamp[1] = {(GLfloat)(GLint)GL_CLAMP};
  345.     static GLfloat linear[1] = {(GLfloat)(GLint)GL_LINEAR};
  346.  
  347.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  348.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE,
  349.              (GLvoid *)texture);
  350.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modulate);
  351.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
  352.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
  353.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear);
  354.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear);
  355.     glEnable(GL_TEXTURE_2D);
  356.     printf("texturing: on\n");
  357.     }
  358.     else {
  359.        glDisable( GL_TEXTURE_2D );
  360.        printf("texturing: off\n");
  361.      }
  362.  
  363.     Viewport(0, 0); Points();
  364.     Viewport(0, 1); Lines();
  365.     Viewport(0, 2); Triangles();
  366.     Viewport(0, 3); Rects();
  367.  
  368.     glFlush();
  369.  
  370.     if (doubleBuffer) {
  371.     tkSwapBuffers();
  372.     }
  373. }
  374.  
  375. static GLenum Args(int argc, char **argv)
  376. {
  377.     GLint i;
  378.  
  379.     rgb = GL_TRUE;
  380.     doubleBuffer = GL_FALSE;
  381.     directRender = GL_TRUE;
  382.  
  383.     for (i = 1; i < argc; i++) {
  384.     if (strcmp(argv[i], "-ci") == 0) {
  385.         rgb = GL_FALSE;
  386.     } else if (strcmp(argv[i], "-rgb") == 0) {
  387.         rgb = GL_TRUE;
  388.     } else if (strcmp(argv[i], "-sb") == 0) {
  389.         doubleBuffer = GL_FALSE;
  390.     } else if (strcmp(argv[i], "-db") == 0) {
  391.         doubleBuffer = GL_TRUE;
  392.     } else if (strcmp(argv[i], "-dr") == 0) {
  393.         directRender = GL_TRUE;
  394.     } else if (strcmp(argv[i], "-ir") == 0) {
  395.         directRender = GL_FALSE;
  396.     } else {
  397.         printf("%s (Bad option).\n", argv[i]);
  398.         return GL_FALSE;
  399.     }
  400.     }
  401.     return GL_TRUE;
  402. }
  403.  
  404. void main(int argc, char **argv)
  405. {
  406.     if (Args(argc, argv) == GL_FALSE) {
  407.     tkQuit();
  408.     }
  409.  
  410.     windW = 600;
  411.     windH = 300;
  412.     tkInitPosition(0, 0, windW, windH);
  413.  
  414.     windType = /*TK_ALPHA |*/ TK_DEPTH;
  415.     windType |= (rgb) ? TK_RGB : TK_INDEX;
  416.     windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  417.     windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  418.     tkInitDisplayMode(windType);
  419.  
  420.     if (tkInitWindow("Speed Test") == GL_FALSE) {
  421.     tkQuit();
  422.     }
  423.  
  424.     tkExposeFunc(SetWindSize);
  425.     tkReshapeFunc(SetWindSize);
  426.     tkKeyDownFunc(Key);
  427.     tkDisplayFunc(Draw);
  428.     tkExec();
  429. }
  430.