oculus1

view src/vr.cc @ 5:681046a82ed2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 05:01:38 +0300
parents bb81990dc7c8
children 3265970a7315
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 LogMaskConstants log_level;
25 // initialize Oculus SDK
26 const char *logenv = getenv("VR_LOGLEVEL");
27 if(logenv) {
28 switch(atoi(logenv)) {
29 case 0:
30 log_level = LogMask_None;
31 break;
32 case 1:
33 log_level = LogMask_Regular;
34 break;
35 case 2:
36 default:
37 log_level = LogMask_All;
38 break;
39 }
40 }
42 System::Init(Log::ConfigureDefaultLog(log_level));
43 if(!(vr_ctx.ovr_devman = DeviceManager::Create())) {
44 fprintf(stderr, "failed to create OVR device manager\n");
45 return false;
46 }
48 // create the display device
49 HMDInfo info;
50 if(!(vr_ctx.ovr_hmd_dev = vr_ctx.ovr_devman->EnumerateDevices<HMDDevice>().CreateDevice())) {
51 fprintf(stderr, "no oculus rift devices found\n");
52 } else {
53 if(vr_ctx.ovr_hmd_dev->GetDeviceInfo(&info)) {
54 printf("oculus device info:\n");
55 printf(" name: %s\n", info.DisplayDeviceName);
56 printf(" ipd: %f\n", info.InterpupillaryDistance);
57 printf(" distortion: %f %f %f %f\n", info.DistortionK[0],
58 info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]);
59 }
61 // calculate and store viewing parameters
62 float vhalfsz = info.VScreenSize * 0.5;
63 vr_ctx.info.fov = 2.0 * atan(vhalfsz / info.EyeToScreenDistance);
65 vr_ctx.info.width = info.HResolution;
66 vr_ctx.info.height = info.VResolution;
67 vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height;
69 vr_ctx.info.ipd = info.InterpupillaryDistance;
70 for(int i=0; i<4; i++) {
71 vr_ctx.info.distort[i] = info.DistortionK[i];
72 }
73 }
75 // get the sensor device
76 if(vr_ctx.ovr_hmd_dev) {
77 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_hmd_dev->GetSensor())) {
78 fprintf(stderr, "failed to get oculus sensor device\n");
79 }
80 } else {
81 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_devman->EnumerateDevices<SensorDevice>().CreateDevice())) {
82 fprintf(stderr, "failed to get oculus sensor device\n");
83 }
84 }
86 if(vr_ctx.ovr_sensor_dev) {
87 SensorInfo sinfo;
88 if(vr_ctx.ovr_sensor_dev->GetDeviceInfo(&sinfo)) {
89 printf("oculus sensor device info:\n");
90 printf(" name: %s\n", sinfo.ProductName);
91 }
93 vr_ctx.ovr_sfusion.AttachToSensor(vr_ctx.ovr_sensor_dev);
94 }
95 return true;
96 }
98 extern "C" int vr_get_width(void)
99 {
100 return vr_ctx.info.width;
101 }
103 extern "C" int vr_get_height(void)
104 {
105 return vr_ctx.info.height;
106 }
108 extern "C" float vr_get_fov(void)
109 {
110 return vr_ctx.info.fov;
111 }
113 extern "C" float vr_get_aspect(void)
114 {
115 return vr_ctx.info.aspect;
116 }
118 extern "C" void vr_set_eyedist(float ipd)
119 {
120 vr_ctx.info.ipd = ipd;
121 }
123 extern "C" float vr_get_eyedist(void)
124 {
125 return vr_ctx.info.ipd;
126 }
128 extern "C" void vr_set_distort(const float *coef)
129 {
130 memcpy(vr_ctx.info.distort, coef, sizeof vr_ctx.info.distort);
131 }
133 extern "C" void vr_get_distort(float *coef)
134 {
135 memcpy(coef, vr_ctx.info.distort, sizeof vr_ctx.info.distort);
136 }
138 extern "C" void vr_set_prediction_sec(float dt)
139 {
140 vr_ctx.ovr_sfusion.SetPrediction(dt);
141 }
143 extern "C" float vr_get_prediction_sec(void)
144 {
145 return vr_ctx.ovr_sfusion.GetPredictionDelta();
146 }
148 extern "C" void vr_get_translation(float *offs)
149 {
150 // current oculus devkit doesn't do translation
151 offs[0] = offs[1] = offs[2] = 0.0f;
152 }
154 extern "C" void vr_get_rotation(float *quat)
155 {
156 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
157 quat[0] = oq.x;
158 quat[1] = oq.y;
159 quat[2] = oq.z;
160 quat[3] = oq.w;
161 }
163 extern "C" void vr_get_rotation_euler(float *euler)
164 {
165 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
166 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
167 }