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!
Update: I experimented with recording the demo using Fraps, which worked rather nicely - I may even consider buying it! I didn't realize it was recording audio though, or I may have chosen a different soundtrack (it recorded a portion of mp3 I was listening to). Hope nobody sues me. Here's the a video of the demo.
This is really great, thanks!
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
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.
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
Hi Jon,
Are you running the latest version of PyOpenGL?
Try replacing the glColor line with the following:
glColor( *self.color )
Will
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
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!
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
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.
It seems that in the latest version, glColor wont accept colours as an iterable of 3 value and requires 3 distinct value. Fortunately the fix is easy, just insert an asterisk to unpack the color.
So the start of the Cube's render method should look like this:
Any idea what could have caused it? Other pygame opengl things seem to work fine (e.g. pygame NeHe samples).
Get this error when I try to run firstopengl.py. (Running Ubuntu 9.04):
jo@tux:~/pygameBook/Chapter09$ python firstopengl.py
get fences failed: -1
param: 6, val: 0
Traceback (most recent call last):
File “firstopengl.py”, line 237, in <module>
run()
File “firstopengl.py”, line 230, in run
map.render()
File “firstopengl.py”, line 141, in render
cube.render()
File “firstopengl.py”, line 94, in render
glVertex( vertices )
File “/usr/lib/python2.6/dist-packages/OpenGL/GL/exceptional.py”, line 133, in glVertex
return glVertexDispatch( args )
ctypes.ArgumentError: argument 1: <type ‘exceptions.TypeError’>: byref() argument must be a ctypes instance, not ‘tuple’
I've added the * to the start of the Cube render method, but get the same error, so it's not related to that.
Any clues?
I'm sure you'll get the drift.
I'm using python 2.5.2
OpenGL.__version__ ‘3.0.0b6’
Did you spend a thought on how to avoid upcoming deprecation issues when using glBegin and glVertex in pyOpenGL 3.1?
I have no idea what to use else.
Screenshot: http://i54.tinypic.com/2h7p0er.jpg
A = Move Left
S = Move Backward
D = Move Right
Q = Strafe Left
E = Strafe Right
Mouse = Move Camera
ESC = Quit (the only one you got right)
Havent you ever actually PLAYED a video game before?
(i never used pyOpenGL before)
(sorry my bad English )
Can't seem to run any pyopengl applications on my Win7 64bit setup. Using python 3.3 but keep getting error saying _init_ takes 2 positional arguments but only one given. Any help would be appreciated please as I'd like to get to grips with python and opengl. Thanks.
I get the following error when i try to run “simpleopengl.py”.
Traceback (most recent call last):
File “firstopengl.py”, line 233, in <module>
run()
File “firstopengl.py”, line 149, in run
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)
pygame.error: Couldn't find matching GLX visual
Any ideas how to fix it. I'm running the default version of Raspbian on a Raspberry Pi with pyopengl installed.
I get the following error when i try to run “simpleopengl.py”.
Traceback (most recent call last):
File “firstopengl.py”, line 233, in <module>
run()
File “firstopengl.py”, line 149, in run
screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)
pygame.error: Couldn't find matching GLX visual
Any ideas how to fix it. I'm running the default version of Raspbian on a Raspberry Pi with pyopengl installed.
Cheers!
It could be updated to expose today's rendering techniques (anything that does not use glBegin/glEnd calls).
Hey...Will...am studying game dev using your book...now I just have a blank PyOpenGL window...but the code is as u wrote...and also can u help me find that Matrix44 module pleade
Traceback (most recent call last): File "C:/Users/Emma/AppData/Local/Programs/Python/Python38-32/opengl.py", line 11, in <module> from gameobjects.matrix44 import * ModuleNotFoundError: No module named 'gameobjects'