Skip to content

Commit c914768

Browse files
committed
Added distortion
1 parent a1bece2 commit c914768

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

OpenVR/FreeTrack/samples/driver_sample/driver_sample.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
//=============== Changed by r57zone (https://github.com/r57zone) ===============
33

44
#include <openvr_driver.h>
5+
//#include "driverlog.h"
56

67
#include <vector>
78
#include <thread>
89
#include <chrono>
10+
11+
#include <atlbase.h>
912
//#include <Windows.h>
1013
//#include "Shlwapi.h"
11-
#include <atlbase.h>
1214

1315
using namespace vr;
1416

@@ -62,6 +64,10 @@ static const char * const k_pch_Sample_RenderWidth_Int32 = "renderWidth";
6264
static const char * const k_pch_Sample_RenderHeight_Int32 = "renderHeight";
6365
static const char * const k_pch_Sample_SecondsFromVsyncToPhotons_Float = "secondsFromVsyncToPhotons";
6466
static const char * const k_pch_Sample_DisplayFrequency_Float = "displayFrequency";
67+
static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
68+
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
69+
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
70+
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";
6571

6672
typedef struct _FreeTrack
6773
{
@@ -207,6 +213,11 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
207213
m_flSecondsFromVsyncToPhotons = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_SecondsFromVsyncToPhotons_Float);
208214
m_flDisplayFrequency = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DisplayFrequency_Float);
209215

216+
m_fDistortionK1 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK1_Float);
217+
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
218+
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
219+
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);
220+
210221
//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
211222
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
212223
//DriverLog( "driver_null: Window: %d %d %d %d\n", m_nWindowX, m_nWindowY, m_nWindowWidth, m_nWindowHeight );
@@ -394,12 +405,27 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
394405
virtual DistortionCoordinates_t ComputeDistortion( EVREye eEye, float fU, float fV )
395406
{
396407
DistortionCoordinates_t coordinates;
397-
coordinates.rfBlue[0] = fU;
398-
coordinates.rfBlue[1] = fV;
399-
coordinates.rfGreen[0] = fU;
400-
coordinates.rfGreen[1] = fV;
401-
coordinates.rfRed[0] = fU;
402-
coordinates.rfRed[1] = fV;
408+
409+
//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
410+
float hX;
411+
float hY;
412+
double rr;
413+
double r2;
414+
double theta;
415+
416+
rr = sqrt((fU - 0.5f)*(fU - 0.5f) + (fV - 0.5f)*(fV - 0.5f));
417+
r2 = rr * (1 + m_fDistortionK1*(rr*rr) + m_fDistortionK2*(rr*rr*rr*rr));
418+
theta = atan2(fU - 0.5f, fV - 0.5f);
419+
hX = sin(theta)*r2*m_fZoomWidth;
420+
hY = cos(theta)*r2*m_fZoomHeight;
421+
422+
coordinates.rfBlue[0] = hX + 0.5f;
423+
coordinates.rfBlue[1] = hY + 0.5f;
424+
coordinates.rfGreen[0] = hX + 0.5f;
425+
coordinates.rfGreen[1] = hY + 0.5f;
426+
coordinates.rfRed[0] = hX + 0.5f;
427+
coordinates.rfRed[1] = hY + 0.5f;
428+
403429
return coordinates;
404430
}
405431

@@ -468,6 +494,10 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
468494
float m_flSecondsFromVsyncToPhotons;
469495
float m_flDisplayFrequency;
470496
float m_flIPD;
497+
float m_fDistortionK1;
498+
float m_fDistortionK2;
499+
float m_fZoomWidth;
500+
float m_fZoomHeight;
471501
};
472502

473503
//-----------------------------------------------------------------------------

OpenVR/UDP/samples/driver_sample/driver_sample.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
//=============== Changed by r57zone (https://github.com/r57zone) ===============
33

44
#include <openvr_driver.h>
5+
//#include "driverlog.h"
56

67
#include <vector>
78
#include <thread>
89
#include <chrono>
910

10-
#include <stdio.h>
1111
#include <winsock2.h>
1212
#pragma comment (lib, "WSock32.Lib")
13+
//#include <Windows.h>
1314

14-
#if defined( _WINDOWS )
15-
#include <windows.h>
16-
#endif
1715

