Skip to content

Commit cf39cdc

Browse files
committed
Added distortion
1 parent 6ae7ac6 commit cf39cdc

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

OpenVR/samples/driver_sample/driver_sample.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include <vector>
66
#include <thread>
77
#include <chrono>
8-
#include <Windows.h>
8+
99
#include <atlbase.h>
10+
//#include <Windows.h>
1011

1112
using namespace vr;
1213

13-
1414
#if defined(_WIN32)
1515
#define HMD_DLL_EXPORT extern "C" __declspec( dllexport )
1616
#define HMD_DLL_IMPORT extern "C" __declspec( dllimport )
@@ -60,6 +60,10 @@ static const char * const k_pch_Sample_RenderWidth_Int32 = "renderWidth";
6060
static const char * const k_pch_Sample_RenderHeight_Int32 = "renderHeight";
6161
static const char * const k_pch_Sample_SecondsFromVsyncToPhotons_Float = "secondsFromVsyncToPhotons";
6262
static const char * const k_pch_Sample_DisplayFrequency_Float = "displayFrequency";
63+
static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
64+
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
65+
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
66+
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";
6367

6468
typedef struct _HMDData
6569
{
@@ -191,6 +195,11 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
191195
m_flSecondsFromVsyncToPhotons = vr::VRSettings()->GetFloat( k_pch_Sample_Section, k_pch_Sample_SecondsFromVsyncToPhotons_Float );
192196
m_flDisplayFrequency = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DisplayFrequency_Float);
193197

198+
m_fDistortionK1 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK1_Float);
199+
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
200+
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
201+
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);
202+
194203
//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
195204
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
196205
//DriverLog( "driver_null: Window: %d %d %d %d\n", m_nWindowX, m_nWindowY, m_nWindowWidth, m_nWindowHeight );
@@ -415,12 +424,27 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
415424
virtual DistortionCoordinates_t ComputeDistortion( EVREye eEye, float fU, float fV )
416425
{
417426
DistortionCoordinates_t coordinates;
418-
coordinates.rfBlue[0] = fU;
419-
coordinates.rfBlue[1] = fV;
420-
coordinates.rfGreen[0] = fU;
421-
coordinates.rfGreen[1] = fV;
422-
coordinates.rfRed[0] = fU;
423-
coordinates.rfRed[1] = fV;
427+
428+
//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
429+
float hX;
430+
float hY;
431+
double rr;
432+
double r2;
433+
double theta;
434+
435+
rr = sqrt((fU - 0.5f)*(fU - 0.5f) + (fV - 0.5f)*(fV - 0.5f));
436+
r2 = rr * (1 + m_fDistortionK1*(rr*rr) + m_fDistortionK2*(rr*rr*rr*rr));
437+
theta = atan2(fU - 0.5f, fV - 0.5f);
438+
hX = sin(theta)*r2*m_fZoomWidth;
439+
hY = cos(theta)*r2*m_fZoomHeight;
440+
441+
coordinates.rfBlue[0] = hX + 0.5f;
442+
coordinates.rfBlue[1] = hY + 0.5f;
443+
coordinates.rfGreen[0] = hX + 0.5f;
444+
coordinates.rfGreen[1] = hY + 0.5f;
445+
coordinates.rfRed[0] = hX + 0.5f;
446+
coordinates.rfRed[1] = hY + 0.5f;
447+
424448
return coordinates;
425449
}
426450

@@ -494,6 +518,10 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
494518
float m_flSecondsFromVsyncToPhotons;
495519
float m_flDisplayFrequency;
496520
float m_flIPD;
521+
float m_fDistortionK1;
522+
float m_fDistortionK2;
523+
float m_fZoomWidth;
524+
float m_fZoomHeight;
497525
};
498526

499527
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)