-2

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()
genpfault
  • 51,148
  • 11
  • 85
  • 139
Carmoreno
  • 1,271
  • 17
  • 29

2 Answers2

0

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 ?

user1245262
  • 6,968
  • 8
  • 50
  • 77
  • Yes you have reason, I erased that line and the program to run, but the initial circle must be color black, and then this must change with the key events. – Carmoreno Feb 15 '15 at 14:12
  • Well, if the initial color must be black, why wouldn't you set it to black in `initGL()`? – Reto Koradi Feb 15 '15 at 15:24
  • @Carlos - Theen, as @Reto Koraldi says, why not set the initial color in `initGL`? Or, at least, not in your glutIdle and glutDisplay function. – user1245262 Feb 15 '15 at 15:48
0

You can do something like this where you store the current shape color globally and update that when key presses are detected

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

import random

shapeColor = [0, 0, 0]

numAgents = 300

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

def dibujarCirculo():
  glClear(GL_COLOR_BUFFER_BIT)
  glColor3f(shapeColor[0], shapeColor[1], shapeColor[2])

  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]
  global shapeColor
  if key == b'r':
    shapeColor = [1,0,0]
  elif key == b'g':
    shapeColor = [0,1,0]
  elif key == b'b':
    shapeColor = [0,0,1]        

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