1816
using namespace vr;
1917

@@ -67,6 +65,10 @@ static const char * const k_pch_Sample_RenderWidth_Int32 = "renderWidth";
6765
static const char * const k_pch_Sample_RenderHeight_Int32 = "renderHeight";
6866
static const char * const k_pch_Sample_SecondsFromVsyncToPhotons_Float = "secondsFromVsyncToPhotons";
6967
static const char * const k_pch_Sample_DisplayFrequency_Float = "displayFrequency";
68+
static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
69+
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
70+
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
71+
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";
7072

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

248+
m_fDistortionK1 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK1_Float);
249+
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
250+
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
251+
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);
252+
246253
//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
247254
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
248255
//DriverLog( "driver_null: Window: %d %d %d %d\n", m_nWindowX, m_nWindowY, m_nWindowWidth, m_nWindowHeight );
@@ -445,12 +452,28 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
445452
virtual DistortionCoordinates_t ComputeDistortion( EVREye eEye, float fU, float fV )
446453
{
447454
DistortionCoordinates_t coordinates;
448-
coordinates.rfBlue[0] = fU;
449-
coordinates.rfBlue[1] = fV;
450-
coordinates.rfGreen[0] = fU;
451-
coordinates.rfGreen[1] = fV;
452-
coordinates.rfRed[0] = fU;
453-
coordinates.rfRed[1] = fV;
455+
456+
//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
457+
float hX;
458+
float hY;
459+
double rr;
460+
double r2;
461+
double theta;
462+
463+
rr = sqrt((fU - 0.5f)*(fU - 0.5f) + (fV - 0.5f)*(fV - 0.5f));
464+
r2 = rr * (1 + m_fDistortionK1*(rr*rr) + m_fDistortionK2*(rr*rr*rr*rr));
465+
theta = atan2(fU - 0.5f, fV - 0.5f);
466+
hX = sin(theta)*r2*m_fZoomWidth;
467+
hY = cos(theta)*r2*m_fZoomHeight;
468+
469+
coordinates.rfBlue[0] = hX + 0.5f;
470+
coordinates.rfBlue[1] = hY + 0.5f;
471+
coordinates.rfGreen[0] = hX + 0.5f;
472+
coordinates.rfGreen[1] = hY + 0.5f;
473+
coordinates.rfRed[0] = hX + 0.5f;
474+
coordinates.rfRed[1] = hY + 0.5f;
475+
476+
454477
return coordinates;
455478
}
456479

@@ -517,6 +540,10 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
517540
float m_flSecondsFromVsyncToPhotons;
518541
float m_flDisplayFrequency;
519542
float m_flIPD;
543+
float m_fDistortionK1;
544+
float m_fDistortionK2;
545+
float m_fZoomWidth;
546+
float m_fZoomHeight;
520547
};
521548

522549
//-----------------------------------------------------------------------------

SteamVR Settings/OpenVR/steamvr.vrsettings

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@
55
"driver_null" : {
66
"enable" : true,
77
"id" : "Null Driver",
8-
"secondsFromVsyncToPhotons" : 0.10000000149011612,
8+
"secondsFromVsyncToPhotons" : 0.10000000149011612,
99
"serialNumber" : "Null 4711",
1010
"renderWidth" : <RENDERWIDTH>,
1111
"renderHeight" : <RENDERHEIGHT>,
1212
"windowWidth" : <WINDOWWIDTH>,
1313
"windowHeight" : <WINDOWHEIGHT>,
1414
"windowX" : <WINDOWX>,
15-
"windowY" : <WINDOWY>
15+
"windowY" : <WINDOWY>,
16+
"DistortionK1" : 0.91,
17+
"DistortionK2" : 0.93,
18+
"ZoomWidth" : 0.8,
19+
"ZoomHeight" : 0.8
1620
},
1721
"steamvr" : {
1822
"activateMultipleDrivers" : true,
1923
"directMode" : false,
2024
"enableHomeApp" : false,
2125
"forcedDriver" : "null",
26+
"mirrorViewGeometry" : "0 0 960 540",
2227
"startMonitorFromAppLaunch" : false
2328
}
2429
}

0 commit comments

Comments
 (0)