oculus1

view src/vr.cc @ 3:b069a5c27388

added a couple more stuff, fixed all the LibOVR line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:10:05 +0300
parents e2f9e4603129
children bb81990dc7c8
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();
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 if(!(vr_ctx.ovr_hmd_dev = vr_ctx.ovr_devman->EnumerateDevices<HMDDevice>().CreateDevice())) {
33 fprintf(stderr, "no oculus rift devices found\n");
34 return false;
35 }
37 HMDInfo info;
38 if(vr_ctx.ovr_hmd_dev->GetDeviceInfo(&info)) {
39 printf("oculus device info:\n");
40 printf(" name: %s\n", info.DisplayDeviceName);
41 printf(" ipd: %f\n", info.InterpupillaryDistance);
42 printf(" distortion: %f %f %f %f\n", info.DistortionK[0],
43 info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]);
44 }
46 // get the sensor device
47 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_hmd_dev->GetSensor())) {
48 fprintf(stderr, "failed to get oculus sensor device\n");
49 return false;
50 }
52 SensorInfo sinfo;
53 if(vr_ctx.ovr_sensor_dev->GetDeviceInfo(&sinfo)) {
54 printf("oculus sensor device info:\n");
55 printf(" name: %s\n", sinfo.ProductName);
56 }
58 vr_ctx.ovr_sfusion.AttachToSensor(vr_ctx.ovr_sensor_dev);
60 // calculate and store viewing parameters
61 float vhalfsz = info.VScreenSize * 0.5;
62 vr_ctx.info.fov = 2.0 * atan(vhalfsz / info.EyeToScreenDistance);
64 vr_ctx.info.width = info.HResolution;
65 vr_ctx.info.height = info.VResolution;
66 vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height;
68 vr_ctx.info.ipd = info.InterpupillaryDistance;
69 for(int i=0; i<4; i++) {
70 vr_ctx.info.distort[i] = info.DistortionK[i];
71 }
73 return true;
74 }
76 extern "C" int vr_get_width(void)
77 {
78 return vr_ctx.info.width;
79 }
81 extern "C" int vr_get_height(void)
82 {
83 return vr_ctx.info.height;
84 }
86 extern "C" float vr_get_fov(void)
87 {
88 return vr_ctx.info.fov;
89 }
91 extern "C" float vr_get_aspect(void)
92 {
93 return vr_ctx.info.aspect;
94 }
96 extern "C" void vr_set_eyedist(float ipd)
97 {
98 vr_ctx.info.ipd = ipd;
99 }
101 extern "C" float vr_get_eyedist(void)
102 {
103 return vr_ctx.info.ipd;
104 }
106 extern "C" void vr_set_distort(const float *coef)
107 {
108 memcpy(vr_ctx.info.distort, coef, sizeof vr_ctx.info.distort);
109 }
111 extern "C" void vr_get_distort(float *coef)
112 {
113 memcpy(coef, vr_ctx.info.distort, sizeof vr_ctx.info.distort);
114 }
116 extern "C" void vr_set_prediction_sec(float dt)
117 {
118 vr_ctx.ovr_sfusion.SetPrediction(dt);
119 }
121 extern "C" float vr_get_prediction_sec(void)
122 {
123 return vr_ctx.ovr_sfusion.GetPredictionDelta();
124 }
126 extern "C" void vr_get_translation(float *offs)
127 {
128 // current oculus devkit doesn't do translation
129 offs[0] = offs[1] = offs[2] = 0.0f;
130 }
132 extern "C" void vr_get_rotation(float *quat)
133 {
134 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
135 quat[0] = oq.x;
136 quat[1] = oq.y;
137 quat[2] = oq.z;
138 quat[3] = oq.w;
139 }
141 extern "C" void vr_get_rotation_euler(float *euler)
142 {
143 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
144 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
145 }