GUI development with 3D view

sturlamolden sturlamolden at yahoo.no
Mon Dec 10 18:57:53 EST 2007


On 10 Des, 13:33, Achim Domma <do... at procoders.net> wrote:

> I'm looking for quite some time now for a gui library for python,
> which allows me to display 3D graphics. Main OS is windows, Mac OS X
> and Linux would be nice to have. I want to use python 2.5. My first
> try was wx + pyOpenGL but there are no working binaries for python
> 2.5.

Huh? The latest version of PyOpenGL uses ctypes and has no binaries as
it is implemented in pure Python. Personally I use my own ctypes
wrapper for the few OpenGL routines I need. There is a lot of unwanted
overhead in PyOpenGL.

Also consider calling OpenGL from a Pyrex extension. That may save you
some overhead if you make a lot of calls to the OpenGL library. A
remedy for that may be to use display lists, or combine vertex arrays
or vertex buffers with NumPy arrays.


Excerpts from my OpenGL wrapper:

from ctypes import *
import numpy
from threading import Lock
opengl_lock = Lock()
GL = windll.opengl32
GLU = windll.glu32

GL_PROJECTION =                 0x1701
GL_MODELVIEW =                  0x1700
GL_DEPTH_TEST =                 0x0B71
GL_VERTEX_ARRAY =               0x8074
GL_NORMAL_ARRAY =               0x8075

glBegin = GL.glBegin
glBegin.argtypes = (c_int,)
glBegin.restype = None

glEnd = GL.glEnd
glEnd.argtypes = ()
glEnd.restype = None

glVertex3f = GL.glVertex3f
glVertex3f.argtypes = (c_float, c_float, c_float)
glVertex3f.restype = None

gltypemap = {
    GL_BYTE : numpy.ctypeslib.ndpointer(dtype = numpy.int8,
flags='aligned,contiguous'),
    GL_UNSIGNED_BYTE : numpy.ctypeslib.ndpointer(dtype = numpy.uint8,
flags='aligned,contiguous'),
    GL_SHORT : numpy.ctypeslib.ndpointer(dtype = numpy.int16,
flags='aligned,contiguous'),
    GL_UNSIGNED_SHORT : numpy.ctypeslib.ndpointer(dtype =
numpy.uint16, flags='aligned,contiguous'),
    GL_INT : numpy.ctypeslib.ndpointer(dtype = numpy.int32,
flags='aligned,contiguous'),
    GL_UNSIGNED_INT : numpy.ctypeslib.ndpointer(dtype = numpy.uint32,
flags='aligned,contiguous'),
    GL_FLOAT : numpy.ctypeslib.ndpointer(dtype = numpy.float32,
flags='aligned,contiguous'),
    GL_DOUBLE : numpy.ctypeslib.ndpointer(dtype = numpy.float64,
flags='aligned,contiguous'),
    GL_UNSIGNED_BYTE_3_3_2 : numpy.ctypeslib.ndpointer(dtype =
numpy.uint8, flags='aligned,contiguous')
}

def glVertexPointer(size, gltype, stride, array):
    opengl_lock.acquire()
    glfun = GL.glVertexPointer
    glfun.argtypes = (c_int, c_int, c_int, gltypemap[gltype])
    glfun.restype = None
    glfun(size, gltype, stride, array)
    opengl_lock.release()

def glGenBuffersARB(n):
    opengl_lock.acquire()
    PFNGLGENBUFFERSARBPROC = WINFUNCTYPE(None, c_uint,
gltypemap[GL_UNSIGNED_INT])
    wglGetProcAddress = GL.wglGetProcAddress
    wglGetProcAddress.argtypes = (c_char_p,)
    wglGetProcAddress.restype = PFNGLGENBUFFERSARBPROC
    glfun = wglGetProcAddress('glGenBuffersARB')
    buffers = numpy.zeros(n, dtype=numpy.uint32)
    glfun(n, buffers)
    opengl_lock.release()
    return buffers


Interfacing Python with OpenGL is not rocket science.










More information about the Python-list mailing list