libgoatvr

view src/vr_libovr.c @ 26:23a654109494

Merge
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Mar 2015 23:32:22 +0200
parents 7f9cc8a4d3a5
children 5136dfcea7b1
line source
1 #ifdef WIN32
2 #define OVR_OS_WIN32
3 #elif defined(__APPLE__)
4 #define OVR_OS_MAC
5 #else
6 #define OVR_OS_LINUX
7 #endif
9 #include "vr_impl.h"
11 #ifdef USE_LIBOVR
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <assert.h>
16 #include "opt.h"
18 #include <OVR_CAPI.h>
19 #include <OVR_CAPI_GL.h>
21 #ifdef OVR_OS_LINUX
22 #include <GL/glx.h>
23 #endif
25 /* undef this if you want the retarded health and safety warning screen */
26 #define DISABLE_RETARDED_HEALTH_WARNING
28 /* just dropping the prototype here to avoid including CAPI_HSWDisplay.h */
29 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enabled);
31 static ovrHmd hmd;
32 static void *optdb;
33 static ovrEyeRenderDesc eye_render_desc[2];
34 static ovrSizei eye_res[2];
35 static ovrGLTexture eye_tex[2];
36 static ovrFovPort eye_fov[2];
37 static ovrPosef pose[2] = {
38 { {0, 0, 0, 1}, {0, 0, 0} },
39 { {0, 0, 0, 1}, {0, 0, 0} }
40 };
41 static int deferred_init_done;
43 static int inside_begin_end;
45 static int init(void)
46 {
47 int i, num_hmds;
48 int use_fake = 0;
49 ovrTrackingCaps tracking;
51 if(!ovr_Initialize()) {
52 return -1;
53 }
54 printf("initialized LibOVR %s\n", ovr_GetVersionString());
56 if(!(num_hmds = ovrHmd_Detect())) {
57 if(getenv("VR_LIBOVR_FAKE")) {
58 use_fake = 1;
59 num_hmds = 1;
60 } else {
61 ovr_Shutdown();
62 return -1;
63 }
64 }
65 printf("%d Oculus HMD(s) found\n", num_hmds);
67 hmd = 0;
68 for(i=0; i<num_hmds; i++) {
69 ovrHmd h = use_fake ? ovrHmd_CreateDebug(ovrHmd_DK2) : ovrHmd_Create(i);
70 if(!h) {
71 break;
72 }
73 printf(" [%d]: %s - %s\n", i, h->Manufacturer, h->ProductName);
75 if(!hmd) {
76 hmd = h;
77 } else {
78 ovrHmd_Destroy(h);
79 }
80 }
82 if(!hmd) {
83 fprintf(stderr, "failed to initialize any Oculus HMDs\n");
84 return -1;
85 }
87 tracking = ovrTrackingCap_Orientation | ovrTrackingCap_Position |
88 ovrTrackingCap_MagYawCorrection;
89 ovrHmd_ConfigureTracking(hmd, tracking, 0);
91 eye_fov[0] = hmd->DefaultEyeFov[0];
92 eye_fov[1] = hmd->DefaultEyeFov[1];
94 eye_res[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, eye_fov[0], 1.0);
95 eye_res[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, eye_fov[1], 1.0);
97 /* create the options database */
98 if((optdb = create_options())) {
99 set_option_int(optdb, VR_DISPLAY_WIDTH, hmd->Resolution.w);
100 set_option_int(optdb, VR_DISPLAY_HEIGHT, hmd->Resolution.h);
101 set_option_int(optdb, VR_LEYE_XRES, eye_res[0].w);
102 set_option_int(optdb, VR_LEYE_YRES, eye_res[0].h);
103 set_option_int(optdb, VR_REYE_XRES, eye_res[1].w);
104 set_option_int(optdb, VR_REYE_YRES, eye_res[1].h);
105 set_option_float(optdb, VR_RENDER_RES_SCALE, 1.0);
106 set_option_float(optdb, VR_EYE_HEIGHT, ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, OVR_DEFAULT_EYE_HEIGHT));
107 set_option_float(optdb, VR_IPD, ovrHmd_GetFloat(hmd, OVR_KEY_IPD, OVR_DEFAULT_IPD));
108 set_option_int(optdb, VR_WIN_XOFFS, hmd->WindowsPos.x);
109 set_option_int(optdb, VR_WIN_YOFFS, hmd->WindowsPos.y);
110 }
112 deferred_init_done = 0;
113 return 0;
114 }
116 static void deferred_init(void)
117 {
118 union ovrGLConfig glcfg;
119 unsigned int dcaps;
120 void *win = 0;
121 float leye_offs[3], reye_offs[3];
123 deferred_init_done = 1;
125 memset(&glcfg, 0, sizeof glcfg);
126 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
127 glcfg.OGL.Header.BackBufferSize = hmd->Resolution;
128 glcfg.OGL.Header.Multisample = 1;
129 #ifdef WIN32
130 win = GetActiveWindow();
131 glcfg.OGL.Window = win;
132 glcfg.OGL.DC = wglGetCurrentDC();
133 #else
134 glcfg.OGL.Disp = glXGetCurrentDisplay();
135 win = (void*)glXGetCurrentDrawable();
137 /* on linux the Oculus SDK docs are instructing users to leave the DK2 screen in
138 * portrait mode. So we'll have to flip width and height
139 */
140 glcfg.OGL.Header.BackBufferSize.w = hmd->Resolution.h;
141 glcfg.OGL.Header.BackBufferSize.h = hmd->Resolution.w;
142 #endif
144 if(!(hmd->HmdCaps & ovrHmdCap_ExtendDesktop)) {
145 ovrHmd_AttachToWindow(hmd, win, 0, 0);
146 printf("running in \"direct-to-rift\" mode\n");
147 } else {
148 printf("running in \"extended desktop\" mode\n");
149 }
150 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
152 dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_TimeWarp |
153 ovrDistortionCap_Overdrive | ovrDistortionCap_NoRestore;
154 #ifdef OVR_OS_LINUX
155 dcaps |= ovrDistortionCap_LinuxDevFullscreen;
156 #endif
158 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, eye_fov, eye_render_desc)) {
159 fprintf(stderr, "failed to configure LibOVR distortion renderer\n");
160 }
162 /* set the eye offset options */
163 leye_offs[0] = eye_render_desc[ovrEye_Left].HmdToEyeViewOffset.x;
164 leye_offs[1] = eye_render_desc[ovrEye_Left].HmdToEyeViewOffset.y;
165 leye_offs[2] = eye_render_desc[ovrEye_Left].HmdToEyeViewOffset.z;
166 reye_offs[0] = eye_render_desc[ovrEye_Right].HmdToEyeViewOffset.x;
167 reye_offs[1] = eye_render_desc[ovrEye_Right].HmdToEyeViewOffset.y;
168 reye_offs[2] = eye_render_desc[ovrEye_Right].HmdToEyeViewOffset.z;
170 /* sanity check ... on linux it seems I'm getting the eye offsets reversed for some reason */
171 if(leye_offs[0] > reye_offs[0]) {
172 fprintf(stderr, "BUG %s:%d: eye offset reversed?! fixing but wtf...\n", __FILE__, __LINE__);
173 set_option_vec(optdb, VR_LEYE_OFFSET, reye_offs);
174 set_option_vec(optdb, VR_REYE_OFFSET, leye_offs);
175 } else {
176 set_option_vec(optdb, VR_LEYE_OFFSET, leye_offs);
177 set_option_vec(optdb, VR_REYE_OFFSET, reye_offs);
178 }
181 #ifdef DISABLE_RETARDED_HEALTH_WARNING
182 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
183 #endif
184 }
186 static void cleanup(void)
187 {
188 if(hmd) {
189 ovrHmd_Destroy(hmd);
190 ovr_Shutdown();
191 }
192 destroy_options(optdb);
193 }
195 static int set_option(const char *opt, enum opt_type type, void *valp)
196 {
197 float fval;
199 switch(type) {
200 case OTYPE_INT:
201 fval = (float)*(int*)valp;
202 set_option_int(optdb, opt, *(int*)valp);
203 break;
205 case OTYPE_FLOAT:
206 fval = *(float*)valp;
207 set_option_float(optdb, opt, fval);
208 break;
210 case OTYPE_VEC:
211 set_option_vec(optdb, opt, valp);
212 break;
213 }
215 if(hmd && strcmp(opt, VR_RENDER_RES_SCALE) == 0) {
216 eye_res[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, eye_fov[0], fval);
217 eye_res[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, eye_fov[1], fval);
219 set_option_int(optdb, VR_LEYE_XRES, eye_res[0].w);
220 set_option_int(optdb, VR_LEYE_YRES, eye_res[0].h);
221 set_option_int(optdb, VR_REYE_XRES, eye_res[1].w);
222 set_option_int(optdb, VR_REYE_YRES, eye_res[1].h);
223 }
225 return 0;
226 }
228 static int get_option(const char *opt, enum opt_type type, void *valp)
229 {
230 switch(type) {
231 case OTYPE_INT:
232 return get_option_int(optdb, opt, valp);
233 case OTYPE_FLOAT:
234 return get_option_float(optdb, opt, valp);
235 case OTYPE_VEC:
236 return get_option_vec(optdb, opt, valp);
237 }
238 return -1;
239 }
241 static void translation(int eye, float *vec)
242 {
243 if(!hmd) {
244 vec[0] = vec[1] = vec[2] = 0;
245 return;
246 }
248 /* if we're inside the begin-end block we can get a fresh pose, otherwise we'll just
249 * reuse the one we got last frame.
250 */
251 if(inside_begin_end) {
252 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
253 }
255 vec[0] = pose[eye].Position.x;
256 vec[1] = pose[eye].Position.y;
257 vec[2] = pose[eye].Position.z;
258 }
260 static void rotation(int eye, float *quat)
261 {
262 if(!hmd) {
263 quat[0] = quat[1] = quat[2] = 0.0f;
264 quat[3] = 1.0f;
265 return;
266 }
268 /* same as above (translation) */
269 if(inside_begin_end) {
270 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
271 }
273 quat[0] = pose[eye].Orientation.x;
274 quat[1] = pose[eye].Orientation.y;
275 quat[2] = pose[eye].Orientation.z;
276 quat[3] = pose[eye].Orientation.w;
277 }
279 static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
281 static void proj_matrix(int eye, float znear, float zfar, float *mat)
282 {
283 int i, j;
284 ovrMatrix4f vmat;
286 if(!hmd) {
287 memcpy(mat, idmat, sizeof idmat);
288 return;
289 }
291 vmat = ovrMatrix4f_Projection(eye_render_desc[eye].Fov, znear, zfar, 1);
293 for(i=0; i<4; i++) {
294 for(j=0; j<4; j++) {
295 *mat++ = vmat.M[j][i];
296 }
297 }
298 }
300 static void begin(int eye)
301 {
302 if(!hmd) return;
304 if(!deferred_init_done) {
305 deferred_init();
306 }
308 if(!inside_begin_end) {
309 ovrHmd_BeginFrame(hmd, 0);
310 inside_begin_end = 1;
311 }
312 }
314 static int present(void)
315 {
316 int cur_prog;
318 if(!hmd) return 0;
320 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
322 ovrHmd_EndFrame(hmd, pose, &eye_tex[0].Texture);
323 inside_begin_end = 0;
325 if(cur_prog) {
326 glUseProgram(0);
327 }
329 return 1;
330 }
332 static void set_eye_texture(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
333 {
334 ovrSizei texsz;
335 ovrRecti rect;
337 glBindTexture(GL_TEXTURE_2D, tex);
338 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texsz.w);
339 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texsz.h);
341 rect.Pos.x = (int)(umin * texsz.w);
342 rect.Pos.y = (int)(vmin * texsz.h);
343 rect.Size.w = (int)((umax - umin) * texsz.w);
344 rect.Size.h = (int)((vmax - vmin) * texsz.h);
346 eye_tex[eye].OGL.Header.API = ovrRenderAPI_OpenGL;
347 eye_tex[eye].OGL.Header.TextureSize = texsz;
348 eye_tex[eye].OGL.Header.RenderViewport = rect;
349 eye_tex[eye].OGL.TexId = tex;
350 }
352 static void recenter(void)
353 {
354 if(hmd) {
355 ovrHmd_RecenterPose(hmd);
356 }
357 }
359 struct vr_module *vr_module_libovr(void)
360 {
361 static struct vr_module m;
363 if(!m.init) {
364 m.name = "libovr";
365 m.init = init;
366 m.cleanup = cleanup;
367 m.set_option = set_option;
368 m.get_option = get_option;
369 m.translation = translation;
370 m.rotation = rotation;
371 m.proj_matrix = proj_matrix;
372 m.begin = begin;
373 m.present = present;
374 m.set_eye_texture = set_eye_texture;
375 m.recenter = recenter;
376 }
377 return &m;
378 }
380 #else /* no libovr */
382 static int init(void)
383 {
384 return -1;
385 }
387 struct vr_module *vr_module_libovr(void)
388 {
389 static struct vr_module m;
391 if(!m.init) {
392 m.name = "libovr";
393 m.init = init;
394 }
395 return &m;
396 }
398 #endif /* USE_LIBOVR */