pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame^] | 1 | /* $Id: drawpix.c,v 1.5 2000/12/24 22:53:54 pesco Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * glDrawPixels demo/test/benchmark |
| 5 | * |
| 6 | * Brian Paul September 25, 1997 This file is in the public domain. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * $Log: drawpix.c,v $ |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame^] | 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". |
| 17 | * |
| 18 | * |
| 19 | * * Makefile.am (EXTRA_DIST): Added top-level regular files. |
| 20 | * |
| 21 | * * include/GL/Makefile.am (INC_X11): Added glxext.h. |
| 22 | * |
| 23 | * |
| 24 | * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include |
| 25 | * Mesa GGI headers in dist even if HAVE_GGI is not given. |
| 26 | * |
| 27 | * * configure.in: Look for GLUT and demo source dirs in $srcdir. |
| 28 | * |
| 29 | * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch]. |
| 30 | * More source list updates in various Makefile.am's. |
| 31 | * |
| 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. |
| 36 | * |
Brian Paul | 9d3e5db | 2000-09-08 21:45:21 +0000 | [diff] [blame] | 37 | * Revision 1.4 2000/09/08 21:45:21 brianp |
| 38 | * added dither key option |
| 39 | * |
Brian Paul | 8de4a38 | 1999-10-28 18:23:29 +0000 | [diff] [blame] | 40 | * Revision 1.3 1999/10/28 18:23:29 brianp |
| 41 | * minor changes to Usage() function |
| 42 | * |
Brian Paul | d13c0a9 | 1999-10-21 22:13:58 +0000 | [diff] [blame] | 43 | * Revision 1.2 1999/10/21 22:13:58 brianp |
| 44 | * added f key to toggle front/back drawing |
| 45 | * |
| 46 | * Revision 1.1.1.1 1999/08/19 00:55:40 jtg |
| 47 | * Imported sources |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 48 | * |
| 49 | * Revision 3.3 1999/03/28 18:18:33 brianp |
| 50 | * minor clean-up |
| 51 | * |
| 52 | * Revision 3.2 1998/11/05 04:34:04 brianp |
| 53 | * moved image files to ../images/ directory |
| 54 | * |
| 55 | * Revision 3.1 1998/02/22 16:43:17 brianp |
| 56 | * added a few casts to silence compiler warnings |
| 57 | * |
| 58 | * Revision 3.0 1998/02/14 18:42:29 brianp |
| 59 | * initial rev |
| 60 | * |
| 61 | */ |
| 62 | |
| 63 | |
| 64 | #include <stdio.h> |
| 65 | #include <stdlib.h> |
| 66 | #include <math.h> |
| 67 | #include <GL/glut.h> |
| 68 | |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame^] | 69 | #include "readtex.c" |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 70 | |
| 71 | #define IMAGE_FILE "../images/girl.rgb" |
| 72 | |
| 73 | static int ImgWidth, ImgHeight; |
| 74 | static GLenum ImgFormat; |
| 75 | static GLubyte *Image = NULL; |
| 76 | |
| 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; |
Brian Paul | d13c0a9 | 1999-10-21 22:13:58 +0000 | [diff] [blame] | 82 | static GLboolean DrawFront = GL_FALSE; |
Brian Paul | 9d3e5db | 2000-09-08 21:45:21 +0000 | [diff] [blame] | 83 | static GLboolean Dither = GL_TRUE; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | static void Reset( void ) |
| 87 | { |
| 88 | Xpos = Ypos = 20; |
| 89 | DrawWidth = ImgWidth; |
| 90 | DrawHeight = ImgHeight; |
| 91 | SkipPixels = SkipRows = 0; |
| 92 | Scissor = 0; |
| 93 | Xzoom = Yzoom = 1.0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static void Display( void ) |
| 98 | { |
| 99 | glClear( GL_COLOR_BUFFER_BIT ); |
| 100 | |
| 101 | #if 0 |
| 102 | glRasterPos2i(Xpos, Ypos); |
| 103 | #else |
| 104 | /* This allows negative raster positions: */ |
| 105 | glRasterPos2i(0, 0); |
| 106 | glBitmap(0, 0, 0, 0, Xpos, Ypos, NULL); |
| 107 | #endif |
| 108 | |
| 109 | glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels); |
| 110 | glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows); |
| 111 | |
| 112 | glPixelZoom( Xzoom, Yzoom ); |
| 113 | |
| 114 | if (Scissor) |
| 115 | glEnable(GL_SCISSOR_TEST); |
| 116 | |
| 117 | glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image); |
| 118 | |
| 119 | glDisable(GL_SCISSOR_TEST); |
| 120 | |
Brian Paul | d13c0a9 | 1999-10-21 22:13:58 +0000 | [diff] [blame] | 121 | if (!DrawFront) |
| 122 | glutSwapBuffers(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | static void Benchmark( void ) |
| 127 | { |
| 128 | int startTime, endTime; |
| 129 | int draws = 500; |
| 130 | double seconds, pixelsPerSecond; |
| 131 | |
| 132 | printf("Benchmarking...\n"); |
| 133 | /* GL set-up */ |
| 134 | glPixelStorei(GL_UNPACK_SKIP_PIXELS, SkipPixels); |
| 135 | glPixelStorei(GL_UNPACK_SKIP_ROWS, SkipRows); |
| 136 | glPixelZoom( Xzoom, Yzoom ); |
| 137 | if (Scissor) |
| 138 | glEnable(GL_SCISSOR_TEST); |
| 139 | |
Brian Paul | d13c0a9 | 1999-10-21 22:13:58 +0000 | [diff] [blame] | 140 | if (DrawFront) |
| 141 | glDrawBuffer(GL_FRONT); |
| 142 | else |
| 143 | glDrawBuffer(GL_BACK); |
| 144 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 145 | /* Run timing test */ |
| 146 | draws = 0; |
| 147 | startTime = glutGet(GLUT_ELAPSED_TIME); |
| 148 | do { |
| 149 | glDrawPixels(DrawWidth, DrawHeight, ImgFormat, GL_UNSIGNED_BYTE, Image); |
| 150 | draws++; |
| 151 | endTime = glutGet(GLUT_ELAPSED_TIME); |
| 152 | } while (endTime - startTime < 4000); /* 4 seconds */ |
| 153 | |
| 154 | /* GL clean-up */ |
| 155 | glDisable(GL_SCISSOR_TEST); |
| 156 | |
| 157 | /* Results */ |
| 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); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | static void Reshape( int width, int height ) |
| 166 | { |
| 167 | glViewport( 0, 0, width, height ); |
| 168 | glMatrixMode( GL_PROJECTION ); |
| 169 | glLoadIdentity(); |
| 170 | glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 ); |
| 171 | glMatrixMode( GL_MODELVIEW ); |
| 172 | glLoadIdentity(); |
| 173 | |
| 174 | glScissor(width/4, height/4, width/2, height/2); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | static void Key( unsigned char key, int x, int y ) |
| 179 | { |
| 180 | (void) x; |
| 181 | (void) y; |
| 182 | switch (key) { |
| 183 | case ' ': |
| 184 | Reset(); |
| 185 | break; |
Brian Paul | 9d3e5db | 2000-09-08 21:45:21 +0000 | [diff] [blame] | 186 | case 'd': |
| 187 | Dither = !Dither; |
| 188 | if (Dither) |
| 189 | glEnable(GL_DITHER); |
| 190 | else |
| 191 | glDisable(GL_DITHER); |
| 192 | break; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 193 | case 'w': |
| 194 | if (DrawWidth > 0) |
| 195 | DrawWidth--; |
| 196 | break; |
| 197 | case 'W': |
| 198 | DrawWidth++; |
| 199 | break; |
| 200 | case 'h': |
| 201 | if (DrawHeight > 0) |
| 202 | DrawHeight--; |
| 203 | break; |
| 204 | case 'H': |
| 205 | DrawHeight++; |
| 206 | break; |
| 207 | case 'p': |
| 208 | if (SkipPixels > 0) |
| 209 | SkipPixels--; |
| 210 | break; |
| 211 | case 'P': |
| 212 | SkipPixels++; |
| 213 | break; |
| 214 | case 'r': |
| 215 | if (SkipRows > 0) |
| 216 | SkipRows--; |
| 217 | break; |
| 218 | case 'R': |
| 219 | SkipRows++; |
| 220 | break; |
| 221 | case 's': |
| 222 | Scissor = !Scissor; |
| 223 | break; |
| 224 | case 'x': |
| 225 | Xzoom -= 0.1; |
| 226 | break; |
| 227 | case 'X': |
| 228 | Xzoom += 0.1; |
| 229 | break; |
| 230 | case 'y': |
| 231 | Yzoom -= 0.1; |
| 232 | break; |
| 233 | case 'Y': |
| 234 | Yzoom += 0.1; |
| 235 | break; |
| 236 | case 'b': |
| 237 | Benchmark(); |
| 238 | break; |
Brian Paul | d13c0a9 | 1999-10-21 22:13:58 +0000 | [diff] [blame] | 239 | case 'f': |
| 240 | DrawFront = !DrawFront; |
| 241 | if (DrawFront) |
| 242 | glDrawBuffer(GL_FRONT); |
| 243 | else |
| 244 | glDrawBuffer(GL_BACK); |
| 245 | printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK"); |
| 246 | break; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 247 | case 27: |
| 248 | exit(0); |
| 249 | break; |
| 250 | } |
| 251 | glutPostRedisplay(); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | static void SpecialKey( int key, int x, int y ) |
| 256 | { |
| 257 | (void) x; |
| 258 | (void) y; |
| 259 | switch (key) { |
| 260 | case GLUT_KEY_UP: |
| 261 | Ypos += 1; |
| 262 | break; |
| 263 | case GLUT_KEY_DOWN: |
| 264 | Ypos -= 1; |
| 265 | break; |
| 266 | case GLUT_KEY_LEFT: |
| 267 | Xpos -= 1; |
| 268 | break; |
| 269 | case GLUT_KEY_RIGHT: |
| 270 | Xpos += 1; |
| 271 | break; |
| 272 | } |
| 273 | glutPostRedisplay(); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static void Init( GLboolean ciMode ) |
| 278 | { |
| 279 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 280 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 281 | |
| 282 | Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat ); |
| 283 | if (!Image) { |
| 284 | printf("Couldn't read %s\n", IMAGE_FILE); |
| 285 | exit(0); |
| 286 | } |
| 287 | |
| 288 | if (ciMode) { |
| 289 | /* Convert RGB image to grayscale */ |
| 290 | GLubyte *indexImage = malloc( ImgWidth * ImgHeight ); |
| 291 | GLint i; |
| 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; |
| 295 | } |
| 296 | free(Image); |
| 297 | Image = indexImage; |
| 298 | ImgFormat = GL_COLOR_INDEX; |
| 299 | |
| 300 | for (i=0;i<255;i++) { |
| 301 | float g = i / 255.0; |
| 302 | glutSetColor(i, g, g, g); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | printf("Loaded %d by %d image\n", ImgWidth, ImgHeight ); |
| 307 | |
| 308 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 309 | glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth); |
| 310 | |
| 311 | Reset(); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | static void Usage(void) |
| 316 | { |
| 317 | printf("Keys:\n"); |
Brian Paul | 8de4a38 | 1999-10-28 18:23:29 +0000 | [diff] [blame] | 318 | printf(" SPACE Reset Parameters\n"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 319 | printf(" Up/Down Move image up/down\n"); |
| 320 | printf(" Left/Right Move image left/right\n"); |
Brian Paul | 8de4a38 | 1999-10-28 18:23:29 +0000 | [diff] [blame] | 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"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 333 | printf(" s Toggle GL_SCISSOR_TEST\n"); |
Brian Paul | d13c0a9 | 1999-10-21 22:13:58 +0000 | [diff] [blame] | 334 | printf(" f Toggle front/back buffer drawing\n"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 335 | printf(" b Benchmark test\n"); |
| 336 | printf(" ESC Exit\n"); |
Brian Paul | 8de4a38 | 1999-10-28 18:23:29 +0000 | [diff] [blame] | 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"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
| 342 | int main( int argc, char *argv[] ) |
| 343 | { |
| 344 | GLboolean ciMode = GL_FALSE; |
| 345 | |
| 346 | if (argc > 1 && strcmp(argv[1], "-ci")==0) { |
| 347 | ciMode = GL_TRUE; |
| 348 | } |
| 349 | |
| 350 | glutInit( &argc, argv ); |
| 351 | glutInitWindowPosition( 0, 0 ); |
| 352 | glutInitWindowSize( 500, 400 ); |
| 353 | |
| 354 | if (ciMode) |
| 355 | glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE ); |
| 356 | else |
Brian Paul | 9d3e5db | 2000-09-08 21:45:21 +0000 | [diff] [blame] | 357 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 358 | |
| 359 | glutCreateWindow(argv[0]); |
| 360 | |
| 361 | Init(ciMode); |
| 362 | Usage(); |
| 363 | |
| 364 | glutReshapeFunc( Reshape ); |
| 365 | glutKeyboardFunc( Key ); |
| 366 | glutSpecialFunc( SpecialKey ); |
| 367 | glutDisplayFunc( Display ); |
| 368 | |
| 369 | glutMainLoop(); |
| 370 | return 0; |
| 371 | } |