0

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 !

Community
  • 1
  • 1
Luatic
  • 8,513
  • 2
  • 13
  • 34
  • For the records: Your title talkes about retrieving the projection matrix, but what you actually do is trying to get the modelview matrix. These two are not the same. – BDL Dec 23 '16 at 12:14
  • Thanks. I didnt notice that. – Luatic Dec 23 '16 at 12:18
  • if you're using opengl 3 .x, it doesnt have matrix stack and that get will fail. oh, and there are preexisting libraries to be used. withc++ usually it is glm. for python there is at least two and should be one 3d oriented libary as there is such 3d engine like Panda3D – Swift - Friday Pie Dec 23 '16 at 12:37
  • thanks for the help, swift – Luatic Dec 23 '16 at 14:28

1 Answers1

1

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I already thought of doing that, but implementing that would take long, and I need to display it using opengl, so I thought fixing this error would do it easy instead of a copied matrix that I transform the same way as I do with the opengl matrix. – Luatic Dec 23 '16 at 12:08
  • I found this article : https://open.gl/transformations – Luatic Dec 23 '16 at 12:09
  • @user7185318: You don't have to implement all the math yourself. You can use NumPy, or of you feel that is overkill Pyrr. See this http://stackoverflow.com/questions/10162659/opengl-matrix-math-utilities-for-python – datenwolf Dec 23 '16 at 13:23
  • I already started implementing a simple camera class... – Luatic Dec 23 '16 at 14:28