oculus1

view src/vr.cc @ 4:bb81990dc7c8

getting sensor input
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:47:12 +0300
parents b069a5c27388
children 681046a82ed2
line source
1 #include <stdio.h>
2 #include "vr.h"
3 #include "vr_impl.h"
5 static bool init_ovr();
7 VRContext vr_ctx;
9 extern "C" int vr_init(enum vr_init_mode mode)
10 {
11 if(!init_ovr()) {
12 return -1;
13 }
14 return 0;
15 }
17 extern "C" void vr_shutdown(void)
18 {
19 //System::Destroy();
20 }
22 static bool init_ovr()
23 {
24 // initialize Oculus SDK
25 System::Init(Log::ConfigureDefaultLog(LogMask_All));
26 if(!(vr_ctx.ovr_devman = DeviceManager::Create())) {
27 fprintf(stderr, "failed to create OVR device manager\n");
28 return false;
29 }
31 // create the display device
32 HMDInfo info;
33 if(!(vr_ctx.ovr_hmd_dev = vr_ctx.ovr_devman->EnumerateDevices<HMDDevice>().CreateDevice())) {
34 fprintf(stderr, "no oculus rift devices found\n");
35 } else {
36 if(vr_ctx.ovr_hmd_dev->GetDeviceInfo(&info)) {
37 printf("oculus device info:\n");
38 printf(" name: %s\n", info.DisplayDeviceName);
39 printf(" ipd: %f\n", info.InterpupillaryDistance);
40 printf(" distortion: %f %f %f %f\n", info.DistortionK[0],
41 info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]);
42 }
44 // calculate and store viewing parameters
45 float vhalfsz = info.VScreenSize * 0.5;
46 vr_ctx.info.fov = 2.0 * atan(vhalfsz / info.EyeToScreenDistance);
48 vr_ctx.info.width = info.HResolution;
49 vr_ctx.info.height = info.VResolution;
50 vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height;
52 vr_ctx.info.ipd = info.InterpupillaryDistance;
53 for(int i=0; i<4; i++) {
54 vr_ctx.info.distort[i] = info.DistortionK[i];
55 }
56 }
58 // get the sensor device
59 if(vr_ctx.ovr_hmd_dev) {
60 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_hmd_dev->GetSensor())) {
61 fprintf(stderr, "failed to get oculus sensor device\n");
62 }
63 } else {
64 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_devman->EnumerateDevices<SensorDevice>().CreateDevice())) {
65 fprintf(stderr, "failed to get oculus sensor device\n");
66 }
67 }
69 if(vr_ctx.ovr_sensor_dev) {
70 SensorInfo sinfo;
71 if(vr_ctx.ovr_sensor_dev->GetDeviceInfo(&sinfo)) {
72 printf("oculus sensor device info:\n");
73 printf(" name: %s\n", sinfo.ProductName);
74 }
76 vr_ctx.ovr_sfusion.AttachToSensor(vr_ctx.ovr_sensor_dev);
77 }
78 return true;
79 }
81 extern "C" int vr_get_width(void)
82 {
83 return vr_ctx.info.width;
84 }
86 extern "C" int vr_get_height(void)
87 {
88 return vr_ctx.info.height;
89 }
91 extern "C" float vr_get_fov(void)
92 {
93 return vr_ctx.info.fov;
94 }
96 extern "C" float vr_get_aspect(void)
97 {
98 return vr_ctx.info.aspect;
99 }
101 extern "C" void vr_set_eyedist(float ipd)
102 {
103 vr_ctx.info.ipd = ipd;
104 }
106 extern "C" float vr_get_eyedist(void)
107 {
108 return vr_ctx.info.ipd;
109 }
111 extern "C" void vr_set_distort(const float *coef)
112 {
113 memcpy(vr_ctx.info.distort, coef, sizeof vr_ctx.info.distort);
114 }
116 extern "C" void vr_get_distort(float *coef)
117 {
118 memcpy(coef, vr_ctx.info.distort, sizeof vr_ctx.info.distort);
119 }
121 extern "C" void vr_set_prediction_sec(float dt)
122 {
123 vr_ctx.ovr_sfusion.SetPrediction(dt);
124 }
126 extern "C" float vr_get_prediction_sec(void)
127 {
128 return vr_ctx.ovr_sfusion.GetPredictionDelta();
129 }
131 extern "C" void vr_get_translation(float *offs)
132 {
133 // current oculus devkit doesn't do translation
134 offs[0] = offs[1] = offs[2] = 0.0f;
135 }
137 extern "C" void vr_get_rotation(float *quat)
138 {
139 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
140 quat[0] = oq.x;
141 quat[1] = oq.y;
142 quat[2] = oq.z;
143 quat[3] = oq.w;
144 }
146 extern "C" void vr_get_rotation_euler(float *euler)
147 {
148 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
149 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
150 }