2 * Test Z compositing with glDrawPixels(GL_DEPTH_COMPONENT) and stencil test.
13 static GLfloat Xrot
= 0, Yrot
= 0, Zpos
= 6;
14 static GLboolean Anim
= GL_FALSE
;
16 static int Width
= 400, Height
= 200;
19 static GLboolean showZ
= 0;
32 * Draw first object, save color+Z images
37 static const GLfloat red
[4] = { 1.0, 0.0, 0.0, 0.0 };
39 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, red
);
41 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
44 glTranslatef(-1, 0, 0);
45 glRotatef(45 + Xrot
, 1, 0, 0);
47 glutSolidTorus(0.75, 2.0, 10, 20);
51 glReadPixels(0, 0, Width
, Height
, GL_DEPTH_COMPONENT
, GL_FLOAT
, Zimg
);
52 glReadPixels(0, 0, Width
, Height
, GL_RGBA
, GL_UNSIGNED_BYTE
, Cimg
);
62 static const GLfloat blue
[4] = { 0.0, 0.0, 1.0, 0.0 };
64 glMaterialfv(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
, blue
);
66 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
69 glTranslatef(+1, 0, 0);
70 glRotatef(-45 + Xrot
, 1, 0, 0);
72 glutSolidTorus(0.75, 2.0, 10, 20);
79 * Composite first/saved image over second rendering.
86 /* Draw Z values, set stencil where Z test passes */
87 glEnable(GL_STENCIL_TEST
);
88 glStencilFunc(GL_ALWAYS
, 1, ~0);
89 glStencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
91 glDrawPixels(Width
, Height
, GL_DEPTH_COMPONENT
, GL_FLOAT
, Zimg
);
94 /* Draw color where stencil==1 */
95 glStencilFunc(GL_EQUAL
, 1, ~0);
96 glStencilOp(GL_KEEP
, GL_KEEP
, GL_KEEP
);
97 glDisable(GL_DEPTH_TEST
);
98 glDrawPixels(Width
, Height
, GL_RGBA
, GL_UNSIGNED_BYTE
, Cimg
);
99 glEnable(GL_DEPTH_TEST
);
101 glDisable(GL_STENCIL_TEST
);
116 Reshape(int width
, int height
)
118 GLfloat ar
= (float) width
/ height
;
119 glViewport(0, 0, width
, height
);
120 glMatrixMode(GL_PROJECTION
);
122 glFrustum(-ar
, ar
, -1.0, 1.0, 5.0, 30.0);
123 glMatrixMode(GL_MODELVIEW
);
125 glTranslatef(0.0, 0.0, -15.0);
134 Zimg
= (float *) malloc(width
* height
* 4);
135 Cimg
= (GLubyte
*) malloc(width
* height
* 4);
140 Key(unsigned char key
, int x
, int y
)
142 const GLfloat step
= 1.0;
163 glutDestroyWindow(Win
);
172 SpecialKey(int key
, int x
, int y
)
174 const GLfloat step
= 3.0;
198 /* setup lighting, etc */
199 glEnable(GL_DEPTH_TEST
);
200 glEnable(GL_LIGHTING
);
206 main(int argc
, char *argv
[])
208 glutInit(&argc
, argv
);
209 glutInitWindowPosition(0, 0);
210 glutInitWindowSize(Width
, Height
);
211 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
| GLUT_STENCIL
);
212 Win
= glutCreateWindow(argv
[0]);
214 glutReshapeFunc(Reshape
);
215 glutKeyboardFunc(Key
);
216 glutSpecialFunc(SpecialKey
);
217 glutDisplayFunc(Draw
);