Python and opengl es on raspberry pi linux - status?


24 posts
by toxibunny » Tue Jan 24, 2012 3:02 pm
...I'm looking around on the net, mucking around a bit in python and I've had a bit of a go with linux and pygame in preparation for getting my hands on a pi. Problem is, the stuff on the pygame website looks awful. Slow and crappy. And that's on my athlon 2600 PC jobbie.

Now, the pi has a moderately crappy CPU, but a sweet GPU - IMHO, to produce something exciting, I'm going to have to dip into a bit of opengl - which seems to be fairly easy on a desktop PC, with stuff like pyopengl, pyglet, other stuff.

Not so easy to find opengl ES solutions, however - googling about, I can find one bloke who got something running on a nokia n900, somebody who's managed to get something running using maemo, there's something that pandora users can do with opengl es 1.1... But there doesn't seem to be any standard, well-known equivalent of pygl for ARM-based thingamabobs like the Raspberry Pi.

Am I wrong? Am I just using the wrong search terms? Anyone know anything about this?

TL:DR - Is there be some sort of equivalent of pyopengl usable on the Raspberry Pi?

PS. PyQt looks nice, but I just can't get my head around OOP - it just removes the whole point of python being nice and easy...
note: I may or may not know what I'm talking about...
Posts: 1143
Joined: Thu Aug 18, 2011 9:21 pm
by toxibunny » Tue Jan 24, 2012 4:46 pm
PPS. I must admit, I didn't really look at all that many examples from the pygame site - it might have been that I just happened to pick a few of the crappier ones first, and got disheartened. Nevertheless, full-HD resolution action games on the pi are going to run like cold tar unless there's at least a bit of hardware graphics-acceleration involved...
note: I may or may not know what I'm talking about...
Posts: 1143
Joined: Thu Aug 18, 2011 9:21 pm
by ArborealSeer » Tue Jan 24, 2012 4:53 pm

toxibunny said:

Nevertheless, full-HD resolution action games on the pi are going to run like cold tar unless there's at least a bit of hardware graphics-acceleration involved...


If performance is an issue, which it certainly will be with the CPU at some point, the non-GPU stuff would surely be better off in C than an interpreted language..

You can't moan about performance when you're coating yourself in that tar..
Pi Status > Farnell, Arrived 24/5- RS, Arrived 1/6
User avatar
Posts: 292
Joined: Tue Jan 24, 2012 9:48 am
by toxibunny » Tue Jan 24, 2012 5:15 pm
Well, we can deal with that later. Right now, I'm interested in having a plan b available for when people following Pygame tutorials try to go fullscreen and find out the effects of having to software blit 60-odd million pixels per second...
note: I may or may not know what I'm talking about...
Posts: 1143
Joined: Thu Aug 18, 2011 9:21 pm
by ArborealSeer » Tue Jan 24, 2012 5:20 pm
I've checked their site, they'll be fine as their backend is C. They'll be accelerated just fine providing OpenGL is available, if not they fall back to the next best thing ™

Here's the fineprint from their site.

Does not require OpenGL.  With many people having broken OpenGL setups, requiring OpenGL exclusively will cut into your user base significantly.  Pygame uses either opengl, directx, windib, X11, linux frame buffer, and many other different backends… including an ASCII art backend!  OpenGL is often broken on linux systems, and also on windows systems – which is why professional games use multiple backends.

Uses optimized C, and Assembly code for core functions.  C code is often 10-20 times faster than python code, and assembly code can easily be 100x or more times faster than python code.

