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