如何使用pyglet保存当前视图矩阵

时间:2019-07-05 14:54:21

标签: python opengl pyglet opengl-compat

我正在使用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通道)重用a,但是现在我只得到输出

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 个答案:

答案 0 :(得分: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_MODELVIEWGL_PROJECTIONGL_TEXTUREGL_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将矩阵加载到当前矩阵。