PyOpenGL is a binding for OpenGL. You need to use some framework to create windows and handle windowing events. In the past I've used pyglet for window creation / input handling / sound. Other common options here include SDL and pySFML but there really are quite a few.
PyOpenGL是OpenGL的绑定。您需要使用一些框架来创建窗口并处理窗口事件。在过去,我使用pyglet进行窗口创建/输入处理/声音。这里的其他常见选项包括SDL和pySFML,但实际上有很多。
After that its a matter of deciding how the camera should work and using the input to modify your camera properties. Once you have, say, well defined position, forward, and up vectors for the camera you can turn these things into a matrix that transforms your scene into the point of view of the camera.
之后,确定相机应如何工作并使用输入来修改相机属性。一旦你拥有了明确定义的摄像机位置,前移和向上矢量,你就可以将这些东西变成一个矩阵,将你的场景转换成摄像机的视角。
Using OpenGL directly is very low level. It's left up to you to define exactly how this should work.
直接使用OpenGL是非常低的水平。您可以自行定义它应该如何工作。
Here's some code of mine that uses numpy. You can manipulate the camera using the interface functions in spatial.py (tie these to the mouse input events)
这是我的一些使用numpy的代码。您可以使用spatial.py中的接口函数操作相机(将这些功能与鼠标输入事件联系起来)
Once those work you can get the relevant matrices (as numpy arrays) with
一旦这些工作,您可以获得相关的矩阵(如numpy数组)
Camera.get_projection_matrix()
and
Camera.get_camera_matrix()
For instance in one of my projects I initialize the camera with
例如,在我的一个项目中,我用相机初始化相机
def setupView(self):
self.camera = Camera()
self.camera.set_position(v3(3, 0, 10))
self.camera.look_at(v3(0, 0, 0))
and then sync the camera to the player (just another Spatial
) with
然后将相机同步到播放器(只是另一个空间)
def updateCamera(self):
up = v3(*self.player.get_world_up())
position = v3(*self.player.get_position())
position += (v3(*self.player.get_world_forward()) * -10.0)
position += (up * 3.0)
self.camera.set_position(position)
self.camera.set_orientation(self.player.get_orientation())
The mouse events affect the player
鼠标事件会影响播放器
def on_mouse_motion(self, x, y, dx, dy):
self.player.yaw(-dx * 0.005)
self.player.pitch(dy * 0.005)
On draw then passes the camera matrix to a vertex shader
在绘制时,然后将相机矩阵传递到顶点着色器
def on_draw(self, renderer, t, dt):
...
matC = camera.get_camera_matrix()
renderer.set_shader_mat44('matC', matC)
...
And the vertex shader takes care of the actual vertex transformations by left multiplying C with the current modelview matrix.
并且顶点着色器通过左边将C与当前模型视图矩阵相乘来处理实际的顶点变换。
This is not for faint of heart :)
这不适合胆小的人:)