virtual int run() {
    SAY("Initializing SDK");
    ovr_Initialize();

    int hmdCount = ovrHmd_Detect();
    SAY("Found %d connected Rift device(s)", hmdCount);
    for (int i = 0; i < hmdCount; ++i) {
      ovrHmd hmd = ovrHmd_Create(i);
      SAY(hmd->ProductName);
      ovrHmd_Destroy(hmd);
    }

    ovrHmd hmd = ovrHmd_CreateDebug(ovrHmd_DK2);
    SAY(hmd->ProductName);
    ovrHmd_Destroy(hmd);

    ovr_Shutdown();

    SAY("Exiting");
    return 0;
  }
Example #2
0
int32_t VR_OVR_Enable()
{
	int32_t failure = 0;
	unsigned int hmdCaps = 0;
	qboolean isDebug = false;
	unsigned int device = 0;
	if (!vr_ovr_enable->value)
		return 0;

	if (!libovrInitialized)
	{
		libovrInitialized = ovr_Initialize();
		if (!libovrInitialized)
		{
			Com_Printf("VR_OVR: Fatal error: could not initialize LibOVR!\n");
			return 0;
		} else {
			Com_Printf("VR_OVR: %s initialized...\n",ovr_GetVersionString());
		}
	}

	{
		int numDevices = ovrHmd_Detect();
		int i;
		qboolean found = false;

		memset(hmdnames,0,sizeof(ovrname_t) * 255);

		Com_Printf("VR_OVR: Enumerating devices...\n");
		Com_Printf("VR_OVR: Found %i devices\n", numDevices);
		for (i = 0 ; i < numDevices ; i++)
		{
			ovrHmd tempHmd = ovrHmd_Create(i);
			if (tempHmd)
			{
				Com_Printf("VR_OVR: Found device #%i '%s' s/n:%s\n",i,tempHmd->ProductName, tempHmd->SerialNumber);
				sprintf(hmdnames[i].label,"%i: %s",i + 1, tempHmd->ProductName);
				sprintf(hmdnames[i].serialnumber,"%s",tempHmd->SerialNumber);
				if (!strncmp(vr_ovr_device->string,tempHmd->SerialNumber,strlen(tempHmd->SerialNumber)))
				{
					device = i;
					found = true;
				}
				ovrHmd_Destroy(tempHmd);
			}
		}
	}

	Com_Printf("VR_OVR: Initializing HMD: ");
	
	withinFrame = false;
	hmd = ovrHmd_Create(device);
	
	if (!hmd)
	{
		Com_Printf("no HMD detected!\n");
		failure = 1;
	}
	else {
		Com_Printf("ok!\n");
	}


	if (failure && vr_ovr_debug->value)
	{
		ovrHmdType temp = ovrHmd_None;
		switch((int) vr_ovr_debug->value)
		{
			default:
				temp = ovrHmd_DK1;
				break;
			case 2:
				temp = ovrHmd_DKHD;
				break;
			case 3:
				temp = ovrHmd_DK2;
				break;

		}
		hmd = ovrHmd_CreateDebug(temp);
		isDebug = true;
		Com_Printf("VR_OVR: Creating debug HMD...\n");
	}

	if (!hmd)
		return 0;

	if (hmd->HmdCaps & ovrHmdCap_ExtendDesktop)
	{
		Com_Printf("...running in extended desktop mode\n");
	} else if (!isDebug) {
		Com_Printf("...running in Direct HMD mode\n");
		Com_Printf("...Direct HMD mode is unsupported at this time\n");
		VR_OVR_Disable();
		VR_OVR_Shutdown();
		return 0;
	}

	Com_Printf("...device positioned at (%i,%i)\n",hmd->WindowsPos.x,hmd->WindowsPos.y);

	if (hmd->HmdCaps & ovrHmdCap_Available)
		Com_Printf("...sensor is available\n");
	if (hmd->HmdCaps & ovrHmdCap_LowPersistence)
		Com_Printf("...supports low persistance\n");
	if (hmd->HmdCaps & ovrHmdCap_DynamicPrediction)
		Com_Printf("...supports dynamic motion prediction\n");
	if (hmd->TrackingCaps & ovrTrackingCap_Position)
		Com_Printf("...supports position tracking\n");

	Com_Printf("...has type %s\n", hmd->ProductName);
	Com_Printf("...has %ux%u native resolution\n", hmd->Resolution.w, hmd->Resolution.h);

	if (!VR_OVR_InitSensor())
	{
		Com_Printf("VR_OVR: Sensor initialization failed!\n");
	}

	ovrHmd_ResetFrameTiming(hmd,0);
	return 1;
}
int HeadMountedDisplay::Detect()
{
    return ovrHmd_Detect();
}
OculusDevice::OculusDevice(float nearClip, float farClip, bool useTimewarp) : m_hmdDevice(0),
	m_nearClip(nearClip), m_farClip(farClip),
	m_useTimeWarp(useTimewarp),
	m_position(osg::Vec3(0.0f, 0.0f, 0.0f)),
	m_orientation(osg::Quat(0.0f, 0.0f, 0.0f, 1.0f))
{
	ovr_Initialize();
	
	// Enumerate HMD devices
	int numberOfDevices = ovrHmd_Detect();
	osg::notify(osg::DEBUG_INFO) << "Number of connected devices: " << numberOfDevices << std::endl;

	// Get first available HMD
	m_hmdDevice = ovrHmd_Create(0);
	
	// If no HMD is found try an emulated device
	if (!m_hmdDevice) {
		osg::notify(osg::WARN) << "Warning: No device could be found. Creating emulated device " << std::endl;
		m_hmdDevice = ovrHmd_CreateDebug(ovrHmd_DK1);
		ovrHmd_ResetFrameTiming(m_hmdDevice, 0);
	}

	if (m_hmdDevice) {
		// Print out some information about the HMD
		osg::notify(osg::ALWAYS) << "Product:         " << m_hmdDevice->ProductName << std::endl;
		osg::notify(osg::ALWAYS) << "Manufacturer:    " << m_hmdDevice->Manufacturer << std::endl;
		osg::notify(osg::ALWAYS) << "VendorId:        " << m_hmdDevice->VendorId << std::endl;
		osg::notify(osg::ALWAYS) << "ProductId:       " << m_hmdDevice->ProductId << std::endl;
		osg::notify(osg::ALWAYS) << "SerialNumber:    " << m_hmdDevice->SerialNumber << std::endl;
		osg::notify(osg::ALWAYS) << "FirmwareVersion: " << m_hmdDevice->FirmwareMajor << "."  << m_hmdDevice->FirmwareMinor << std::endl;

		// Get more details about the HMD.
		m_resolution = m_hmdDevice->Resolution;
		
		// Compute recommended render texture size
		float pixelsPerDisplayPixel = 1.0f; // Decrease this value to scale the size on render texture on lower performance hardware. Values above 1.0 is unnecessary.

		ovrSizei recommenedLeftTextureSize = ovrHmd_GetFovTextureSize(m_hmdDevice, ovrEye_Left, m_hmdDevice->DefaultEyeFov[0], pixelsPerDisplayPixel);
		ovrSizei recommenedRightTextureSize = ovrHmd_GetFovTextureSize(m_hmdDevice, ovrEye_Right, m_hmdDevice->DefaultEyeFov[1], pixelsPerDisplayPixel);

		// Compute size of render target
		m_renderTargetSize.w = recommenedLeftTextureSize.w + recommenedRightTextureSize.w;
		m_renderTargetSize.h = osg::maximum(recommenedLeftTextureSize.h, recommenedRightTextureSize.h);

		// Initialize ovrEyeRenderDesc struct.
		m_eyeRenderDesc[0] = ovrHmd_GetRenderDesc(m_hmdDevice, ovrEye_Left, m_hmdDevice->DefaultEyeFov[0]);
		m_eyeRenderDesc[1] = ovrHmd_GetRenderDesc(m_hmdDevice, ovrEye_Right, m_hmdDevice->DefaultEyeFov[1]);
		
		ovrVector3f leftEyeAdjust = m_eyeRenderDesc[0].ViewAdjust;
		m_leftEyeAdjust.set(leftEyeAdjust.x, leftEyeAdjust.y, leftEyeAdjust.z);
		ovrVector3f rightEyeAdjust = m_eyeRenderDesc[1].ViewAdjust;
		m_rightEyeAdjust.set(rightEyeAdjust.x, rightEyeAdjust.y, rightEyeAdjust.z);

		bool isRightHanded = true;
		
		ovrMatrix4f leftEyeProjectionMatrix = ovrMatrix4f_Projection(m_eyeRenderDesc[0].Fov, m_nearClip, m_farClip, isRightHanded);
		// Transpose matrix
		m_leftEyeProjectionMatrix.set(leftEyeProjectionMatrix.M[0][0], leftEyeProjectionMatrix.M[1][0], leftEyeProjectionMatrix.M[2][0], leftEyeProjectionMatrix.M[3][0],
			leftEyeProjectionMatrix.M[0][1], leftEyeProjectionMatrix.M[1][1], leftEyeProjectionMatrix.M[2][1], leftEyeProjectionMatrix.M[3][1],
			leftEyeProjectionMatrix.M[0][2], leftEyeProjectionMatrix.M[1][2], leftEyeProjectionMatrix.M[2][2], leftEyeProjectionMatrix.M[3][2],
			leftEyeProjectionMatrix.M[0][3], leftEyeProjectionMatrix.M[1][3], leftEyeProjectionMatrix.M[2][3], leftEyeProjectionMatrix.M[3][3]);

		ovrMatrix4f rightEyeProjectionMatrix = ovrMatrix4f_Projection(m_eyeRenderDesc[1].Fov, m_nearClip, m_farClip, isRightHanded);
		// Transpose matrix
		m_rightEyeProjectionMatrix.set(rightEyeProjectionMatrix.M[0][0], rightEyeProjectionMatrix.M[1][0], rightEyeProjectionMatrix.M[2][0], rightEyeProjectionMatrix.M[3][0],
			rightEyeProjectionMatrix.M[0][1], rightEyeProjectionMatrix.M[1][1], rightEyeProjectionMatrix.M[2][1], rightEyeProjectionMatrix.M[3][1],
			rightEyeProjectionMatrix.M[0][2], rightEyeProjectionMatrix.M[1][2], rightEyeProjectionMatrix.M[2][2], rightEyeProjectionMatrix.M[3][2],
			rightEyeProjectionMatrix.M[0][3], rightEyeProjectionMatrix.M[1][3], rightEyeProjectionMatrix.M[2][3], rightEyeProjectionMatrix.M[3][3]);

		// Start the sensor which provides the Rift’s pose and motion.
		ovrHmd_ConfigureTracking(m_hmdDevice, ovrTrackingCap_Orientation |
			ovrTrackingCap_MagYawCorrection |
			ovrTrackingCap_Position, 0);

		beginFrameTiming();
	}
}
Example #5
0
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
oculus_OculusRiftDK2_init(oculus_OculusRiftDK2 *self,
                          PyObject *args,
                          PyObject *kwargs)
{
    /* If LibOVR has not been initialised yet */
    if (!IS_INITIALISED)
    {
        /* Initialise Oculus */
        if (!ovr_Initialize(0))
            goto Init_Error;
        IS_INITIALISED = true;
    }

    /* Increase instance counter, which is using the initialised LibOVR */
    ++CONTROLLER_COUNT;

    /* Check if there are any HMDs available */
    if (!ovrHmd_Detect())
        goto Init_No_Devices_Error;

    /* Check global error */
    // if (error = ovrHmd_GetLastError(NULL))
    //     fprintf(stderr, "Error (init): '%s'\n", error);


    /* TODO: make index optional !!! */

    /* Set default values */
    int device_index    = 0;
    self->head_shift[0] = 0.f;
    self->head_shift[1] = 0.f;
    self->head_shift[2] = 0.f;
    self->head_factor   = 1.f;

    /* Unpack arguments */
    static char *kwlist[] = {"device_index",
                             "head_factor",
                             "head_shift_x",
                             "head_shift_y",
                             "head_shift_z",
                             NULL};
    if (!PyArg_ParseTupleAndKeywords(args,
                                     kwargs,
                                     "|iffff:__init__",
                                     kwlist,
                                     &device_index,
                                     &self->head_factor,
                                     self->head_shift + 0,
                                     self->head_shift + 1,
                                     self->head_shift + 2))
        goto Init_Error_Python;

    /* Get the device */
    ovrHmd device = ovrHmd_Create(device_index);
    if (!device)
        goto Init_HMD_Error;

    /* Check global error */
    // if (error = ovrHmd_GetLastError(NULL))
    //     fprintf(stderr, "Error (init): '%s'\n", error);
    /* Check for device specific error */
    // if (error = ovrHmd_GetLastError(device))
    //     fprintf(stderr, "Error (init): '%s'\n", error);

    /* Configure tracking */
    ovrHmd_ConfigureTracking(device, ovrTrackingCap_Orientation |
                                     ovrTrackingCap_MagYawCorrection |
                                     ovrTrackingCap_Position, 0);

    /* Check global error */
    // if (error = ovrHmd_GetLastError(NULL))
    //     fprintf(stderr, "Error (init): '%s'\n", error);
    /* Check for device specific error */
    // if (error = ovrHmd_GetLastError(device))
    //     fprintf(stderr, "Error (init): '%s'\n", error);

    /* If everything went fine */
    self->device = device;
    return PYTHON_RETURN_VALUE_SUCCESS;

    /* If there was a problem */
    Init_HMD_Error:
        _oculus_raise_python_exception(device,
                                       oculus_InitialisationError,
                                       "Cannot initialise Oculus device",
                                       NULL);
    Init_No_Devices_Error:
        _oculus_raise_python_exception(NULL,
                                       oculus_InitialisationError,
                                       "Cannot initialise Oculus device (No device found)",
                                       NULL);
    Init_Error:
        _oculus_raise_python_exception(NULL,
                                       oculus_InitialisationError,
                                       "Cannot initialise Oculus SDK: '",
                                       "'\n(HINT: Is oculusd/ovrd running?)");
    Init_Error_Python:
        self->device = NULL;
        return PYTHON_RETURN_VALUE_FAILURE;
}