Showing the changes to plugins/oculus/src/OculusBaseDisplayPlugin.cpp (1 of 47 files). Show all
Unified · Split
| @@ -0,0 +1,171 @@ | |||
| 1 | // | ||
| 2 | // Created by Bradley Austin Davis on 2014/04/13. | ||
| 3 | // Copyright 2015 High Fidelity, Inc. | ||
| 4 | // | ||
| 5 | // Distributed under the Apache License, Version 2.0. | ||
| 6 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 7 | // | ||
| 8 | #include "OculusBaseDisplayPlugin.h" | ||
| 9 | |||
| 10 | #include <ViewFrustum.h> | ||
| 11 | |||
| 12 | #include "OculusHelpers.h" | ||
| 13 | |||
| 14 | uvec2 OculusBaseDisplayPlugin::getRecommendedRenderSize() const { | ||
| 15 | return _desiredFramebufferSize; | ||
| 16 | } | ||
| 17 | |||
| 18 | void OculusBaseDisplayPlugin::preRender() { | ||
| 19 | #if (OVR_MAJOR_VERSION >= 6) | ||
| 20 | ovrFrameTiming ftiming = ovr_GetFrameTiming(_hmd, _frameIndex); | ||
| 21 | _trackingState = ovr_GetTrackingState(_hmd, ftiming.DisplayMidpointSeconds); | ||
| 22 | #endif | ||
| 23 | } | ||
| 24 | |||
| 25 | glm::mat4 OculusBaseDisplayPlugin::getProjection(Eye eye, const glm::mat4& baseProjection) const { | ||
| 26 | return _eyeProjections[eye]; | ||
| 27 | } | ||
| 28 | |||
| 29 | void OculusBaseDisplayPlugin::resetSensors() { | ||
| 30 | #if (OVR_MAJOR_VERSION >= 6) | ||
| 31 | ovr_RecenterPose(_hmd); | ||
| 32 | #endif | ||
| 33 | } | ||
| 34 | |||
| 35 | glm::mat4 OculusBaseDisplayPlugin::getEyeToHeadTransform(Eye eye) const { | ||
| 36 | return glm::translate(mat4(), toGlm(_eyeOffsets[eye])); | ||
| 37 | } | ||
| 38 | |||
| 39 | glm::mat4 OculusBaseDisplayPlugin::getHeadPose() const { | ||
| 40 | return toGlm(_trackingState.HeadPose.ThePose); | ||
| 41 | } | ||
| 42 | |||
| 43 | void OculusBaseDisplayPlugin::setEyeRenderPose(Eye eye, const glm::mat4& pose) { | ||
| 44 | _eyePoses[eye] = ovrPoseFromGlm(pose); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | bool OculusBaseDisplayPlugin::isSupported() const { | ||
| 49 | #if (OVR_MAJOR_VERSION >= 6) | ||
| 50 | if (!OVR_SUCCESS(ovr_Initialize(nullptr))) { | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | bool result = false; | ||
| 54 | if (ovrHmd_Detect() > 0) { | ||
| 55 | result = true; | ||
| 56 | } | ||
| 57 | ovr_Shutdown(); | ||
| 58 | return result; | ||
| 59 | #else | ||
| 60 | return false; | ||
| 61 | #endif | ||
| 62 | } | ||
| 63 | |||
| 64 | // DLL based display plugins MUST initialize GLEW inside the DLL code. | ||
| 65 | void OculusBaseDisplayPlugin::customizeContext() { | ||
| 66 | glewExperimental = true; | ||
| 67 | GLenum err = glewInit(); | ||
| 68 | glGetError(); | ||
| 69 | WindowOpenGLDisplayPlugin::customizeContext(); | ||
| 70 | } | ||
| 71 | |||
| 72 | void OculusBaseDisplayPlugin::init() { | ||
| 73 | } | ||
| 74 | |||
| 75 | void OculusBaseDisplayPlugin::deinit() { | ||
| 76 | } | ||
| 77 | |||
| 78 | void OculusBaseDisplayPlugin::activate() { | ||
| 79 | #if (OVR_MAJOR_VERSION >= 6) | ||
| 80 | if (!OVR_SUCCESS(ovr_Initialize(nullptr))) { | ||
| 81 | qFatal("Could not init OVR"); | ||
| 82 | } | ||
| 83 | |||
| 84 | #if (OVR_MAJOR_VERSION == 6) | ||
| 85 | if (!OVR_SUCCESS(ovr_Create(0, &_hmd))) { | ||
| 86 | #elif (OVR_MAJOR_VERSION == 7) | ||
| 87 | if (!OVR_SUCCESS(ovr_Create(&_hmd, &_luid))) { | ||
| 88 | #endif | ||
| 89 | Q_ASSERT(false); | ||
| 90 | qFatal("Failed to acquire HMD"); | ||
| 91 | } | ||
| 92 | |||
| 93 | _hmdDesc = ovr_GetHmdDesc(_hmd); | ||
| 94 | |||
| 95 | _ipd = ovr_GetFloat(_hmd, OVR_KEY_IPD, _ipd); | ||
| 96 | |||
| 97 | glm::uvec2 eyeSizes[2]; | ||
| 98 | ovr_for_each_eye([&](ovrEyeType eye) { | ||
| 99 | _eyeFovs[eye] = _hmdDesc.DefaultEyeFov[eye]; | ||
| 100 | ovrEyeRenderDesc& erd = _eyeRenderDescs[eye] = ovr_GetRenderDesc(_hmd, eye, _eyeFovs[eye]); | ||
| 101 | ovrMatrix4f ovrPerspectiveProjection = | ||
| 102 | ovrMatrix4f_Projection(erd.Fov, DEFAULT_NEAR_CLIP, DEFAULT_FAR_CLIP, ovrProjection_RightHanded); | ||
| 103 | _eyeProjections[eye] = toGlm(ovrPerspectiveProjection); | ||
| 104 | |||
| 105 | ovrPerspectiveProjection = | ||
| 106 | ovrMatrix4f_Projection(erd.Fov, 0.001f, 10.0f, ovrProjection_RightHanded); | ||
| 107 | _compositeEyeProjections[eye] = toGlm(ovrPerspectiveProjection); | ||
| 108 | |||
| 109 | _eyeOffsets[eye] = erd.HmdToEyeViewOffset; | ||
| 110 | eyeSizes[eye] = toGlm(ovr_GetFovTextureSize(_hmd, eye, erd.Fov, 1.0f)); | ||
| 111 | }); | ||
| 112 | ovrFovPort combined = _eyeFovs[Left]; | ||
| 113 | combined.LeftTan = std::max(_eyeFovs[Left].LeftTan, _eyeFovs[Right].LeftTan); | ||
| 114 | combined.RightTan = std::max(_eyeFovs[Left].RightTan, _eyeFovs[Right].RightTan); | ||
| 115 | ovrMatrix4f ovrPerspectiveProjection = | ||
| 116 | ovrMatrix4f_Projection(combined, DEFAULT_NEAR_CLIP, DEFAULT_FAR_CLIP, ovrProjection_RightHanded); | ||
| 117 | _eyeProjections[Mono] = toGlm(ovrPerspectiveProjection); | ||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | _desiredFramebufferSize = uvec2( | ||
| 122 | eyeSizes[0].x + eyeSizes[1].x, | ||
| 123 | std::max(eyeSizes[0].y, eyeSizes[1].y)); | ||
| 124 | |||
| 125 | _frameIndex = 0; | ||
| 126 | |||
| 127 | if (!OVR_SUCCESS(ovr_ConfigureTracking(_hmd, | ||
| 128 | ovrTrackingCap_Orientation | ovrTrackingCap_Position | ovrTrackingCap_MagYawCorrection, 0))) { | ||
| 129 | qFatal("Could not attach to sensor device"); | ||
| 130 | } | ||
| 131 | |||
| 132 | // Parent class relies on our _hmd intialization, so it must come after that. | ||
| 133 | memset(&_sceneLayer, 0, sizeof(ovrLayerEyeFov)); | ||
| 134 | _sceneLayer.Header.Type = ovrLayerType_EyeFov; | ||
| 135 | _sceneLayer.Header.Flags = ovrLayerFlag_TextureOriginAtBottomLeft; | ||
| 136 | ovr_for_each_eye([&](ovrEyeType eye) { | ||
| 137 | ovrFovPort & fov = _sceneLayer.Fov[eye] = _eyeRenderDescs[eye].Fov; | ||
| 138 | ovrSizei & size = _sceneLayer.Viewport[eye].Size = ovr_GetFovTextureSize(_hmd, eye, fov, 1.0f); | ||
| 139 | _sceneLayer.Viewport[eye].Pos = { eye == ovrEye_Left ? 0 : size.w, 0 }; | ||
| 140 | }); | ||
| 141 | |||
| 142 | if (!OVR_SUCCESS(ovr_ConfigureTracking(_hmd, | ||
| 143 | ovrTrackingCap_Orientation | ovrTrackingCap_Position | ovrTrackingCap_MagYawCorrection, 0))) { | ||
| 144 | qFatal("Could not attach to sensor device"); | ||
| 145 | } | ||
| 146 | #endif | ||
| 147 | |||
| 148 | WindowOpenGLDisplayPlugin::activate(); | ||
| 149 | } | ||
| 150 | |||
| 151 | void OculusBaseDisplayPlugin::deactivate() { | ||
| 152 | WindowOpenGLDisplayPlugin::deactivate(); | ||
| 153 | |||
| 154 | #if (OVR_MAJOR_VERSION >= 6) | ||
| 155 | ovr_Destroy(_hmd); | ||
| 156 | _hmd = nullptr; | ||
| 157 | ovr_Shutdown(); | ||
| 158 | #endif | ||
| 159 | } | ||
| 160 | |||
| 161 | void OculusBaseDisplayPlugin::display(GLuint finalTexture, const glm::uvec2& sceneSize) { | ||
| 162 | ++_frameIndex; | ||
| 163 | } | ||
| 164 | |||
| 165 | float OculusBaseDisplayPlugin::getIPD() const { | ||
| 166 | float result = OVR_DEFAULT_IPD; | ||
| 167 | #if (OVR_MAJOR_VERSION >= 6) | ||
| 168 | result = ovr_GetFloat(_hmd, OVR_KEY_IPD, result); | ||
| 169 | #endif | ||
| 170 | return result; | ||
| 171 | } | ||