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