为什么我的深度测试在 PyOpenGL 中不起作用

Why is my depth testing not working in PyOpenGL

我在 3D 图形方面还很陌生,所以我只是在学习 :-D。我想创建一个立方体作为我的第一个项目。我选择 PyOpenGl 和 运行 来解决以下问题:深度测试对我不起作用。我在互联网上搜索了教程,但没有任何帮助我找到代码中的错误。谁能给我建议?这是我的代码:

import glfw
import math
from OpenGL.GL import *


glfw.init()

window = glfw.create_window(800, 600, "First Project", None, None)
glfw.set_window_pos(window, 400, 200)
glfw.make_context_current(window)

glEnableClientState(GL_VERTEX_ARRAY)

angle = 0.0005

cube = [
    [-0.5,  0.5, 0],
    [ 0.5,  0.5, 0],
    [ 0.5,  -0.5, 0],
    [-0.5, -0.5, 0],

    [-0.5,  0.5, -1],
    [ 0.5,  0.5, -1],
    [ 0.5,  -0.5, -1],
    [-0.5, -0.5, -1]
]

def RotateMatrix3DAroundX(x, y, z, angle):
    Rx = 1 * x + 0 * y + 0 * z
    Ry = 0 * x + math.cos(angle) * y + (-math.sin(angle) * z)
    Rz = 0 * x + math.sin(angle) * y + math.cos(angle) * z

    return [Rx, Ry, Rz]

def Drawing():

    glBegin(GL_QUADS)

    glColor3f(255, 0, 0)
    glVertex3f(cube[0][0], cube[0][1], cube[0][2])
    glVertex3f(cube[1][0], cube[1][1], cube[1][2])

    glVertex3f(cube[1][0], cube[1][1], cube[1][2])
    glVertex3f(cube[2][0], cube[2][1], cube[2][2])

    glVertex3f(cube[2][0], cube[2][1], cube[2][2])
    glVertex3f(cube[3][0], cube[3][1], cube[3][2])

    glVertex3f(cube[3][0], cube[3][1], cube[3][2])
    glVertex3f(cube[0][0], cube[0][1], cube[0][2])


    glColor3f(0, 255, 0)
    glVertex3f(cube[4][0], cube[4][1], cube[4][2])
    glVertex3f(cube[5][0], cube[5][1], cube[5][2])

    glVertex3f(cube[5][0], cube[5][1], cube[5][2])
    glVertex3f(cube[6][0], cube[6][1], cube[6][2])

    glVertex3f(cube[6][0], cube[6][1], cube[6][2])
    glVertex3f(cube[7][0], cube[7][1], cube[7][2])

    glVertex3f(cube[7][0], cube[7][1], cube[7][2])
    glVertex3f(cube[4][0], cube[4][1], cube[4][2])


    glColor3f(0, 0, 255)
    glVertex3f(cube[0][0], cube[0][1], cube[0][2])
    glVertex3f(cube[4][0], cube[4][1], cube[4][2])

    glVertex3f(cube[4][0], cube[4][1], cube[4][2])
    glVertex3f(cube[7][0], cube[7][1], cube[7][2])

    glVertex3f(cube[7][0], cube[7][1], cube[7][2])
    glVertex3f(cube[3][0], cube[3][1], cube[3][2])

    glVertex3f(cube[3][0], cube[3][1], cube[3][2])
    glVertex3f(cube[0][0], cube[0][1], cube[0][2])

    glColor3f(150, 150, 150)
    glVertex3f(cube[0][0], cube[0][1], cube[0][2])
    glVertex3f(cube[4][0], cube[4][1], cube[4][2])

    glVertex3f(cube[4][0], cube[4][1], cube[4][2])
    glVertex3f(cube[5][0], cube[5][1], cube[5][2])

    glVertex3f(cube[5][0], cube[5][1], cube[5][2])
    glVertex3f(cube[1][0], cube[1][1], cube[1][2])

    glVertex3f(cube[1][0], cube[1][1], cube[1][2])
    glVertex3f(cube[0][0], cube[0][1], cube[0][2])

    glColor3f(150, 0, 150)
    glVertex3f(cube[1][0], cube[1][1], cube[1][2])
    glVertex3f(cube[5][0], cube[5][1], cube[5][2])

    glVertex3f(cube[5][0], cube[5][1], cube[5][2])
    glVertex3f(cube[6][0], cube[6][1], cube[6][2])

    glVertex3f(cube[6][0], cube[6][1], cube[6][2])
    glVertex3f(cube[2][0], cube[2][1], cube[2][2])

    glVertex3f(cube[2][0], cube[2][1], cube[2][2])
    glVertex3f(cube[1][0], cube[1][1], cube[1][2])

    glColor3f(150, 150, 0)
    glVertex3f(cube[3][0], cube[3][1], cube[3][2])
    glVertex3f(cube[7][0], cube[7][1], cube[7][2])

    glVertex3f(cube[7][0], cube[7][1], cube[7][2])
    glVertex3f(cube[6][0], cube[6][1], cube[6][2])

    glVertex3f(cube[6][0], cube[6][1], cube[6][2])
    glVertex3f(cube[2][0], cube[2][1], cube[2][2])

    glVertex3f(cube[2][0], cube[2][1], cube[2][2])
    glVertex3f(cube[3][0], cube[3][1], cube[3][2])
    glEnd()

    for i in range(8):
        cube[i] = RotateMatrix3DAroundX(cube[i][0], cube[i][1], cube[i][2], angle)

while not(glfw.window_should_close(window)):

    glfw.poll_events()
    glClearColor(0, 0, 0, 0)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LESS)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    Drawing()

    glfw.swap_buffers(window)

glfw.terminate()

这是我得到的结果图片: image_of_my_result

立方体的某些部分被观察体积的近平面和远平面裁剪。设置一个Orthographic projection到近平面和远平面的距离更大:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-1, 1, -1, 1, -2, 2)
glMatrixMode(GL_MODELVIEW)

while not(glfw.window_should_close(window)):
    # [...]

不改变立方体的顶点,而是使用旋转矩阵:

def Drawing():
    # [...]

    glRotate(math.degrees(angle), 1, 0, 0)

    # DELETE
    #for i in range(8):
    #    cube[i] = RotateMatrix3DAroundX(cube[i][0], cube[i][1], cube[i][2], angle)