blob: 3d8c557b37062411389dd6016e350ff1006cc871 [file] [log] [blame]
Brian Paul2230b852003-08-23 21:42:57 +00001/*
2 * Test glRead/DrawPixels for GL_DEPTH_COMPONENT, with pixelzoom.
3 *
4 * Brian Paul
5 * 23 August 2003
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
Keith Whitwella58065d2009-03-10 13:11:23 +000011#include <GL/glew.h>
Brian Paul2230b852003-08-23 21:42:57 +000012#include <GL/glut.h>
13
14static GLint WinWidth = 500, WinHeight = 500;
15
16
17static void Display(void)
18{
19 GLfloat depth[100 * 100];
20 GLfloat depth2[400 * 400];
21 GLfloat min, max;
22 int i;
23
24 glClearColor(0.5, 0.5, 0.5, 0);
25 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
26
27 /* draw a sphere */
28 glViewport(0, 0, 100, 100);
29 glMatrixMode(GL_PROJECTION);
30 glLoadIdentity();
31 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
32 glMatrixMode(GL_MODELVIEW);
33 glLoadIdentity();
34 glutSolidSphere(1.0, 20, 10);
35
36 /* read the depth image */
37 glReadPixels(0, 0, 100, 100, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
38 min = max = depth[0];
39 for (i = 1; i < 100 * 100; i++) {
40 if (depth[i] < min)
41 min = depth[i];
42 if (depth[i] > max)
43 max = depth[i];
44 }
45 printf("Depth value range: [%f, %f]\n", min, max);
46
47 /* draw depth image with scaling (into z buffer) */
48 glPixelZoom(4.0, 4.0);
49 glWindowPos2i(100, 0);
50 glDrawPixels(100, 100, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
51
52 /* read back scaled depth image */
53 glReadPixels(100, 0, 400, 400, GL_DEPTH_COMPONENT, GL_FLOAT, depth2);
54 /* draw as luminance */
55 glPixelZoom(1.0, 1.0);
56 glDrawPixels(400, 400, GL_LUMINANCE, GL_FLOAT, depth2);
57
58 glutSwapBuffers();
59}
60
61
62static void Reshape(int width, int height)
63{
64 WinWidth = width;
65 WinHeight = height;
66 glViewport(0, 0, width, height);
67}
68
69
70static void Key(unsigned char key, int x, int y)
71{
72 (void) x;
73 (void) y;
74 switch (key) {
75 case 27:
76 exit(0);
77 break;
78 }
79 glutPostRedisplay();
80}
81
82
83static void Init(void)
84{
85 const GLfloat blue[4] = {.1, .1, 1.0, 0.0};
86 const GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
87 const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
88 const GLfloat pos[4] = {0, 0, 10, 0};
89
90 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
91 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
92
93 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
94 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
95 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
96 glLightfv(GL_LIGHT0, GL_POSITION, pos);
97 glEnable(GL_LIGHTING);
98 glEnable(GL_LIGHT0);
99 glEnable(GL_DEPTH_TEST);
100}
101
102
103int main(int argc, char *argv[])
104{
105 glutInit(&argc, argv);
106 glutInitWindowPosition(0, 0);
107 glutInitWindowSize(WinWidth, WinHeight);
108 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
109 glutCreateWindow(argv[0]);
Keith Whitwella58065d2009-03-10 13:11:23 +0000110 glewInit();
Brian Paul2230b852003-08-23 21:42:57 +0000111 glutReshapeFunc(Reshape);
112 glutKeyboardFunc(Key);
113 glutDisplayFunc(Display);
114 Init();
115 glutMainLoop();
116 return 0;
117}