我试图用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()
当我运行代码时,会显示:
当我试图改变这个场景的时候:
有人知道我哪里错了吗?在
视口的初始化以及投影和模型视图矩阵的sting是无用的
因为视口和正交投影是在应用程序启动时设置的。在
见pyglet - The OpenGL interface:
如果你用透视投影
然后三角形就会消失,因为它会被透视投影的近平面(
0.1
)剪裁。在要解决此问题,请将透视投影的设置置于
draw
事件:在视图空间中,x轴从左到右,y轴从下到上指向上。 要在XY平面上旋转,必须绕Z轴旋转。在
定义三角形的位置和Y角。Z坐标必须为负,到对象的距离必须在近平面和远平面之间。如果“近”为0.1,而“远”为100,则:
^{pr2}$例如
在以下情况下操纵位置和角度:
将平移和旋转应用于
draw
中的模型视图矩阵堆栈:画一个围绕(0,0,0)排列的对象。注意对象的位置由
pos
设置,在透视投影中,原点(0,0,0)位于窗口的中心:应用建议更改的完整代码:
相关问题 更多 >
编程相关推荐