Getting started


Lets start with creating a window.  Here is a file with a basic display initialization function.

gutil.py:
    1 
2 import pygame
3
4 from OpenGL.GL import *
5 from OpenGL.GLU import *
6
7 def initializeDisplay(w, h):
8 pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF)
9
10 glClearColor(0.0, 0.0, 0.0, 1.0)
11 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
12
13 glMatrixMode(GL_PROJECTION);
14 glLoadIdentity();
15 gluOrtho2D(0, w, 0, h);
16 glMatrixMode(GL_MODELVIEW);

Let me explain what the above does.

We import PyGame, but we don't bring it into our namespace because we won't use a lot of calls.  We import the full OpenGL namespace because we will be using many of it's functions and definitions.

The display initialization takes window width and height parameters, in pixels.

Line 8 uses PyGame to set the display to the desired height and width, in OpenGL mode, using double buffering.

Lines 10 and 11 set the screen clear color to black, and clear any old data that may be around.

The remaining lines Set OpenGL to use a 2d Orthographic projection.  This means that 1 OpenGL unit is 1 pixel in size, the origin is in the bottom left of the screen, and up and right are positive.  Note that this is different from traditional 2d graphics programming systems where the origin is in the top left, and down and right are positive.

Using the library

Here is an example using the function.

example1a.py:
    1 #!/usr/bin/python
2
3 import gutil
4 import pygame
5
6 def main():
7 pygame.init()
8 gutil.initializeDisplay(800, 600)
9
10 done = False
11
12 while not done:
13 pygame.display.flip()
14
15 if __name__ == '__main__':
16 main()


This program covers the bare minimum for getting a graphics window up and running.  First it imports our gutil library and PyGame.  Then on line 7 it initializes PyGame.  Line 8 creates an 800 x 600 windows.  The main body of the program is an infinite loop that does nothing but refresh the window.

This should open a black window that won't close unless you kill the program.

Something (a little) more interesting

Lets expand on example1a.py

example1b.py:
    1 #!/usr/bin/python
2
3 import gutil
4 import pygame
5 from pygame.locals import *
6 from OpenGL.GL import *
7
8
9 def main():
10 pygame.init()
11 gutil.initializeDisplay(800, 600)
12
13 glColor4f(1.0,1.0,1.0,1.0)
14
15 done = False
16
17 while not done:
18 glBegin(GL_TRIANGLES)
19 glVertex2f(10,400)
20 glVertex2f(400, 400)
21 glVertex2f(400, 200)
22 glEnd()
23
24 pygame.display.flip()
25
26 eventlist = pygame.event.get()
27 for event in eventlist:
28 if event.type == QUIT \
29 or event.type == KEYDOWN and event.key == K_ESCAPE:
30 done = True
31
32 if __name__ == '__main__':
33 main()

Lines 5 and 6 are new.  We are importing some PyGame constants into our namespace, and adding OpenGL so that we can do some drawing.

On line 13 we are telling OpenGL what color we want to draw with.  The format is Red, Green, Blue, Alpha, with 0 as the minimum and 1.0 as the maximum.  Here we are drawing with 100% white, with 100% opacity.

Lines 18 to 22 in the main display loop draw a triangle on the screen.

Finally, on lines 26 to 30 we find code to listen for quit commands.  We use PyGame to read a list of system events that have happened since our last time through the loop.  We scan the list of events looking for either the quit command or the escape key.  If we received either, we exit the main loop, which exits the program.



Back to Introduction
On to Example 2a