Time for Action – drawing the Sierpinski gasket

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.

  1. First, start out by initializing some of the OpenGL related primitives. This includes setting the display mode and background color. A line-by-line explanation is given at the end of this section:
    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)
  2. The algorithm requires us to display points, the more the better. First, we set the drawing color to red. Second, we define the vertices (I call them points myself) of a triangle. Then, we define random indices, which are to be used to choose one of the three triangle vertices. We pick a random point somewhere in the middle—it doesn't really matter where. After this, draw points halfway between the previous point and one of the vertices picked at random. Finally, flush the result:
        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:

    Time for Action – drawing the Sierpinski gasket

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

What just happened?

As promised, the following is a line-by-line explanation of the most important parts of the example:

Function

Description

pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF)

This function sets the display mode to the required width, height, and OpenGL display

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

This function clears the buffers using a mask. Here we clear the color buffer and depth buffer bits

gluOrtho2D(0, w, 0, h)

This function defines a two-dimensional orthographic projection matrix with the coordinates of the left, right, top, and bottom clipping planes

glColor3f(1.0, 0, 0)

This function defines the current drawing color using three float values for RGB (red, green, blue). In this case, we will be painting in red

glBegin(GL_POINTS)

This function delimits the vertices of primitives or a group of primitives. Here the primitives are points

glVertex2fv(point)

This function renders a point given a vertex

glEnd()

This function closes a section of code started with glBegin()

glFlush()

This function forces the execution of GL commands

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset