# -*- coding: utf-8 -*-
# -------------------------------------------
# quidam_01.py Three-dimensional space The world coordinate system and triangle
# -------------------------------------------
from OpenGL.GL import *
from OpenGL.GLUT import *
def draw():
# -------------------------------------------- -------------------
glBegin(GL_LINES) # Start drawing a line segment (world coordinate system)
# Draw the x-axis in red
glColor4f(1.0, 0.0, 0.0, 1.0) # Settings The current color is red and opaque
glVertex3f(-0.8, 0.0, 0.0) # Set the x-axis vertex (x-axis negative direction)
glVertex3f(0.8, 0.0, 0.0) # Set the x-axis vertex (x-axis positive direction)
# Draw the y-axis in green
glColor4f(0.0, 1.0, 0.0, 1.0) # Set the current color to green and opaque
glVertex3f(0.0, -0.8, 0.0) # Set the y-axis vertex (y-axis negative direction)
glVertex3f(0.0, 0.8, 0.0) # Set the y-axis vertex (Y axis positive direction)
# Draw the z axis in blue
glColor4f(0.0, 0.0, 1.0, 1.0) # Set the current color to blue and opaque
glVertex3f(0.0, 0.0, -0.8) # Set the z-axis vertex (z-axis negative direction )
glVertex3f(0.0, 0.0, 0.8) # Set the vertex of the z-axis (the positive direction of the z-axis)
glEnd() # End drawing the line segment
# ----------------------- ----------------------------------------
glBegin(GL_TRIANGLES) # Start drawing a triangle (z The negative half of the axis)
glColor4f(1.0, 0.0, 0.0, 1.0) # Set the current color to red and opaque
glVertex3f(-0.5, -0.366, -0.5) # Set the triangle vertex
glColor4f(0.0, 1.0, 0.0, 1.0) # Set the current The color is green and opaque
glVertex3f(0.5, -0.366, -0.5) # Set the triangle vertex
glColor4f(0.0, 0.0, 1.0, 1.0) # Set the current color to blue and opaque
glVertex3f(0.0, 0.5, -0.5) # Set the triangle vertex
glEnd () # End drawing triangle
# ------------------------------------------- --------------------
glFlush() # Clear the buffer and send the instructions to the hardware for immediate execution
if __name__ == "__main__":
glutInit() # 1. Initialize the glut library
glutCreateWindow('Quidam Of OpenGL') # 2. Create the glut window
glutDisplayFunc(draw) # 3. Register the callback function draw()
glutMainLoop() # 4. Enter the glut main loop
The results show that: