尽管在GLUT_DEPTH
中使用glutInitDisplayMode
作为参数并执行glClear(GL_DEPTH_BUFFER_BIT)
,我已经使用OpenGL进行了一段时间的测试并且无法使深度缓冲区正常工作}在显示功能的开头。我不知道我还缺少什么。
下面是最小工作示例和图1和2.当您想要查看另一个时,请注释掉图1和图2中的参数。
图1(蓝色上方):
图2(红色下方):
示例:
#include <vector>
#include <gl\glut.h>
typedef std::vector<float> floatvec;
// Figure 1 (above blue)
float posX = 8.00f;
float posY = 7.54f;
float posZ = -0.89f;
float angleX = 300.50f;
float angleY = 45.33f;
// Figure 2 (below red)
float posX = 4.12f;
float posY = -4.87f;
float posZ = -3.84f;
float angleX = 343.25f;
float angleY = -45.00f;
int screenW = 720;
int screenH = 540;
float fMin = 0.5;
float fMax = 100.0;
float alpha = 60.0;
// Draws a rectangle in 3D
void DrawQuad(floatvec c) {
glBegin(GL_QUADS);
glVertex3f(-c[0], -c[1], -c[2]);
glVertex3f(-c[3], -c[4], -c[5]);
glVertex3f(-c[6], -c[7], -c[8]);
glVertex3f(-c[9], -c[10], -c[11]);
glEnd();
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glRotatef(angleY, 1.0f, 0.0f, 0.0f);
glRotatef(angleX, 0.0f, 1.0f, 0.0f);
glRotatef(180.0, 0.0f, 0.0f, 1.0f);
glTranslatef(posX, posY, posZ);
// Draws the tiles
glColor3f(1.0, 0.0, 0.0); // Red
DrawQuad({ 0, 0, 0,
5, 0, 0,
5, 0, 5,
0, 0, 5 });
glColor3f(0.0, 0.0, 1.0); // Blue
DrawQuad({ 0, 3, 0,
5, 3, 0,
5, 3, 5,
0, 3, 5 });
glPopMatrix();
glutSwapBuffers();
}
void Reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(alpha, (GLfloat)width / (GLfloat)height, fMin, fMax);
glMatrixMode(GL_MODELVIEW);
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glEnable(GL_DEPTH_TEST);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenW, screenH);
glutCreateWindow("Example");
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}
答案 0 :(得分:6)
您只需更改拨打glEnable(GL_DEPTH_TEST);
将它放在glutCreateWindow
之后,它会正常工作。
这样的事情:
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenW, screenH);
glutCreateWindow("Example");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}
刚刚在这里测试过它。
答案 1 :(得分:1)
我在OpenGL中有点生疏,但是,将glEnable行放在Display函数中就可以了。
#include <vector>
#include <GL/glut.h>
typedef std::vector<float> floatvec;
//Figure 1 (above blue)
//float posX = 8.00f;
//float posY = 7.54f;
//float posZ = -0.89f;
//float angleX = 300.50f;
//float angleY = 45.33f;
// Figure 2 (below red)
float posX = 4.12f;
float posY = -4.87f;
float posZ = -3.84f;
float angleX = 343.25f;
float angleY = -45.00f;
int screenW = 720;
int screenH = 540;
float fMin = 0.5;
float fMax = 100.0;
float alpha = 60.0;
// Draws a rectangle in 3D
void DrawQuad(floatvec c) {
glBegin(GL_QUADS);
glVertex3f(-c[0], -c[1], -c[2]);
glVertex3f(-c[3], -c[4], -c[5]);
glVertex3f(-c[6], -c[7], -c[8]);
glVertex3f(-c[9], -c[10], -c[11]);
glEnd();
}
void Display() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glRotatef(angleY, 1.0f, 0.0f, 0.0f);
glRotatef(angleX, 0.0f, 1.0f, 0.0f);
glRotatef(180.0, 0.0f, 0.0f, 1.0f);
glTranslatef(posX, posY, posZ);
// Draws the tiles
glColor3f(1.0, 0.0, 0.0); // Red
DrawQuad({ 0, 0, 0,
5, 0, 0,
5, 0, 5,
0, 0, 5 });
glColor3f(0.0, 0.0, 1.0); // Blue
DrawQuad({ 0, 3, 0,
5, 3, 0,
5, 3, 5,
0, 3, 5 });
glPopMatrix();
glutSwapBuffers();
}
void Reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(alpha, (GLfloat)width / (GLfloat)height, fMin, fMax);
glMatrixMode(GL_MODELVIEW);
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenW, screenH);
glutCreateWindow("Example");
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}