Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想两次使用object a类,但它';没有发生什么_Python_Opengl_Pyopengl - Fatal编程技术网

Python 我想两次使用object a类,但它';没有发生什么

Python 我想两次使用object a类,但它';没有发生什么,python,opengl,pyopengl,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)

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的教程,我没有找到好的。

说明

glClear(GL_COLOR_BUFFER_BIT)
清除frambuffer的完整颜色平面。这意味着您在绘制第二个圆之前清除frambuffer

draw
方法中删除
glClear(GL\u COLOR\u BUFFER\u BIT)
,并在绘图说明开始时执行此操作。进一步的
glFlush
应在图纸说明之后进行

见:

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

你的架构不合适。您不应该从
Draw
派生
Circle
,但是
Draw
应该使用
Circle

这意味着您必须创建一个
类Draw
,它进行初始化并处理主循环。另一个分开的
类圆
绘制一个圆:

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


它在第一行“glClear(GL\u COLOR\u BUFFER\u BIT)cycle=Circle(100200)cycle.draw()cycle=Circle(220300)cycle.draw()”中给出了错误@BishwaKarki您得到了什么错误?代码没有改变。只有说明的顺序已更改。-请参阅对answer.OpenGL.error.GLError的更改:GLError(err=1282,description=b'invalid operation',baseOperation=glClear,cArguments=(GL\u COLOR\u BUFFER\u BIT,))这是错误produce@BishwaKarki查看对答案的更改
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()