我想在OpenGL中創建2個盒子,但它們出現內外,有時他們不正確重疊。視頻http://www.youtube.com/watch?v=IVJu4zJFp7QOpenGL渲染盒子裏面出來
我猜這是一個深度問題,但我一直無法弄清楚我做錯了什麼。
這裏是我使用的初始化代碼:
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
這裏是我的distplay功能 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();
謝謝。
不幸的是,這doesn解決不了問題。我嘗試重新排序頂點,但沒有效果。恢復到原始頂點並將glFrontFace更改爲CW仍未修復渲染。 – Ares
@Ares查看編輯答案。 – Vallentin
這也是我的問題。 (使用Ubuntu 16.04,python GL和GLUT,我使用GLUT來繪製立方體而不是我自己的頂點來測試和隔離它;如果GLUT自己的立方體在裏面,那麼它不是繞組問題,但需要打開深度標記。 –