Pygles

Would a python binding of GLES be useful?

  • Great idea, go for it!

    Votes: 12 70.6%
  • Some might find it useful

    Votes: 4 23.5%
  • Terrible idea

    Votes: 1 5.9%

  • Total voters
    17

chris_c

Member
Joined
Jun 25, 2010
Messages
393
Age
55
I've had a fair bit of experience with interfacing python directly with C libraries (such as Xlib for example in
X-tile a window tiler for none compiz systems)

I wonder if people would find a python binding for GLES useful?
I did a quick google but couldn't seem to find anything really useful ??? (maybe I missed something)

I'd be prepared to make a start on this once my pandora arrives but would it be useful for people?


edit: ages after this first post
http://dl.openhandhelds.org/cgi-bin/pandora.cgi?0,0,0,0,19,226
working module and framework...
 
Well, of course this is useful. What Python developer wouldn't want this? Although not a Python myself. I find the prospect exciting. :)
 
Well, PyOpenGL already exists, and I think I've heard rumours that they're planning to add ES support. Even if they're not, their code could be a good place for you to start.

I probably wouldn't use direct OGLES bindings myself because I am only an occasional programmer, and low-level stuff scares me. But I imagine many others would put them to good use.
 
YES PLEASE! This would be immensely helpful! I, like you, couldn't really find much on Python + OpenGL ES, if it is possible I highly doubt it's currently workable to the point of being practical. I'm not sure if working from PyOpenGL is worthwhile personally; it has a lot of error checking and debugging stuff that slows it down in order to be more Pythonic, but this isn't necessarily a good thing on the Pandora.
 
My take on it was to simply use ctypes to access the gles library and provide a few simple support routines and the defines needed.

For those of you not familiar with ctypes you basically get a reference to the library and then the functions inside the library
(usually a shared object (.so) similar to a windows dll) are accessed as if they are class members

glesRef.glMatrixMode(GL_PROJECTION)

glesRef could be called anything its the "variable" used to reference the library
glMatrixMode would not be part of the "binding" as ctypes looks up the name and calls the appropriate function
GL_PROJECTION would be one of a number of defines that the binding would have to provide

There would be no error checking short of what python or gles itself would do, however I've noticed on a number of platforms that
gl is very robust and can stand all sorts of abuse without actually crashing...

I care not one whit for being "pythonic" I just want the thinnest convenience that will let me access gles...

If someone with a Pandora wants to get in touch I can work up some test code using gles on gl, and then talk them through getting it working on the pandora
 
OK the hardest task is the "simple" job of opening a window with a gles context...

I'm just using a desktop at the moment while I wait for my pandora, but using the linux reference driver (sitting on top of opengl) I've made a test to open up a window and clear it to grey

now it looks complex but this will all be hidden from the end user as a single function
openWindow(width,height,name)

If someone would do me the favour of testing it I'd really appreciate it!
You'll need to change the library name for the correct gles lib on the pandora and I'll bet it won't work first time (but you never know!)

anyhow if you're feeling brave here it is...

* EDIT - code fixed and now works!
Code:
#!/usr/bin/env python
from ctypes import *
from time import *

width=320
height=200
name=c_char_p("pyGLES window")

#gles = CDLL("libGLES_CL.so") # <--- this will need changing this is the desktop software reference driver library
gles = CDLL("libGLES_CM.so") 

xlib = CDLL("libX11.so")


GL_DEPTH_BUFFER_BIT     =   0x00000100
GL_STENCIL_BUFFER_BIT   =   0x00000400
GL_COLOR_BUFFER_BIT     =   0x00004000

EGL_RED_SIZE            =   0x3024
EGL_DEPTH_SIZE          =   0x3025
EGL_NONE                =   0x3038
EGL_FALSE               =   0
EGL_TRUE                =   1
EGL_NATIVE_VISUAL_ID    =   0x302E

VisualIDMask		    =   0x1
AllocNone		        =   0	
PMinSize                =   8
PMaxSize                =   16

ExposureMask			=   1<<15 
StructureNotifyMask		=   1<<17 
KeyPressMask			=   1<<0  
ButtonPressMask			=   1<<2
ButtonReleaseMask		=   1<<3

InputOutput		        =   1

CWBorderPixel           =   1<<3
CWColormap		        =   1<<13
CWEventMask		        =   1<<11

class XVisualInfo(Structure):
    _fields_ = [
        ("visual", POINTER(c_long)), 
        ("visualid", c_long),
        ("screen", c_int),
        ("depth", c_uint),
        ("class", c_int),
        ("red_mask", c_ulong),
        ("green_mask", c_ulong),
        ("blue_mask", c_ulong),
        ("colormap_size", c_int),
        ("bits_per_rpg", c_int)
        ]

class XSetWindowAttributes(Structure):
    _fields_ = [
        ("background_pixmap", c_ulong), 
        ("background_pixel", c_ulong), 
        ("border_pixmap", c_ulong), 
        ("border_pixel", c_ulong), 
        ("bit_gravity", c_int), 
        ("win_gravity", c_int), 
        ("backing_store", c_int), 
        ("backing_planes", c_ulong), 
        ("backing_pixel", c_ulong), 
        ("save_under", c_int), 
        ("event_mask", c_long), 
        ("do_not_propagate_mask", c_long), 
        ("override_redirect", c_int), 
        ("colormap", c_ulong), 
        ("cursor", c_ulong)
        ]

