Search code examples
python3dpygamepyopenglvertices

Having trouble understanding pyOpenGl vertices and edges


So I am trying to learn about very basic level 3D modeling in python however I am struggling to understand how the Vertices and edges are positioned and what the numbers I am passing do. Here is an example:

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

I made this following a great tutorial from sentdex Open GL and Python. However I am having a hard time understanding why he puts in the numbers he does for the vertices. If anyone could explain the system of numbering that would be greatly appreciated! Thanks!


Solution

  • vertices is an array of 8 different 3 dimensional Cartesian coordinates, with the indices from 0 to 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
    )
    

    The coordinates define the corner points of a cube.

    edges is an array which defines the edges of the cube. Each pair of indices in the array defines a line from one corner point to another.

    e.g. (0, 1) defines an edge from (1, -1, -1) to (1, 1, -1).

    The following function takes each pair of indices of the array, reads the 2 coordinates which belong to the indices and draws a line from the first coordinate to the second. For this is used the OpenGL Primitive type GL_LINE, which draws a bunch of line segments between 2 consecutive points.

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