Hello, my friends,
I tried to draw a simple cube, and wanted to show
the front-scene and back-scene by the switch,
// front-scene
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glClear(GL_DEPTH_BUFFER_BIT);
// back-scene
glClearDepth(0.0);
glDepthFunc(GL_GREATER);
glClear(GL_DEPTH_BUFFER_BIT);
But I failed! There appears some garbages in one scene.
The full test code is as following,
#include <GL/glut.h>
float hQuadSize = 3.28;
float R = 96.0;
float halfCone = 6.8;
#define width 500
#define height 500
// The following function is from INTERNET,
// although I forgot where it was.
void renderCube(void)
{
# define V(a,b,c) glColor3f(0.5 a 0.5, 0.5 b 0.5, 0.5 c 0.5 ); \
glVertex3f( a hQuadSize, b hQuadSize, c hQuadSize );
# define N(a,b,c) glNormal3d( a, b, c );
/*
* PWO: I dared to convert the code to use macros...
*/
glBegin( GL_QUADS );
//N( 1.0, 0.0, 0.0);
//glEnd();
//glBegin( GL_QUADS );
//N(-1.0, 0.0, 0.0);
V(-,-,+); V(-,+,+); V(-,+,-); V(-,-,-);
//glEnd();
//glBegin( GL_QUADS );
//N( 0.0,-1.0, 0.0);
V(-,-,+); V(-,-,-); V(+,-,-); V(+,-,+);
//glEnd();
//glBegin( GL_QUADS );
//N( 0.0, 0.0,-1.0);
V(-,-,-); V(-,+,-); V(+,+,-); V(+,-,-);
V(+,-,+); V(+,-,-); V(+,+,-); V(+,+,+);
//glEnd();
//glBegin( GL_QUADS );
//N( 0.0, 1.0, 0.0);
V(+,+,+); V(+,+,-); V(-,+,-); V(-,+,+);
//glEnd();
//glBegin( GL_QUADS );
//N( 0.0, 0.0, 1.0);
V(+,+,+); V(-,+,+); V(-,-,+); V(+,-,+);
glEnd();
# undef V
# undef N
}
void myDisplay(void)
{
// Init the env
glEnable(GL_DEPTH_TEST);
glClearColor(0,0,0,0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDepthFunc(GL_LESS);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(R,R, R, 0,0,0, 0,0,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(0.8*halfCone, 1.0, 0.001, 200);
// Back-scene
glClearDepth(0.0);
glDepthFunc(GL_GREATER);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0,0, width/2, height/2);
renderCube();
// Front-scene
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(width/2, height/2, width/2, height/2);
renderCube();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow("Render A Cube");
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}