(I'd dispute their claim about C vs ASM performance though)
Pi Status > Farnell, Arrived 24/5- RS, Arrived 1/6
User avatar
Posts: 292
Joined: Tue Jan 24, 2012 9:48 am
by jamesh » Tue Jan 24, 2012 5:25 pm
Agree with the last line - a decent C compiler should be able to get within a couple of percent of hand written assembler, and that's from a programmer who really knows his assembler.
Soon to be employed engineer - Hurrah! Volunteer at the Raspberry Pi Foundation, helper at PiAcademy September 2014.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 11923
Joined: Sat Jul 30, 2011 7:41 pm
by toxibunny » Tue Jan 24, 2012 6:28 pm
Ah, I think we're getting slightly off-track here.  I wasn't asking whether pygame would run, I was asking whether there is any sort of equivalent to pyopengl for things like the raspberry pi, which doesn't have opengl proper, but opengl es...

Someone's asked a similar question regarding pygame and opengl es here:  http://archives.seul.org/pygame/users/Oct-2011/msg00088.html

We've seen the Qt demos, so I figure PyQt would be fairly easy to get working, the problem is that programming Qt itself seems like a bit of a hassle...
note: I may or may not know what I'm talking about...
Posts: 1143
Joined: Thu Aug 18, 2011 9:21 pm
by ArborealSeer » Tue Jan 24, 2012 7:19 pm
Gotcha.. I've not been properly distinguishing between ES and Mainstream GL.

I'll be interested though, as although I plan to do my OpenGL ES work in C I may end up looking at and using samples made in python.

I found this http://pyopenorgl.sourceforge......ation.html


OpenGL-ES is on the "todo" list for PyOpenGL's developers



I also found something that might be more useful here http://www.meegoexperts.com/20.....-and-n900/

which seems to create some kind of import header for python if I understand it correctly?
Pi Status > Farnell, Arrived 24/5- RS, Arrived 1/6
User avatar
Posts: 292
Joined: Tue Jan 24, 2012 9:48 am
by jamesh » Tue Jan 24, 2012 8:19 pm
As I understand it there isn't a whole load of difference from GL to GL ES. It didn't take long to convert Quake from GL to GL ES for example. It's just a matter of knowing what to do. Which unfortunately I don't. Might be able to find out eventually though.
Soon to be employed engineer - Hurrah! Volunteer at the Raspberry Pi Foundation, helper at PiAcademy September 2014.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 11923
Joined: Sat Jul 30, 2011 7:41 pm
by ArborealSeer » Tue Jan 24, 2012 8:38 pm
Not seen the differences for myself yet..but from what i've read the begin/end calls are obseleted and there are some differences due to a lack of floating point stuff which i think is mainly in the shader stuff.
Pi Status > Farnell, Arrived 24/5- RS, Arrived 1/6
User avatar
Posts: 292
Joined: Tue Jan 24, 2012 9:48 am
by spock » Sat Mar 31, 2012 2:16 pm
https://bitbucket.org/pygame/pygame/wiki/gsoc2012ideas

there are summer of code suggestions for making pyopengl opengl es 2.0 compatible. the raspberry pi is mentioned too. :) hopefully someone will do this project.

and maybe next year raspbeery pi could take part in gsoc itself with some interesting projects?
Posts: 50
Joined: Sun Oct 02, 2011 10:33 am
by jamesh » Sat Mar 31, 2012 3:33 pm
I've been trying to get pyglesv2 working - mostly there. Unfortunately  the original impmentation was done on  Windows and I don't think ever tested on Linux so there are some issues.

Cannot decide whether to make Xlib calls directly or use the Xlib Python library. Probably the latter to be consistent.
Soon to be employed engineer - Hurrah! Volunteer at the Raspberry Pi Foundation, helper at PiAcademy September 2014.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 11923
Joined: Sat Jul 30, 2011 7:41 pm
by RobinWoodo » Tue May 15, 2012 5:30 pm
Any news about this?
Posts: 7
Joined: Sat Mar 03, 2012 1:52 pm
by jamesh » Tue May 15, 2012 5:50 pm
Sorry, I've had no time to look at this.
Soon to be employed engineer - Hurrah! Volunteer at the Raspberry Pi Foundation, helper at PiAcademy September 2014.
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 11923
Joined: Sat Jul 30, 2011 7:41 pm
by cheery » Tue May 15, 2012 6:24 pm
There's that hello_pi example in https://github.com/raspberrypi/firmware ... opt/vc/src

I'd propose you give this time if you don't understand opengl yet. The linux side of RPi graphics is still changing. While giving it time you could study opengl examples and read opengl ES spec and references from opengl.org

Once I get the platform, I think I'll make opengl ES bindings if there aren't already ones.
User avatar
Posts: 219
Joined: Wed Jan 25, 2012 9:39 pm
by dom » Tue May 15, 2012 7:10 pm
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 4042
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by tipam » Thu Jun 07, 2012 9:43 pm


I've got to say that this code has great potential - I'm hoping to explore it a little further and create a Python class 'pi3d' that provides simple 3D functions for kids to understand(!) such as ...

#initialize display
pi3d.setupDisplay(x,y,width,height)

#Create objects
myplanet = pi3d.mesh.sphere(radius, segments, "earth_texture.jpg", colour)
myship = pi3d.mesh.load("ship.p3d")

#initial position
myship.translate(0.0,0.0,20.0)

while 1:
. #Draw meshes ...
. myship.draw()
. myplanet.draw()

. #whizz ship around planet ...
. myship.translate(0.0,0.0,20.0)
. myship.rotate(0.0, 1.0, 0.0, 0.01)

. #pause
. time.sleep(0.01)

(please don't read much into this 'code' - it's just to kick off an idea!) ;)
If anyone is willing to work on this, please let me know.
Last edited by tipam on Thu Jun 07, 2012 10:10 pm, edited 1 time in total.
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by jmacey » Thu Jun 07, 2012 10:05 pm
I do have a boost python wrapper for the desktop version of my OpenGL library http://nccastaff.bournemouth.ac.uk/jmacey/GraphicsLib/index.htmlI'm in the process of porting it to the pi at the moment using C++ but will at some stage build the python version as well. All it would then need is some python wrapping for EGL to create the windows (which should also be fairly easy) this is still a little way off tho as the c++ port is taking some time. You can see it in action here http://m.youtube.com/watch?v=Jr1XL11ogJ ... b=comments
Posts: 135
Joined: Thu May 31, 2012 1:05 pm
by tipam » Fri Jun 08, 2012 1:00 am
jmacey wrote:I do have a boost python wrapper for the desktop version of my OpenGL library http://nccastaff.bournemouth.ac.uk/jmacey/GraphicsLib/index.htmlI'm in the process of porting it to the pi at the moment using C++ but will at some stage build the python version as well. All it would then need is some python wrapping for EGL to create the windows (which should also be fairly easy) this is still a little way off tho as the c++ port is taking some time. You can see it in action here http://m.youtube.com/watch?v=Jr1XL11ogJ ... b=comments


