Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame^] | 1 | /* $Id: trispd.c,v 1.4 2002/07/12 15:54:02 brianp Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Simple GLUT program to measure triangle strip rendering speed. |
| 5 | * Brian Paul February 15, 1997 This file is in the public domain. |
| 6 | */ |
| 7 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <math.h> |
| 11 | #include <string.h> |
| 12 | #include <GL/glut.h> |
| 13 | |
| 14 | |
| 15 | static float MinPeriod = 2.0; /* 2 seconds */ |
| 16 | static float Width = 400.0; |
| 17 | static float Height = 400.0; |
| 18 | static int Loops = 1; |
| 19 | static int Size = 50; |
| 20 | static int Texture = 0; |
| 21 | |
| 22 | |
| 23 | |
| 24 | static void Idle( void ) |
| 25 | { |
| 26 | glutPostRedisplay(); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | static void Display( void ) |
| 31 | { |
| 32 | float x, y; |
| 33 | float xStep; |
| 34 | float yStep; |
| 35 | double t0, t1; |
| 36 | double triRate; |
| 37 | double pixelRate; |
| 38 | int triCount; |
| 39 | int i; |
| 40 | float red[3] = { 1.0, 0.0, 0.0 }; |
| 41 | float blue[3] = { 0.0, 0.0, 1.0 }; |
| 42 | |
| 43 | xStep = yStep = sqrt( 2.0 * Size ); |
| 44 | |
| 45 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 46 | |
| 47 | triCount = 0; |
| 48 | t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001; |
| 49 | if (Texture) { |
| 50 | float uStep = xStep / Width; |
| 51 | float vStep = yStep / Height; |
| 52 | float u, v; |
| 53 | for (i=0; i<Loops; i++) { |
| 54 | for (y=1.0, v=0.0f; y<Height-yStep; y+=yStep, v+=vStep) { |
| 55 | glBegin(GL_TRIANGLE_STRIP); |
| 56 | for (x=1.0, u=0.0f; x<Width; x+=xStep, u+=uStep) { |
| 57 | glColor3fv(red); |
| 58 | glTexCoord2f(u, v); |
| 59 | glVertex2f(x, y); |
| 60 | glColor3fv(blue); |
| 61 | glTexCoord2f(u, v+vStep); |
| 62 | glVertex2f(x, y+yStep); |
| 63 | triCount += 2; |
| 64 | } |
| 65 | glEnd(); |
| 66 | triCount -= 2; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | else { |
| 71 | for (i=0; i<Loops; i++) { |
| 72 | for (y=1.0; y<Height-yStep; y+=yStep) { |
| 73 | glBegin(GL_TRIANGLE_STRIP); |
| 74 | for (x=1.0; x<Width; x+=xStep) { |
| 75 | glColor3fv(red); |
| 76 | glVertex2f(x, y); |
| 77 | glColor3fv(blue); |
| 78 | glVertex2f(x, y+yStep); |
| 79 | triCount += 2; |
| 80 | } |
| 81 | glEnd(); |
| 82 | triCount -= 2; |
| 83 | } |
| 84 | } |
| 85 | } |
Brian Paul | 27a26bf | 2000-10-26 15:26:14 +0000 | [diff] [blame] | 86 | glFinish(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 87 | t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001; |
| 88 | |
| 89 | if (t1-t0 < MinPeriod) { |
| 90 | /* Next time draw more triangles to get longer elapsed time */ |
| 91 | Loops *= 2; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | triRate = triCount / (t1-t0); |
| 96 | pixelRate = triRate * Size; |
| 97 | printf("Rate: %d tri in %gs = %g tri/s %d pixels/s\n", |
| 98 | triCount, t1-t0, triRate, (int)pixelRate); |
| 99 | |
| 100 | glutSwapBuffers(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static 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 | |
| 117 | static 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 | |
| 130 | static void LoadTex(int comp, int filter) |
| 131 | { |
| 132 | GLubyte *pixels; |
| 133 | int x, y; |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame^] | 134 | pixels = (GLubyte *) malloc(4*256*256); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 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 | |
| 153 | static 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 | |
| 214 | static 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 | |
| 229 | int main( int argc, char *argv[] ) |
| 230 | { |
| 231 | printf("For options: %s -help\n", argv[0]); |
| 232 | glutInit( &argc, argv ); |
| 233 | glutInitWindowSize( (int) Width, (int) Height ); |
| 234 | glutInitWindowPosition( 0, 0 ); |
| 235 | |
| 236 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); |
| 237 | |
| 238 | glutCreateWindow( argv[0] ); |
| 239 | |
| 240 | if (argc==2 && strcmp(argv[1],"-help")==0) { |
| 241 | Help(argv[0]); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | Init( argc, argv ); |
| 246 | |
| 247 | glutReshapeFunc( Reshape ); |
| 248 | glutKeyboardFunc( Key ); |
| 249 | glutDisplayFunc( Display ); |
| 250 | glutIdleFunc( Idle ); |
| 251 | |
| 252 | glutMainLoop(); |
| 253 | return 0; |
| 254 | } |