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