For the purpose of demonstration, we will draw a Sierpinski gasket, also known as Sierpinski triangle or Sierpinski Sieve with OpenGL. This is a fractal pattern in the shape of a triangle created by the mathematician Waclaw Sierpinski. The triangle is obtained via a recursive and, in principle infinite procedure.
def display_openGL(w, h): pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) gluOrtho2D(0, w, 0, h)
glColor3f(1.0, 0, 0) vertices = np.array([[0, 0], [DIM/2, DIM], [DIM, 0]]) NPOINTS = 9000 indices = np.random.random_integers(0, 2, NPOINTS) point = [175.0, 150.0] for i in xrange(NPOINTS): glBegin(GL_POINTS) point = (point + vertices[indices[i]])/2.0 glVertex2fv(point) glEnd() glFlush()
The Sierpinski triangle looks like the following:
The full Sierpinski gasket demo code with all the imports is as follows:
import pygame from pygame.locals import * import numpy as np from OpenGL.GL import * from OpenGL.GLU import * def display_openGL(w, h): pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) gluOrtho2D(0, w, 0, h) def main(): pygame.init() pygame.display.set_caption('OpenGL Demo') DIM = 400 display_openGL(DIM, DIM) glColor3f(1.0, 0, 0) vertices = np.array([[0, 0], [DIM/2, DIM], [DIM, 0]]) NPOINTS = 9000 indices = np.random.random_integers(0, 2, NPOINTS) point = [175.0, 150.0] for i in xrange(NPOINTS): glBegin(GL_POINTS) point = (point + vertices[indices[i]])/2.0 glVertex2fv(point) glEnd() glFlush() pygame.display.flip() while True: for event in pygame.event.get(): if event.type == QUIT: return if __name__ == '__main__': main()
As promised, the following is a line-by-line explanation of the most important parts of the example: