使用PyOpenGL更改图形的颜色

时间:2015-02-15 04:54:21

标签: python python-2.7 opengl pyopengl

[已关闭] 我必须使用Opengl库在Python中执行基本程序...当有人按下键时,数字会变为红色,有人按下键#g;改变绿色,当有人按下' b'改变蓝色。我不知道为什么颜色没有变化,但我知道程序知道何时按下一个键,这是我的代码......

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

1 个答案:

答案 0 :(得分:0)

我怀疑因为dibujarCirculo中的第二行将glColor3f重置为(0,0,0),所以你继续失去你在keyPressed中所做的更改。您是否尝试过将glColor3f初始化为dibujarCirculo以外的某个地方?