我正在使用 pyglet 并希望有一个可以为场景背景绘制颜色的功能。我目前只是在屏幕上绘制一个从左下角到右上角的矩形。在我开始应用转换之前,这很有效。例如,如果我使用旋转命令,“背景”矩形也会旋转
我尝试使用 glGetFloatv 获取当前矩阵,将其重置为标识,然后将其重置。
a = (GLfloat * 16)() # do something?
modelMat = glGetFloatv(GL_MODELVIEW_MATRIX, a) # save matrix
glLoadIdentity() # reset matrix
x1, y1, x2, y2 = 0, 0, self.width, self.height # get the coords
# self.width / self.height give the width and height of the screen
quad = pyglet.graphics.vertex_list(4,
('v2i', (x1, y1, x2, y1, x2, y2, x1, y2)),
('c4B', (
r, g, b, a,
r, g, b, a,
r, g, b, a,
r, g, b, a))
) # define the rectangle
quad.draw(GL_QUADS) # draw rectangle
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # do alpha
glMultMatrixf(modelMat) # reset matrix
此代码返回此错误:
Traceback (most recent call last):
File "/Users/jakemonsky/Development/pythonApps/PygletTest/main.py", line 34, in <module>
APP.run()
File "/Users/jakemonsky/Development/pythonApps/PygletTest/processing.py", line 364, in run
pyglet.app.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/app/__init__.py", line 138, in run
event_loop.run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/app/base.py", line 142, in run
self._run()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/app/base.py", line 154, in _run
timeout = self.idle()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/app/base.py", line 281, in idle
window.dispatch_event('on_draw')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/window/__init__.py", line 1232, in dispatch_event
if EventDispatcher.dispatch_event(self, *args) != False:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/event.py", line 371, in dispatch_event
event_type, args, getattr(self, event_type))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/event.py", line 367, in dispatch_event
if getattr(self, event_type)(*args):
File "/Users/jakemonsky/Development/pythonApps/PygletTest/processing.py", line 223, in on_draw
self.bind_draw(dt)
File "/Users/jakemonsky/Development/pythonApps/PygletTest/processing.py", line 41, in __call__
func(*args, **kwargs)
File "/Users/jakemonsky/Development/pythonApps/PygletTest/main.py", line 14, in draw
Background(0,0,0,255)
File "/Users/jakemonsky/Development/pythonApps/PygletTest/processingLocalizer.py", line 62, in Background
Application.Background(r, g, b, a)
File "/Users/jakemonsky/Development/pythonApps/PygletTest/processing.py", line 449, in Background
r, g, b, a))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/graphics/__init__.py", line 292, in vertex_list
return _get_default_batch().add(count, 0, None, *data)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/graphics/__init__.py", line 372, in add
vlist._set_attribute_data(i, array)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pyglet/graphics/vertexdomain.py", line 443, in _set_attribute_data
region.array[:] = data
TypeError: incompatible types, c_float_Array_16 instance instead of c_ubyte instance
[Finished in 2.3s with exit code 1]
我不确定是什么原因造成的,或者我是否正确地解决了这个问题。我对 openGL 和 pyglet 非常陌生,不确定导致此错误的幕后情况。有没有更好的方法来“重置”转换或在屏幕空间中将某些东西绘制到屏幕上而不是我缺少的转换空间?
编辑:错误是因为我愚蠢地重用了矩阵(也是我的alpha通道)但现在我只是得到了输出
2019-07-05 11:03:46.789 Python[853:21283] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
[Finished in 2.1s with exit code -11]
当命令被调用并且程序刚刚结束时。OSX 还会弹出一个窗口询问它是否正确关闭