【问题标题】:Change color of figures with PyOpenGL用 PyOpenGL 改变图形的颜色
【发布时间】:2015-04-15 21:12:00
【问题描述】:

[CLOSED]我必须使用 Opengl 库在 Python 中编写一个基本程序...当有人按下键“r”时,当有人按下键“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()

【问题讨论】:

    标签: python python-2.7 opengl pyopengl


    【解决方案1】:

    我怀疑因为dibujarCirculo 中的第二行将 glColor3f 重置为 (0,0,0),所以您一直丢失在 keyPressed 中所做的更改。您是否尝试过在 dibujarCirculo 以外的其他地方初始化 glColor3f ?

    【讨论】:

    • 是的,你有理由,我删除了那行和要运行的程序,但最初的圆圈必须是黑色的,然后这必须随着关键事件而改变。
    • 好吧,如果初始颜色必须是黑色,为什么不在initGL()中设置为黑色呢?
    • @Carlos - 那么,正如@Reto Koraldi 所说,为什么不在initGL 中设置初始颜色?或者,至少,不在您的 glutIdle 和 glutDisplay 函数中。
    猜你喜欢
    • 1970-01-01
    • 2012-07-02
    • 2021-11-28
    • 2017-09-29
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多