Skip to main content
 首页 » 编程设计

python之创建一个修剪立方体 Pyglet 的平面

2024年11月24日7zhengyun_ustc

我有一个移动、缩放、旋转的立方体,我需要创建一个平面来像那样修剪立方体

下面是绘图代码

pgl.glLoadIdentity() 
pgl.glViewport(650, 500, 650, 500) 
pgl.glMatrixMode(ogl.GL_PROJECTION) 
pgl.glLoadIdentity() 
 
pgl.gluPerspective(self.dist, 1.3, 1, 1000) 
 
pgl.glMatrixMode(ogl.GL_MODELVIEW) 
 
pgl.glTranslatef(0, 0, -400) 
 
pgl.glPushMatrix() 
pgl.glTranslatef(self.x, self.y, self.z) 
pgl.glRotatef(self.xRotation, 1, 0, 0) 
pgl.glRotatef(self.yRotation, 0, 1, 0) 
pgl.glRotatef(self.zRotation, 0, 0, 1) 
pgl.glScalef(self.zoom, self.zoom, self.zoom) 
 
 
if not transparant: 
    pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL) 
else: 
    pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE) 
 
draw_big() 
 
pgl.glPopMatrix() 

请您参考如下方法:

与 Legacy OpenGL固定功能管道,你可以设置一个裁剪平面。

可以有超过 1 个裁剪平面,这些平面必须由 glEnable(GL_CLIP_PLANEi) 启用.

平面由glClipPlane设置.剪切平面的参数被解释为 Plane Equation . 平面方程的前 3 个分量是剪切平面的法向量。第 4 个分量是到原点的距离:

plane = plane = [-1.0, -1.0, -1.0, -280] 
ogl.glClipPlane(pgl.GL_CLIP_PLANE0, plane) 

有关详细规范,请参阅 OpenGL 4.6 API Compatibility Profile Specification - 13.7. PRIMITIVE CLIPPING ;第 537 页。
请注意,当前模型 View 矩阵的逆在指定时应用于裁剪平面系数。

看例子,是根据问题的代码:

def on_draw(self) : 
 
    self.clear() 
    pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT) 
 
    pgl.glViewport(0, 0, 500, 500) 
 
    pgl.glMatrixMode(ogl.GL_PROJECTION) 
    pgl.glLoadIdentity() 
    pgl.gluPerspective(45, 1, 1, 1000) 
 
    pgl.glMatrixMode(ogl.GL_MODELVIEW) 
    pgl.glLoadIdentity() 
    pgl.glTranslatef(0, 0, -400) 
 
    pgl.glPushMatrix() 
    pgl.glTranslatef(self.x, self.y, self.z) 
    pgl.glRotatef(self.xRotation, 1, 0, 0) 
    pgl.glRotatef(self.yRotation, 0, 1, 0) 
    pgl.glRotatef(self.zRotation, 0, 0, 1) 
    pgl.glScalef(self.zoom, self.zoom, self.zoom) 
 
    if not transparant: 
        pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL) 
    else: 
        pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE) 
 
    # set and enable clip plane 
    plane = plane = [-1.0, -1.0, -1.0, -280] 
    ogl.glEnable(pgl.GL_CLIP_PLANE0) 
    ogl.glClipPlane(pgl.GL_CLIP_PLANE0, plane) 
 
    draw_big() 
    ogl.glDisable(pgl.GL_CLIP_PLANE0) 
 
    pgl.glPopMatrix()