我正在尝试在 OpenGL 中创建 2 个框,但它们从里到外出现,有时它们不能正确重叠。视频http://www.youtube.com/watch?v=IVJu4zJFp7Q
我猜这是一个深度问题,但我无法弄清楚我做错了什么。
这是我用来初始化的代码:
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
这是我的显示功能 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
drawYellowBox();
drawBlueBox();
我用来绘制形状的代码:
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(0.15, 0.15, 0.0);
// draw front
glVertex3f(-1.0, -1.5, 1.0);
glVertex3f( 1.0, -1.5, 1.0);
glVertex3f( 0.75, 1.5, 0.75);
glVertex3f(-0.75, 1.5, 0.75);
glColor3f(0.3, 0.3, 0.0);
// draw back
glVertex3f( 1.0, -1.5, -1.0);
glVertex3f(-1.0, -1.5, -1.0);
glVertex3f(-0.75, 1.5, -0.75);
glVertex3f( 0.75, 1.5, -0.75);
glColor3f(0.45, 0.45, 0.0);
// draw left
glVertex3f(-1.0, -1.5, -1.0);
glVertex3f(-1.0, -1.5, 1.0);
glVertex3f(-0.75, 1.5, 0.75);
glVertex3f(-0.75, 1.5, -0.75);
glColor3f(0.6, 0.6, 0.0);
// draw right
glVertex3f( 1.0, -1.5, 1.0);
glVertex3f( 1.0, -1.5, -1.0);
glVertex3f( 0.75, 1.5, -0.75);
glVertex3f( 0.75, 1.5, 0.75);
glColor3f(0.75, 0.75, 0.0);
// draw top
glVertex3f(-0.75, 1.5, 0.75);
glVertex3f( 0.75, 1.5, 0.75);
glVertex3f( 0.75, 1.5, -0.75);
glVertex3f(-0.75, 1.5, -0.75);
glColor3f(0.9, 0.9, 0.0);
// draw bottom
glVertex3f(-1.0, -1.5, -1.0);
glVertex3f( 1.0, -1.5, -1.0);
glVertex3f( 1.0, -1.5, 1.0);
glVertex3f(-1.0, -1.5, 1.0);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,2.5,0.0);
glRotatef(joint_ui_data->getDOF(Keyframe::HEAD), 0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.0, 0.15, 0.15);
// draw front face
glVertex3f(-0.8, -0.45, 0.8);
glVertex3f( 0.8, -0.45, 0.8);
glVertex3f( 0.75, 0.45, 0.75);
glVertex3f(-0.75, 0.45, 0.75);
glColor3f(0.0, 0.3, 0.3);
// draw back face
glVertex3f( 0.8, -0.45, -0.8);
glVertex3f(-0.8, -0.45, -0.8);
glVertex3f(-0.75, 0.45, -0.75);
glVertex3f( 0.75, 0.45, -0.75);
glColor3f(0.0, 0.45, 0.45);
// draw left face
glVertex3f(-0.8, -0.45, -0.8);
glVertex3f(-0.8, -0.45, 0.8);
glVertex3f(-0.75, 0.45, 0.75);
glVertex3f(-0.75, 0.45, -0.75);
glColor3f(0.0, 0.6, 0.6);
// draw right face
glVertex3f( 0.8, -0.45, 0.8);
glVertex3f( 0.8, -0.45, -0.8);
glVertex3f( 0.75, 0.45, -0.75);
glVertex3f( 0.75, 0.45, 0.75);
glColor3f(0.0, 0.75, 0.75);
// draw top
glVertex3f(-0.75, 0.45, 0.75);
glVertex3f( 0.75, 0.45, 0.75);
glVertex3f( 0.75, 0.45, -0.75);
glVertex3f(-0.75, 0.45, -0.75);
glColor3f(0.0, 0.9, 0.9);
// draw bottom
glVertex3f(-0.8, -0.45, -0.8);
glVertex3f(-0.8, -0.45, 0.8);
glVertex3f( 0.8, -0.45, 0.8);
glVertex3f( 0.8, -0.45, -0.8);
glEnd();
glPopMatrix();
谢谢你。