Change color of figures with PyOpenGL

Question:

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()
Asked By: Carmoreno

||

Answers:

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 ?

Answered By: user1245262

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()
Answered By: Abdullah Shafqat
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.