e25cb3374acce90ec9cd9624f261c90a0989ad0a
3 * glDrawPixels demo/test/benchmark
5 * Brian Paul September 25, 1997 This file is in the public domain.
15 #define IMAGE_FILE "../images/girl.rgb"
17 static int ImgWidth
, ImgHeight
;
18 static GLenum ImgFormat
;
19 static GLubyte
*Image
= NULL
;
21 static int Xpos
, Ypos
;
22 static int SkipPixels
, SkipRows
;
23 static int DrawWidth
, DrawHeight
;
24 static int Scissor
= 0;
26 static GLfloat Zpos
= -1.0;
27 static float Xzoom
, Yzoom
;
28 static GLboolean DrawFront
= GL_FALSE
;
29 static GLboolean Dither
= GL_TRUE
;
32 static void Reset( void )
36 DrawHeight
= ImgHeight
;
37 SkipPixels
= SkipRows
= 0;
45 static void Display( void )
47 glClear( GL_COLOR_BUFFER_BIT
);
50 glRasterPos2i(Xpos
, Ypos
);
52 /* This allows negative raster positions: */
53 glRasterPos3f(0, 0, Zpos
);
54 glBitmap(0, 0, 0, 0, Xpos
, Ypos
, NULL
);
57 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
58 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
60 glPixelZoom( Xzoom
, Yzoom
);
63 glEnable(GL_SCISSOR_TEST
);
68 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
70 glDisable(GL_SCISSOR_TEST
);
78 static void Benchmark( void )
80 int startTime
, endTime
;
82 double seconds
, pixelsPerSecond
;
84 printf("Benchmarking...\n");
86 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
87 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
88 glPixelZoom( Xzoom
, Yzoom
);
90 glEnable(GL_SCISSOR_TEST
);
95 glDrawBuffer(GL_FRONT
);
97 glDrawBuffer(GL_BACK
);
101 startTime
= glutGet(GLUT_ELAPSED_TIME
);
103 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
105 endTime
= glutGet(GLUT_ELAPSED_TIME
);
106 } while (endTime
- startTime
< 4000); /* 4 seconds */
109 glDisable(GL_SCISSOR_TEST
);
113 seconds
= (double) (endTime
- startTime
) / 1000.0;
114 pixelsPerSecond
= draws
* DrawWidth
* DrawHeight
/ seconds
;
115 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
116 draws
, seconds
, pixelsPerSecond
);
120 static void Reshape( int width
, int height
)
122 glViewport( 0, 0, width
, height
);
123 glMatrixMode( GL_PROJECTION
);
125 glOrtho( 0.0, width
, 0.0, height
, 0.0, 2.0 );
126 glMatrixMode( GL_MODELVIEW
);
129 glScissor(width
/4, height
/4, width
/2, height
/2);
133 static void Key( unsigned char key
, int x
, int y
)
146 glDisable(GL_DITHER
);
193 printf("RasterPos Z = %g\n", Zpos
);
197 printf("RasterPos Z = %g\n", Zpos
);
204 printf("Fog %d\n", Fog
);
207 DrawFront
= !DrawFront
;
209 glDrawBuffer(GL_FRONT
);
211 glDrawBuffer(GL_BACK
);
212 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
222 static void SpecialKey( int key
, int x
, int y
)
244 static void Init( GLboolean ciMode
)
246 static const GLfloat fogColor
[4] = {0, 1, 0, 0};
248 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
249 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
251 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
253 printf("Couldn't read %s\n", IMAGE_FILE
);
258 /* Convert RGB image to grayscale */
259 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
261 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
262 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
263 indexImage
[i
] = gray
/ 3;
267 ImgFormat
= GL_COLOR_INDEX
;
269 for (i
=0;i
<255;i
++) {
271 glutSetColor(i
, g
, g
, g
);
275 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
277 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
278 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
280 glFogi(GL_FOG_MODE
, GL_LINEAR
);
281 glFogf(GL_FOG_START
, 0);
282 glFogf(GL_FOG_END
, 2);
283 glFogfv(GL_FOG_COLOR
, fogColor
);
289 static void Usage(void)
292 printf(" SPACE Reset Parameters\n");
293 printf(" Up/Down Move image up/down\n");
294 printf(" Left/Right Move image left/right\n");
295 printf(" x Decrease X-axis PixelZoom\n");
296 printf(" X Increase X-axis PixelZoom\n");
297 printf(" y Decrease Y-axis PixelZoom\n");
298 printf(" Y Increase Y-axis PixelZoom\n");
299 printf(" w Decrease glDrawPixels width*\n");
300 printf(" W Increase glDrawPixels width*\n");
301 printf(" h Decrease glDrawPixels height*\n");
302 printf(" H Increase glDrawPixels height*\n");
303 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
304 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
305 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
306 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
307 printf(" s Toggle GL_SCISSOR_TEST\n");
308 printf(" F Toggle GL_FOG\n");
309 printf(" z Decrease RasterPos Z\n");
310 printf(" Z Increase RasterPos Z\n");
312 printf(" f Toggle front/back buffer drawing\n");
313 printf(" b Benchmark test\n");
314 printf(" ESC Exit\n");
315 printf("* Warning: no limits are imposed on these parameters so it's\n");
316 printf(" possible to cause a segfault if you go too far.\n");
320 int main( int argc
, char *argv
[] )
322 GLboolean ciMode
= GL_FALSE
;
324 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
328 glutInit( &argc
, argv
);
329 glutInitWindowPosition( 0, 0 );
330 glutInitWindowSize( 500, 400 );
333 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
335 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
337 glutCreateWindow(argv
[0]);
342 glutReshapeFunc( Reshape
);
343 glutKeyboardFunc( Key
);
344 glutSpecialFunc( SpecialKey
);
345 glutDisplayFunc( Display
);