我想使用python,pyopengl和pygame进行3D Planet Simulation。一切正常,但现在我想确定观众的位置,以便他可以在那里添加新的行星。我已经发现了帖子:
Using glGetFloatv to retrieve the modelview matrix in pyglet
但它对我不起作用。结合互联网上的一些东西,我有这个代码:
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]);
但是当我尝试执行它时,它会给我以下错误:
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>)
它说glGetFloatv只接受一个参数,但它还有什么用呢?我已经尝试过了:
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]);
这会出现以下错误:
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>)
似乎还有另一个论点...... pname或者这样的东西......
非常感谢任何帮助!
答案 0 :(得分:1)
不要滥用OpenGL作为矩阵数学库!而是获得一个合适的矩阵数学库,它具有3D图形所需的功能,并使用它完成所有矩阵数学运算。然后使用glLoadMatrix
将容易准备的矩阵加载到OpenGL中。结果就是,您可以复制您准备的每个中间矩阵,并将其传递给以后的计算,就像您想要执行的那样。
不仅使您的代码更容易理解,而且还使其具有前瞻性,因为所有矩阵操作内容都已从OpenGL-3.3内核中删除。