Skip to content
debug.py 3.71 KiB
Newer Older
Dalai Felinto's avatar
Dalai Felinto committed
"""
Debug
=====

Debug device for testing
"""

from . import HMD_Base, HMD_Data
Dalai Felinto's avatar
Dalai Felinto committed

Dalai Felinto's avatar
Dalai Felinto committed
VERBOSE = False

def print_debug(*args):
    if VERBOSE:
        print("Debug: {0}".format(*args))


class Debug(HMD_Base):
Dalai Felinto's avatar
Dalai Felinto committed
    def __init__(self):
        super(Debug, self).__init__('Debug')
Dalai Felinto's avatar
Dalai Felinto committed

    def isConnected(self):
        """
        Check if device is connected

        :return: return True if the device is connected
        :rtype: bool
        """
Dalai Felinto's avatar
Dalai Felinto committed
        print_debug('isConnected()')
Dalai Felinto's avatar
Dalai Felinto committed
        return True

    def init(self):
        """
        Initialize device

        :return: return True if the device was properly initialized
        :rtype: bool
        """
Dalai Felinto's avatar
Dalai Felinto committed
        print_debug('init()')

Dalai Felinto's avatar
Dalai Felinto committed
        self._width = 1024
Dalai Felinto's avatar
Dalai Felinto committed
        self._height = 512

        return super(Debug, self).init()
Dalai Felinto's avatar
Dalai Felinto committed

    def loop(self):
        """
        Get fresh tracking data
        """
Dalai Felinto's avatar
Dalai Felinto committed
        print_debug('loop()')
        debug_draw(self._offscreen_object, self._width, self._height)
Dalai Felinto's avatar
Dalai Felinto committed

    def frameReady(self):
        """
        The frame is ready to be send to the device
        """
Dalai Felinto's avatar
Dalai Felinto committed
        print_debug('frameReady()')
Dalai Felinto's avatar
Dalai Felinto committed

    def quit(self):
        """
        Garbage collection
        """
Dalai Felinto's avatar
Dalai Felinto committed
        print_debug('quit()')
        return super(Debug, self).quit()


# ##################
# Debug Debug Debug
# ##################
Dalai Felinto's avatar
Dalai Felinto committed

from bgl import *


global _time
_time = 0


# ##################
# 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()


# ##################
# Draw an animated cube on the offscreen object
# ##################

def debug_draw(offscreen_object, width, height):
    """
    draw in the FBO
    """
    import time
    import math
    import gpu
    # setup
    viewport = Buffer(GL_INT, 4)
    glGetIntegerv(GL_VIEWPORT, viewport)
    glViewport(0, 0, width, height)
    gpu.offscreen_object_bind(offscreen_object, True)
    glClearColor(1.0, 1.0, 1.0, 1.0)
    glClearDepth(1.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glDisable(GL_DEPTH_TEST)
    glDepthFunc(GL_LESS)
    # actual drawing
    speed = 0.01
    _time, _int = math.modf(_time + speed)
    factor = _time * 2.0
    if factor > 1.0:
        factor = 2.0 - factor;
    one = one - factor;
    zer = factor - zer;
    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()
    glDisable(GL_DEPTH_TEST)
    view_reset()
    glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
    # unbinding
    gpu.offscreen_object_unbind(offscreen_object, True)