我是python和OpenGL的初学者,我试图绘制带线的立方体。这是我的代码。它只是给我一个白色框架,里面没有任何东西。什么都没发生。我在这里做错了什么?调用函数的顺序是否有问题,或者投影有问题?任何帮助,将不胜感激。
def myInit():
glClearColor(0.0, 0.0, 0.0, 1.0)
glColor3f(0.2, 0.5, 0.4)
gluPerspective(45, 1.33, 0.1, 50.0)
vertices= (
(100, -100, -100),
(100, 100, -100),
(-100, 100, -100),
(-100, -100, -100),
(100, -100, 100),
(100, 100, 100),
(-100, -100, 100),
(-100, 100, 100)
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
)
def Display():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(800, 600)
myInit()
glutDisplayFunc(Display)
glutMainLoop()
gluPerspective
。投影矩阵旨在设置为当前投影矩阵(gluPerspective
)。参见GL_PROJECTION
:用glMatrixMode
清除每帧的显示:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.33, 0.1, 1000.0)
Translate ([`glTranslate`](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml)) the model along the negative z axis, in between the near plane (0.1) and far (plane). The model or view matrix has to be set to the current model view matrix (`GL_MODELVIEW`):
```py
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslate(0, 0, -500)
用glClear
交换当前双缓冲窗口的缓冲区,并通过调用glClear
连续更新显示。
def Display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# [...]
请参见示例:
glutSwapBuffers