PyopenGL(2) from scratch: simple three-dimensional graphics rendering

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = ( # vertices coordinates are written blindly
    (2, 0, 0),
    (0, 1, 0),
    (0, 1, 1),
    (0, 0, 1)
    )


edges = ( #order of edges
    (0,1),
    (0,2),
    (0,3),
    (1,2),
    (2,3),
    (3,1)
    )


def Cube():
    glColor3f(1.0, 0.0, 0.0) # set the color
    glBegin(GL_LINES) #glBegin and glEnd() are necessary functions for drawing
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex]) #This function is to connect the points. This function is executed twice to draw a line. The two points determine a straight line. The parameters are three-dimensional coordinates.
    glEnd()

def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    glClearColor(1.0, 1.0, 1.0, 1.0) # set background color
    gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
     #Z axis is the axis from our eyes to the screen. Negative is far, and it is near. In fact, it is to move the object relative to the screen in the XYZ directions by a few distances.
    glTranslatef(0, 0, -5)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: #Exit event response
                pygame.quit()
                quit()
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Used to delete the picture, empty the canvas
        Cube() #Create model
        glRotatef(1, 0, 1, 1) # Rotation matrix

        pygame.display.flip() #Display screen
        pygame.time.wait(10) #10ms refresh once

main()


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326014443&siteId=291194637