From 1b8ddaa87b325484e7c9575a1ab1d034ea5a7d97 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Wed, 26 Jun 2013 16:59:08 +0200 Subject: [PATCH] Add 2D interface shifting calculations, see #43 --- code/nel/include/nel/3d/stereo_ovr.h | 6 ++++ code/nel/src/3d/stereo_ovr.cpp | 31 +++++++++++++++++-- code/snowballs2/client/src/camera.h | 1 - code/snowballs2/client/src/commands.cpp | 15 +++++++++ code/snowballs2/client/src/snowballs_client.h | 2 ++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/code/nel/include/nel/3d/stereo_ovr.h b/code/nel/include/nel/3d/stereo_ovr.h index 9a91e0bd0..cd466a7aa 100644 --- a/code/nel/include/nel/3d/stereo_ovr.h +++ b/code/nel/include/nel/3d/stereo_ovr.h @@ -95,6 +95,8 @@ public: /// Gets the current viewport virtual const NL3D::CViewport &getCurrentViewport() const; /// Gets the current camera frustum + virtual const NL3D::CFrustum &getCurrentFrustum() const; + /// Gets the current camera frustum virtual void getCurrentFrustum(NL3D::UCamera *camera) const; /// Gets the current camera matrix virtual void getCurrentMatrix(NL3D::UCamera *camera) const; @@ -119,6 +121,8 @@ public: /// Get the HMD orientation virtual NLMISC::CQuat getOrientation() const; + /// Get GUI center (1 = width, 1 = height, 0 = center) (todo: move to CStereoHMD) + void getInterface2DShift(float &x, float &y, float distance); static void listDevices(std::vector &devicesOut); @@ -140,6 +144,8 @@ private: CFrustum m_LeftFrustum; CFrustum m_RightFrustum; CMatrix m_CameraMatrix; + mutable bool m_OrientationCached; + mutable NLMISC::CQuat m_OrientationCache; }; /* class CStereoOVR */ diff --git a/code/nel/src/3d/stereo_ovr.cpp b/code/nel/src/3d/stereo_ovr.cpp index b9c829d5b..09e079f81 100644 --- a/code/nel/src/3d/stereo_ovr.cpp +++ b/code/nel/src/3d/stereo_ovr.cpp @@ -146,7 +146,7 @@ public: OVR::HMDInfo HMDInfo; }; -CStereoOVR::CStereoOVR(const CStereoDeviceInfo &deviceInfo) : m_Stage(0) +CStereoOVR::CStereoOVR(const CStereoDeviceInfo &deviceInfo) : m_Stage(0), m_OrientationCached(false) { ++s_DeviceCounter; m_DevicePtr = new CStereoOVRDevicePtr(); @@ -270,6 +270,7 @@ bool CStereoOVR::nextPass() case 6: m_Stage = 0; // present + m_OrientationCached = false; return false; } } @@ -280,6 +281,12 @@ const NL3D::CViewport &CStereoOVR::getCurrentViewport() const else return m_RightViewport; } +const NL3D::CFrustum &CStereoOVR::getCurrentFrustum() const +{ + if (m_Stage % 2) return m_LeftFrustum; + else return m_RightFrustum; +} + void CStereoOVR::getCurrentFrustum(NL3D::UCamera *camera) const { if (m_Stage % 2) camera->setFrustum(m_LeftFrustum); @@ -360,6 +367,9 @@ void CStereoOVR::endInterface2D() NLMISC::CQuat CStereoOVR::getOrientation() const { + if (m_OrientationCached) + return m_OrientationCache; + OVR::Quatf quatovr = m_DevicePtr->SensorFusion.GetPredictedOrientation(); NLMISC::CMatrix coordsys; float csys[] = { @@ -374,7 +384,24 @@ NLMISC::CQuat CStereoOVR::getOrientation() const 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(); + NLMISC::CQuat finalquat = matnel.getRot(); + m_OrientationCache = finalquat; + m_OrientationCached = true; + return finalquat; +} + +/// Get GUI shift (todo: move to CStereoHMD) +void CStereoOVR::getInterface2DShift(float &x, float &y, float distance) +{ + NLMISC::CVector vector = CVector(0.f, -distance, 0.f); + NLMISC::CQuat rot = getOrientation(); + rot.invert(); + NLMISC::CMatrix mat; + mat.rotate(rot); + mat.translate(vector); + CVector proj = getCurrentFrustum().project(mat.getPos()); + x = proj.x - 0.5f; + y = proj.y - 0.5f; } void CStereoOVR::listDevices(std::vector &devicesOut) diff --git a/code/snowballs2/client/src/camera.h b/code/snowballs2/client/src/camera.h index 5eae47c53..c2d61d866 100644 --- a/code/snowballs2/client/src/camera.h +++ b/code/snowballs2/client/src/camera.h @@ -42,7 +42,6 @@ namespace SBCLIENT { extern NL3D::UCamera Camera; extern NL3D::UCamera SkyCamera; extern NL3D::UVisualCollisionEntity *CamCollisionEntity; -extern NL3D::CStereoOVR *StereoHMD; extern NL3D::UScene *SkyScene; // diff --git a/code/snowballs2/client/src/commands.cpp b/code/snowballs2/client/src/commands.cpp index ed8a1dc7d..795371cb6 100644 --- a/code/snowballs2/client/src/commands.cpp +++ b/code/snowballs2/client/src/commands.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include "network.h" #include "snowballs_client.h" @@ -374,6 +375,9 @@ void updateCommands() uint32 _width, _height; Driver->getWindowSize(_width, _height); float width = (float)_width, height = (float)_height; + NL3D::CViewport vp = Driver->getViewport(); + width *= vp.getWidth(); + height *= vp.getHeight(); float CommandsLineHeight = CommandsFontSize / height; float CommandsBoxX = ((float)(sint32)(SBCLIENT::CommandsBoxX * width)) / width; float CommandsBoxWidth = ((float)(sint32)(SBCLIENT::CommandsBoxWidth * width)) / width; @@ -381,6 +385,17 @@ void updateCommands() float CommandsBoxHeight = ((float)(sint32)((CommandsNbLines + 1) * CommandsLineHeight * width)) / width; float CommandsBoxBorderX = ((float)(sint32)(SBCLIENT::CommandsBoxBorder * width)) / width; float CommandsBoxBorderY = ((float)(sint32)(SBCLIENT::CommandsBoxBorder * height)) / height; + if (StereoHMD) + { + float xshift, yshift; + StereoHMD->getInterface2DShift(xshift, yshift, 1.0f); + // snap to pixels + xshift = ((float)(sint32)(xshift * width)) / width; + yshift = ((float)(sint32)(yshift * height)) / height; + // adjust + CommandsBoxX += xshift; + CommandsBoxY += yshift; + } // Display the background Driver->setMatrixMode2D11 (); diff --git a/code/snowballs2/client/src/snowballs_client.h b/code/snowballs2/client/src/snowballs_client.h index 67116f7e6..bc955d317 100644 --- a/code/snowballs2/client/src/snowballs_client.h +++ b/code/snowballs2/client/src/snowballs_client.h @@ -42,6 +42,7 @@ namespace NL3D { class UScene; class UTextContext; class ULandscape; + class CStereoOVR; } namespace SBCLIENT { @@ -58,6 +59,7 @@ public: }; extern NL3D::UDriver *Driver; +extern NL3D::CStereoOVR *StereoHMD; extern NL3D::UScene *Scene; extern NL3D::UTextContext *TextContext; extern NLMISC::CConfigFile *ConfigFile;