如何使用 opengl 和 pyglet 设置裁剪平面

How to set clipping planes with opengl and pyglet

我正在解决我的代码中的一个问题,即如果任何图元的深度值不为零,它将不会在屏幕上呈现。我怀疑它被剪掉了。

有没有一种简单的 pythonic 方法可以在 pyglet 中设置我的剪裁平面?

到目前为止,这是我的代码:

import pyglet
from pyglet.gl import *
import pywavefront
from camera import FirstPersonCamera


def drawloop(win,camera):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    #glClearColor(255,255,255,255)
    glLoadIdentity()
    camera.draw()

    pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
    ('v3f', (10.0, 15.0, 0.0, 30.0, 35.0, 150.0))
    )
    glPointSize(20.)
    return pyglet.event.EVENT_HANDLED


def main():
    win = pyglet.window.Window()
    win.set_exclusive_mouse(True)
    win.clear()
    camera = FirstPersonCamera(win)
    @win.event
    def on_draw(): 
        drawloop(win,camera)
    def on_update(delta_time):
        camera.update(delta_time)
    pyglet.clock.schedule(on_update)
    pyglet.app.run()

if __name__ == '__main__':
    main()

我正在使用此处的 FirstPersonCamera 片段:

https://gist.github.com/mr-linch/f6dacd2a069887a47fbc

I am troubleshooting a problem with my code that if the depth value of any primitive is not zero it will not render on the screen. I suspect that it gets clipped away.

你必须设置一个投影矩阵来解决这个问题。要么设置正交投影矩阵,要么设置透视投影矩阵。

投影矩阵描述了从场景视图的 3D 点到视口上的 2D 点的映射。它从眼睛 space 变换到剪辑 space,并且剪辑 space 中的坐标通过除以 w 分量转换为标准化设备坐标(NDC)剪辑坐标。 NDC 的范围为 (-1,-1,-1) 到 (1,1,1)。
超出剪辑 space 的每个几何图形都被剪辑。

在正交投影中,视图中的坐标 space 线性映射到剪辑 space 坐标,剪辑 space 坐标等于标准化设备坐标,因为 w分量为1(对于笛卡尔输入坐标)。
left、right、bottom、top、near 和 far 的值定义了一个框。盒子体积内的所有几何体在视口上都是 "visible"。

在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口的 2D 点的映射。
相机平截头体(截棱锥)中的眼睛 space 坐标映射到立方体(归一化设备坐标)。

要设置投影矩阵,必须通过 glMatrixMode 选择投影矩阵堆栈。

可以通过glOrhto设置正交投影:

w, h = 640, 480 # default pyglet window size

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho( -w/2, w/2, -h/2, h/2, -1000.0, 1000.0) # [near, far] = [-1000, 1000]

glMatrixMode(GL_MODELVIEW)
....

可以通过gluPerspective设置透视投影:

w, h = 640, 480 # default pyglet window size

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective( 90.0, 640.0/480, 0.1, 1000.0) # fov = 90 degrees; [near, far] = [0.1, 1000]

glMatrixMode(GL_MODELVIEW)
....

我建议使用以下坐标,"see"上述两种情况下的点:

例如:

pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
    ('v3f', (-50.0, -20.0, -200.0, 40.0, 20.0, -250.0)))
glPointSize(20.0)