3 * Simple GLUT program to measure triangle strip rendering speed.
4 * Brian Paul February 15, 1997 This file is in the public domain.
14 static float MinPeriod
= 2.0; /* 2 seconds */
15 static float Width
= 400.0;
16 static float Height
= 400.0;
19 static int Texture
= 0;
23 static void Idle( void )
29 static void Display( void )
39 float red
[3] = { 1.0, 0.0, 0.0 };
40 float blue
[3] = { 0.0, 0.0, 1.0 };
42 xStep
= yStep
= sqrt( 2.0 * Size
);
44 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
47 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
49 float uStep
= xStep
/ Width
;
50 float vStep
= yStep
/ Height
;
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
) {
60 glTexCoord2f(u
, v
+vStep
);
61 glVertex2f(x
, y
+yStep
);
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
) {
77 glVertex2f(x
, y
+yStep
);
86 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
88 if (t1
-t0
< MinPeriod
) {
89 /* Next time draw more triangles to get longer elapsed time */
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
);
103 static void Reshape( int width
, int height
)
107 glViewport( 0, 0, width
, height
);
108 glMatrixMode( GL_PROJECTION
);
110 glOrtho(0.0, width
, 0.0, height
, -1.0, 1.0);
111 glMatrixMode( GL_MODELVIEW
);
116 static void Key( unsigned char key
, int x
, int y
)
129 static void LoadTex(int comp
, int filter
)
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;
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");
152 static void Init( int argc
, char *argv
[] )
155 GLint rBits
, gBits
, bBits
;
156 int filter
= GL_NEAREST
, comp
= 3;
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)
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]);
176 else if (strcmp(argv
[i
],"-texture")==0)
178 else if (strcmp(argv
[i
],"+texture")==0)
180 else if (strcmp(argv
[i
],"-linear")==0)
182 else if (strcmp(argv
[i
],"+linear")==0)
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]);
193 printf("Unknown option: %s\n", argv
[i
]);
196 glGetIntegerv(GL_SHADE_MODEL
, &shade
);
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
);
204 LoadTex(comp
, filter
);
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
);
213 static void Help( const char *program
)
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");
228 int main( int argc
, char *argv
[] )
230 printf("For options: %s -help\n", argv
[0]);
231 glutInit( &argc
, argv
);
232 glutInitWindowSize( (int) Width
, (int) Height
);
233 glutInitWindowPosition( 0, 0 );
235 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
237 glutCreateWindow( argv
[0] );
239 if (argc
==2 && strcmp(argv
[1],"-help")==0) {
246 glutReshapeFunc( Reshape
);
247 glutKeyboardFunc( Key
);
248 glutDisplayFunc( Display
);
249 glutIdleFunc( Idle
);