繁体   English   中英

为什么我的深度测试在 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

立方体的一部分被查看体积的近平面和远平面剪裁。 设置到近平面和远平面距离较大的正交投影

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM