Skip to content
oculus.py 2.91 KiB
Newer Older
Dalai Felinto's avatar
Dalai Felinto committed
"""
Oculus
======

Oculus (oculus.com) head mounted display
It uses a C app to connect with the SDK

The bridge code is hosted at Visgraf:
http://git.impa.br/dfelinto/oculus_sdk_bridge
"""

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

from ..lib import (
        checkModule,
        )

class Oculus(HMD_Base):
    def __init__(self, error_callback):
        super(Oculus, self).__init__('Oculus', error_callback)
        checkModule('oculus_sdk_bridge')
Dalai Felinto's avatar
Dalai Felinto committed

Dalai Felinto's avatar
Dalai Felinto committed
        # self._debug()
Dalai Felinto's avatar
Dalai Felinto committed

Dalai Felinto's avatar
Dalai Felinto committed
    def _debug(self):
        import bridge
        import bridge_wrapper
Dalai Felinto's avatar
Dalai Felinto committed

Dalai Felinto's avatar
Dalai Felinto committed
        input = 3
        device = bridge_wrapper.Debug_new(input)
        factor = bridge_wrapper.Debug_multiplicationFactor()
        print("Multiplication factor is {0}".format(factor))

        output = bridge_wrapper.Debug_multiplicationResult(device)
        print("Return of {0} is {1}".format(input, output))
Dalai Felinto's avatar
Dalai Felinto committed

Dalai Felinto's avatar
Dalai Felinto committed
    def init(self, context):
Dalai Felinto's avatar
Dalai Felinto committed
        """
        Initialize device

        :return: return True if the device was properly initialized
        :rtype: bool
        """
        try:
            from bridge.oculus import HMD
            self._hmd = HMD()
Dalai Felinto's avatar
Dalai Felinto committed

            # gather arguments from HMD
Dalai Felinto's avatar
Dalai Felinto committed

            near, far = self._getCameraClipping(context)

            self.setEye(0)
            self.width = self._hmd.width_left
            self.height = self._hmd.height_left
            self.projection_matrix = self._hmd.getProjectionMatrixLeft(near, far)

            self.setEye(1)
            self.width = self._hmd.width_right
            self.height = self._hmd.height_right
            self.projection_matrix = self._hmd.getProjectionMatrixRight(near, far)

            # initialize FBO
            super(Oculus, self).init()
            # send it back to HMD
            if not self._hmd.setup(self._framebuffer_object[0], self._framebuffer_object[1]):
                raise Exception("Failed to setup HMD")

        except Exception as E:
            self.error("init", E, True)
            self._hmd = None
            return False

        else:
            return True
Dalai Felinto's avatar
Dalai Felinto committed

    def loop(self, context):
Dalai Felinto's avatar
Dalai Felinto committed
        """
        Get fresh tracking data
        """
        try:
            data = self._hmd.update()
Dalai Felinto's avatar
Dalai Felinto committed

            self._eye_orientation_raw[0] = data[0]
            self._eye_orientation_raw[1] = data[2]
            self._eye_position_raw[0] = data[1]
            self._eye_position_raw[1] = data[3]
            # update matrices
            super(Oculus, self).loop(context)
        except Exception as E:
Dalai Felinto's avatar
Dalai Felinto committed
            self.error("loop", E, False)
            return False

        return True
Dalai Felinto's avatar
Dalai Felinto committed

    def frameReady(self):
        """
        The frame is ready to be sent to the device
Dalai Felinto's avatar
Dalai Felinto committed
        """
        try:
            self._hmd.frameReady()
        except Exception as E:
            self.error("frameReady", E, False)
            return False
Dalai Felinto's avatar
Dalai Felinto committed

        return True

Dalai Felinto's avatar
Dalai Felinto committed
    def quit(self):
        """
        Garbage collection
        """
        self._hmd = None
        return super(Oculus, self).quit()
Dalai Felinto's avatar
Dalai Felinto committed