Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 理解pyOpenGl顶点和边有困难_Python_3d_Pygame_Pyopengl_Vertices - Fatal编程技术网

Python 理解pyOpenGl顶点和边有困难

Python 理解pyOpenGl顶点和边有困难,python,3d,pygame,pyopengl,vertices,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, -

所以我试图学习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的一个很棒的教程制作的。然而,我很难理解为什么他要输入他对顶点所做的数字。如果有人能解释一下编号系统,我将不胜感激!谢谢

顶点
是一个由8个不同的三维顶点组成的数组,索引范围为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
)
坐标定义立方体的角点

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

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

下面的函数获取数组的每对索引,读取属于索引的2个坐标,并从第一个坐标到第二个坐标绘制一条线。为此,使用了OpenGL类型
GL\u LINE
,它在两个连续点之间绘制一组线段

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

非常感谢你!维基链接也非常有用:)