Re: [PyOpenGL-Users] Using glDrawElements on C-allocated data
Brought to you by:
mcfletch
From: Mads I. <mp...@co...> - 2009-06-02 08:25:08
|
Gordon L. Kindlmann wrote: > Hello, > > I've recently started using PyOpenGL as the framework for re-writing C/ > C++ OpenGL wrappers around an large amount of research code in C. Its > been a pleasure to have something that allows such literal translation > from the successful C/C++ OpenGL code to Python code; thanks for > creating this! > > The main hitch I've had is in using glDrawElements. I've looked > around for answers to this, and its clear that people people using > PyOpenGL are using it with geometry that is created on the python > side. In my case, all the geometry information is created on the C > side, and I want to control its display from Python. > > The line that's causing problems is: > > glDrawElements(glpt[lpld.type[primIdx]], vertCnt, GL_UNSIGNED_INT, > lpld.indx + vertIdx) > > where lpld.indx is a ctypes-created pointer to a buffer of unsigned > ints allocated and filled in C, which causes this problem: > > Traceback (most recent call last): > File "pgltDemo.py", line 118, in display > pgltDraw(pgltObject, uva) > File "pgltDemo.py", line 76, in pgltDraw > GL_UNSIGNED_INT, lpld.indx + vertIdx) > TypeError: unsupported operand type(s) for +: 'LP_c_ulong' and 'int' > > because apparently pointer arithmetic games aren't quite so simply in > Python. If you just replace "lpld.indx + vertIdx" with "lpld.indx", > which removes the pointer arithmetic stuff, it segfaults. I know that > others have looked at this issue from the context of having array data > that's created via numpy: > > http://www.mail-archive.com/pyg...@go.../msg01356/t.py > > I've tried many different iterations of things like this, and I get > either python errors or segfaults. > > Getting this to work with PyOpenGL would make a huge difference in how > I can get research work done with python, so I've taken some type to > put a self-contained example online: > > http://people.cs.uchicago.edu/~glk/pglt/ > > This includes a C library the generates some geometry to render, and a > ctypeslib-generated wrapper around the library. pgltDemo.c contains a > "pgltDraw()" function that shows how I'm currently doing things > without (for display lists) and with vertex arrays. pgltDemo.py > contains the same function, but the vertex array code is broken. > > Hopefully someone on this list will be able to spend a little time > working with these examples, and figure out how to get my > "pgltDemo.py" to work with vertex arrays. > > Thanks very much, > Gordon > > > ------------------------------------------------------------------------------ > Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT > is a gathering of tech-side developers & brand creativity professionals. Meet > the minds behind Google Creative Lab, Visual Complexity, Processing, & > iPhoneDevCamp as they present alongside digital heavyweights like Barbarian > Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com > _______________________________________________ > PyOpenGL Homepage > http://pyopengl.sourceforge.net > _______________________________________________ > PyOpenGL-Users mailing list > PyO...@li... > https://lists.sourceforge.net/lists/listinfo/pyopengl-users > > Using ctypes, I wrote the following interface to glMultiDrawElements: # Interface to glMultiDrawElements def multiDrawElements(mode, count, gl_type, index): """ """ # Set up interface for the call n_count = count.shape[0] libGL.glMultiDrawElements.restype = ctypes.c_void_p libGL.glMultiDrawElements.argtypes = [ctypes.c_int32, ctypes.POINTER(ctypes.c_int32), ctypes.c_int32, ctypes.POINTER(ctypes.c_int32)*n_count, ctypes.c_int32, ] # Set pointer to data in the count array count_ptr = count.ctypes.data_as(ctypes.POINTER(ctypes.c_int32)) # Set array of pointers to the index array iptr = ctypes.POINTER(ctypes.c_int32) index_ptr = (iptr*len(index))(*[row.ctypes.data_as(iptr) for row in index]) # Do the actual call to glMultiDrawElements libGL.glMultiDrawElements(int(mode), count_ptr, int(gl_type), index_ptr, n_count) Then, using vertex arrays, I call glMultiDrawElements as follows: multiDrawElements(GL_QUAD_STRIP, count, GL_UNSIGNED_INT, index) You should be able to boil this down to a working example for glDrawElements(), whose arguments are a subset of the arguments to glMultiDrawElements. Best regards, Mads |