Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Ovr.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <Ovr.h>
6 
10 static char * mockData = NULL;
11 static int MOCK_SIZE = 0;
12 
14 {
15  if(DEBUG)
16  std::cout << "Initializing Ovr" << std::endl;
17 
18  this->ovrInitiated = false;
19  this->mock = false;
20 }
21 
23 {
24  if(DEBUG)
25  std::cout << "Destroying Ovr" << std::endl;
26 
27  if(ovrInitiated)
28  {
29  ovrHmd_Destroy(this->hmd);
30  ovr_Shutdown();
31  }
32 
33  if(mock)
34  {
35  delete[] mockData;
36  }
37 }
38 
39 bool Ovr::init()
40 {
41  // Initializes LibOVR.
43 
44  // Creates a OVR device
45  this->hmd = ovrHmd_Create(0);
46  if (this->hmd)
47  {
48  // Get more details about the HMD
49  ovrHmd_GetDesc(this->hmd, &this->hmdDesc);
50  this->ovrInitiated = true;
51  std::cout << "OVR detected" << std::endl;
52  return true;
53  }
54 
55  std::cout << "No OVR detected!!!" << std::endl;
56  std::cout << "Creating a Debug version of DK1" << std::endl;
58  mock = true;
59 
60  if(!this->hmd)
61  {
62  std::cout << "Something really wrong just happened!" << std::endl;
63  exit(-1);
64  }
65 
66  ovrHmd_GetDesc(this->hmd, &this->hmdDesc);
67  this->ovrInitiated = true;
68 
69  // If we got here, let's build mockData, dataset to be used as Ovr readings
70  std::ifstream file ("files/ovr_mock.txt", std::ios::in | std::ios::binary | std::ios::ate);
71  if(file.is_open())
72  {
73  std::streampos size = file.tellg();
74  mockData = new char[size];
75  MOCK_SIZE = size;
76  file.seekg(0, std::ios::beg);
77  file.read(mockData, size);
78  file.close();
79  std::cout << "Ovr mockData loaded: " << size << " bytes long" << std::endl;
80  }
81  else
82  {
83  std::cout << "Failed to load Ovr mockData" << std::endl;
84  mockData = new char[1];
85  *mockData = '0';
86  }
87 
88  return this->ovrInitiated;
89 }
90 
92 {
93  // Start the sensor which provides the Rift’s pose and motion.
97 
98  if(!success)
99  {
100  std::cout << "The required capabilities are not present!" << std::endl;
101  exit(-2);
102  }
103  return false;
104 }
105 
106 void Ovr::getXYZW(double * x, double * y, double * z, double * w)
107 {
108  static int mockIndex = 0;
109  // If mock is true, means it's time to mock data
110  if(mock)
111  {
112  if(!mockData)
113  {
114  std::cout << "No mockData" << std::endl;
115  *x = 0.0;
116  *y = 0.0;
117  *z = 0.0;
118  *w = 0.0;
119  return;
120  }
121 
122  if(mockIndex >= MOCK_SIZE)
123  mockIndex = 0;
124 
125  sscanf(&mockData[mockIndex], "%lf %lf %lf %lf", x, y, z, w);
126  //std::cout << "Reading mockData" << std::endl;
127  while(mockData[mockIndex] != '\n' && mockIndex++ < MOCK_SIZE); // skip line break
128  mockIndex++;
129  return;
130  }
131  // Query the HMD for the sensor state at a given time. "0.0" means "most recent time".
132  ovrSensorState ss = ovrHmd_GetSensorState(this->hmd, 0.0);
133 
134  int attempts = 10000;
135 
137  attempts--;
138 
139  if(attempts)
140  {
141  ovrPosef pose = ss.Predicted.Pose;
142 
143  ovrQuatf orientation = pose.Orientation;
144  ovrVector3f position = pose.Position;
145 
146  *x = (double)orientation.x;
147  *y = (double)orientation.y;
148  *z = (double)orientation.z;
149  *w = (double)orientation.w;
150 
151  //std::cout << "x = " << x << ", y = " << y << ", z = " << z << ", w = " << w << std::endl;
152 
153  return;
154  }
155 
156  *x = 0.0;
157  *y = 0.0;
158  *z = 0.0;
159  *w = 0.0;
160 }
161 
162 std::ostream& operator<<(std::ostream& os, const Ovr& sxovr)
163 {
164  os << "***** Ovr ******" << std::endl;
165  os << "\tovrInitiated: " << (sxovr.ovrInitiated ? "true" : "false") << std::endl;
166 
167  if(sxovr.ovrInitiated)
168  {
169  os << "\tType: " << sxovr.hmdDesc.Type << " ";
170  switch(sxovr.hmdDesc.Type)
171  {
172  case ovrHmd_None: os << "ovrHmd_None" ; break;
173  case ovrHmd_DK1: os << "ovrHmd_DK1" ; break;
174  case ovrHmd_DKHD: os << "ovrHmd_DKHD" ; break;
175  case ovrHmd_DK2: os << "ovrHmd_DK2" ; break;
176  case ovrHmd_Other: os << "ovrHmd_Other"; break;
177  case ovrHmd_CrystalCoveProto: os << "ovrHmd_CrystalCoveProto"; break;
178  }
179  os << std::endl;
180  }
181 
182  os << "\tProduct Name: " << sxovr.hmdDesc.ProductName << std::endl;
183  os << "\tManufacturer: " << sxovr.hmdDesc.Manufacturer << std::endl;
184  os << "\tHmdCaps: " << sxovr.hmdDesc.HmdCaps << std::endl;
185  os << "\tSensorCaps: " << sxovr.hmdDesc.SensorCaps << std::endl;
186  os << "\tDistortionCaps: " << sxovr.hmdDesc.DistortionCaps << std::endl;
187  os << "\tResolution: h=" << sxovr.hmdDesc.Resolution.w << ", h=" << sxovr.hmdDesc.Resolution.h << std::endl;
188  os << "\tWindowsPos: x=" << sxovr.hmdDesc.WindowsPos.x << ", y=" << sxovr.hmdDesc.WindowsPos.x << std::endl;
189 
190  os << "\tDefaultEyeFov: " << std::endl <<
191  "\t\t[left]UpTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Left].UpTan << std::endl <<
192  "\t\t[left]DownTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Left].DownTan << std::endl <<
193  "\t\t[left]LeftTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Left].LeftTan << std::endl <<
194  "\t\t[left]RightTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Left].RightTan << std::endl <<
195  "\t\t[right]UpTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Right].UpTan << std::endl <<
196  "\t\t[right]DownTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Right].DownTan << std::endl <<
197  "\t\t[right]LeftTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Right].LeftTan << std::endl <<
198  "\t\t[right]RightTan = " << sxovr.hmdDesc.DefaultEyeFov[ovrEye_Right].RightTan << std::endl;
199 
200  os << "\tMaxEyeFov: " << std::endl <<
201  "\t\t[left]UpTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Left].UpTan << std::endl <<
202  "\t\t[left]DownTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Left].DownTan << std::endl <<
203  "\t\t[left]LeftTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Left].LeftTan << std::endl <<
204  "\t\t[left]RightTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Left].RightTan << std::endl <<
205  "\t\t[right]UpTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Right].UpTan << std::endl <<
206  "\t\t[right]DownTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Right].DownTan << std::endl <<
207  "\t\t[right]LeftTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Right].LeftTan << std::endl <<
208  "\t\t[right]RightTan = " << sxovr.hmdDesc.MaxEyeFov[ovrEye_Right].RightTan << std::endl;
209 
210  //os << "\tEyeRenderOrder: " << sxovr.hmdDesc.EyeRenderOrder << std::endl;
211  os << "\tDisplayDeviceName: " << sxovr.hmdDesc.DisplayDeviceName << std::endl;
212  os << "\tDisplayId: " << sxovr.hmdDesc.DisplayId << std::endl;
213 
214  os << "*** END-Ovr ****" << std::endl;
215  return os;
216 }
float UpTan
Definition: OVR_CAPI.h:102
ovrVector3f Position
Definition: OVR_CAPI.h:83
ovrPoseStatef Predicted
Definition: OVR_CAPI.h:257
static int MOCK_SIZE
Definition: Ovr.cpp:11
OVR_EXPORT void ovrHmd_GetDesc(ovrHmd hmd, ovrHmdDesc *desc)
Definition: OVR_CAPI.cpp:396
#define NULL
OVR_EXPORT ovrSensorState ovrHmd_GetSensorState(ovrHmd hmd, double absTime)
Definition: OVR_CAPI.cpp:377
int h
Definition: OVR_CAPI.h:54
bool startSensor()
Definition: Ovr.cpp:91
float w
Definition: OVR_CAPI.h:65
unsigned int StatusFlags
Definition: OVR_CAPI.h:265
OVR_EXPORT void ovrHmd_Destroy(ovrHmd hmd)
Definition: OVR_CAPI.cpp:292
ovrVector2i WindowsPos
Definition: OVR_CAPI.h:208
~Ovr()
Definition: Ovr.cpp:22
unsigned int HmdCaps
Definition: OVR_CAPI.h:199
OVR_EXPORT ovrHmd ovrHmd_Create(int index)
Definition: OVR_CAPI.cpp:268
OVR_EXPORT void ovr_Shutdown()
Definition: OVR_CAPI.cpp:228
OVR_EXPORT ovrBool ovr_Initialize()
Definition: OVR_CAPI.cpp:211
float y
Definition: OVR_CAPI.h:65
static char * mockData
Definition: Ovr.cpp:10
OVR_EXPORT ovrBool ovrHmd_StartSensor(ovrHmd hmd, unsigned int supportedCaps, unsigned int requiredCaps)
Definition: OVR_CAPI.cpp:358
ovrFovPort MaxEyeFov[ovrEye_Count]
Definition: OVR_CAPI.h:212
float LeftTan
Definition: OVR_CAPI.h:104
ovrHmdType Type
Definition: OVR_CAPI.h:192
bool ovrInitiated
Definition: Ovr.h:27
ovrFovPort DefaultEyeFov[ovrEye_Count]
Definition: OVR_CAPI.h:211
bool mock
Definition: Ovr.h:22
float DownTan
Definition: OVR_CAPI.h:103
ovrSizei Resolution
Definition: OVR_CAPI.h:206
ovrHmd hmd
Definition: Ovr.h:32
float x
Definition: OVR_CAPI.h:65
const char * Manufacturer
Definition: OVR_CAPI.h:196
std::ostream & operator<<(std::ostream &os, const Ovr &sxovr)
Definition: Ovr.cpp:162
bool init()
Definition: Ovr.cpp:39
#define DEBUG
Definition: Ovr.h:5
ovrHmdDesc hmdDesc
Definition: Ovr.h:37
unsigned int DistortionCaps
Definition: OVR_CAPI.h:203
float RightTan
Definition: OVR_CAPI.h:105
OVR_EXPORT ovrHmd ovrHmd_CreateDebug(ovrHmdType type)
Definition: OVR_CAPI.cpp:283
Definition: Ovr.h:15
int DisplayId
Definition: OVR_CAPI.h:227
float z
Definition: OVR_CAPI.h:65
void getXYZW(double *x, double *y, double *z, double *w)
Definition: Ovr.cpp:106
const char * ProductName
Definition: OVR_CAPI.h:195
int w
Definition: OVR_CAPI.h:54
ovrPosef Pose
Definition: OVR_CAPI.h:89
unsigned int SensorCaps
Definition: OVR_CAPI.h:201
const char * DisplayDeviceName
Definition: OVR_CAPI.h:225
Ovr()
Definition: Ovr.cpp:13
ovrQuatf Orientation
Definition: OVR_CAPI.h:82