aboutsummaryrefslogtreecommitdiffstats
path: root/progs/demos/trispd.c
blob: c07c3409472d4414c071ce9ed824eb4fc4b2c5d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

/*
 * Simple GLUT program to measure triangle strip rendering speed.
 * Brian Paul  February 15, 1997  This file is in the public domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <GL/glut.h>


static float MinPeriod = 2.0;   /* 2 seconds */
static float Width = 400.0;
static float Height = 400.0;
static int Loops = 1;
static int Size = 50;
static int Texture = 0;



static void Idle( void )
{
   glutPostRedisplay();
}


static void Display( void )
{
   float x, y;
   float xStep;
   float yStep;
   double t0, t1;
   double triRate;
   double pixelRate;
   int triCount;
   int i;
   float red[3] = { 1.0, 0.0, 0.0 };
   float blue[3] = { 0.0, 0.0, 1.0 };

   xStep = yStep = sqrt( 2.0 * Size );

   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   triCount = 0;
   t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
   if (Texture) {
      float uStep = xStep / Width;
      float vStep = yStep / Height;
      float u, v;
      for (i=0; i<Loops; i++) {
	 for (y=1.0, v=0.0f; y<Height-yStep; y+=yStep, v+=vStep) {
	    glBegin(GL_TRIANGLE_STRIP);
	    for (x=1.0, u=0.0f; x<Width; x+=xStep, u+=uStep) {
	       glColor3fv(red);
	       glTexCoord2f(u, v);	    
	       glVertex2f(x, y);
	       glColor3fv(blue);
	       glTexCoord2f(u, v+vStep);
	       glVertex2f(x, y+yStep);
	       triCount += 2;
	    }
	    glEnd();
	    triCount -= 2;
	 }
      }
   }
   else {
      for (i=0; i<Loops; i++) {
	 for (y=1.0; y<Height-yStep; y+=yStep) {
	    glBegin(GL_TRIANGLE_STRIP);
	    for (x=1.0; x<Width; x+=xStep) {
	       glColor3fv(red);
	       glVertex2f(x, y);
	       glColor3fv(blue);
	       glVertex2f(x, y+yStep);
	       triCount += 2;
	    }
	    glEnd();
	    triCount -= 2;
	 }
      }
   }
   glFinish();
   t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
   
   if (t1-t0 < MinPeriod) {
      /* Next time draw more triangles to get longer elapsed time */
      Loops *= 2;
      return;
   }

   triRate = triCount / (t1-t0);
   pixelRate = triRate * Size;
   printf("Rate: %d tri in %gs = %g tri/s  %d pixels/s\n",
          triCount, t1-t0, triRate, (int)pixelRate);

   glutSwapBuffers();
}


static void Reshape( int width, int height )
{
   Width = width;
   Height = height;
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         exit(0);
         break;
   }
   glutPostRedisplay();
}


static void LoadTex(int comp, int filter)
{
   GLubyte *pixels;
   int x, y;
   pixels = (GLubyte *) malloc(4*256*256);
   for (y = 0; y < 256; ++y)
      for (x = 0; x < 256; ++x) {
	 pixels[(y*256+x)*4+0] = (int)(128.5 + 127.0 * cos(0.024544 * x));
	 pixels[(y*256+x)*4+1] = 255;
	 pixels[(y*256+x)*4+2] = (int)(128.5 + 127.0 * cos(0.024544 * y));
	 pixels[(y*256+x)*4+3] = 255;
      }
   glEnable(GL_TEXTURE_2D);
   glTexImage2D(GL_TEXTURE_2D, 0, comp, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   printf("Texture: GL_MODULATE, %d comps, %s\n", comp, filter == GL_NEAREST ? "GL_NEAREST" : "GL_LINEAR");
}


static void Init( int argc, char *argv[] )
{
   GLint shade;
   GLint rBits, gBits, bBits;
   int filter = GL_NEAREST, comp = 3;

   int i;
   for (i=1; i<argc; i++) {
      if (strcmp(argv[i],"-dither")==0)
         glDisable(GL_DITHER);
      else if (strcmp(argv[i],"+dither")==0)
         glEnable(GL_DITHER);
      else if (strcmp(argv[i],"+smooth")==0)
         glShadeModel(GL_SMOOTH);
      else if (strcmp(argv[i],"+flat")==0)
         glShadeModel(GL_FLAT);
      else if (strcmp(argv[i],"+depth")==0)
         glEnable(GL_DEPTH_TEST);
      else if (strcmp(argv[i],"-depth")==0)
         glDisable(GL_DEPTH_TEST);
      else if (strcmp(argv[i],"-size")==0) {
         Size = atoi(argv[i+1]);
         i++;
      }
      else if (strcmp(argv[i],"-texture")==0)
	 Texture = 0;
      else if (strcmp(argv[i],"+texture")==0)
	 Texture = 1;
      else if (strcmp(argv[i],"-linear")==0)
	 filter = GL_NEAREST;
      else if (strcmp(argv[i],"+linear")==0)
	 filter = GL_LINEAR;
      else if (strcmp(argv[i],"-persp")==0)
	 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
      else if (strcmp(argv[i],"+persp")==0)
	 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
      else if (strcmp(argv[i],"-comp")==0) {
	 comp = atoi(argv[i+1]);
	 i++;
      }
      else
         printf("Unknown option: %s\n", argv[i]);
   }

   glGetIntegerv(GL_SHADE_MODEL, &shade);

   printf("Dither: %s\n", glIsEnabled(GL_DITHER) ? "on" : "off");
   printf("ShadeModel: %s\n", (shade==GL_FLAT) ? "flat" : "smooth");
   printf("DepthTest: %s\n", glIsEnabled(GL_DEPTH_TEST) ? "on" : "off");
   printf("Size: %d pixels\n", Size);

   if (Texture)
      LoadTex(comp, filter);

   glGetIntegerv(GL_RED_BITS, &rBits);
   glGetIntegerv(GL_GREEN_BITS, &gBits);
   glGetIntegerv(GL_BLUE_BITS, &bBits);
   printf("RedBits: %d  GreenBits: %d  BlueBits: %d\n", rBits, gBits, bBits);
}


static void Help( const char *program )
{
   printf("%s options:\n", program);
   printf("  +/-dither      enable/disable dithering\n");
   printf("  +/-depth       enable/disable depth test\n");
   printf("  +flat          flat shading\n");
   printf("  +smooth        smooth shading\n");
   printf("  -size pixels   specify pixels/triangle\n");
   printf("  +/-texture     enable/disable texture\n");
   printf("  -comp n        texture format\n");
   printf("  +/-linear      bilinear texture filter\n");
   printf("  +/-persp       perspective correction hint\n");
}


int main( int argc, char *argv[] )
{
   glutInitWindowSize( (int) Width, (int) Height );
   glutInit( &argc, argv );
   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
   glutCreateWindow( argv[0] );

   printf("For options:  %s -help\n", argv[0]);
   if (argc==2 && strcmp(argv[1],"-help")==0) {
      Help(argv[0]);
      return 0;
   }

   Init( argc, argv );

   glutReshapeFunc( Reshape );
   glutKeyboardFunc( Key );
   glutDisplayFunc( Display );
   glutIdleFunc( Idle );

   glutMainLoop();
   return 0;
}