我试图使用 OpenGL、Python 和 pyglet 创建一个 3D 空间中的平面三角形,我在互联网上看到了一些教程,在 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()
当我运行代码时,会出现以下内容:
当我尝试旋转场景时,它发生了:
有人知道我错在哪里吗?
最佳答案
视口(viewport)的初始化以及投影和模型 View 矩阵的初始化是没有用的
glViewport(0,0,500,500) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(35,1,0.1,1000) glMatrixMode(GL_MODELVIEW)
因为视口(viewport)和正交投影是在应用程序启动时设置的。
参见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?
在 View 空间中,x 轴从左到右,y 轴从下到上。 要在 XY 平面中旋转,您必须绕 Z 轴旋转。
定义三角形的位置和 Y 角度。 Z 坐标必须为负,并且到物体的距离必须位于近平面和远平面之间。如果近为 0.1,远为 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
中的模型 View 矩阵堆栈:
@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()
关于python - 如何在 pyglet 中制作 3D?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188353/