forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglproxy.py
More file actions
97 lines (79 loc) · 2.63 KB
/
Copy pathglproxy.py
File metadata and controls
97 lines (79 loc) · 2.63 KB
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
'''Mikes wrapper for the visualizer???'''
from contextlib import contextmanager
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import OpenGL
__all__ = '''
gl
glu
glut
'''.strip().split()
class ModuleProxy(object):
def __init__(self, name, module):
self.name = name
self.module = module
def __getattr__(self, name):
if name.isupper():
return getattr(self.module, self.name.upper() + '_' + name)
else:
# convert to camel case
name = name.split('_')
name = [x[0].upper() + x[1:] for x in name]
name = ''.join(name)
return getattr(self.module, self.name + name)
class GLProxy(ModuleProxy):
@contextmanager
def matrix(self):
self.module.glPushMatrix()
try:
yield
finally:
self.module.glPopMatrix()
@contextmanager
def attrib(self, *args):
mask = 0
for arg in args:
if isinstance(arg, basestring):
arg = getattr(self.module, 'GL_%s_BIT' % arg.upper())
mask |= arg
self.module.glPushAttrib(mask)
try:
yield
finally:
self.module.glPopAttrib()
def enable(self, *args, **kwargs):
self._enable(True, args, kwargs)
return self._apply_on_exit(self._enable, False, args, kwargs)
def disable(self, *args, **kwargs):
self._enable(False, args, kwargs)
return self._apply_on_exit(self._enable, True, args, kwargs)
def _enable(self, enable, args, kwargs):
todo = []
for arg in args:
if isinstance(arg, basestring):
arg = getattr(self.module, 'GL_%s' % arg.upper())
todo.append((arg, enable))
for key, value in kwargs.iteritems():
flag = getattr(self.module, 'GL_%s' % key.upper())
value = value if enable else not value
todo.append((flag, value))
for flag, value in todo:
if value:
self.module.glEnable(flag)
else:
self.module.glDisable(flag)
def begin(self, arg):
if isinstance(arg, basestring):
arg = getattr(self.module, 'GL_%s' % arg.upper())
self.module.glBegin(arg)
return self._apply_on_exit(self.module.glEnd)
@contextmanager
def _apply_on_exit(self, func, *args, **kwargs):
try:
yield
finally:
func(*args, **kwargs)
gl = GLProxy('gl', OpenGL.GL)
glu = ModuleProxy('glu', OpenGL.GLU)
glut = ModuleProxy('glut', OpenGL.GLUT)