cubepyopengldepth-buffer

Why is my depth testing not working in PyOpenGL


I'm quite new in 3D graphics so I'm just learning :-D. I wanted to create a cube as my first project. I chose PyOpenGl and ran into the following problem: Depth testing didn't work for me. I searched the tutorials on the internet but nothing helped me find a bug in my code. Can anyone advise me? Here is my code:

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()

Here is image of result I am getting: image_of_my_result


Solution

  • Parts of your cube are clipped by the near and far plane of the viewing volume. Set an Orthographic projection with a larger distance to the near and far plane:

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-1, 1, -1, 1, -2, 2)
    glMatrixMode(GL_MODELVIEW)
    
    while not(glfw.window_should_close(window)):
        # [...]
    

    Instead of changing the cube vertices, use a rotation matrix:

    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)