3 * glDrawPixels demo/test/benchmark
5 * Brian Paul September 25, 1997 This file is in the public domain.
16 #define IMAGE_FILE "../images/girl.rgb"
18 static int ImgWidth
, ImgHeight
;
19 static GLenum ImgFormat
;
20 static GLubyte
*Image
= NULL
;
22 static int Xpos
, Ypos
;
23 static int SkipPixels
, SkipRows
;
24 static int DrawWidth
, DrawHeight
;
25 static int Scissor
= 0;
27 static GLfloat Zpos
= -1.0;
28 static float Xzoom
, Yzoom
;
29 static GLboolean DrawFront
= GL_FALSE
;
30 static GLboolean Dither
= GL_TRUE
;
34 static void Reset( void )
38 DrawHeight
= ImgHeight
;
39 SkipPixels
= SkipRows
= 0;
47 static void Display( void )
49 glClear( GL_COLOR_BUFFER_BIT
);
52 glRasterPos2i(Xpos
, Ypos
);
54 /* This allows negative raster positions: */
55 glRasterPos3f(0, 0, Zpos
);
56 glBitmap(0, 0, 0, 0, Xpos
, Ypos
, NULL
);
59 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
60 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
62 glPixelZoom( Xzoom
, Yzoom
);
65 glEnable(GL_SCISSOR_TEST
);
70 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
72 glDisable(GL_SCISSOR_TEST
);
82 static void Benchmark( void )
84 int startTime
, endTime
;
86 double seconds
, pixelsPerSecond
;
88 printf("Benchmarking...\n");
90 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
91 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
92 glPixelZoom( Xzoom
, Yzoom
);
94 glEnable(GL_SCISSOR_TEST
);
99 glDrawBuffer(GL_FRONT
);
101 glDrawBuffer(GL_BACK
);
103 /* Run timing test */
105 startTime
= glutGet(GLUT_ELAPSED_TIME
);
107 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
109 endTime
= glutGet(GLUT_ELAPSED_TIME
);
110 } while (endTime
- startTime
< 4000); /* 4 seconds */
113 glDisable(GL_SCISSOR_TEST
);
117 seconds
= (double) (endTime
- startTime
) / 1000.0;
118 pixelsPerSecond
= draws
* DrawWidth
* DrawHeight
/ seconds
;
119 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
120 draws
, seconds
, pixelsPerSecond
);
124 static void Reshape( int width
, int height
)
126 glViewport( 0, 0, width
, height
);
127 glMatrixMode( GL_PROJECTION
);
129 glOrtho( 0.0, width
, 0.0, height
, 0.0, 2.0 );
130 glMatrixMode( GL_MODELVIEW
);
133 glScissor(width
/4, height
/4, width
/2, height
/2);
137 static void Key( unsigned char key
, int x
, int y
)
150 glDisable(GL_DITHER
);
197 printf("RasterPos Z = %g\n", Zpos
);
201 printf("RasterPos Z = %g\n", Zpos
);
208 printf("Fog %d\n", Fog
);
211 DrawFront
= !DrawFront
;
213 glDrawBuffer(GL_FRONT
);
215 glDrawBuffer(GL_BACK
);
216 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
219 glutDestroyWindow(win
);
227 static void SpecialKey( int key
, int x
, int y
)
249 static void Init( GLboolean ciMode
, const char *filename
)
251 static const GLfloat fogColor
[4] = {0, 1, 0, 0};
253 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
254 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
256 Image
= LoadRGBImage( filename
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
258 printf("Couldn't read %s\n", filename
);
263 /* Convert RGB image to grayscale */
264 GLubyte
*indexImage
= (GLubyte
*) malloc( ImgWidth
* ImgHeight
);
266 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
267 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
268 indexImage
[i
] = gray
/ 3;
272 ImgFormat
= GL_COLOR_INDEX
;
274 for (i
=0;i
<255;i
++) {
276 glutSetColor(i
, g
, g
, g
);
280 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
282 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
283 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
285 glFogi(GL_FOG_MODE
, GL_LINEAR
);
286 glFogf(GL_FOG_START
, 0);
287 glFogf(GL_FOG_END
, 2);
288 glFogfv(GL_FOG_COLOR
, fogColor
);
294 static void Usage(void)
297 printf(" SPACE Reset Parameters\n");
298 printf(" Up/Down Move image up/down\n");
299 printf(" Left/Right Move image left/right\n");
300 printf(" x Decrease X-axis PixelZoom\n");
301 printf(" X Increase X-axis PixelZoom\n");
302 printf(" y Decrease Y-axis PixelZoom\n");
303 printf(" Y Increase Y-axis PixelZoom\n");
304 printf(" w Decrease glDrawPixels width*\n");
305 printf(" W Increase glDrawPixels width*\n");
306 printf(" h Decrease glDrawPixels height*\n");
307 printf(" H Increase glDrawPixels height*\n");
308 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
309 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
310 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
311 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
312 printf(" s Toggle GL_SCISSOR_TEST\n");
313 printf(" F Toggle GL_FOG\n");
314 printf(" z Decrease RasterPos Z\n");
315 printf(" Z Increase RasterPos Z\n");
317 printf(" f Toggle front/back buffer drawing\n");
318 printf(" b Benchmark test\n");
319 printf(" ESC Exit\n");
320 printf("* Warning: no limits are imposed on these parameters so it's\n");
321 printf(" possible to cause a segfault if you go too far.\n");
325 int main( int argc
, char *argv
[] )
327 GLboolean ciMode
= GL_FALSE
;
328 const char *filename
= IMAGE_FILE
;
331 glutInitWindowSize( 500, 400 );
332 glutInit( &argc
, argv
);
334 if (argc
> i
&& strcmp(argv
[i
], "-ci")==0) {
343 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
345 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
347 win
= glutCreateWindow(argv
[0]);
349 Init(ciMode
, filename
);
352 glutReshapeFunc( Reshape
);
353 glutKeyboardFunc( Key
);
354 glutSpecialFunc( SpecialKey
);
355 glutDisplayFunc( Display
);