class XSizeHints(Structure):
   _fields_ = [("flags",c_long),
               ("x",c_int),        # Obsolete
               ("y",c_int),        # Obsolete
               ("width",c_int),    # Obsolete
               ("height",c_int),   # Obsolete
               ("min_width",c_int),
               ("min_height",c_int),
               ("max_width",c_int),
               ("max_height",c_int),
               ("width_inc",c_int),
               ("height_inc",c_int),
               ("min_asp_width",c_int),
               ("min_asp_height",c_int),
               ("max_asp_width",c_int),
               ("max_asp_height",c_int),
               ("base_width",c_int),
               ("base_height",c_int),
               ("win_gravity",c_int)]





dpy = xlib.XOpenDisplay(0)
egldisplay = gles.eglGetDisplay(dpy);

gles.eglInitialize(egldisplay, 0, 0)

attributeList = (c_int * 5)(EGL_RED_SIZE, 1, EGL_DEPTH_SIZE, 1, EGL_NONE)

config = (c_int * 4)(0,0,0,0)
nconf = c_int(0)

r=gles.eglChooseConfig(egldisplay,attributeList,config,4,pointer(nconf))
if r!=EGL_TRUE:
    print "can't find requested config"
    exit()

cx = gles.eglCreateContext(egldisplay, config[0], 0, 0);

vid=c_int(0)
gles.eglGetConfigAttrib(egldisplay, config[0], EGL_NATIVE_VISUAL_ID, pointer(vid))

vi=XVisualInfo()
tmp=XVisualInfo()
tmp.visualid = vid

n=c_int(0)
vi = XVisualInfo(cast(xlib.XGetVisualInfo(dpy, VisualIDMask, pointer(tmp), pointer(n)),POINTER(c_long)))

swa=XSetWindowAttributes()

rw=xlib.XDefaultRootWindow(dpy)

swa.colormap = xlib.XCreateColormap(dpy, rw, vi.visual, AllocNone);

sizehints=XSizeHints()
sizehints.flags = 0;
sizehints.flags = PMinSize | PMaxSize;
sizehints.min_width = sizehints.max_width = width;
sizehints.min_height = sizehints.max_height = height;

swa.border_pixel = 0;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask;

window = xlib.XCreateWindow(dpy, rw, 0, 0, width, height,
                        0, vi.depth, InputOutput, vi.visual,
                        CWBorderPixel|CWColormap|CWEventMask, pointer(swa));

xlib.XSetStandardProperties(dpy, window, name, name, 0, 0, 0, pointer(sizehints));
xlib.XMapWindow(dpy, window)

eglwindow = gles.eglCreateWindowSurface(egldisplay, config[0], window, 0)
gles.eglMakeCurrent(egldisplay, eglwindow, eglwindow, cx)

gles.glClearColorx(32768, 32768, 32768, 0)
gles.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
gles.eglSwapBuffers(egldisplay, eglwindow)

sleep(1)
 
@chris_c- tried it nothing happens at all no errors in terminal. the Pandora module is called libGLES_CM.so and the user will need to install python-ctypes from the repo
 
mindlord said:
@chris_c- tried it nothing happens at all no errors in terminal. the Pandora module is called libGLES_CM.so and the user will need to install python-ctypes from the repo
well... at least it didn't crash! thanks for taking a look, I'll admit to being a little puzzled, do you not even get any lone window decorations appearing for a second?
 
Last edited by a moderator:
I'd just like to thank mindlord for testing and putting up with me sending him different test version so I could figure out what was wrong with the code!

It now works, however the software implementation of gles on my desktop is fixed point only which is a major PITA so further development by me will have to wait until my pandora actually arrives...

However anyone who's even half way competent at C and Python should be able to take the above and make a simple rotating box using the c_types techniques I've shown above
(I'd be happy to advise anyone who wants to run with it...)
 
Hi chris_c, I just wanted to let you know that I'm part-way through adding an 'export' option to objview.py that will export the loaded object as a C-array for use in Wakebreaker .. ;)

http://w1xer.at/pandora/

When I'm done with ironing out the wrinkles, I plan on doing a tutorial that will show new-comers how they can use this pygles tool and the Wakebreaker sources to build their own, new, C++-based 3D GLES application for the Pandora.
 
torpor said:
Hi chris_c, I just wanted to let you know that I'm part-way through adding an 'export' option to objview.py that will export the loaded object as a C-array for use in Wakebreaker .. ;)

http://w1xer.at/pandora/

When I'm done with ironing out the wrinkles, I plan on doing a tutorial that will show new-comers how they can use this pygles tool and the Wakebreaker sources to build their own, new, C++-based 3D GLES application for the Pandora.
cool its great to see people contributing to the developer community :D

take a look at my GLES C framework, something in it may be useful
 
Last edited by a moderator:
I already looked at the GLES C framework, I'll be checking it out some more tonight or tomorrow when I get more Pandora-hacking time .. in the meantime maybe you wanna check out Wakebreaker?!

;)
 
torpor said:
I already looked at the GLES C framework, I'll be checking it out some more tonight or tomorrow when I get more Pandora-hacking time .. in the meantime maybe you wanna check out Wakebreaker?!

;)
I mailed you x2 about it already ;) :p
 
Last edited by a moderator:
D'oh, really? I'm shitty at remembering such things .. will search my mailbox in case I owe you a reply. ;)
 
torpor said:
D'oh, really? I'm shitty at remembering such things .. will search my mailbox in case I owe you a reply. ;)
no no NEW mail today to the addy on your site (so check that one ;) )
:p
 
Last edited by a moderator:
Back
Top