2 * Test glRead/DrawPixels for GL_DEPTH_COMPONENT, with pixelzoom.
14 static GLint WinWidth
= 500, WinHeight
= 500;
15 static GLboolean Invert
= GL_FALSE
;
18 static void Display(void)
20 GLfloat depth
[100 * 100];
21 GLfloat depth2
[400 * 400];
25 glClearColor(0.5, 0.5, 0.5, 1.0);
26 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
28 glEnable(GL_DEPTH_TEST
);
31 glViewport(0, 0, 100, 100);
32 glMatrixMode(GL_PROJECTION
);
34 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
35 glMatrixMode(GL_MODELVIEW
);
37 glutSolidSphere(1.0, 20, 10);
39 /* read the depth image */
40 glReadPixels(0, 0, 100, 100, GL_DEPTH_COMPONENT
, GL_FLOAT
, depth
);
42 for (i
= 1; i
< 100 * 100; i
++) {
48 printf("Depth value range: [%f, %f]\n", min
, max
);
50 /* draw depth image with scaling (into z buffer) */
51 glPixelZoom(4.0, 4.0);
52 glColor4f(1, 0, 0, 0);
53 glWindowPos2i(100, 0);
55 glPixelTransferf(GL_DEPTH_SCALE
, -1.0);
56 glPixelTransferf(GL_DEPTH_BIAS
, 1.0);
58 glDrawPixels(100, 100, GL_DEPTH_COMPONENT
, GL_FLOAT
, depth
);
60 glPixelTransferf(GL_DEPTH_SCALE
, 1.0);
61 glPixelTransferf(GL_DEPTH_BIAS
, 0.0);
64 glDisable(GL_DEPTH_TEST
);
66 /* read back scaled depth image */
67 glReadPixels(100, 0, 400, 400, GL_DEPTH_COMPONENT
, GL_FLOAT
, depth2
);
68 /* draw as luminance */
69 glPixelZoom(1.0, 1.0);
70 glDrawPixels(400, 400, GL_LUMINANCE
, GL_FLOAT
, depth2
);
76 static void Reshape(int width
, int height
)
80 glViewport(0, 0, width
, height
);
84 static void Key(unsigned char key
, int x
, int y
)
100 static void Init(void)
102 const GLfloat blue
[4] = {.1, .1, 1.0, 1.0};
103 const GLfloat gray
[4] = {0.2, 0.2, 0.2, 1.0};
104 const GLfloat white
[4] = {1.0, 1.0, 1.0, 1.0};
105 const GLfloat pos
[4] = {0, 0, 10, 0};
107 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
108 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
110 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, blue
);
111 glLightfv(GL_LIGHT0
, GL_AMBIENT
, gray
);
112 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, white
);
113 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
114 glEnable(GL_LIGHTING
);
119 int main(int argc
, char *argv
[])
121 glutInit(&argc
, argv
);
122 glutInitWindowPosition(0, 0);
123 glutInitWindowSize(WinWidth
, WinHeight
);
124 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
125 glutCreateWindow(argv
[0]);
127 glutReshapeFunc(Reshape
);
128 glutKeyboardFunc(Key
);
129 glutDisplayFunc(Display
);