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.
CodePudding user response:
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)