void RenderScene(void)
{
// 设置正面为填充模式
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glPolygonMode(GL_FRONT, GL_FILL);
// 设置反面为线形模式
glPolygonMode(GL_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST); // 1), Open the depth test
glDepthFunc(GL_LESS); //the default is GL_LESS, the less is front
// 设置逆时针绘制一个正方形
#define Zorder -0.3f //2)the z orde that the depth
#if 0
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT_AND_BACK);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.0f, -0.5f);
glVertex2f(0.0f, 0.0f);
glVertex2f(-0.5f, 0.0f);
glEnd();
glDisable(GL_CULL_FACE);
#else
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex3f(-0.5f, -0.5f, Zorder);
glVertex3f(0.0f, -0.5f, Zorder);
glVertex3f(0.0f, 0.0f, Zorder);
glVertex3f(-0.5f, 0.0f, Zorder);
glEnd();
#endif
// 设置顺时针绘制一个正方形
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
//glVertex2f(0.0f, 0.0f);
//glVertex2f(0.0f, 0.5f);
//glVertex2f(0.5f, 0.5f);
//glVertex2f(0.5f, 0.0f);
glVertex3f(-0.3f, -0.3f, -Zorder);
glVertex3f(0.2f, -0.3f, -Zorder);
glVertex3f(0.2f, 0.2f, -Zorder);
glVertex3f(-0.3f, 0.2f, -Zorder);
glEnd();
// Flush drawing commands
glutSwapBuffers();
glPopMatrix();
glDisable(GL_DEPTH_TEST); // 3),close the depth
}
Opengl深度测试
最新推荐文章于 2024-08-08 15:11:43 发布