如何在pyglet中制作3D?

2024-09-30 16:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用OpenGL,Python和pyglet来创建三维空间中的一个平面三角形,我在互联网上看到了一些教程,在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()

当我运行代码时,会显示:

enter image description here

当我试图改变这个场景的时候:

enter image description here

有人知道我哪里错了吗?在


Tags: key代码importeventconfigifondef
1条回答
网友
1楼 · 发布于 2024-09-30 16:32:18

视口的初始化以及投影和模型视图矩阵的sting是无用的

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?

在视图空间中,x轴从左到右,y轴从下到上指向上。 要在XY平面上旋转,必须绕Z轴旋转。在

定义三角形的位置和Y角。Z坐标必须为负,到对象的距离必须在近平面和远平面之间。如果“近”为0.1,而“远”为100,则:

^{pr2}$

例如

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)位于窗口的中心:

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()

相关问题 更多 >