Search code examples
pythonpython-2.7openglpyopengl

Change color of figures with PyOpenGL


I have to do a basic program in Python using the library Opengl...when somebody press the key 'r', the figure change to red, when somebody pressed key 'g' change green and when somebody pressed 'b' change blue. I don't know why the color doesn't change, but i know the program know when a key is pressed, this is my code...

from OpenGL.GL import *
from OpenGL.GLUT import *
from math import pi 
from math import sin
from math import cos

def initGL(width, height):
   glClearColor(0.529, 0.529, 0.529, 0.0)
   glMatrixMode(GL_PROJECTION)

def dibujarCirculo():
  glClear(GL_COLOR_BUFFER_BIT)
  glColor3f(0.0, 0.0, 0.0)

  glBegin(GL_POLYGON)
  for i in range(400):
    x = 0.25*sin(i) #Cordenadas polares x = r*sin(t) donde r = radio/2  (Circunferencia centrada en el origen)
    y = 0.25*cos(i) #Cordenadas polares y = r*cos(t)
    glVertex2f(x, y)            
  glEnd()
  glFlush()

def keyPressed(*args):
  key = args[0]
  if key == "r":
    glColor3f(1.0, 0.0, 0.0)
    print "Presionaste",key
  elif key == "g":
    glColor3f(0.0, 1.0, 0.0)
    print "Presionaste g"
  elif key ==   "b":
    glColor3f(0.0, 0.0, 1.0)
    print "Presionaste b"           

def main():
  global window
  glutInit(sys.argv)
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
  glutInitWindowSize(500,500)
  glutInitWindowPosition(200,200)

  #creando la ventana
  window = glutCreateWindow("Taller uno")

  glutDisplayFunc(dibujarCirculo)
  glutIdleFunc(dibujarCirculo)
  glutKeyboardFunc(keyPressed)
  initGL(500,500)
  glutMainLoop()

if __name__ == "__main__":
  main()

Solution

  • I suspect that because the 2nd line in dibujarCirculo resets glColor3f to (0,0,0), you keep losing the change you made in keyPressed. Have you tried initializing glColor3f somewhere other than dibujarCirculo ?