-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathOpenHMD.cpp
143 lines (117 loc) · 3.71 KB
/
OpenHMD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include "OpenHMD.h"
OpenHMD::OpenHMD() {
rotation = std::vector<float>(4);
leftprojectionmatrix = std::vector<float>(16);
rightprojectionmatrix = std::vector<float>(16);
leftviewmatrix = std::vector<float>(16);
rightviewmatrix = std::vector<float>(16);
connect();
}
OpenHMD::~OpenHMD() {
ohmd_ctx_destroy(ctx);
}
void OpenHMD::connect() {
ctx = ohmd_ctx_create();
// Probe for devices
num_devices = ohmd_ctx_probe(ctx);
if(num_devices < 0){
printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx));
return;
}
// Open default device (0)
hmd = ohmd_list_open_device(ctx, 0);
if(!hmd){
printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx));
return;
}
}
void OpenHMD::printDeviceInfo() {
printf("num devices: %d\n\n", num_devices);
// Print device information
for(int i = 0; i < num_devices; i++){
printf("device %d\n", i);
printf(" vendor: %s\n", ohmd_list_gets(ctx, i, OHMD_VENDOR));
printf(" product: %s\n", ohmd_list_gets(ctx, i, OHMD_PRODUCT));
printf(" path: %s\n\n", ohmd_list_gets(ctx, i, OHMD_PATH));
}
// Print hardware information for the opened device
int ivals[2];
ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, ivals);
ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, ivals + 1);
printf("resolution: %i x %i\n", ivals[0], ivals[1]);
print("hsize:", 1, OHMD_SCREEN_HORIZONTAL_SIZE);
print("vsize:", 1, OHMD_SCREEN_VERTICAL_SIZE);
print("lens separation:", 1, OHMD_LENS_HORIZONTAL_SEPARATION);
print("lens vcenter:", 1, OHMD_LENS_VERTICAL_POSITION);
print("left eye fov:", 1, OHMD_LEFT_EYE_FOV);
print("right eye fov:", 1, OHMD_RIGHT_EYE_FOV);
print("left eye aspect:", 1, OHMD_LEFT_EYE_ASPECT_RATIO);
print("right eye aspect:", 1, OHMD_RIGHT_EYE_ASPECT_RATIO);
print("distortion k:", 6, OHMD_DISTORTION_K);
int bcount[1];
ohmd_device_geti(hmd, OHMD_BUTTON_COUNT, bcount);
printf("digital button count: %i\n", bcount[0]);
printf("\n");
}
void OpenHMD::inputLoop() {
// Ask for n rotation quaternions
for(int i = 0; i < 10000; i++){
poll();
printSensors();
}
}
void OpenHMD::printSensors() {
printf("rotation quat:");
for(int i = 0; i < 4; i++)
printf("%f ", rotation[i]);
printf("\n");
}
void OpenHMD::poll() {
float rot[4];
float matrix[16];
ohmd_ctx_update(ctx);
//Rotation Quaternion
ohmd_device_getf(hmd, OHMD_ROTATION_QUAT, rot);
for(int i = 0; i < 4; i++)
rotation[i] = rot[i];
//Left Projection Matrix
ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_PROJECTION_MATRIX, matrix);
for(int i = 0; i < 16; i++)
leftprojectionmatrix[i] = matrix[i];
//Left Modelview Matrix
ohmd_device_getf(hmd, OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX, matrix);
for(int i = 0; i < 16; i++)
leftviewmatrix[i] = matrix[i];
//Right Projection Matrix
ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_PROJECTION_MATRIX, matrix);
for(int i = 0; i < 16; i++)
rightprojectionmatrix[i] = matrix[i];
//Right Modelview Matrix
ohmd_device_getf(hmd, OHMD_RIGHT_EYE_GL_MODELVIEW_MATRIX, matrix);
for(int i = 0; i < 16; i++)
rightviewmatrix[i] = matrix[i];
sleep(.01);
}
void OpenHMD::sleep(double seconds) {
struct timespec sleepfor;
sleepfor.tv_sec = (time_t)seconds;
sleepfor.tv_nsec = (long)((seconds - sleepfor.tv_sec) * 1000000000.0);
nanosleep(&sleepfor, NULL);
}
// gets float values from the device and prints them
void OpenHMD::print(std::string name, int len, ohmd_float_value val) {
float f[len];
ohmd_device_getf(hmd, val, f);
printf("%-20s", name.c_str());
for(int i = 0; i < len; i++)
printf("%f ", f[i]);
printf("\n");
}
void OpenHMD::reset(){
ohmd_ctx_destroy(ctx);
sleep(.1);
connect();
}