Search code examples
pythonpython-2.7openglpyopengl

Why I cannot rotate my cube in PyOpenGL?


I am trying to rotate my cube when I pushed the key 'r' (keyevent), I am using Pyopengl, but I don't know why this doesn't it.

This is a part of my code.

def init(): 
   glMatrixMode (GL_PROJECTION)
   glClearColor(0.529, 0.529, 0.529, 0.0)#Definimos color de ventana
   glColor3f(0.0, 1.0, 1.0)#Definimos color inicial para el cubo

def dibujarCubo():
   glClear (GL_COLOR_BUFFER_BIT)
   glColor3f (1.0, 1.0, 1.0)
   glLoadIdentity ()
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
   glutWireCube (1.0)
   glFlush ()

def reshape (w, h): #Esta funcion se escarga de reescalar la ventana
   glViewport (0, 0, w, h)
   glLoadIdentity ()
   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
   glMatrixMode (GL_MODELVIEW)

def keyPressed(*args):
   key = args[0]
   if key == "r" or key =="R":
      glRotatef(30, 1, 0, 0)#Rotate in x

Solution

  • Generally you only want to call OpenGL functions from the display callback.

    Modify keyPressed to increment a variable & call glutPostRedisplay() and dibujarCubo to use that variable as a glRotatef() parameter.