Skip to content

Commit

Permalink
Added distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
r57zone committed Oct 26, 2017
1 parent a1bece2 commit c914768
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
44 changes: 37 additions & 7 deletions OpenVR/FreeTrack/samples/driver_sample/driver_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
//=============== Changed by r57zone (https://github.com/r57zone) ===============

#include <openvr_driver.h>
//#include "driverlog.h"

#include <vector>
#include <thread>
#include <chrono>

#include <atlbase.h>
//#include <Windows.h>
//#include "Shlwapi.h"
#include <atlbase.h>

using namespace vr;

Expand Down Expand Up @@ -62,6 +64,10 @@ static const char * const k_pch_Sample_RenderWidth_Int32 = "renderWidth";
static const char * const k_pch_Sample_RenderHeight_Int32 = "renderHeight";
static const char * const k_pch_Sample_SecondsFromVsyncToPhotons_Float = "secondsFromVsyncToPhotons";
static const char * const k_pch_Sample_DisplayFrequency_Float = "displayFrequency";
static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";

typedef struct _FreeTrack
{
Expand Down Expand Up @@ -207,6 +213,11 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
m_flSecondsFromVsyncToPhotons = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_SecondsFromVsyncToPhotons_Float);
m_flDisplayFrequency = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DisplayFrequency_Float);

m_fDistortionK1 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK1_Float);
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);

//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
//DriverLog( "driver_null: Window: %d %d %d %d\n", m_nWindowX, m_nWindowY, m_nWindowWidth, m_nWindowHeight );
Expand Down Expand Up @@ -394,12 +405,27 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
virtual DistortionCoordinates_t ComputeDistortion( EVREye eEye, float fU, float fV )
{
DistortionCoordinates_t coordinates;
coordinates.rfBlue[0] = fU;
coordinates.rfBlue[1] = fV;
coordinates.rfGreen[0] = fU;
coordinates.rfGreen[1] = fV;
coordinates.rfRed[0] = fU;
coordinates.rfRed[1] = fV;

//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
float hX;
float hY;
double rr;
double r2;
double theta;

rr = sqrt((fU - 0.5f)*(fU - 0.5f) + (fV - 0.5f)*(fV - 0.5f));
r2 = rr * (1 + m_fDistortionK1*(rr*rr) + m_fDistortionK2*(rr*rr*rr*rr));
theta = atan2(fU - 0.5f, fV - 0.5f);
hX = sin(theta)*r2*m_fZoomWidth;
hY = cos(theta)*r2*m_fZoomHeight;

coordinates.rfBlue[0] = hX + 0.5f;
coordinates.rfBlue[1] = hY + 0.5f;
coordinates.rfGreen[0] = hX + 0.5f;
coordinates.rfGreen[1] = hY + 0.5f;
coordinates.rfRed[0] = hX + 0.5f;
coordinates.rfRed[1] = hY + 0.5f;

return coordinates;
}

Expand Down Expand Up @@ -468,6 +494,10 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
float m_flSecondsFromVsyncToPhotons;
float m_flDisplayFrequency;
float m_flIPD;
float m_fDistortionK1;
float m_fDistortionK2;
float m_fZoomWidth;
float m_fZoomHeight;
};

//-----------------------------------------------------------------------------
Expand Down
47 changes: 37 additions & 10 deletions OpenVR/UDP/samples/driver_sample/driver_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
//=============== Changed by r57zone (https://github.com/r57zone) ===============

#include <openvr_driver.h>
//#include "driverlog.h"

#include <vector>
#include <thread>
#include <chrono>

#include <stdio.h>
#include <winsock2.h>
#pragma comment (lib, "WSock32.Lib")
//#include <Windows.h>

#if defined( _WINDOWS )
#include <windows.h>
#endif

using namespace vr;

