blob: 65fb81ed481ccd16f875a9a09bd096b5995d0532 [file] [log] [blame]
Brian34d535a2007-08-10 13:06:07 -06001/*
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
12static int Width = 250, Height = 250;
13
14static 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
22static 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
34static 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
45static 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
Brianb7693ee2007-08-13 13:57:31 -060060 glBegin(GL_QUADS);
61 glColor3f(1, 1, 1);
62 glVertex2f(-1.0, -1.0);
63 glVertex2f(-0.9, -1.0);
64 glVertex2f(-0.9, -0.9);
65 glVertex2f(-1.0, -0.9);
66 glEnd();
67
Brian34d535a2007-08-10 13:06:07 -060068 glReadPixels(0, 0, Width, Height, GL_RGBA, GL_FLOAT, image);
69 printf("Pixel(0,0) = %f, %f, %f, %f\n",
70 image[0], image[1], image[2], image[3]);
71 /* draw to right half of window */
72 glWindowPos2iARB(Width, 0);
73 glDrawPixels(Width, Height, GL_RGBA, GL_FLOAT, image);
74 free(image);
75
76 glutSwapBuffers();
77}
78
79int main(int argc, char **argv)
80{
81 glutInit(&argc, argv);
82 glutInitWindowPosition(0, 0);
83 glutInitWindowSize(Width*2, Height);
84 glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE);
85 if (glutCreateWindow(argv[0]) == GL_FALSE) {
86 exit(1);
87 }
88
89 Init();
90
91 glutReshapeFunc(Reshape);
92 glutKeyboardFunc(Key);
93 glutDisplayFunc(Draw);
94 glutMainLoop();
95 return 0;
96}