2 * Test glRead/DrawPixels for GL_DEPTH_COMPONENT, with pixelzoom.
14 static GLint WinWidth
= 500, WinHeight
= 500;
15 static GLboolean Invert
= GL_FALSE
;
16 static GLboolean TestPacking
= GL_FALSE
;
17 static GLboolean TestList
= GL_FALSE
;
20 static void Display(void)
22 GLfloat depth
[100 * 100 * 2];
23 GLfloat depth2
[400 * 400]; /* *2 to test pixelstore stuff */
25 GLenum depthType
= GL_FLOAT
;
27 glClearColor(0.5, 0.5, 0.5, 1.0);
28 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
30 glEnable(GL_DEPTH_TEST
);
33 glViewport(0, 0, 100, 100);
34 glMatrixMode(GL_PROJECTION
);
36 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
37 glMatrixMode(GL_MODELVIEW
);
39 glutSolidSphere(1.0, 20, 10);
42 glPixelStorei(GL_PACK_ROW_LENGTH
, 120);
43 glPixelStorei(GL_PACK_SKIP_PIXELS
, 5);
46 /* read the depth image */
47 glReadPixels(0, 0, 100, 100, GL_DEPTH_COMPONENT
, depthType
, depth
);
48 if (depthType
== GL_FLOAT
) {
52 for (i
= 1; i
< 100 * 100; i
++) {
58 printf("Depth value range: [%f, %f]\n", min
, max
);
61 /* Draw the Z image as luminance above original rendering */
62 glWindowPos2i(0, 100);
63 glDrawPixels(100, 100, GL_LUMINANCE
, depthType
, depth
);
66 glPixelStorei(GL_PACK_ROW_LENGTH
, 0);
67 glPixelStorei(GL_PACK_SKIP_PIXELS
, 0);
68 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 120);
69 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 5);
72 /* draw depth image with scaling (into z buffer) */
73 glPixelZoom(4.0, 4.0);
74 glColor4f(1, 0, 0, 0);
75 glWindowPos2i(100, 0);
77 glPixelTransferf(GL_DEPTH_SCALE
, -1.0);
78 glPixelTransferf(GL_DEPTH_BIAS
, 1.0);
82 glNewList(list
, GL_COMPILE
);
83 glDrawPixels(100, 100, GL_DEPTH_COMPONENT
, depthType
, depth
);
86 glDeleteLists(list
, 1);
89 glDrawPixels(100, 100, GL_DEPTH_COMPONENT
, depthType
, depth
);
92 glPixelTransferf(GL_DEPTH_SCALE
, 1.0);
93 glPixelTransferf(GL_DEPTH_BIAS
, 0.0);
97 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
98 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
101 glDisable(GL_DEPTH_TEST
);
103 /* read back scaled depth image */
104 glReadPixels(100, 0, 400, 400, GL_DEPTH_COMPONENT
, GL_FLOAT
, depth2
);
105 /* draw as luminance */
106 glPixelZoom(1.0, 1.0);
107 glWindowPos2i(100, 0);
108 glDrawPixels(400, 400, GL_LUMINANCE
, GL_FLOAT
, depth2
);
114 static void Reshape(int width
, int height
)
118 glViewport(0, 0, width
, height
);
122 static void Key(unsigned char key
, int x
, int y
)
131 TestPacking
= !TestPacking
;
132 printf("Test pixel pack/unpack: %d\n", TestPacking
);
135 TestList
= !TestList
;
136 printf("Test dlist: %d\n", TestList
);
146 static void Init(void)
148 const GLfloat blue
[4] = {.1, .1, 1.0, 1.0};
149 const GLfloat gray
[4] = {0.2, 0.2, 0.2, 1.0};
150 const GLfloat white
[4] = {1.0, 1.0, 1.0, 1.0};
151 const GLfloat pos
[4] = {0, 0, 10, 0};
153 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
154 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
156 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, blue
);
157 glLightfv(GL_LIGHT0
, GL_AMBIENT
, gray
);
158 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, white
);
159 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
160 glEnable(GL_LIGHTING
);
165 int main(int argc
, char *argv
[])
167 glutInit(&argc
, argv
);
168 glutInitWindowPosition(0, 0);
169 glutInitWindowSize(WinWidth
, WinHeight
);
170 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
171 glutCreateWindow(argv
[0]);
173 glutReshapeFunc(Reshape
);
174 glutKeyboardFunc(Key
);
175 glutDisplayFunc(Display
);