如何使用OpenGL绘制线立方体?

2024-09-28 13:12:49 发布

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

我想画一个有线条的立方体

这是我的密码。它给了我一个白色的框架,里面什么都没有。什么也没发生。我做错了什么?这是调用函数的顺序有问题还是投影有问题


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


Tags: in框架密码fordefdisplay线条vertex
1条回答
网友
1楼 · 发布于 2024-09-28 13:12:49

所有不在Viewing frustum中的几何体都将被剪裁。多维数据集的大小为200x200x200。您必须创建一个足够大的视锥

例如,设置远平面为1000.0的透视投影矩阵^{}。投影矩阵旨在设置为当前投影矩阵(GL_PROJECTION)。见^{}

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)

通过^{}清除每个帧中的显示:

def Display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # [...]

通过glutSwapBuffers交换当前双缓冲窗口的缓冲区,并通过调用^{}持续更新显示

def Display():
    # [...]

    glutSwapBuffers()
    glutPostRedisplay()

另见Immediate mode and legacy OpenGL

请参见示例:

def init():
    glClearColor(0.0, 0.0, 0.0, 1.0) 
    
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1.33, 0.1, 1000.0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glTranslate(0, 0, -500)

def Display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glColor3f(0.2, 0.5, 0.4)
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

    glutSwapBuffers()
    glutPostRedisplay()

相关问题 更多 >

    热门问题