如何使用pyglet保存当前视图矩阵
How to save the current view matrix using pyglet
我正在使用 pyglet 并希望有一个可以为场景背景绘制颜色的函数。我目前只是在屏幕上绘制一个从左下角到右上角的矩形。在我开始应用转换之前,这很好用。例如,如果我使用旋转命令,'background' 矩形也会旋转
我试过使用 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
此代码returns此错误:
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 的新手,不确定是什么导致了这个错误。有没有更好的方法来 'reset' 转换或在屏幕 space 中将某些东西绘制到屏幕上而不是我缺少的转换 space 的方法?
编辑:
错误是因为我愚蠢地将 a 重用于矩阵(也是我的 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 还会弹出一个 window 询问是否正常关闭
How to save the current view matrix using 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
没有return任何值。它将矩阵读取到传递给函数的内存中。
您必须将数组传递给 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. A matrix can be loaded to the current matrix by glLoadMatrix
。
我正在使用 pyglet 并希望有一个可以为场景背景绘制颜色的函数。我目前只是在屏幕上绘制一个从左下角到右上角的矩形。在我开始应用转换之前,这很好用。例如,如果我使用旋转命令,'background' 矩形也会旋转
我试过使用 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
此代码returns此错误:
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 的新手,不确定是什么导致了这个错误。有没有更好的方法来 'reset' 转换或在屏幕 space 中将某些东西绘制到屏幕上而不是我缺少的转换 space 的方法?
编辑: 错误是因为我愚蠢地将 a 重用于矩阵(也是我的 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 还会弹出一个 window 询问是否正常关闭
How to save the current view matrix using 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
没有return任何值。它将矩阵读取到传递给函数的内存中。
您必须将数组传递给 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. A matrix can be loaded to the current matrix by glLoadMatrix
。