首页 > 解决方案 > 无法理解 pyOpenGl 顶点和边

问题描述

因此,我正在尝试学习 Python 中非常基本的 3D 建模,但是我很难理解顶点和边的位置以及我传递的数字的作用。这是一个例子:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

"""
- A Cube has 8 Nodes/Verticies
- 12 Lines/connections
- 6 Sides
"""

vertices = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
)

edges = ( #Contains vertexes/nodes
    (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 Cube():
    glBegin(GL_LINES)

    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex]) #Draws vertex's in position given according to vertices array
    glEnd()


def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(35, (display[0]/display[1]), 0.1, 50.0) #FOV, aspect ratio. clipping plane

    glTranslatef(0.0, 0.0, -5) #X,Y,Z -5 to zoom out on z axis
    glRotatef(20, 0, 0, 0) #Degrees, x,y,z

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Clears the screen

        Cube()
        pygame.display.flip() #Cant use update
        pygame.time.wait(10)

main()




pygame.quit()
quit()

我是根据 sentdex Open GL 和 Python的精彩教程制作的。但是,我很难理解他为什么要输入他为顶点所做的数字。如果有人能解释编号系统,将不胜感激!谢谢!

标签: python3dpygamepyopenglvertices

解决方案


vertices是一个由 8 个不同的 3 维笛卡尔坐标组成的数组,索引从 0 到 7:

vertices = (
    ( 1, -1, -1),   # 0
    ( 1,  1, -1),   # 1
    (-1,  1, -1),   # 2
    (-1, -1, -1),   # 3
    ( 1, -1,  1),   # 4
    ( 1,  1,  1),   # 5
    (-1, -1,  1),   # 6
    (-1,  1,  1)    # 7
)

坐标定义立方体的角点。

edges是一个定义立方体边缘的数组。数组中的每一对索引定义了从一个角点到另一个角点的线。

例如 (0, 1) 定义从 (1, -1, -1) 到 (1, 1, -1) 的边。

以下函数获取数组的每对索引,读取属于索引的 2 个坐标,并从第一个坐标到第二个坐标画一条线。为此使用 OpenGL Primitive类型GL_LINE,它在 2 个连续点之间绘制一堆线段。

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex]) 
    glEnd()

推荐阅读