Looks great! - I'm amazed at what this little pi can do
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by Gibble » Fri Jun 08, 2012 9:24 am
I've been having a fiddle with Python and OpenGLES 2.0 from the code linked above (I know neither). I've managed to get basic stuff like a cube displaying with a basic texture, and perspective, rotation, translation matrices working. I'm sure there's lots of bugs in my stuff though, as I'm not confident in what I'm doing. Half the fun though!

I did get stuck trying to get numpy arrays working for vertices when passing to OpenGLES if anyone gets that far, I'd be interested (I think somewhere its going astray with floats vs eglfloats, or array alignments etc, it draws but coordinates are out).

If anyone else is fluffing about in the same area, I'm happy to try and help out or contribute etc, although my knowledge is a bit limited.
Posts: 56
Joined: Wed May 09, 2012 9:52 am
by tipam » Sat Jun 09, 2012 12:34 am
Made some code for the 'pi3d' module to demonstrate simple 3D programming. It includes all the support files provided by Peter de Rivaz so it should just up and run in Python.

You can get it from http://www.github.com/tipam/pi3d
boxtest.py demonstrates the simple programming bit and implements the pi3d module.

I need to write a specification for pi3d to make 3D programming as easy-peasy as possible ... yet make it a lot of fun and productive.
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Sat Jun 09, 2012 12:39 am
Gibble wrote:I've been having a fiddle with Python and OpenGLES 2.0 from the code linked above (I know neither). I've managed to get basic stuff like a cube displaying with a basic texture, and perspective, rotation, translation matrices working. I'm sure there's lots of bugs in my stuff though, as I'm not confident in what I'm doing. Half the fun though!

I did get stuck trying to get numpy arrays working for vertices when passing to OpenGLES if anyone gets that far, I'd be interested (I think somewhere its going astray with floats vs eglfloats, or array alignments etc, it draws but coordinates are out).

If anyone else is fluffing about in the same area, I'm happy to try and help out or contribute etc, although my knowledge is a bit limited.


Sounds like you've made a good start ... even the NeHe guy who wrote tutorials started out like that! It would be great to see some of your code for the pi. I'm certainly no expert!
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by RichardUK » Sat Jun 09, 2012 9:23 am
Don't knock the CPU. Arm chips take a little more care to make them fly but with some little work they really can fly. Remember, they have a lot of registers but not much cache so try not to do multiple passes on data, when you have the data do all the work on it before moving on.

Classic example is a game that will first update the anims for objects, then update sounds, then update physics, then transform to view space, then view reject and finally render.

Instead, do for the all these steps on one object before moving on to the next.

Ok this is a simplified explanation but I think it get's my point across. Also more than the x86 platform Arm can really benefit from some custom asm because of how every asm instruction can be conditionally executed.

Another old school trick is to use fixed point maths, even though there is hardware floating point fixed point with it's little short cuts can be a lot faster.

This does mean this system is a great learning tool as to get the best out of it you need to learn the chip even if you using %100 c/c++ knowing the chip helps you to stop making slow code. I've have worked on a very successful application that although was the market leader could have been so much faster if it have not been almost solely developed on a PC and only ported to hardware at the last minute.

And one final note, the best optimisation is to change your algorithm, only optimise the algorithm when you are sure it is the best way. And only optimise when the app is finished.
User avatar
Posts: 131
Joined: Fri Jun 01, 2012 5:12 pm
by Gibble » Wed Jun 13, 2012 8:01 am
tipam wrote:Made some code for the 'pi3d' module to demonstrate simple 3D programming. It includes all the support files provided by Peter de Rivaz so it should just up and run in Python.

You can get it from http://www.github.com/tipam/pi3d
boxtest.py demonstrates the simple programming bit and implements the pi3d module.

I need to write a specification for pi3d to make 3D programming as easy-peasy as possible ... yet make it a lot of fun and productive.


Hiya, just wanted to say I've had a quick play with this, and works great. Nice to get something up and looking good without problems, and makes sense for someone like me. There's quite a lot more bits than I had figured out myself, so I suspect I may end up playing more with your code than my own mess :). Good for me to learn it though!

Thanks for the work you've been doing :). If I come up with anything useful I'll try and feed it back.
Posts: 56
Joined: Wed May 09, 2012 9:52 am