Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 1 | /* |
| 2 | * glRead/DrawPixels test |
| 3 | */ |
| 4 | |
| 5 | |
José Fonseca | 479ea7d | 2009-01-23 14:35:36 +0000 | [diff] [blame] | 6 | #include <GL/glew.h> |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 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; |
Brian | 9f51e18 | 2007-08-13 18:18:45 -0600 | [diff] [blame] | 13 | static GLfloat Zoom = 1.0; |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 14 | |
| 15 | static void Init(void) |
| 16 | { |
| 17 | fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 18 | fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 19 | fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
Keith Whitwell | eb9801c | 2009-03-24 16:35:29 +0000 | [diff] [blame] | 20 | fflush(stderr); |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 21 | glClearColor(0.3, 0.1, 0.3, 0.0); |
| 22 | } |
| 23 | |
| 24 | static void Reshape(int width, int height) |
| 25 | { |
| 26 | Width = width / 2; |
| 27 | Height = height; |
| 28 | /* draw on left half (we'll read that area) */ |
| 29 | glViewport(0, 0, Width, height); |
| 30 | glMatrixMode(GL_PROJECTION); |
| 31 | glLoadIdentity(); |
| 32 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 33 | glMatrixMode(GL_MODELVIEW); |
| 34 | } |
| 35 | |
| 36 | static void Key(unsigned char key, int x, int y) |
| 37 | { |
| 38 | switch (key) { |
| 39 | case 27: |
| 40 | exit(0); |
| 41 | default: |
Vinson Lee | 3790c6a | 2009-11-19 13:03:12 -0800 | [diff] [blame] | 42 | break; |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 43 | } |
| 44 | glutPostRedisplay(); |
| 45 | } |
| 46 | |
| 47 | static void Draw(void) |
| 48 | { |
| 49 | GLfloat *image = (GLfloat *) malloc(Width * Height * 4 * sizeof(GLfloat)); |
| 50 | |
| 51 | glClear(GL_COLOR_BUFFER_BIT); |
| 52 | |
| 53 | glBegin(GL_TRIANGLES); |
| 54 | glColor3f(.8,0,0); |
| 55 | glVertex3f(-0.9, -0.9, -30.0); |
| 56 | glColor3f(0,.9,0); |
| 57 | glVertex3f( 0.9, -0.9, -30.0); |
| 58 | glColor3f(0,0,.7); |
| 59 | glVertex3f( 0.0, 0.9, -30.0); |
| 60 | glEnd(); |
| 61 | |
Brian | b7693ee | 2007-08-13 13:57:31 -0600 | [diff] [blame] | 62 | glBegin(GL_QUADS); |
| 63 | glColor3f(1, 1, 1); |
| 64 | glVertex2f(-1.0, -1.0); |
| 65 | glVertex2f(-0.9, -1.0); |
| 66 | glVertex2f(-0.9, -0.9); |
| 67 | glVertex2f(-1.0, -0.9); |
| 68 | glEnd(); |
| 69 | |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 70 | glReadPixels(0, 0, Width, Height, GL_RGBA, GL_FLOAT, image); |
| 71 | printf("Pixel(0,0) = %f, %f, %f, %f\n", |
| 72 | image[0], image[1], image[2], image[3]); |
| 73 | /* draw to right half of window */ |
| 74 | glWindowPos2iARB(Width, 0); |
Brian | 9f51e18 | 2007-08-13 18:18:45 -0600 | [diff] [blame] | 75 | glPixelZoom(Zoom, Zoom); |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 76 | glDrawPixels(Width, Height, GL_RGBA, GL_FLOAT, image); |
| 77 | free(image); |
| 78 | |
| 79 | glutSwapBuffers(); |
| 80 | } |
| 81 | |
| 82 | int main(int argc, char **argv) |
| 83 | { |
| 84 | glutInit(&argc, argv); |
| 85 | glutInitWindowPosition(0, 0); |
| 86 | glutInitWindowSize(Width*2, Height); |
| 87 | glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE); |
| 88 | if (glutCreateWindow(argv[0]) == GL_FALSE) { |
| 89 | exit(1); |
| 90 | } |
| 91 | |
Brian | 9f51e18 | 2007-08-13 18:18:45 -0600 | [diff] [blame] | 92 | if (argc > 1) |
| 93 | Zoom = atof(argv[1]); |
| 94 | |
José Fonseca | 479ea7d | 2009-01-23 14:35:36 +0000 | [diff] [blame] | 95 | glewInit(); |
| 96 | |
Brian | 34d535a | 2007-08-10 13:06:07 -0600 | [diff] [blame] | 97 | Init(); |
| 98 | |
| 99 | glutReshapeFunc(Reshape); |
| 100 | glutKeyboardFunc(Key); |
| 101 | glutDisplayFunc(Draw); |
| 102 | glutMainLoop(); |
| 103 | return 0; |
| 104 | } |