Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readpixels.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * glRead/DrawPixels test
  3. */
  4. #define GL_GLEXT_PROTOTYPES
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <GL/glut.h>
  9. static int Width = 250, Height = 250;
  10. static GLfloat Zoom = 1.0;
  11. static void Init(void)
  12. {
  13. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  14. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  15. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  16. glClearColor(0.3, 0.1, 0.3, 0.0);
  17. }
  18. static void Reshape(int width, int height)
  19. {
  20. Width = width / 2;
  21. Height = height;
  22. /* draw on left half (we'll read that area) */
  23. glViewport(0, 0, Width, height);
  24. glMatrixMode(GL_PROJECTION);
  25. glLoadIdentity();
  26. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  27. glMatrixMode(GL_MODELVIEW);
  28. }
  29. static void Key(unsigned char key, int x, int y)
  30. {
  31. switch (key) {
  32. case 27:
  33. exit(0);
  34. default:
  35. return;
  36. }
  37. glutPostRedisplay();
  38. }
  39. static void Draw(void)
  40. {
  41. GLfloat *image = (GLfloat *) malloc(Width * Height * 4 * sizeof(GLfloat));
  42. glClear(GL_COLOR_BUFFER_BIT);
  43. glBegin(GL_TRIANGLES);
  44. glColor3f(.8,0,0);
  45. glVertex3f(-0.9, -0.9, -30.0);
  46. glColor3f(0,.9,0);
  47. glVertex3f( 0.9, -0.9, -30.0);
  48. glColor3f(0,0,.7);
  49. glVertex3f( 0.0, 0.9, -30.0);
  50. glEnd();
  51. glBegin(GL_QUADS);
  52. glColor3f(1, 1, 1);
  53. glVertex2f(-1.0, -1.0);
  54. glVertex2f(-0.9, -1.0);
  55. glVertex2f(-0.9, -0.9);
  56. glVertex2f(-1.0, -0.9);
  57. glEnd();
  58. glReadPixels(0, 0, Width, Height, GL_RGBA, GL_FLOAT, image);
  59. printf("Pixel(0,0) = %f, %f, %f, %f\n",
  60. image[0], image[1], image[2], image[3]);
  61. /* draw to right half of window */
  62. glWindowPos2iARB(Width, 0);
  63. glPixelZoom(Zoom, Zoom);
  64. glDrawPixels(Width, Height, GL_RGBA, GL_FLOAT, image);
  65. free(image);
  66. glutSwapBuffers();
  67. }
  68. int main(int argc, char **argv)
  69. {
  70. glutInit(&argc, argv);
  71. glutInitWindowPosition(0, 0);
  72. glutInitWindowSize(Width*2, Height);
  73. glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE);
  74. if (glutCreateWindow(argv[0]) == GL_FALSE) {
  75. exit(1);
  76. }
  77. if (argc > 1)
  78. Zoom = atof(argv[1]);
  79. Init();
  80. glutReshapeFunc(Reshape);
  81. glutKeyboardFunc(Key);
  82. glutDisplayFunc(Draw);
  83. glutMainLoop();
  84. return 0;
  85. }