This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
statement.py
295 lines (232 loc) · 7.19 KB
/
statement.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'''
Statements: OpenGL statement for the "with" keyword
Save and restore the matrix ::
with gx_matrix:
glTranslatef(55, 34, 0)
# draw stuff
# here, the matrix will be restored as previous state
Modify a display list (compiled OpenGL operations) ::
dl = GlDisplayList()
with dl:
# draw stuff
# here you can call the display list
dl.draw()
Bind a texture and draw with OpenGL ::
with DO(gx_texture(my_texture), gx_begin(GL_TRIANGLE_FAN)):
# call multiple time glVertex2f()
Save and restore the color after drawing ::
set_color(1, 0, 0) # color is red.
with gx_color(0, 1, 0):
# here the color is green
# draw stuff
pass
# here the color is restored back to red
'''
__all__ = [
# class for with statement
'DO',
'GlDisplayList', 'GlBlending',
'GlMatrix', 'GlEnable', 'GlBegin',
'GlAttrib', 'GlColor', 'GlTexture',
# aliases
'gx_blending', 'gx_alphablending',
'gx_matrix', 'gx_matrix_identity',
'gx_enable', 'gx_begin',
'gx_attrib', 'gx_color',
'gx_texture', 'gx_blending_replace'
]
import pymt
from OpenGL.GL import *
gl_displaylist_generate = False
class GlDisplayList:
'''Abstraction to opengl display-list usage. Here is an example of usage
::
dl = GlDisplayList()
with dl:
# do draw function, like drawLabel etc...
dl.draw()
'''
def __init__(self):
self.dl = glGenLists(1)
self.compiled = False
self.do_compile = True
def __enter__(self):
self.start()
def __exit__(self, type, value, traceback):
self.stop()
def start(self):
'''Start recording GL operation'''
global gl_displaylist_generate
if gl_displaylist_generate:
self.do_compile = False
else:
gl_displaylist_generate = True
self.do_compile = True
glNewList(self.dl, GL_COMPILE)
def stop(self):
'''Stop recording GL operation'''
global gl_displaylist_generate
if self.do_compile:
glEndList()
self.compiled = True
gl_displaylist_generate = False
def clear(self):
'''Clear compiled flag'''
self.compiled = False
def is_compiled(self):
'''Return compiled flag'''
return self.compiled
def draw(self):
'''Call the list only if it's compiled'''
if not self.compiled:
return
glCallList(self.dl)
class DO:
'''A way to do multiple action in with statement
::
with DO(stmt1, stmt2):
print 'something'
'''
def __init__(self, *args):
self.args = args
def __enter__(self):
for item in self.args:
item.__enter__()
def __exit__(self, type, value, traceback):
for item in reversed(self.args):
item.__exit__(type, value, traceback)
class GlBlending:
'''Abstraction to use blending ! Don't use directly this class.
We've got an alias you can use ::
with gx_blending:
# do draw function
'''
def __init__(self, sfactor=GL_SRC_ALPHA, dfactor=GL_ONE_MINUS_SRC_ALPHA):
self.sfactor = sfactor
self.dfactor = dfactor
def __enter__(self):
glEnable(GL_BLEND)
glBlendFunc(self.sfactor, self.dfactor)
def __exit__(self, type, value, traceback):
glDisable(GL_BLEND)
class GlMatrix:
'''Statement of glPushMatrix/glPopMatrix, designed to be use with "with" keyword.
Alias: gx_matrix, gx_matrix_identity ::
with gx_matrix:
# do draw function
with gx_matrix_identity:
# do draw function
'''
def __init__(self, matrixmode=GL_MODELVIEW, do_loadidentity=False):
self.do_loadidentity = do_loadidentity
self.matrixmode = matrixmode
def __enter__(self):
glMatrixMode(self.matrixmode)
glPushMatrix()
if self.do_loadidentity:
glLoadIdentity()
def __exit__(self, type, value, traceback):
glMatrixMode(self.matrixmode)
glPopMatrix()
class GlEnable:
'''Statement of glEnable/glDisable, designed to be use with "with" keyword.
Alias: gx_enable.
'''
def __init__(self, flag):
self.flag = flag
def __enter__(self):
glEnable(self.flag)
def __exit__(self, type, value, traceback):
glDisable(self.flag)
gx_enable = GlEnable
class GlBegin:
'''Statement of glBegin/glEnd, designed to be use with "with" keyword
Alias: gx_begin.
'''
def __init__(self, flag):
self.flag = flag
def __enter__(self):
glBegin(self.flag)
def __exit__(self, type, value, traceback):
glEnd()
class GlAttrib:
'''Statement of glPushAttrib/glPopAttrib, designed to be use with "with" keyword
Alias: gx_attrib.
'''
def __init__(self, flag):
self.flag = flag
def __enter__(self):
glPushAttrib(self.flag)
def __exit__(self, type, value, traceback):
glPopAttrib()
class GlColor:
'''Statement of glPushAttrib/glPopAttrib on COLOR BUFFER + color,
designed to be use with "with" keyword
Alias: gx_color.
'''
def __init__(self, r, g, b, a=None):
if a is None:
self.color = (r, g, b)
else:
self.color = (r, g, b, a)
def __enter__(self):
glPushAttrib(GL_COLOR_BUFFER_BIT)
if len(self.color) == 3:
glColor3f(*self.color)
else:
glColor4f(*self.color)
def __exit__(self, type, value, traceback):
glPopAttrib()
class GlTexture:
'''Statement of setting a texture
Alias: gx_texture.
'''
def __init__(self, texture):
self.texture = texture
def __enter__(self):
self.bind()
def __exit__(self, type, value, traceback):
self.release()
def bind(self):
target = self.get_target()
glPushAttrib(GL_ENABLE_BIT)
glEnable(target)
glBindTexture(target, self.get_id())
def release(self):
glPopAttrib()
def get_id(self):
'''Return the openid of texture'''
if isinstance(self.texture, pymt.TextureRegion):
return self.texture.owner.id
elif isinstance(self.texture, pymt.Texture):
return self.texture.id
else:
return self.texture
def get_target(self):
if isinstance(self.texture, pymt.TextureRegion):
return self.texture.owner.target
elif isinstance(self.texture, pymt.Texture):
return self.texture.target
else:
return GL_TEXTURE_2D
#
# Aliases
#
#: Alias to GlBlending(sfactor=GL_DST_COLOR, dfactor=GL_ONE_MINUS_SRC_ALPHA)
gx_alphablending = GlBlending(sfactor=GL_DST_COLOR, dfactor=GL_ONE_MINUS_SRC_ALPHA)
#: Repalce the content with the source content
gx_blending_replace = GlBlending(sfactor=GL_ONE, dfactor=GL_ZERO)
#: Alias to GlAttrib
gx_attrib = GlAttrib
#: Alias to GlBegin
gx_begin = GlBegin
#: Alias to GlBlending()
gx_blending = GlBlending()
#: Alias to GlColor
gx_color = GlColor
#: Alias to GlMatrix()
gx_matrix = GlMatrix()
#: Alias to GlMatrix(do_loadidentity=True)
gx_matrix_identity = GlMatrix(do_loadidentity=True)
#: Alias to GlTexture
gx_texture = GlTexture