无法理解pyOpenGl顶点和边缘

时间:2018-05-12 10:16:41

标签: python 3d pygame pyopengl vertices

所以我正在尝试学习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()

我在senddex Open GL and Python的一个很棒的教程之后做了这个。然而,我很难理解为什么他把数字放入顶点。如果有人能解释编号系统,将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

vertices是一个包含8个不同的3维Cartesian coordinates的数组,索引从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是一个定义多维数据集边缘的数组。数组中的每对索引定义从一个角点到另一个角点的线。

e.g。 (0,1)定义从(1,-1,-1)到(1,1,-1)的边。

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

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