trispd.c revision 32001f49
1
2/*
3 * Simple GLUT program to measure triangle strip rendering speed.
4 * Brian Paul  February 15, 1997  This file is in the public domain.
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10#include <string.h>
11#include "glut_wrap.h"
12
13
14static float MinPeriod = 2.0;   /* 2 seconds */
15static float Width = 400.0;
16static float Height = 400.0;
17static int Loops = 1;
18static int Size = 50;
19static int Texture = 0;
20
21
22
23static void Idle( void )
24{
25   glutPostRedisplay();
26}
27
28
29static void Display( void )
30{
31   float x, y;
32   float xStep;
33   float yStep;
34   double t0, t1;
35   double triRate;
36   double pixelRate;
37   int triCount;
38   int i;
39   float red[3] = { 1.0, 0.0, 0.0 };
40   float blue[3] = { 0.0, 0.0, 1.0 };
41
42   xStep = yStep = sqrt( 2.0 * Size );
43
44   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
45
46   triCount = 0;
47   t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
48   if (Texture) {
49      float uStep = xStep / Width;
50      float vStep = yStep / Height;
51      float u, v;
52      for (i=0; i<Loops; i++) {
53	 for (y=1.0, v=0.0f; y<Height-yStep; y+=yStep, v+=vStep) {
54	    glBegin(GL_TRIANGLE_STRIP);
55	    for (x=1.0, u=0.0f; x<Width; x+=xStep, u+=uStep) {
56	       glColor3fv(red);
57	       glTexCoord2f(u, v);
58	       glVertex2f(x, y);
59	       glColor3fv(blue);
60	       glTexCoord2f(u, v+vStep);
61	       glVertex2f(x, y+yStep);
62	       triCount += 2;
63	    }
64	    glEnd();
65	    triCount -= 2;
66	 }
67      }
68   }
69   else {
70      for (i=0; i<Loops; i++) {
71	 for (y=1.0; y<Height-yStep; y+=yStep) {
72	    glBegin(GL_TRIANGLE_STRIP);
73	    for (x=1.0; x<Width; x+=xStep) {
74	       glColor3fv(red);
75	       glVertex2f(x, y);
76	       glColor3fv(blue);
77	       glVertex2f(x, y+yStep);
78	       triCount += 2;
79	    }
80	    glEnd();
81	    triCount -= 2;
82	 }
83      }
84   }
85   glFinish();
86   t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
87
88   if (t1-t0 < MinPeriod) {
89      /* Next time draw more triangles to get longer elapsed time */
90      Loops *= 2;
91      return;
92   }
93
94   triRate = triCount / (t1-t0);
95   pixelRate = triRate * Size;
96   printf("Rate: %d tri in %gs = %g tri/s  %d pixels/s\n",
97          triCount, t1-t0, triRate, (int)pixelRate);
98
99   glutSwapBuffers();
100}
101
102
103static void Reshape( int width, int height )
104{
105   Width = width;
106   Height = height;
107   glViewport( 0, 0, width, height );
108   glMatrixMode( GL_PROJECTION );
109   glLoadIdentity();
110   glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
111   glMatrixMode( GL_MODELVIEW );
112   glLoadIdentity();
113}
114
115
116static void Key( unsigned char key, int x, int y )
117{
118   (void) x;
119   (void) y;
120   switch (key) {
121      case 27:
122         exit(0);
123         break;
124   }
125   glutPostRedisplay();
126}
127
128
129static void LoadTex(int comp, int filter)
130{
131   GLubyte *pixels;
132   int x, y;
133   pixels = (GLubyte *) malloc(4*256*256);
134   for (y = 0; y < 256; ++y)
135      for (x = 0; x < 256; ++x) {
136	 pixels[(y*256+x)*4+0] = (int)(128.5 + 127.0 * cos(0.024544 * x));
137	 pixels[(y*256+x)*4+1] = 255;
138	 pixels[(y*256+x)*4+2] = (int)(128.5 + 127.0 * cos(0.024544 * y));
139	 pixels[(y*256+x)*4+3] = 255;
140      }
141   glEnable(GL_TEXTURE_2D);
142   glTexImage2D(GL_TEXTURE_2D, 0, comp, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
143   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
144   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
145   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
146   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
147   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
148   printf("Texture: GL_MODULATE, %d comps, %s\n", comp, filter == GL_NEAREST ? "GL_NEAREST" : "GL_LINEAR");
149}
150
151
152static void Init( int argc, char *argv[] )
153{
154   GLint shade;
155   GLint rBits, gBits, bBits;
156   int filter = GL_NEAREST, comp = 3;
157
158   int i;
159   for (i=1; i<argc; i++) {
160      if (strcmp(argv[i],"-dither")==0)
161         glDisable(GL_DITHER);
162      else if (strcmp(argv[i],"+dither")==0)
163         glEnable(GL_DITHER);
164      else if (strcmp(argv[i],"+smooth")==0)
165         glShadeModel(GL_SMOOTH);
166      else if (strcmp(argv[i],"+flat")==0)
167         glShadeModel(GL_FLAT);
168      else if (strcmp(argv[i],"+depth")==0)
169         glEnable(GL_DEPTH_TEST);
170      else if (strcmp(argv[i],"-depth")==0)
171         glDisable(GL_DEPTH_TEST);
172      else if (strcmp(argv[i],"-size")==0) {
173         Size = atoi(argv[i+1]);
174         i++;
175      }
176      else if (strcmp(argv[i],"-texture")==0)
177	 Texture = 0;
178      else if (strcmp(argv[i],"+texture")==0)
179	 Texture = 1;
180      else if (strcmp(argv[i],"-linear")==0)
181	 filter = GL_NEAREST;
182      else if (strcmp(argv[i],"+linear")==0)
183	 filter = GL_LINEAR;
184      else if (strcmp(argv[i],"-persp")==0)
185	 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
186      else if (strcmp(argv[i],"+persp")==0)
187	 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
188      else if (strcmp(argv[i],"-comp")==0) {
189	 comp = atoi(argv[i+1]);
190	 i++;
191      }
192      else
193         printf("Unknown option: %s\n", argv[i]);
194   }
195
196   glGetIntegerv(GL_SHADE_MODEL, &shade);
197
198   printf("Dither: %s\n", glIsEnabled(GL_DITHER) ? "on" : "off");
199   printf("ShadeModel: %s\n", (shade==GL_FLAT) ? "flat" : "smooth");
200   printf("DepthTest: %s\n", glIsEnabled(GL_DEPTH_TEST) ? "on" : "off");
201   printf("Size: %d pixels\n", Size);
202
203   if (Texture)
204      LoadTex(comp, filter);
205
206   glGetIntegerv(GL_RED_BITS, &rBits);
207   glGetIntegerv(GL_GREEN_BITS, &gBits);
208   glGetIntegerv(GL_BLUE_BITS, &bBits);
209   printf("RedBits: %d  GreenBits: %d  BlueBits: %d\n", rBits, gBits, bBits);
210}
211
212
213static void Help( const char *program )
214{
215   printf("%s options:\n", program);
216   printf("  +/-dither      enable/disable dithering\n");
217   printf("  +/-depth       enable/disable depth test\n");
218   printf("  +flat          flat shading\n");
219   printf("  +smooth        smooth shading\n");
220   printf("  -size pixels   specify pixels/triangle\n");
221   printf("  +/-texture     enable/disable texture\n");
222   printf("  -comp n        texture format\n");
223   printf("  +/-linear      bilinear texture filter\n");
224   printf("  +/-persp       perspective correction hint\n");
225}
226
227
228int main( int argc, char *argv[] )
229{
230   glutInitWindowSize( (int) Width, (int) Height );
231   glutInit( &argc, argv );
232   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
233   glutCreateWindow( argv[0] );
234
235   printf("For options:  %s -help\n", argv[0]);
236   if (argc==2 && strcmp(argv[1],"-help")==0) {
237      Help(argv[0]);
238      return 0;
239   }
240
241   Init( argc, argv );
242
243   glutReshapeFunc( Reshape );
244   glutKeyboardFunc( Key );
245   glutDisplayFunc( Display );
246   glutIdleFunc( Idle );
247
248   glutMainLoop();
249   return 0;
250}
251