如何使用pyglet保存当前视图矩阵
Posted
技术标签:
【中文标题】如何使用pyglet保存当前视图矩阵【英文标题】:How to save the current view matrix using pyglet 【发布时间】:2019-07-05 14:54:21 【问题描述】:我正在使用 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 还会弹出一个窗口询问它是否正确关闭
【问题讨论】:
【参考方案1】:如何使用 pyglet 保存当前的视图矩阵
当前矩阵有一堆矩阵。矩阵可以通过glPushMatrix
glPopMatrix
推入堆栈和从堆栈中弹出。
变换前推入矩阵,变换后弹出:
glPushMatrix()
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
glPopMatrix()
请注意,每种矩阵模式都有一堆矩阵(GL_MODELVIEW
、GL_PROJECTION
、GL_TEXTURE
和 GL_COLOR
)。见glMatrixMode
。
错误是因为glGetFloatv
没有返回任何值。它将矩阵读取到传递给函数的内存中。
您必须将数组传递给glMultMatrixf
modelMat = (GLfloat * 16)()
glGetFloatv(GL_MODELVIEW_MATRIX, modelMat) # read to array "modelMat"
glLoadIdentity()
# [...]
glMultMatrixf(modelMat) # multiply matrix in the array "modelMat"
注意,顾名思义,glMultMatrixf
将矩阵乘以当前矩阵。这适用于您的情况,因为当前矩阵是Identity matrix。可以通过glLoadMatrix
将矩阵加载到当前矩阵。
【讨论】:
啊,谢谢!我知道我可以推送/弹出,但我认为glLoadIdentity()
命令重置了矩阵,很高兴知道它只是添加到堆栈中。
@Jmonsky 您最初的想法也很有效。请参阅答案的第二部分。以上是关于如何使用pyglet保存当前视图矩阵的主要内容,如果未能解决你的问题,请参考以下文章