blob: 88aac2684a8608f61119d956167aefd04bd1d913 [file] [log] [blame]
Brian34d535a2007-08-10 13:06:07 -06001/*
2 * glRead/DrawPixels test
3 */
4
5
José Fonseca479ea7d2009-01-23 14:35:36 +00006#include <GL/glew.h>
Brian34d535a2007-08-10 13:06:07 -06007#include <stdio.h>
8#include <string.h>
9#include <stdlib.h>
10#include <GL/glut.h>
11
12static int Width = 250, Height = 250;
Brian9f51e182007-08-13 18:18:45 -060013static GLfloat Zoom = 1.0;
Brian34d535a2007-08-10 13:06:07 -060014
15static 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));
20 glClearColor(0.3, 0.1, 0.3, 0.0);
21}
22
23static void Reshape(int width, int height)
24{
25 Width = width / 2;
26 Height = height;
27 /* draw on left half (we'll read that area) */
28 glViewport(0, 0, Width, height);
29 glMatrixMode(GL_PROJECTION);
30 glLoadIdentity();
31 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
32 glMatrixMode(GL_MODELVIEW);
33}
34
35static void Key(unsigned char key, int x, int y)
36{
37 switch (key) {
38 case 27:
39 exit(0);
40 default:
41 return;
42 }
43 glutPostRedisplay();
44}
45
46static void Draw(void)
47{
48 GLfloat *image = (GLfloat *) malloc(Width * Height * 4 * sizeof(GLfloat));
49
50 glClear(GL_COLOR_BUFFER_BIT);
51
52 glBegin(GL_TRIANGLES);
53 glColor3f(.8,0,0);
54 glVertex3f(-0.9, -0.9, -30.0);
55 glColor3f(0,.9,0);
56 glVertex3f( 0.9, -0.9, -30.0);
57 glColor3f(0,0,.7);
58 glVertex3f( 0.0, 0.9, -30.0);
59 glEnd();
60
Brianb7693ee2007-08-13 13:57:31 -060061 glBegin(GL_QUADS);
62 glColor3f(1, 1, 1);
63 glVertex2f(-1.0, -1.0);
64 glVertex2f(-0.9, -1.0);
65 glVertex2f(-0.9, -0.9);
66 glVertex2f(-1.0, -0.9);
67 glEnd();
68
Brian34d535a2007-08-10 13:06:07 -060069 glReadPixels(0, 0, Width, Height, GL_RGBA, GL_FLOAT, image);
70 printf("Pixel(0,0) = %f, %f, %f, %f\n",
71 image[0], image[1], image[2], image[3]);
72 /* draw to right half of window */
73 glWindowPos2iARB(Width, 0);
Brian9f51e182007-08-13 18:18:45 -060074 glPixelZoom(Zoom, Zoom);
Brian34d535a2007-08-10 13:06:07 -060075 glDrawPixels(Width, Height, GL_RGBA, GL_FLOAT, image);
76 free(image);
77
78 glutSwapBuffers();
79}
80
81int main(int argc, char **argv)
82{
83 glutInit(&argc, argv);
84 glutInitWindowPosition(0, 0);
85 glutInitWindowSize(Width*2, Height);
86 glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE);
87 if (glutCreateWindow(argv[0]) == GL_FALSE) {
88 exit(1);
89 }
90
Brian9f51e182007-08-13 18:18:45 -060091 if (argc > 1)
92 Zoom = atof(argv[1]);
93
José Fonseca479ea7d2009-01-23 14:35:36 +000094 glewInit();
95
Brian34d535a2007-08-10 13:06:07 -060096 Init();
97
98 glutReshapeFunc(Reshape);
99 glutKeyboardFunc(Key);
100 glutDisplayFunc(Draw);
101 glutMainLoop();
102 return 0;
103}