libgoatvr

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