为什么此 wxPython/PyOpenGL 代码会在 glPushMatrix 上引发错误?
Why does this wxPython/PyOpenGL code raise an error on glPushMatrix?
我正在清除一些用于 运行 但现在不再使用的 wxPython/PyOpenGL 代码 -- 显然它在一个或另一个模块中进行了一些更新后停止工作,我不知道什么时候。
这是一个重现错误的小型示例应用程序。
import wx
from wx import glcanvas
from OpenGL.GLUT import *
from OpenGL.GL import *
class WxMyTestApp(wx.App):
def __init__(self):
wx.App.__init__(self)
def OnInit(self):
self.viewFrame = wx.Frame(None, -1, 'viewFrame', wx.DefaultPosition, (400,400))
self.canvas = glcanvas.GLCanvas(self.viewFrame)
self.canvas.Bind(wx.EVT_PAINT, self.OnPaint)
return True
def OnPaint(self, ev):
print "OnPaint ",
dc = wx.PaintDC(self.canvas)
self.canvas.SetCurrent()
self.redraw(ev)
def redraw(self, ignoredEvent):
glPushMatrix()
glColor3f(1.0, 1.0, 0.0)
glLineWidth(1)
glutWireSphere(0.5, 10,10)
glPopMatrix()
glFlush()
self.canvas.SwapBuffers()
if __name__ == '__main__':
d = WxMyTestApp()
d.viewFrame.Show()
d.MainLoop()
(注意我使用的是wx.App而不是wx.PySimpleApp,因为我的真实代码使用了多个window。)运行测试代码,框架显示了一个左上角的白色小矩形在调整大小时会重复出现此错误:
Traceback (most recent call last):
File "myTestApp.py", line 18, in OnPaint
self.redraw(ev)
File "myTestApp.py", line 20, in redraw
glPushMatrix()
File "C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages\OpenGL\platform\baseplatform.py", line 402, in __call__
return self( *args, **named )
File "C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages\OpenGL\error.py", line 232, in glCheckError
baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
err = 1282,
description = 'invalid operation',
baseOperation = glPushMatrix,
cArguments = ()
这里推送真的有问题吗?我怀疑问题与 GL 设备上下文有关,但不知道如何调试它。 Python 2.7.10,wxPython 3.0.2.0-3,PyOpenGL 3.1.0-2(Canopy 分布)。
我受益于 GLCanvas and GLContext 的文档,显然是在 wxWidgets/wxPython Phoenix 的努力下产生的。似乎较早版本的 wxWidgets 会隐式地为您创建一个 GLContext,但这已被弃用。使用此信息,我能够更新代码。我需要的两个更改是:
在OnInit
赋值后添加一行self.canvas
:
self.canvas = glcanvas.GLCanvas(self.viewFrame)
self.context = glcanvas.GLContext(self.canvas) # add this line
然后在 OnPaint
中更改此行:
self.canvas.SetCurrent()
对此:
self.canvas.SetCurrent(self.context) # add the argument
这些小改动让我的代码重新启动 运行。希望其他人可以从中受益。
我正在清除一些用于 运行 但现在不再使用的 wxPython/PyOpenGL 代码 -- 显然它在一个或另一个模块中进行了一些更新后停止工作,我不知道什么时候。
这是一个重现错误的小型示例应用程序。
import wx
from wx import glcanvas
from OpenGL.GLUT import *
from OpenGL.GL import *
class WxMyTestApp(wx.App):
def __init__(self):
wx.App.__init__(self)
def OnInit(self):
self.viewFrame = wx.Frame(None, -1, 'viewFrame', wx.DefaultPosition, (400,400))
self.canvas = glcanvas.GLCanvas(self.viewFrame)
self.canvas.Bind(wx.EVT_PAINT, self.OnPaint)
return True
def OnPaint(self, ev):
print "OnPaint ",
dc = wx.PaintDC(self.canvas)
self.canvas.SetCurrent()
self.redraw(ev)
def redraw(self, ignoredEvent):
glPushMatrix()
glColor3f(1.0, 1.0, 0.0)
glLineWidth(1)
glutWireSphere(0.5, 10,10)
glPopMatrix()
glFlush()
self.canvas.SwapBuffers()
if __name__ == '__main__':
d = WxMyTestApp()
d.viewFrame.Show()
d.MainLoop()
(注意我使用的是wx.App而不是wx.PySimpleApp,因为我的真实代码使用了多个window。)运行测试代码,框架显示了一个左上角的白色小矩形在调整大小时会重复出现此错误:
Traceback (most recent call last):
File "myTestApp.py", line 18, in OnPaint
self.redraw(ev)
File "myTestApp.py", line 20, in redraw
glPushMatrix()
File "C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages\OpenGL\platform\baseplatform.py", line 402, in __call__
return self( *args, **named )
File "C:\users\user\appdata\local\enthought\canopy\user\lib\site-packages\OpenGL\error.py", line 232, in glCheckError
baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
err = 1282,
description = 'invalid operation',
baseOperation = glPushMatrix,
cArguments = ()
这里推送真的有问题吗?我怀疑问题与 GL 设备上下文有关,但不知道如何调试它。 Python 2.7.10,wxPython 3.0.2.0-3,PyOpenGL 3.1.0-2(Canopy 分布)。
我受益于 GLCanvas and GLContext 的文档,显然是在 wxWidgets/wxPython Phoenix 的努力下产生的。似乎较早版本的 wxWidgets 会隐式地为您创建一个 GLContext,但这已被弃用。使用此信息,我能够更新代码。我需要的两个更改是:
在OnInit
赋值后添加一行self.canvas
:
self.canvas = glcanvas.GLCanvas(self.viewFrame)
self.context = glcanvas.GLContext(self.canvas) # add this line
然后在 OnPaint
中更改此行:
self.canvas.SetCurrent()
对此:
self.canvas.SetCurrent(self.context) # add the argument
这些小改动让我的代码重新启动 运行。希望其他人可以从中受益。