Expand Down Expand Up @@ -67,6 +65,10 @@ static const char * const k_pch_Sample_RenderWidth_Int32 = "renderWidth";
static const char * const k_pch_Sample_RenderHeight_Int32 = "renderHeight";
static const char * const k_pch_Sample_SecondsFromVsyncToPhotons_Float = "secondsFromVsyncToPhotons";
static const char * const k_pch_Sample_DisplayFrequency_Float = "displayFrequency";
static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";

//OpenTrack vars
double qW, qX, qY, qZ;
Expand Down Expand Up @@ -243,6 +245,11 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
m_flSecondsFromVsyncToPhotons = vr::VRSettings()->GetFloat( k_pch_Sample_Section, k_pch_Sample_SecondsFromVsyncToPhotons_Float );
m_flDisplayFrequency = vr::VRSettings()->GetFloat( k_pch_Sample_Section, k_pch_Sample_DisplayFrequency_Float );

m_fDistortionK1 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK1_Float);
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);

//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
//DriverLog( "driver_null: Window: %d %d %d %d\n", m_nWindowX, m_nWindowY, m_nWindowWidth, m_nWindowHeight );
Expand Down Expand Up @@ -445,12 +452,28 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
virtual DistortionCoordinates_t ComputeDistortion( EVREye eEye, float fU, float fV )
{
DistortionCoordinates_t coordinates;
coordinates.rfBlue[0] = fU;
coordinates.rfBlue[1] = fV;
coordinates.rfGreen[0] = fU;
coordinates.rfGreen[1] = fV;
coordinates.rfRed[0] = fU;
coordinates.rfRed[1] = fV;

//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
float hX;
float hY;
double rr;
double r2;
double theta;

rr = sqrt((fU - 0.5f)*(fU - 0.5f) + (fV - 0.5f)*(fV - 0.5f));
r2 = rr * (1 + m_fDistortionK1*(rr*rr) + m_fDistortionK2*(rr*rr*rr*rr));
theta = atan2(fU - 0.5f, fV - 0.5f);
hX = sin(theta)*r2*m_fZoomWidth;
hY = cos(theta)*r2*m_fZoomHeight;

coordinates.rfBlue[0] = hX + 0.5f;
coordinates.rfBlue[1] = hY + 0.5f;
coordinates.rfGreen[0] = hX + 0.5f;
coordinates.rfGreen[1] = hY + 0.5f;
coordinates.rfRed[0] = hX + 0.5f;
coordinates.rfRed[1] = hY + 0.5f;


return coordinates;
}

Expand Down Expand Up @@ -517,6 +540,10 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
float m_flSecondsFromVsyncToPhotons;
float m_flDisplayFrequency;
float m_flIPD;
float m_fDistortionK1;
float m_fDistortionK2;
float m_fZoomWidth;
float m_fZoomHeight;
};

//-----------------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions SteamVR Settings/OpenVR/steamvr.vrsettings
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
"driver_null" : {
"enable" : true,
"id" : "Null Driver",
"secondsFromVsyncToPhotons" : 0.10000000149011612,
"secondsFromVsyncToPhotons" : 0.10000000149011612,
"serialNumber" : "Null 4711",
"renderWidth" : <RENDERWIDTH>,
"renderHeight" : <RENDERHEIGHT>,
"windowWidth" : <WINDOWWIDTH>,
"windowHeight" : <WINDOWHEIGHT>,
"windowX" : <WINDOWX>,
"windowY" : <WINDOWY>
"windowY" : <WINDOWY>,
"DistortionK1" : 0.91,
"DistortionK2" : 0.93,
"ZoomWidth" : 0.8,
"ZoomHeight" : 0.8
},
"steamvr" : {
"activateMultipleDrivers" : true,
"directMode" : false,
"enableHomeApp" : false,
"forcedDriver" : "null",
"mirrorViewGeometry" : "0 0 960 540",
"startMonitorFromAppLaunch" : false
}
}

0 comments on commit c914768

Please sign in to comment.