OpenGL sample code for PyGame
I’ve uploaded a simple example of how to use OpenGL with PyGame. It’s a listing from my forthcoming book, Beginning Game Development with Python and PyGame, and demonstrates how to initialize OpenGL, create a light and draw a simple 3D ‘world’. It also shows how to use the camera matrix to create a simple ‘fly-cam’.
To run it you will need PyOpenGL, which you can download from the website, or from the command line if you have Easy Install (type easy_install PyOpenGL). Use the cursor keys to look around, and the Q/A keys to move forward and back. You can also press Z and X to roll the camera.
Here’s a screenshot.
I’m impressed by the ease of using OpenGL with PyGame. It’s a one liner to create an OpenGL display, and the OpenGL bindings work well. With good use of display lists or vertex arrays, performance can be on par with commercial games. To put it in perspective, you could render an entire 3D object in less time it takes to draw a simple 2D sprite. I hope people take advantage of it, because the ‘casual games’ market is huge right now, and Python could give developers a serious advantage in getting games to market quickly!

June 7th, 2007 at 5:02 pm
This is really great, thanks!
June 10th, 2007 at 5:28 pm
Will,
Thanks for sharing this. Great coding. It gave me a huge leg up on understanding GL in PyGame and a great start on my (non-commercial) project.
But you didn’t include any GNU license… and I’d like to give credit where it’s due.
Also, did you write the matrix44 & vector3 classes? Very elegant and infinitely useful… they should be part of the standard library.
(BTW If you are not the author, do you know who is? Again: credit due)
So thanks again for the great code!
rd
June 12th, 2007 at 10:13 pm
Well,I like to say thank you this gets me closer to understandig opengl. Since my compt refuses to let me play anymore it claims it cannot open the gl,weird ha, mohaa.os was before ME i SWITCH TO,WINDOWS XP HOME EDITION.MORE POWER TO YA SOON i SHALL PLAY AGAIN.
October 12th, 2007 at 11:08 pm
Hi, I’m getting the following error, any ideas how to fix it. Thanks.
Traceback (most recent call last):
File “firstopengl.py”, line 231, in
run()
File “firstopengl.py”, line 225, in run
map.render()
File “firstopengl.py”, line 133, in render
cube.render()
File “firstopengl.py”, line 73, in render
glColor( self.color )
File “build/bdist.macosx-10.3-fat/egg/OpenGL/GL/exceptional.py”, line 207, in glColor
KeyError: 1
October 13th, 2007 at 4:50 pm
Hi Jon,
Are you running the latest version of PyOpenGL?
Try replacing the glColor line with the following:
glColor( *self.color )
Will
October 13th, 2007 at 6:22 pm
Hi Will,
Thanks Will that’s fixed it. What was the cause of the error if you don’t mind me asking ?
BTW do you know when your book will be available in the UK ?
Cheers
Jon
October 13th, 2007 at 9:30 pm
I’m guessing that the version of PyOpenGL I was using expands Python sequences and calls an appropriate glColor*v function in C. The version you are using must require four separate values. If I’m right you could also do glColorv(self color), which is probably more efficient.
The book should be out everywhere on the 24th, but Amazon.co.uk are showing the wrong date!
May 19th, 2008 at 9:41 am
I’m getting this error trying to run “firstopengl”:
Traceback (most recent call last):
File “/home/rudin/Skrivebord/python-bok/firstopengl.py”, line 184, in
run()
File “/home/rudin/Skrivebord/python-bok/firstopengl.py”, line 169, in run
camera_matrix.translate += movement * time_passed_seconds
File “/usr/lib/python2.5/site-packages/gameobjects/vector3.py”, line 247, in __radd__
ox, oy, oz = lhs
ValueError: too many values to unpack
petzolt@online.no
August 22nd, 2008 at 6:43 am
I’m guessing that the version of PyOpenGL I was using expands Python sequences and calls an appropriate glColor*v function in C. The version you are using must require four separate values. If I’m right you could also do glColorv(self color), which is probably more efficient.