1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
import sys
import OpenGL
"""
This module performs some nasty trickery in order to automatically
detect OpenGL errors, by wrapping each callable function with a
function which calls the function, but afterwards checks to see if an
error code was set, in which case it raises an exception
This is highly experimental. To disable it, call the fast()
function. To reenable it, call the careful() function.
"""
if OpenGL._numeric:
from Numeric import ArrayType
try:
import OpenGL.dynload._opengl_num
_opengl = OpenGL.dynload._opengl_num
except ImportError:
import OpenGL.dynload._opengl
_opengl = OpenGL.dynload._opengl
try:
import OpenGL.dynload.openglutil_num
openglutil = OpenGL.dynload.openglutil_num
except ImportError:
import OpenGL.dynload.openglutil
openglutil = OpenGL.dynload.openglutil
else:
import OpenGL.dynload._opengl
_opengl = OpenGL.dynload._opengl
import OpenGL.dynload.openglutil
openglutil = OpenGL.dynload.openglutil
origdict = _opengl.__dict__.copy()
origdict.update(openglutil.__dict__)
if sys.platform == 'win32':
from OpenGL.dynload.wgl import *
from OpenGL.dynload import wgl
from wglconst import *
from glconst import *
_safetylevel = 0
# Constants
import glconst
constant_names = {}
for k, v in glconst.__dict__.items():
if type(v) == type(1):
constant_names[v] = k
error = origdict['glGetError']
class Error:
def __init__(self, num):
self.num = num
self.str = constant_names.get(num, 'Unknown Error')
def __str__(self):
return '%s (%d)' % (self.str, self.num)
class CarefulFunction:
def __init__(self, name, function):
self.name = name
self.func = function
def __repr__(self):
return "<careful function wrapper around %s>" % self.function
def __call__(self, *args, **kw):
# did someone check in a debug (non-production) version of this module?
# this was _not_ done in the 1.1 release (causes huge gobs of printing
# when the demos are run).
## print "calling %s with %s, %s" % (self.func, args, kw)
retval = apply(self.func, args, kw)
err = error()
if err:
if _verysafe:
raise Error(err)
else:
print Error(err)
return retval
carefuldict = {}
for name, func in origdict.items():
if callable(func):
carefuldict[name] = CarefulFunction(name, func)
# These do the same sorts of things that the C versions would
def careful():
cd = carefuldict.copy()
if cd.has_key('error'): del cd['error']
sys.modules['OpenGL.GL'].__dict__.update(cd)
import string
def fast():
cd = origdict.copy()
if cd.has_key('error'): del cd['error']
if cd.has_key('glconst'): del cd['glconst']
sys.modules['OpenGL.GL'].__dict__.update(cd)
# by default, be careful
if _safetylevel >= 1:
careful()
else:
fast()
if _safetylevel >= 2:
_verysafe = 1
else:
_verysafe = 0
def glGetIntegerv(*args):
r = apply(glGetDoublev, args)
if isinstance(r, type(())):
return tuple(map(int, r))
elif OpenGL._numeric and isinstance(r, ArrayType):
return tuple(r.astype('i').tolist())
else:
return int(r)
glGetInteger = glGetIntegerv
glGetBooleanv = glGetBoolean = glGetFloatv = glGetFloat = glGetDouble
# for b/w compatibility w/ 1.0??
GL_LEVELS = 0x0010
__version__ = '1.5.4'
|