From 4289248d56506879066862641fdfa24ed2fafc8b Mon Sep 17 00:00:00 2001 From: kaetemi Date: Wed, 26 Jun 2013 04:57:58 +0200 Subject: [PATCH] Read sensor data and set camera in snowballs, ref #43 --- code/nel/include/nel/3d/stereo_ovr.h | 4 ++ code/nel/src/3d/stereo_ovr.cpp | 52 ++++++++++++++++--- code/snowballs2/client/src/camera.cpp | 20 ++++++- code/snowballs2/client/src/snowballs_config.h | 2 +- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/code/nel/include/nel/3d/stereo_ovr.h b/code/nel/include/nel/3d/stereo_ovr.h index 5f61c0595..3858b046e 100644 --- a/code/nel/include/nel/3d/stereo_ovr.h +++ b/code/nel/include/nel/3d/stereo_ovr.h @@ -80,11 +80,15 @@ public: CStereoOVR(const CStereoDeviceInfo &deviceInfo); virtual ~CStereoOVR(); + virtual NLMISC::CQuat getOrientation(); + static void listDevices(std::vector &devicesOut); static CStereoOVR *createDevice(const CStereoDeviceInfo &deviceInfo); static bool isLibraryInUse(); static void releaseLibrary(); + bool isDeviceCreated(); + private: CStereoOVRDevicePtr *m_DevicePtr; diff --git a/code/nel/src/3d/stereo_ovr.cpp b/code/nel/src/3d/stereo_ovr.cpp index 40d94f901..44daae9ef 100644 --- a/code/nel/src/3d/stereo_ovr.cpp +++ b/code/nel/src/3d/stereo_ovr.cpp @@ -129,7 +129,7 @@ CStereoOVRSystem s_StereoOVRSystem; class CStereoOVRDeviceHandle : public NLMISC::CRefCount { public: - OVR::DeviceHandle DeviceHandle; + OVR::DeviceEnumerator DeviceHandle; }; sint s_DeviceCounter = 0; @@ -140,6 +140,8 @@ class CStereoOVRDevicePtr { public: OVR::Ptr HMDDevice; + OVR::Ptr SensorDevice; + OVR::SensorFusion SensorFusion; }; CStereoOVR::CStereoOVR(const CStereoDeviceInfo &deviceInfo) @@ -147,20 +149,53 @@ CStereoOVR::CStereoOVR(const CStereoDeviceInfo &deviceInfo) ++s_DeviceCounter; m_DevicePtr = new CStereoOVRDevicePtr(); - // CStereoOVRDeviceHandle *handle = static_cast(deviceInfo.Factory.getPtr()); - // OVR::DeviceHandle dh = handle->DeviceHandle; - // dh.CreateDevice(); + CStereoOVRDeviceHandle *handle = static_cast(deviceInfo.Factory.getPtr()); + OVR::DeviceEnumerator dh = handle->DeviceHandle; + m_DevicePtr->HMDDevice = dh.CreateDevice(); + + if (m_DevicePtr->HMDDevice) + { + m_DevicePtr->SensorDevice = m_DevicePtr->HMDDevice->GetSensor(); + m_DevicePtr->SensorFusion.AttachToSensor(m_DevicePtr->SensorDevice); + m_DevicePtr->SensorFusion.SetGravityEnabled(true); + m_DevicePtr->SensorFusion.SetPredictionEnabled(true); + m_DevicePtr->SensorFusion.SetYawCorrectionEnabled(true); + } } CStereoOVR::~CStereoOVR() { - // ... + if (m_DevicePtr->SensorDevice) + m_DevicePtr->SensorDevice->Release(); + m_DevicePtr->SensorDevice.Clear(); + if (m_DevicePtr->HMDDevice) + m_DevicePtr->HMDDevice->Release(); + m_DevicePtr->HMDDevice.Clear(); delete m_DevicePtr; m_DevicePtr = NULL; --s_DeviceCounter; } +NLMISC::CQuat CStereoOVR::getOrientation() +{ + OVR::Quatf quatovr = m_DevicePtr->SensorFusion.GetPredictedOrientation(); + NLMISC::CMatrix coordsys; + float csys[] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, -1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f, + }; + coordsys.set(csys); + NLMISC::CMatrix matovr; + matovr.setRot(NLMISC::CQuat(quatovr.x, quatovr.y, quatovr.z, quatovr.w)); + NLMISC::CMatrix matr; + matr.rotateX(NLMISC::Pi * 0.5f); // fix this properly... :) (note: removing this allows you to use rift while lying down) + NLMISC::CMatrix matnel = matr * matovr * coordsys; + return matnel.getRot(); +} + void CStereoOVR::listDevices(std::vector &devicesOut) { s_StereoOVRSystem.Init(); @@ -190,7 +225,7 @@ void CStereoOVR::listDevices(std::vector &devicesOut) CStereoOVR *CStereoOVR::createDevice(const CStereoDeviceInfo &deviceInfo) { - return NULL; + return new CStereoOVR(deviceInfo); } bool CStereoOVR::isLibraryInUse() @@ -205,6 +240,11 @@ void CStereoOVR::releaseLibrary() s_StereoOVRSystem.Release(); } +bool CStereoOVR::isDeviceCreated() +{ + return m_DevicePtr->HMDDevice != NULL; +} + } /* namespace NL3D */ /* end of file */ diff --git a/code/snowballs2/client/src/camera.cpp b/code/snowballs2/client/src/camera.cpp index 38db2d599..f1a76e552 100644 --- a/code/snowballs2/client/src/camera.cpp +++ b/code/snowballs2/client/src/camera.cpp @@ -108,13 +108,19 @@ void initCamera() break; } } - //s_StereoHMD->createDevice( + if (deviceInfo) + { + nlinfo("Create HMD device!"); + s_StereoHMD = CStereoOVR::createDevice(*deviceInfo); + } } // Set up directly the camera Camera = Scene->getCam(); Camera.setTransformMode (UTransformable::DirectMatrix); - Camera.setPerspective ((float)Pi/2.f, 1.33f, 0.1f, 1000); + Camera.setPerspective((float)Pi/2.f, + ConfigFile->getVar("ScreenWidth").asFloat() / ConfigFile->getVar("ScreenHeight").asFloat(), + 0.1f, 1000.f); Camera.lookAt (CVector(ConfigFile->getVar("StartPoint").asFloat(0), ConfigFile->getVar("StartPoint").asFloat(1), ConfigFile->getVar("StartPoint").asFloat(2)), @@ -153,11 +159,21 @@ void releaseCamera() Scene->deleteInstance(Snow); VisualCollisionManager->deleteEntity(CamCollisionEntity); + delete s_StereoHMD; + s_StereoHMD = NULL; CStereoOVR::releaseLibrary(); } void updateCamera() { + if (s_StereoHMD) + { + NLMISC::CQuat hmdOrient = s_StereoHMD->getOrientation(); + NLMISC::CMatrix camMatrix = Camera.getMatrix(); + NLMISC::CMatrix hmdMatrix; + hmdMatrix.setRot(hmdOrient); + Camera.setMatrix(camMatrix * hmdMatrix); + } // Set the new position of the snow emitter CMatrix mat = CMatrix::Identity; mat.setPos (Camera.getMatrix().getPos()/*+CVector (0.0f, 0.0f, -10.0f)*/); diff --git a/code/snowballs2/client/src/snowballs_config.h b/code/snowballs2/client/src/snowballs_config.h index fb304ba68..2e6b848fb 100644 --- a/code/snowballs2/client/src/snowballs_config.h +++ b/code/snowballs2/client/src/snowballs_config.h @@ -49,7 +49,7 @@ #define SBCLIENT_DEV_SOUND 0 #define SBCLIENT_DEV_STEREO 0 #define SBCLIENT_DEV_MEMLEAK 0 -#define SBCLIENT_DEV_PIXEL_PROGRAM 1 +#define SBCLIENT_DEV_PIXEL_PROGRAM 0