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