如何在 pyglet 中制作 3D?

How do I make 3D in pyglet?

我尝试使用 OpenGL、Python 和 pyglet 创建一个 3D 平面三角形 space,我在互联网上看到了一些教程,在 YouTube 上看到了一些视频,最后我把这段代码写在那里,问题是它没有像我预期的那样工作,我想如果我尝试旋转,我会看到三角形变平,当我走开时​​,三角形不必减少?

import pyglet
from pyglet.gl import *

config = Config(sample_buffers=1, samples=8)
tela = pyglet.window.Window(height=500, width=500, config=config)

glViewport(0,0,500,500)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(35,1,0.1,1000)
glMatrixMode(GL_MODELVIEW)

@tela.event
def on_draw():
    glBegin(GL_POLYGON)
    glVertex3f(10,10,0)
    glVertex3f(100,10,0)
    glVertex3f(50,100,0)
    glEnd()
    glFlush()

@tela.event
def on_key_press(s,m):
    tela.clear()
    if s == pyglet.window.key.W:
        glTranslatef(0,0,1)
    if s == pyglet.window.key.S:
        glTranslatef(0,0,-1)
    if s == pyglet.window.key.A:
        glRotatef(1,0,1,0)
    if s == pyglet.window.key.D:
        glRotatef(-1,0,1,0)

pyglet.app.run()

当我 运行 代码出现时:

当我尝试旋转场景时,它发生了:

有谁知道我错在哪里?

视口的初始化以及投影和模型视图矩阵的字符串无用

glViewport(0,0,500,500)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(35,1,0.1,1000)
glMatrixMode(GL_MODELVIEW) 

因为视口和正交投影是在应用程序启动时设置的。

参见pyglet - The OpenGL interface:

[...] pyglet sets up the viewport and an orthographic projection on each window automatically.

如果你会使用透视投影

gluPerspective(35,1,0.1,1000)

那么三角形就会消失,因为它会被透视投影的近平面裁剪(0.1)。


为了解决这个问题,把透视投影的设置放到draw事件中:

@tela.event
def on_draw():

    tela.clear()

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90, 1, 0.1, 100)

I thought that if I tried to spin, I would see the triangle turning flat, and when I walked away, the triangle did not have to diminish?

在视图space中,x轴从左指向右,y轴从下向上。 要在XY平面旋转,就得绕Z轴旋转。

定义三角形的位置和 Y 角。 Z 坐标必须为负,到物体的距离必须在近平面和远平面之间。如果near为0.1,far为100,则:

0.1 <= -z <= 100

例如

pos = [0, 0, -20]
rot_y = 0

在事件中操纵位置和角度:

@tela.event
def on_key_press(s,m):

    global pos_z, rot_y

    if s == pyglet.window.key.W:
        pos[2] -= 1
    if s == pyglet.window.key.S:
        pos[2] += 1
    if s == pyglet.window.key.A:
        rot_y += 5
    if s == pyglet.window.key.D:
        rot_y -= 5

将平移和旋转应用于 draw 中的模型视图矩阵堆栈:

@tela.event
def on_draw():

    global pos_z, rot_y

    # [...]

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glTranslatef(*pos)
    glRotatef(rot_y, 0, 1, 0)

画一个围绕(0, 0, 0)排列的物体。请注意,对象的位置由 pos 设置,在透视投影中,原点 (0, 0, 0) 位于 window:

的中心
glBegin(GL_POLYGON)
glVertex3f(-5,-5,0)
glVertex3f(5,-5,0)
glVertex3f(0,5,0)
glEnd()

已应用建议更改的完整代码:

import pyglet
from pyglet.gl import *

pos = [0, 0, -20]
rot_y = 0

config = Config(sample_buffers=1, samples=8)
tela = pyglet.window.Window(height=500, width=500, config=config)

@tela.event
def on_draw():

    global pos_z, rot_y

    tela.clear()

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90, 1, 0.1, 100)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glTranslatef(*pos)
    glRotatef(rot_y, 0, 1, 0)

    glBegin(GL_POLYGON)
    glVertex3f(-5,-5,0)
    glVertex3f(5,-5,0)
    glVertex3f(0,5,0)
    glEnd()

    glFlush()

@tela.event
def on_key_press(s,m):

    global pos_z, rot_y

    if s == pyglet.window.key.W:
        pos[2] -= 1
    if s == pyglet.window.key.S:
        pos[2] += 1
    if s == pyglet.window.key.A:
        rot_y += 5
    if s == pyglet.window.key.D:
        rot_y -= 5

pyglet.app.run()