Search code examples
pythonopenglpygletray-picking

Using glGetFloatv to retrieve the modelview matrix in pyglet


I'm doing 3d visualization in python using pyglet, and need to retrieve the modelview and projection matrices to do some picking. I define my window using:

from pyglet.gl import *
from pyglet.window import *

win = Window(fullscreen=True, visible=True, vsync=True)

I then define all of my window events:

@win.event
def on_draw():
    # All of the drawing happens here

@win.event
def on_mouse_release(x, y, button, modifiers):
    if button == mouse.LEFT:

    # This is where I'm having problems
    a = GLfloat()
    mvm = glGetFloatv(GL_MODELVIEW_MATRIX, a)
    print a.value

When I click, it will print...

1.0
Segmentation fault

and crash. Calling glGetFloatv with GL_MODELVIEW_MATRIX is supposed to return 16 values, and I'm not exactly sure how to handle that. I tried defining a = GLfloat*16 but I get the following error:

ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: expected LP_c_float instance instead of _ctypes.PyCArrayType

How can I retrieve these matrices?


Solution

  • You need to pass 16 element float array. To do that use following code:

      a = (GLfloat * 16)()
      mvm = glGetFloatv(GL_MODELVIEW_MATRIX, a)
      print list(a)
    

    Of course, you can access individual elements of "a" by using a[0] syntax.