2

I'm trying to draw a 3D cube using Python and its packages, pygame, numpy, PyOpenGL, and PyOpenGL_accelerate. The 3D cube also rotates to other directions when I press the arrow buttons. For example, it rotates to right when I press the right arrow.

First, I've tried it on other PC and it runs perfectly. But then, I've also tried it on my laptop and when I run it, it doesn't show anything in the screen. I successfully installed the required packages and all but it still doesn't show anything.

Here's my code:

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()

display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

pygame.display.set_caption("03 lab 1 ")

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)

angle = 1
rotatex = 0
rotatey = 0
rotatez = 0


def draw_cube():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glBegin(GL_LINES)
    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 0)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 0)


    glVertex3f(-0.5, 0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(0.5, -0.5, 1)


    glVertex3f(0.5, -0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 1)

    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 1)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 1)

    glEnd()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    rotatey -= 1
                if event.key == pygame.K_RIGHT:
                    rotatex += 1
                if event.key == pygame.K_UP:
                    rotatey += 1
                if event.key == pygame.K_DOWN:
                    rotatex -= 1


        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

glRotate(angle, rotatex, rotatey,rotatez)
pygame.display.flip()
pygame.time.wait(15)
draw_cube()

So, here's my output. It just show a blank screen.

output

genpfault
  • 51,148
  • 11
  • 85
  • 139
Ai-Yue
  • 23
  • 6

1 Answers1

1

You have to update the display after drawing the cube. Call pygame.display.flip() after glEnd(). You need to redraw the cube in each frame and change the rotation matrix in each frame. It is a matter of Indentation. The indentation of the application loop is wrong.

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

pygame.init()

display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

pygame.display.set_caption("03 lab 1 ")

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)

angle = 1
rotatex = 0
rotatey = 0
rotatez = 0


def draw_cube():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glBegin(GL_LINES)
    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 0)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 0)


    glVertex3f(-0.5, 0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(-0.5, -0.5, 1)
    glVertex3f(0.5, -0.5, 1)


    glVertex3f(0.5, -0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(0.5, 0.5, 1)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(-0.5, 0.5, 0)
    glVertex3f(-0.5, 0.5, 1)

    glVertex3f(0.5, 0.5, 0)
    glVertex3f(0.5, 0.5, 1)

    glVertex3f(-0.5, -0.5, 0)
    glVertex3f(-0.5, -0.5, 1)

    glVertex3f(0.5, -0.5, 0)
    glVertex3f(0.5, -0.5, 1)

    glEnd()

# INDENTATION
#<--| 

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                rotatey -= 1
            if event.key == pygame.K_RIGHT:
                rotatex += 1
            if event.key == pygame.K_UP:
                rotatey += 1
            if event.key == pygame.K_DOWN:
                rotatex -= 1


    if event.type == pygame.QUIT:
        pygame.quit()
        quit()

    # INDENTATION
#-->|

    glRotate(angle, rotatex, rotatey,rotatez)
    draw_cube()
    pygame.display.flip()
    pygame.time.wait(15)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • The rotate and wait calls probably need to be there too, although I think more needs to change to actually make the cube rotate. – Yun Oct 28 '22 at 17:02
  • I have tried what you suggested. It does show the cube but it's not rotating. – Ai-Yue Oct 28 '22 at 17:20
  • @Ai-Yue Of course not. You have to redraw r cube in every frame and you habe to change the rotation matrix in every frame. It's a matter of indentation. The indentation of the applikation loop ist wrong. – Rabbid76 Oct 28 '22 at 17:32