| 421 | |
| 422 | |
| 423 | static void |
| 424 | WriteFile(const char *filename) |
| 425 | { |
| 426 | FILE *f; |
| 427 | GLubyte *image; |
| 428 | int i; |
| 429 | |
| 430 | image = malloc(gWidth * gHeight * 3 * sizeof(GLubyte)); |
| 431 | if (!image) { |
| 432 | printf("Error: couldn't allocate image buffer\n"); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 437 | glReadPixels(0, 0, gWidth, gHeight, GL_RGB, GL_UNSIGNED_BYTE, image); |
| 438 | |
| 439 | f = fopen(filename, "w"); |
| 440 | if (!f) { |
| 441 | printf("Couldn't open image file: %s\n", filename); |
| 442 | return; |
| 443 | } |
| 444 | fprintf(f,"P6\n"); |
| 445 | fprintf(f,"# ppm-file created by %s\n", "trdemo2"); |
| 446 | fprintf(f,"%i %i\n", gWidth, gHeight); |
| 447 | fprintf(f,"255\n"); |
| 448 | fclose(f); |
| 449 | f = fopen(filename, "ab"); /* now append binary data */ |
| 450 | if (!f) { |
| 451 | printf("Couldn't append to image file: %s\n", filename); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | for (i=0;i<gHeight;i++) { |
| 456 | GLubyte *rowPtr; |
| 457 | /* Remember, OpenGL images are bottom to top. Have to reverse. */ |
| 458 | rowPtr = image + (gHeight-1-i) * gWidth*3; |
| 459 | fwrite(rowPtr, 1, gWidth*3, f); |
| 460 | } |
| 461 | |
| 462 | fclose(f); |
| 463 | free(image); |
| 464 | |
| 465 | printf("Wrote %d by %d image file: %s\n", gWidth, gHeight, filename); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | |