1 /* $Id: drawpix.c,v 1.5 2000/12/24 22:53:54 pesco Exp $ */
4 * glDrawPixels demo/test/benchmark
6 * Brian Paul September 25, 1997 This file is in the public domain.
11 * Revision 1.5 2000/12/24 22:53:54 pesco
12 * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
13 * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
14 * Essentially the same.
15 * Program files updated to include "readtex.c", not "../util/readtex.c".
16 * * demos/reflect.c: Likewise for "showbuffer.c".
19 * * Makefile.am (EXTRA_DIST): Added top-level regular files.
21 * * include/GL/Makefile.am (INC_X11): Added glxext.h.
24 * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
25 * Mesa GGI headers in dist even if HAVE_GGI is not given.
27 * * configure.in: Look for GLUT and demo source dirs in $srcdir.
29 * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
30 * More source list updates in various Makefile.am's.
32 * * Makefile.am (dist-hook): Remove CVS directory from distribution.
33 * (DIST_SUBDIRS): List all possible subdirs here.
34 * (SUBDIRS): Only list subdirs selected for build again.
35 * The above two applied to all subdir Makefile.am's also.
37 * Revision 1.4 2000/09/08 21:45:21 brianp
38 * added dither key option
40 * Revision 1.3 1999/10/28 18:23:29 brianp
41 * minor changes to Usage() function
43 * Revision 1.2 1999/10/21 22:13:58 brianp
44 * added f key to toggle front/back drawing
46 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
49 * Revision 3.3 1999/03/28 18:18:33 brianp
52 * Revision 3.2 1998/11/05 04:34:04 brianp
53 * moved image files to ../images/ directory
55 * Revision 3.1 1998/02/22 16:43:17 brianp
56 * added a few casts to silence compiler warnings
58 * Revision 3.0 1998/02/14 18:42:29 brianp
71 #define IMAGE_FILE "../images/girl.rgb"
73 static int ImgWidth
, ImgHeight
;
74 static GLenum ImgFormat
;
75 static GLubyte
*Image
= NULL
;
77 static int Xpos
, Ypos
;
78 static int SkipPixels
, SkipRows
;
79 static int DrawWidth
, DrawHeight
;
80 static int Scissor
= 0;
81 static float Xzoom
, Yzoom
;
82 static GLboolean DrawFront
= GL_FALSE
;
83 static GLboolean Dither
= GL_TRUE
;
86 static void Reset( void )
90 DrawHeight
= ImgHeight
;
91 SkipPixels
= SkipRows
= 0;
97 static void Display( void )
99 glClear( GL_COLOR_BUFFER_BIT
);
102 glRasterPos2i(Xpos
, Ypos
);
104 /* This allows negative raster positions: */
106 glBitmap(0, 0, 0, 0, Xpos
, Ypos
, NULL
);
109 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
110 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
112 glPixelZoom( Xzoom
, Yzoom
);
115 glEnable(GL_SCISSOR_TEST
);
117 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
119 glDisable(GL_SCISSOR_TEST
);
126 static void Benchmark( void )
128 int startTime
, endTime
;
130 double seconds
, pixelsPerSecond
;
132 printf("Benchmarking...\n");
134 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, SkipPixels
);
135 glPixelStorei(GL_UNPACK_SKIP_ROWS
, SkipRows
);
136 glPixelZoom( Xzoom
, Yzoom
);
138 glEnable(GL_SCISSOR_TEST
);
141 glDrawBuffer(GL_FRONT
);
143 glDrawBuffer(GL_BACK
);
145 /* Run timing test */
147 startTime
= glutGet(GLUT_ELAPSED_TIME
);
149 glDrawPixels(DrawWidth
, DrawHeight
, ImgFormat
, GL_UNSIGNED_BYTE
, Image
);
151 endTime
= glutGet(GLUT_ELAPSED_TIME
);
152 } while (endTime
- startTime
< 4000); /* 4 seconds */
155 glDisable(GL_SCISSOR_TEST
);
158 seconds
= (double) (endTime
- startTime
) / 1000.0;
159 pixelsPerSecond
= draws
* DrawWidth
* DrawHeight
/ seconds
;
160 printf("Result: %d draws in %f seconds = %f pixels/sec\n",
161 draws
, seconds
, pixelsPerSecond
);
165 static void Reshape( int width
, int height
)
167 glViewport( 0, 0, width
, height
);
168 glMatrixMode( GL_PROJECTION
);
170 glOrtho( 0.0, width
, 0.0, height
, -1.0, 1.0 );
171 glMatrixMode( GL_MODELVIEW
);
174 glScissor(width
/4, height
/4, width
/2, height
/2);
178 static void Key( unsigned char key
, int x
, int y
)
191 glDisable(GL_DITHER
);
240 DrawFront
= !DrawFront
;
242 glDrawBuffer(GL_FRONT
);
244 glDrawBuffer(GL_BACK
);
245 printf("glDrawBuffer(%s)\n", DrawFront
? "GL_FRONT" : "GL_BACK");
255 static void SpecialKey( int key
, int x
, int y
)
277 static void Init( GLboolean ciMode
)
279 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
280 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
282 Image
= LoadRGBImage( IMAGE_FILE
, &ImgWidth
, &ImgHeight
, &ImgFormat
);
284 printf("Couldn't read %s\n", IMAGE_FILE
);
289 /* Convert RGB image to grayscale */
290 GLubyte
*indexImage
= malloc( ImgWidth
* ImgHeight
);
292 for (i
=0; i
<ImgWidth
*ImgHeight
; i
++) {
293 int gray
= Image
[i
*3] + Image
[i
*3+1] + Image
[i
*3+2];
294 indexImage
[i
] = gray
/ 3;
298 ImgFormat
= GL_COLOR_INDEX
;
300 for (i
=0;i
<255;i
++) {
302 glutSetColor(i
, g
, g
, g
);
306 printf("Loaded %d by %d image\n", ImgWidth
, ImgHeight
);
308 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
309 glPixelStorei(GL_UNPACK_ROW_LENGTH
, ImgWidth
);
315 static void Usage(void)
318 printf(" SPACE Reset Parameters\n");
319 printf(" Up/Down Move image up/down\n");
320 printf(" Left/Right Move image left/right\n");
321 printf(" x Decrease X-axis PixelZoom\n");
322 printf(" X Increase X-axis PixelZoom\n");
323 printf(" y Decrease Y-axis PixelZoom\n");
324 printf(" Y Increase Y-axis PixelZoom\n");
325 printf(" w Decrease glDrawPixels width*\n");
326 printf(" W Increase glDrawPixels width*\n");
327 printf(" h Decrease glDrawPixels height*\n");
328 printf(" H Increase glDrawPixels height*\n");
329 printf(" p Decrease GL_UNPACK_SKIP_PIXELS*\n");
330 printf(" P Increase GL_UNPACK_SKIP_PIXELS*\n");
331 printf(" r Decrease GL_UNPACK_SKIP_ROWS*\n");
332 printf(" R Increase GL_UNPACK_SKIP_ROWS*\n");
333 printf(" s Toggle GL_SCISSOR_TEST\n");
334 printf(" f Toggle front/back buffer drawing\n");
335 printf(" b Benchmark test\n");
336 printf(" ESC Exit\n");
337 printf("* Warning: no limits are imposed on these parameters so it's\n");
338 printf(" possible to cause a segfault if you go too far.\n");
342 int main( int argc
, char *argv
[] )
344 GLboolean ciMode
= GL_FALSE
;
346 if (argc
> 1 && strcmp(argv
[1], "-ci")==0) {
350 glutInit( &argc
, argv
);
351 glutInitWindowPosition( 0, 0 );
352 glutInitWindowSize( 500, 400 );
355 glutInitDisplayMode( GLUT_INDEX
| GLUT_DOUBLE
);
357 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
359 glutCreateWindow(argv
[0]);
364 glutReshapeFunc( Reshape
);
365 glutKeyboardFunc( Key
);
366 glutSpecialFunc( SpecialKey
);
367 glutDisplayFunc( Display
);