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 fflush(stdout); 99 100 glutSwapBuffers(); 101} 102 103 104static void Reshape( int width, int height ) 105{ 106 Width = width; 107 Height = height; 108 glViewport( 0, 0, width, height ); 109 glMatrixMode( GL_PROJECTION ); 110 glLoadIdentity(); 111 glOrtho(0.0, width, 0.0, height, -1.0, 1.0); 112 glMatrixMode( GL_MODELVIEW ); 113 glLoadIdentity(); 114} 115 116 117static void Key( unsigned char key, int x, int y ) 118{ 119 (void) x; 120 (void) y; 121 switch (key) { 122 case 27: 123 exit(0); 124 break; 125 } 126 glutPostRedisplay(); 127} 128 129 130static void LoadTex(int comp, int filter) 131{ 132 GLubyte *pixels; 133 int x, y; 134 pixels = (GLubyte *) malloc(4*256*256); 135 for (y = 0; y < 256; ++y) 136 for (x = 0; x < 256; ++x) { 137 pixels[(y*256+x)*4+0] = (int)(128.5 + 127.0 * cos(0.024544 * x)); 138 pixels[(y*256+x)*4+1] = 255; 139 pixels[(y*256+x)*4+2] = (int)(128.5 + 127.0 * cos(0.024544 * y)); 140 pixels[(y*256+x)*4+3] = 255; 141 } 142 glEnable(GL_TEXTURE_2D); 143 glTexImage2D(GL_TEXTURE_2D, 0, comp, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); 144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); 145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); 146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 148 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 149 printf("Texture: GL_MODULATE, %d comps, %s\n", comp, filter == GL_NEAREST ? "GL_NEAREST" : "GL_LINEAR"); 150} 151 152 153static void Init( int argc, char *argv[] ) 154{ 155 GLint shade; 156 GLint rBits, gBits, bBits; 157 int filter = GL_NEAREST, comp = 3; 158 159 int i; 160 for (i=1; i<argc; i++) { 161 if (strcmp(argv[i],"-dither")==0) 162 glDisable(GL_DITHER); 163 else if (strcmp(argv[i],"+dither")==0) 164 glEnable(GL_DITHER); 165 else if (strcmp(argv[i],"+smooth")==0) 166 glShadeModel(GL_SMOOTH); 167 else if (strcmp(argv[i],"+flat")==0) 168 glShadeModel(GL_FLAT); 169 else if (strcmp(argv[i],"+depth")==0) 170 glEnable(GL_DEPTH_TEST); 171 else if (strcmp(argv[i],"-depth")==0) 172 glDisable(GL_DEPTH_TEST); 173 else if (strcmp(argv[i],"-size")==0) { 174 Size = atoi(argv[i+1]); 175 i++; 176 } 177 else if (strcmp(argv[i],"-texture")==0) 178 Texture = 0; 179 else if (strcmp(argv[i],"+texture")==0) 180 Texture = 1; 181 else if (strcmp(argv[i],"-linear")==0) 182 filter = GL_NEAREST; 183 else if (strcmp(argv[i],"+linear")==0) 184 filter = GL_LINEAR; 185 else if (strcmp(argv[i],"-persp")==0) 186 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); 187 else if (strcmp(argv[i],"+persp")==0) 188 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 189 else if (strcmp(argv[i],"-comp")==0) { 190 comp = atoi(argv[i+1]); 191 i++; 192 } 193 else 194 printf("Unknown option: %s\n", argv[i]); 195 } 196 197 glGetIntegerv(GL_SHADE_MODEL, &shade); 198 199 printf("Dither: %s\n", glIsEnabled(GL_DITHER) ? "on" : "off"); 200 printf("ShadeModel: %s\n", (shade==GL_FLAT) ? "flat" : "smooth"); 201 printf("DepthTest: %s\n", glIsEnabled(GL_DEPTH_TEST) ? "on" : "off"); 202 printf("Size: %d pixels\n", Size); 203 204 if (Texture) 205 LoadTex(comp, filter); 206 207 glGetIntegerv(GL_RED_BITS, &rBits); 208 glGetIntegerv(GL_GREEN_BITS, &gBits); 209 glGetIntegerv(GL_BLUE_BITS, &bBits); 210 printf("RedBits: %d GreenBits: %d BlueBits: %d\n", rBits, gBits, bBits); 211} 212 213 214static void Help( const char *program ) 215{ 216 printf("%s options:\n", program); 217 printf(" +/-dither enable/disable dithering\n"); 218 printf(" +/-depth enable/disable depth test\n"); 219 printf(" +flat flat shading\n"); 220 printf(" +smooth smooth shading\n"); 221 printf(" -size pixels specify pixels/triangle\n"); 222 printf(" +/-texture enable/disable texture\n"); 223 printf(" -comp n texture format\n"); 224 printf(" +/-linear bilinear texture filter\n"); 225 printf(" +/-persp perspective correction hint\n"); 226} 227 228 229int main( int argc, char *argv[] ) 230{ 231 glutInitWindowSize( (int) Width, (int) Height ); 232 glutInit( &argc, argv ); 233 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 234 glutCreateWindow( argv[0] ); 235 236 printf("For options: %s -help\n", argv[0]); 237 if (argc==2 && strcmp(argv[1],"-help")==0) { 238 Help(argv[0]); 239 return 0; 240 } 241 242 Init( argc, argv ); 243 244 glutReshapeFunc( Reshape ); 245 glutKeyboardFunc( Key ); 246 glutDisplayFunc( Display ); 247 glutIdleFunc( Idle ); 248 249 glutMainLoop(); 250 return 0; 251} 252