Python opengl cant get modelview matrix

799 views Asked by At

I want to make a 3D Planet Simulation using python,pyopengl and pygame. All works fine but now I want to determine the viewers position so that he can add new planets there. I already found that post :

Using glGetFloatv to retrieve the modelview matrix in pyglet

But it doesnt works for me. Combined with some things from the internet I have this code :

x=0;
y=0;
z=0;
mdl=(GLfloat * 16)()
glGetFloatv(GL_MODELVIEW_MATRIX, mdl);
x = -(mdl[0] * mdl[12] + mdl[1] * mdl[13] + mdl[2] * mdl[14]);
y = -(mdl[4] * mdl[12] + mdl[5] * mdl[13] + mdl[6] * mdl[14]);
z = -(mdl[8] * mdl[12] + mdl[9] * mdl[13] + mdl[10] * mdl[14]);

But when I try to execute it, it gives me the following error :

Traceback (most recent call last):
  File "Planets.py", line 372, in <module>
    main() 
  File "Planets.py", line 264, in main
    glGetFloatv(GL_MODELVIEW_MATRIX, mdl);
  File "/usr/lib/python2.7/dist-packages/OpenGL/latebind.py", line 45, in __call__
    return self._finalCall( *args, **named )
  File "/usr/lib/python2.7/dist-packages/OpenGL/wrapper.py", line 570, in wrapperCall
    pyArgs = tuple( calculate_pyArgs( args ))
  File "/usr/lib/python2.7/dist-packages/OpenGL/wrapper.py", line 347, in calculate_pyArgs
    args
ValueError: glGetFloatv requires 1 arguments (pname), received 2: (GL_MODELVIEW_MATRIX, <__main__.c_float_Array_16 object at 0x7f0d01f4e830>)

It says that glGetFloatv only takes one argument, but how else should it work ? I already tried that :

x=0;
y=0;
z=0;
mdl=(GLfloat * 16)()
glGetFloatv(mdl);
x = -(mdl[0] * mdl[12] + mdl[1] * mdl[13] + mdl[2] * mdl[14]);
y = -(mdl[4] * mdl[12] + mdl[5] * mdl[13] + mdl[6] * mdl[14]);
z = -(mdl[8] * mdl[12] + mdl[9] * mdl[13] + mdl[10] * mdl[14]);

This gives the following error :

Traceback (most recent call last):
  File "Planets.py", line 372, in <module>
    main() 
  File "Planets.py", line 264, in main
    glGetFloatv(mdl);
  File "/usr/lib/python2.7/dist-packages/OpenGL/latebind.py", line 45, in __call__
    return self._finalCall( *args, **named )
  File "/usr/lib/python2.7/dist-packages/OpenGL/wrapper.py", line 571, in wrapperCall
    cArgs = tuple(calculate_cArgs( pyArgs ))
  File "/usr/lib/python2.7/dist-packages/OpenGL/wrapper.py", line 374, in calculate_cArgs
    yield converter( pyArgs, index, self )
  File "/usr/lib/python2.7/dist-packages/OpenGL/converters.py", line 195, in __call__
    return self.arrayType.zeros( self.getSize(pyArgs) )
  File "/usr/lib/python2.7/dist-packages/OpenGL/converters.py", line 232, in getSize
    return self.lookup( specifier )
TypeError: ('unhashable type', 'Failure in cConverter <OpenGL.converters.SizedOutput object at 0x7f91788a9380>', (<__main__.c_float_Array_16 object at 0x7f915d804830>,), 1, <ctypes.glGetFloatv object at 0x7f917890c0e0>)

It seems that another argument is expected... pname or such a thing...

Any help would be greatly appreciated !

1

There are 1 answers

4
datenwolf On

Don't abuse OpenGL as a matrix math library! Instead get a proper matrix math library that has the functions required for 3D graphics and do all the matrix math with that. Then load the readily prepared matrices into OpenGL using glLoadMatrix. The upshot of that is, that you can take copies of every intermediary matrix you prepare and pass that on for later calculations, like the one you want to perform.

Not only makes this your code easier to follow, it also makes it future proof, because all the matrix manipulation stuff has been removed from OpenGL-3.3 core onward.