333cab36086058ff543cdba6f194f525e07eef15
1 /* $Id: drawpix.c,v 1.6 2002/01/26 17:49:30 brianp Exp $ */
4 * glDrawPixels demo/test/benchmark
6 * Brian Paul September 25, 1997 This file is in the public domain.
11 * Revision 1.6 2002/01/26 17:49:30 brianp
12 * added fog and raster Z position controls
14 * Revision 1.5 2000/12/24 22:53:54 pesco
15 * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
16 * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
17 * Essentially the same.
18 * Program files updated to include "readtex.c", not "../util/readtex.c".
19 * * demos/reflect.c: Likewise for "showbuffer.c".
22 * * Makefile.am (EXTRA_DIST): Added top-level regular files.
24 * * include/GL/Makefile.am (INC_X11): Added glxext.h.
27 * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
28 * Mesa GGI headers in dist even if HAVE_GGI is not given.
30 * * configure.in: Look for GLUT and demo source dirs in $srcdir.
32 * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
33 * More source list updates in various Makefile.am's.
35 * * Makefile.am (dist-hook): Remove CVS directory from distribution.
36 * (DIST_SUBDIRS): List all possible subdirs here.
37 * (SUBDIRS): Only list subdirs selected for build again.
38 * The above two applied to all subdir Makefile.am's also.
40 * Revision 1.4 2000/09/08 21:45:21 brianp
41 * added dither key option
43 * Revision 1.3 1999/10/28 18:23:29 brianp
44 * minor changes to Usage() function
46 * Revision 1.2 1999/10/21 22:13:58 brianp
47 * added f key to toggle front/back drawing
49 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
52 * Revision 3.3 1999/03/28 18:18:33 brianp
55 * Revision 3.2 1998/11/05 04:34:04 brianp
56 * moved image files to ../images/ directory
58 * Revision 3.1 1998/02/22 16:43:17 brianp
59 * added a few casts to silence compiler warnings
61 * Revision 3.0 1998/02/14 18:42:29 brianp
74 #define IMAGE_FILE "../images/girl.rgb"
76 static int ImgWidth
, ImgHeight
;
77 static GLenum ImgFormat
;
78 static GLubyte
*Image
= NULL
;
80 static int Xpos
, Ypos
;
81 static int SkipPixels
, SkipRows
;
82 static int DrawWidth
, DrawHeight
;
83 static int Scissor
= 0;
85 static GLfloat Zpos
= -1.0;
86 static float Xzoom
, Yzoom
;
87 static GLboolean DrawFront
= GL_FALSE
;
88 static GLboolean Dither
= GL_TRUE
;
91 static void Reset( void )
95 DrawHeight
= ImgHeight
;
96 SkipPixels
= SkipRows
= 0;
104 static void Display( void )
106 glClear( GL_COLOR_BUFFER_BIT
);
109 glRasterPos2i(Xpos
, Ypos
);
111 /* This allows negative raster positions: */
112 glRasterPos3f(0, 0, Zpos
);
113 glBitmap(0, 0, 0, 0, Xpos
, Ypos
, NULL
);
116 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
117 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
119 glPixelZoom( Xzoom
, Yzoom
);
122 glEnable(GL_SCISSOR_TEST
);
127 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
129 glDisable(GL_SCISSOR_TEST
);
137 static void Benchmark( void )
139 int startTime
, endTime
;
141 double seconds
, pixelsPerSecond
;
143 printf("Benchmarking...\n");
145 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
146 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
147 glPixelZoom( Xzoom
, Yzoom
);
149 glEnable(GL_SCISSOR_TEST
);
154 glDrawBuffer(GL_FRONT
);
156 glDrawBuffer(GL_BACK
);
158 /* Run timing test */
160 startTime
= glutGet(GLUT_ELAPSED_TIME
);
162 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
164 endTime
= glutGet(GLUT_ELAPSED_TIME
);
165 } while (endTime
- startTime
< 4000); /* 4 seconds */
168 glDisable(GL_SCISSOR_TEST
);
172 seconds
= (double) (endTime
- startTime
) / 1000.0;
173 pixelsPerSecond
= draws
* DrawWidth
* DrawHeight
/ seconds
;
174 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
175 draws
, seconds
, pixelsPerSecond
);
179 static void Reshape( int width
, int height
)
181 glViewport( 0, 0, width
, height
);
182 glMatrixMode( GL_PROJECTION
);
184 glOrtho( 0.0, width
, 0.0, height
, 0.0, 2.0 );
185 glMatrixMode( GL_MODELVIEW
);
188 glScissor(width
/4, height
/4, width
/2, height
/2);
192 static void Key( unsigned char key
, int x
, int y
)
205 glDisable(GL_DITHER
);
252 printf("RasterPos Z = %g\n", Zpos
);
256 printf("RasterPos Z = %g\n", Zpos
);
263 printf("Fog %d\n", Fog
);
266 DrawFront
= !DrawFront
;
268 glDrawBuffer(GL_FRONT
);
270 glDrawBuffer(GL_BACK
);
271 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
281 static void SpecialKey( int key
, int x
, int y
)
303 static void Init( GLboolean ciMode
)
305 static const GLfloat fogColor
[4] = {0, 1, 0, 0};
307 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
308 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
310 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
312 printf("Couldn't read %s\n", IMAGE_FILE
);
317 /* Convert RGB image to grayscale */
318 GLubyte
*indexImage
= malloc( ImgWidth
* ImgHeight
);
320 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
321 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
322 indexImage
[i
] = gray
/ 3;
326 ImgFormat
= GL_COLOR_INDEX
;
328 for (i
=0;i
<255;i
++) {
330 glutSetColor(i
, g
, g
, g
);
334 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
336 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
337 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
339 glFogi(GL_FOG_MODE
, GL_LINEAR
);
340 glFogf(GL_FOG_START
, 0);
341 glFogf(GL_FOG_END
, 2);
342 glFogfv(GL_FOG_COLOR
, fogColor
);
348 static void Usage(void)
351 printf(" SPACE Reset Parameters\n");
352 printf(" Up/Down Move image up/down\n");
353 printf(" Left/Right Move image left/right\n");
354 printf(" x Decrease X-axis PixelZoom\n");
355 printf(" X Increase X-axis PixelZoom\n");
356 printf(" y Decrease Y-axis PixelZoom\n");
357 printf(" Y Increase Y-axis PixelZoom\n");
358 printf(" w Decrease glDrawPixels width*\n");
359 printf(" W Increase glDrawPixels width*\n");
360 printf(" h Decrease glDrawPixels height*\n");
361 printf(" H Increase glDrawPixels height*\n");
362 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
363 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
364 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
365 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
366 printf(" s Toggle GL_SCISSOR_TEST\n");
367 printf(" F Toggle GL_FOG\n");
368 printf(" z Decrease RasterPos Z\n");
369 printf(" Z Increase RasterPos Z\n");
371 printf(" f Toggle front/back buffer drawing\n");
372 printf(" b Benchmark test\n");
373 printf(" ESC Exit\n");
374 printf("* Warning: no limits are imposed on these parameters so it's\n");
375 printf(" possible to cause a segfault if you go too far.\n");
379 int main( int argc
, char *argv
[] )
381 GLboolean ciMode
= GL_FALSE
;
383 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
387 glutInit( &argc
, argv
);
388 glutInitWindowPosition( 0, 0 );
389 glutInitWindowSize( 500, 400 );
392 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
394 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
396 glutCreateWindow(argv
[0]);
401 glutReshapeFunc( Reshape
);
402 glutKeyboardFunc( Key
);
403 glutSpecialFunc( SpecialKey
);
404 glutDisplayFunc( Display
);