From 07f7cc42ad5b3c624e5d7c4e2283888edd250a3b Mon Sep 17 00:00:00 2001 From: barnold1953 Date: Thu, 26 Jun 2014 12:58:23 -0700 Subject: [PATCH] Made spacebar reconnect the oculus. Code cleanup. --- interface/src/devices/OculusManager.cpp | 79 ++++++++++++++++++------- interface/src/devices/OculusManager.h | 15 +---- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/interface/src/devices/OculusManager.cpp b/interface/src/devices/OculusManager.cpp index e23801754a..9e4c2c1149 100644 --- a/interface/src/devices/OculusManager.cpp +++ b/interface/src/devices/OculusManager.cpp @@ -41,12 +41,13 @@ ovrHmdDesc OculusManager::_ovrHmdDesc; ovrFovPort OculusManager::_eyeFov[ovrEye_Count]; ovrSizei OculusManager::_renderTargetSize; ovrVector2f OculusManager::_UVScaleOffset[ovrEye_Count][2]; -GLuint OculusManager::_vbo[ovrEye_Count]; -GLuint OculusManager::_indicesVbo[ovrEye_Count]; -GLsizei OculusManager::_meshSize[ovrEye_Count]; +GLuint OculusManager::_vbo[ovrEye_Count] = { 0, 0 }; +GLuint OculusManager::_indicesVbo[ovrEye_Count] = { 0, 0 }; +GLsizei OculusManager::_meshSize[ovrEye_Count] = { 0, 0 }; ovrFrameTiming OculusManager::_hmdFrameTiming; unsigned int OculusManager::_frameIndex = 0; bool OculusManager::_frameTimingActive = false; +bool OculusManager::_programInitialized = false; #endif @@ -78,34 +79,69 @@ void OculusManager::connect() { ovrSensorCap_Position, ovrSensorCap_Orientation); - _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/oculus.vert"); - _program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + "shaders/oculus.frag"); - _program.link(); + if (!_programInitialized) { + // Shader program + _programInitialized = true; + _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/oculus.vert"); + _program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + "shaders/oculus.frag"); + _program.link(); + + // Uniforms + _textureLocation = _program.uniformLocation("texture"); + _eyeToSourceUVScaleLocation = _program.uniformLocation("EyeToSourceUVScale"); + _eyeToSourceUVOffsetLocation = _program.uniformLocation("EyeToSourceUVOffset"); + _eyeRotationStartLocation = _program.uniformLocation("EyeRotationStart"); + _eyeRotationEndLocation = _program.uniformLocation("EyeRotationEnd"); + + // Attributes + _positionAttributeLocation = _program.attributeLocation("position"); + _colorAttributeLocation = _program.attributeLocation("color"); + _texCoord0AttributeLocation = _program.attributeLocation("texCoord0"); + _texCoord1AttributeLocation = _program.attributeLocation("texCoord1"); + _texCoord2AttributeLocation = _program.attributeLocation("texCoord2"); + } + + //Generate the distortion VBOs generateDistortionMesh(); - // Uniforms - _textureLocation = _program.uniformLocation("texture"); - _eyeToSourceUVScaleLocation = _program.uniformLocation("EyeToSourceUVScale"); - _eyeToSourceUVOffsetLocation = _program.uniformLocation("EyeToSourceUVOffset"); - _eyeRotationStartLocation = _program.uniformLocation("EyeRotationStart"); - _eyeRotationEndLocation = _program.uniformLocation("EyeRotationEnd"); - - // Attributes - _positionAttributeLocation = _program.attributeLocation("position"); - _colorAttributeLocation = _program.attributeLocation("color"); - _texCoord0AttributeLocation = _program.attributeLocation("texCoord0"); - _texCoord1AttributeLocation = _program.attributeLocation("texCoord1"); - _texCoord2AttributeLocation = _program.attributeLocation("texCoord2"); - } else { + _isConnected = false; + ovrHmd_Destroy(_ovrHmd); ovr_Shutdown(); } #endif } +//Disconnects and deallocates the OR +void OculusManager::disconnect() { + if (_isConnected) { + _isConnected = false; + ovrHmd_Destroy(_ovrHmd); + ovr_Shutdown(); + + //Free the distortion mesh data + for (int i = 0; i < ovrEye_Count; i++) { + if (_vbo[i] != 0) { + glDeleteBuffers(1, &(_vbo[i])); + _vbo[i] = 0; + } + if (_indicesVbo[i] != 0) { + glDeleteBuffers(1, &(_indicesVbo[i])); + _indicesVbo[i] = 0; + } + } + } +} + void OculusManager::generateDistortionMesh() { #ifdef HAVE_LIBOVR + //Already have the distortion mesh + if (_vbo[0] != 0) { + printf("WARNING: Tried to generate Oculus distortion mesh twice without freeing the VBOs."); + return; + } + ovrEyeRenderDesc eyeDesc[ovrEye_Count]; eyeDesc[0] = ovrHmd_GetRenderDesc(_ovrHmd, ovrEye_Left, _eyeFov[0]); eyeDesc[1] = ovrHmd_GetRenderDesc(_ovrHmd, ovrEye_Right, _eyeFov[1]); @@ -346,7 +382,8 @@ void OculusManager::display(Camera& whichCamera) { void OculusManager::reset() { #ifdef HAVE_LIBOVR - //_sensorFusion->Reset(); + disconnect(); + connect(); #endif } diff --git a/interface/src/devices/OculusManager.h b/interface/src/devices/OculusManager.h index 8a34caf84f..ab17c94486 100644 --- a/interface/src/devices/OculusManager.h +++ b/interface/src/devices/OculusManager.h @@ -18,10 +18,8 @@ #include "../src/Util/Util_Render_Stereo.h" - #include "renderer/ProgramObject.h" - const float DEFAULT_OCULUS_UI_ANGULAR_SIZE = 72.0f; class Camera; @@ -30,19 +28,12 @@ class Camera; class OculusManager { public: static void connect(); - - static void generateDistortionMesh(); - + static void disconnect(); static bool isConnected(); - static void beginFrameTiming(); - static void endFrameTiming(); - static void configureCamera(Camera& camera, int screenWidth, int screenHeight); - static void display(Camera& whichCamera); - static void reset(); /// param \yaw[out] yaw in radians @@ -50,9 +41,8 @@ public: /// param \roll[out] roll in radians static void getEulerAngles(float& yaw, float& pitch, float& roll); - static void updateYawOffset(); - private: + static void generateDistortionMesh(); struct DistortionVertex { glm::vec2 pos; @@ -97,6 +87,7 @@ private: static ovrFrameTiming _hmdFrameTiming; static unsigned int _frameIndex; static bool _frameTimingActive; + static bool _programInitialized; #endif };