我想两次使用对象,但它没有发生

时间:2017-12-23 07:52:44

标签: python opengl pyopengl

basic.py

from OpenGL.GL import*
from OpenGL.GLU import*
from OpenGL.GLUT import*
import sys

class Draw:
   # def draw_func(self):
   #     pass

    def draw(self):
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
        glutInitWindowSize(1000,1000)
        glutInitWindowPosition(50,50)
        glutCreateWindow(b'Cycle')
        glutDisplayFunc(self.draw_func)
        glClearColor(0,0,0,1)
        gluOrtho2D(-500,500,-500,100)
        glutMainLoop()

cycle.py

class Circle(Draw):
   def __init__(self,xv,yv):
      self.xv=xv
      self.yv= yv

   def draw_circle(self):
      glClear(GL_COLOR_BUFFER_BIT)
      glColor3f(1, 0, 0)
      glPointSize(3)
    for t in arange(self.xv, self.yv, 0.001):
        x = 50 * sin(t)
        y = 50 * cos(t)
        glBegin(GL_POINTS)
        glVertex2f(x, y)
        glEnd()
        glFlush()

   def draw_func(self):
      self.draw_circle()

cycle = Circle(100,200)
cycle.draw()
cycle = Circle(220,300)
cycle.draw()

这里我使用pyOpenGl来学习图形,我想使用Circle对象两次绘制2个圆圈,但它只绘制一个。我不知道我缺少哪些知识。另外,请建议我学习pyOpenGl的任何教程我没有找到好的。

1 个答案:

答案 0 :(得分:1)

指令

glClear(GL_COLOR_BUFFER_BIT)

清除frambuffer的完整色彩平面。这意味着在绘制第二个圆之前清除frambuffer。

glClear(GL_COLOR_BUFFER_BIT)方法中删除draw,并在绘图说明的开头执行此操作。进一步glFlush应在绘图说明后完成。

请参阅glClear

  

glClear将窗口的位平面区域设置为glClearColor之前选择的值。

您的架构不合适。您不应从Circle派生Draw,但Draw应使用Circle
这意味着您必须创建一个class Draw来执行初始化并处理主循环。另一个分隔的class Circle绘制一个圆圈:

class Circle:
    def __init__(self,xv,yv):
        self.xv=xv
        self.yv= yv

    def draw(self):
        glColor3f(1, 0, 0)
        glPointSize(3)
        for t in arange(self.xv, self.yv, 0.001):
        x = 50 * sin(t)
        y = 50 * cos(t)
        glBegin(GL_POINTS)
        glVertex2f(x, y)
        glEnd()

class Draw:
    def __init__(self,cx,cy):
        self.cx = cx
        self.cy = cy

    def draw(self):
        glutInit(sys.argv)
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
        glutInitWindowSize(self.cx,self.cy)
        glutInitWindowPosition(50,50)
        glutCreateWindow(b'Cycle')
        glutDisplayFunc(self.draw_loop)
        glClearColor(0,0,0,1)
        gluOrtho2D(-self.cx/2,self.cx/2,-self.cy/2,self.cy/2)
        glutMainLoop()

    def draw_loop(self):
        glClear(GL_COLOR_BUFFER_BIT) 

        cycle = Circle(100,200)
        cycle.draw()
        cycle = Circle(220,300)
        cycle.draw()

        glFlush()
        glutPostRedisplay()          

wnd = Draw(1000,1000)
wnd.draw()