Newer
Older
"""
Debug
=====
Debug device for testing
"""
VERBOSE = False
def print_debug(*args):
if VERBOSE:
print("Debug: {0}".format(*args))
@property
def texture(self):
return self._fbo._gl_data.color_tex
def isConnected(self):
"""
Check if device is connected
:return: return True if the device is connected
:rtype: bool
"""
return True
def init(self):
"""
Initialize device
:return: return True if the device was properly initialized
:rtype: bool
"""
return True
def loop(self):
"""
Get fresh tracking data
"""
def frameReady(self):
"""
The frame is ready to be send to the device
"""
def quit(self):
"""
Garbage collection
"""
from bgl import *
global _time
_time = 0
# ##################
# Data struct
# ##################
class GLdata:
def __init__(self):
self.color_tex = -1
self.fb = -1
self.rb = -1
# ##################
# OpenGL generic routines
# ##################
def view_setup():
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_TEXTURE)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glOrtho(-1, 1, -1, 1, -20, 20)
gluLookAt(0.0, 0.0, 1.0, 0.0,0.0,0.0, 0.0,1.0,0.0)
# Get texture info
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_TEXTURE)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
# ##################
# FBO related routines
# ##################
class FBO:
__slots__ = {
"_gl_data",
}
# initialize opengl data
self._gl_data = GLdata()
# initialize fbo
gl_data.width = width
gl_data.height = height
id_buf = Buffer(GL_INT, 1)
act_fbo = Buffer(GL_INT, 1)
glGetIntegerv(GL_FRAMEBUFFER, act_fbo)
act_tex = Buffer(GL_INT, 1)
glGetIntegerv(GL_ACTIVE_TEXTURE, act_tex)
#RGBA8 2D texture, 24 bit depth texture, width x height
glGenTextures(1, id_buf)
gl_data.color_tex = id_buf.to_list()[0]
glBindTexture(GL_TEXTURE_2D, gl_data.color_tex)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# NULL means reserve texture memory, but texels are undefined
null_buffer = Buffer(GL_BYTE, [(gl_data.width + 1) * (gl_data.height + 1) * 4])
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, gl_data.width, gl_data.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, null_buffer)
glBindTexture(GL_TEXTURE_2D, act_tex[0])
glGenFramebuffers(1, id_buf)
gl_data.fb = id_buf.to_list()[0]
glBindFramebuffer(GL_FRAMEBUFFER, gl_data.fb)
# Attach 2D texture to this FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gl_data.color_tex, 0)
glGenRenderbuffers(1, id_buf)
gl_data.depth_rb = id_buf.to_list()[0]
glBindRenderbuffer(GL_RENDERBUFFER, gl_data.depth_rb)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, gl_data.width, gl_data.height)
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
# Attach depth buffer to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gl_data.depth_rb)
# Does the GPU support current FBO configuration?
status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
glBindFramebuffer(GL_FRAMEBUFFER, act_fbo[0])
if status == GL_FRAMEBUFFER_COMPLETE:
print("FBO: good: {0} : {1} : {2}".format(gl_data.color_tex, gl_data.depth_rb, gl_data.fb))
else:
print("FBO: error", status)
def run(self):
"""
draw in the FBO
"""
gl_data = self._gl_data
act_fbo = Buffer(GL_INT, 1)
glGetIntegerv(GL_FRAMEBUFFER, act_fbo)
# setup
viewport = Buffer(GL_INT, 4)
glGetIntegerv(GL_VIEWPORT, viewport)
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
glBindFramebuffer(GL_FRAMEBUFFER, gl_data.fb)
glActiveTexture(GL_TEXTURE0)
glClearColor(1.0, 1.0, 1.0, 1.0)
glClearDepth(1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
act_tex = Buffer(GL_INT, 1)
glGetIntegerv(GL_ACTIVE_TEXTURE, act_tex)
glDisable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
# actual drawing
view_setup()
glEnable(GL_TEXTURE_2D)
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, gl_data.color_tex)
# actual drawing
self._draw_a_quad()
glBindTexture(GL_TEXTURE_2D, act_tex[0])
glDisable(GL_TEXTURE_2D)
glDisable(GL_DEPTH_TEST)
view_reset()
glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# unbinding
glBindFramebuffer(GL_FRAMEBUFFER, act_fbo[0])
glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
def _draw_a_quad(self):
"""
draw an animated quad on the screen
"""
import time
import math
global _time
speed = 0.01
one = 1.0
zer = 0.0
_time, _int = math.modf(_time + speed)
factor = _time * 2.0
if factor > 1.0:
factor = 2.0 - factor;
one = one - factor;
zer = factor - zer;
glClearColor(1.0, 1.0, 1.0, 1.0)
glClearDepth(1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, 20.0)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
current_color = Buffer(GL_FLOAT, 4)
glGetFloatv(GL_CURRENT_COLOR, current_color);
glEnable(GL_COLOR_MATERIAL)
glBegin(GL_QUADS)
glColor3f(one, zer, zer)
glVertex3f(-0.75, -0.75, 0.0)
glColor3f(zer, one, zer)
glVertex3f( 0.75, -0.75, 0.0)
glColor3f(zer, zer, one)
glVertex3f( 0.75, 0.75, 0.0)
glColor3f(one, one, zer)
glVertex3f(-0.75, 0.75, 0.0)
glEnd()
glColor4fv(current_color)
glDisable(GL_COLOR_MATERIAL)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
def _debug_quad(self):
viewport = Buffer(GL_INT, 4)
glGetIntegerv(GL_VIEWPORT, viewport)
glViewport(300, 200, 256, 256)
glScissor(300, 200, 256, 256)
# actual drawing
self._draw_a_quad()
glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
glScissor(viewport[0], viewport[1], viewport[2], viewport[3])
def delete(self):
"""
cleanup FBO data
"""
gl_data = self._gl_data
id_buf = Buffer(GL_INT, 1)
id_buf.to_list()[0] = gl_data.color_tex
glDeleteTextures(1, id_buf)
id_buf.to_list()[0] = gl_data.depth_rb
glDeleteRenderbuffers(1, id_buf)
id_buf.to_list()[0] = gl_data.fb
glDeleteFramebuffers(1, id_buf)
def __del__(self):
self.delete()