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

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