libgoatvr

annotate src/vr_libovr.c @ 9:d12592558809

build on macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 20 Sep 2014 16:52:42 +0300
parents 6896f9cf9621
children 34d4643d61f9
rev   line source
nuclear@0 1 #ifdef WIN32
nuclear@0 2 #define OVR_OS_WIN32
nuclear@0 3 #endif
nuclear@9 4 #ifdef __APPLE__
nuclear@9 5 #define OVR_OS_MAC
nuclear@9 6 #endif
nuclear@0 7
nuclear@0 8 #include "vr_impl.h"
nuclear@0 9
nuclear@0 10 #ifdef USE_LIBOVR
nuclear@0 11 #include <stdio.h>
nuclear@0 12 #include <stdlib.h>
nuclear@9 13 #include <string.h>
nuclear@0 14 #include <assert.h>
nuclear@0 15 #include "opt.h"
nuclear@0 16
nuclear@0 17 #include <OVR_CAPI.h>
nuclear@0 18 #include <OVR_CAPI_GL.h>
nuclear@0 19
nuclear@0 20 #define DISABLE_RETARDED_HEALTH_WARNING
nuclear@0 21
nuclear@0 22 /* just dropping the prototype here to avoid including CAPI_HSWDisplay.h */
nuclear@0 23 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enabled);
nuclear@0 24
nuclear@0 25 static ovrHmd hmd;
nuclear@0 26 static void *optdb;
nuclear@0 27 static ovrEyeRenderDesc eye_render_desc[2];
nuclear@0 28 static ovrSizei eye_res[2];
nuclear@0 29 static ovrGLTexture eye_tex[2];
nuclear@0 30 static ovrFovPort eye_fov[2];
nuclear@0 31 static ovrPosef pose[2];
nuclear@0 32 static int deferred_init_done;
nuclear@0 33
nuclear@0 34 static int init(void)
nuclear@0 35 {
nuclear@0 36 int i, num_hmds;
nuclear@9 37 int use_fake = 0;
nuclear@0 38
nuclear@0 39 if(!ovr_Initialize()) {
nuclear@0 40 return -1;
nuclear@0 41 }
nuclear@0 42 printf("initialized LibOVR %s\n", ovr_GetVersionString());
nuclear@0 43
nuclear@0 44 if(!(num_hmds = ovrHmd_Detect())) {
nuclear@9 45 if(getenv("VR_LIBOVR_FAKE")) {
nuclear@9 46 use_fake = 1;
nuclear@9 47 num_hmds = 1;
nuclear@9 48 } else {
nuclear@9 49 ovr_Shutdown();
nuclear@9 50 return -1;
nuclear@9 51 }
nuclear@0 52 }
nuclear@0 53 printf("%d Oculus HMD(s) found\n", num_hmds);
nuclear@0 54
nuclear@0 55 hmd = 0;
nuclear@0 56 for(i=0; i<num_hmds; i++) {
nuclear@9 57 ovrHmd h = use_fake ? ovrHmd_CreateDebug(ovrHmd_DK2) : ovrHmd_Create(i);
nuclear@9 58 if(!h) {
nuclear@0 59 break;
nuclear@0 60 }
nuclear@0 61 printf(" [%d]: %s - %s\n", i, h->Manufacturer, h->ProductName);
nuclear@0 62
nuclear@0 63 if(!hmd) {
nuclear@0 64 hmd = h;
nuclear@0 65 } else {
nuclear@0 66 ovrHmd_Destroy(h);
nuclear@0 67 }
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 if(!hmd) {
nuclear@0 71 fprintf(stderr, "failed to initialize any Oculus HMDs\n");
nuclear@0 72 return -1;
nuclear@0 73 }
nuclear@0 74
nuclear@0 75 ovrHmd_ConfigureTracking(hmd, 0xffffffff, 0);
nuclear@0 76
nuclear@0 77 eye_fov[0] = hmd->DefaultEyeFov[0];
nuclear@0 78 eye_fov[1] = hmd->DefaultEyeFov[1];
nuclear@0 79
nuclear@0 80 eye_res[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, eye_fov[0], 1.0);
nuclear@0 81 eye_res[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, eye_fov[1], 1.0);
nuclear@0 82
nuclear@0 83 /* create the options database */
nuclear@0 84 if((optdb = create_options())) {
nuclear@0 85 set_option_int(optdb, VR_OPT_DISPLAY_WIDTH, hmd->Resolution.w);
nuclear@0 86 set_option_int(optdb, VR_OPT_DISPLAY_HEIGHT, hmd->Resolution.h);
nuclear@0 87 set_option_int(optdb, VR_OPT_LEYE_XRES, eye_res[0].w);
nuclear@0 88 set_option_int(optdb, VR_OPT_LEYE_YRES, eye_res[0].h);
nuclear@0 89 set_option_int(optdb, VR_OPT_REYE_XRES, eye_res[1].w);
nuclear@0 90 set_option_int(optdb, VR_OPT_REYE_YRES, eye_res[1].h);
nuclear@0 91 set_option_float(optdb, VR_OPT_EYE_HEIGHT, ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, OVR_DEFAULT_EYE_HEIGHT));
nuclear@0 92 set_option_float(optdb, VR_OPT_IPD, ovrHmd_GetFloat(hmd, OVR_KEY_IPD, OVR_DEFAULT_IPD));
nuclear@0 93 set_option_int(optdb, VR_OPT_WIN_XOFFS, hmd->WindowsPos.x);
nuclear@0 94 set_option_int(optdb, VR_OPT_WIN_YOFFS, hmd->WindowsPos.y);
nuclear@0 95 }
nuclear@0 96
nuclear@0 97 deferred_init_done = 0;
nuclear@0 98 return 0;
nuclear@0 99 }
nuclear@0 100
nuclear@0 101 static void deferred_init(void)
nuclear@0 102 {
nuclear@0 103 union ovrGLConfig glcfg;
nuclear@0 104 unsigned int dcaps;
nuclear@0 105 void *win = 0;
nuclear@0 106
nuclear@0 107 deferred_init_done = 1;
nuclear@0 108
nuclear@0 109 memset(&glcfg, 0, sizeof glcfg);
nuclear@0 110 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
nuclear@0 111 glcfg.OGL.Header.RTSize = hmd->Resolution;
nuclear@0 112 glcfg.OGL.Header.Multisample = 1;
nuclear@0 113 #ifdef WIN32
nuclear@0 114 win = GetActiveWindow();
nuclear@0 115 /*glcfg.OGL.Window = win;
nuclear@0 116 glcfg.OGL.DC = wglGetCurrentDC();
nuclear@0 117 assert(glcfg.OGL.Window);
nuclear@0 118 assert(glcfg.OGL.DC);*/
nuclear@0 119 #endif
nuclear@0 120
nuclear@0 121 if(!(hmd->HmdCaps & ovrHmdCap_ExtendDesktop)) {
nuclear@0 122 ovrHmd_AttachToWindow(hmd, win, 0, 0);
nuclear@0 123 printf("running in \"direct-to-rift\" mode\n");
nuclear@0 124 } else {
nuclear@0 125 printf("running in \"extended desktop\" mode\n");
nuclear@0 126 }
nuclear@0 127 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
nuclear@0 128
nuclear@0 129 dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
nuclear@0 130 ovrDistortionCap_Overdrive | ovrDistortionCap_NoRestore;
nuclear@0 131
nuclear@0 132 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, eye_fov, eye_render_desc)) {
nuclear@0 133 fprintf(stderr, "failed to configure LibOVR distortion renderer\n");
nuclear@0 134 }
nuclear@0 135
nuclear@0 136 #ifdef DISABLE_RETARDED_HEALTH_WARNING
nuclear@0 137 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
nuclear@0 138 #endif
nuclear@0 139 }
nuclear@0 140
nuclear@0 141 static void cleanup(void)
nuclear@0 142 {
nuclear@0 143 if(hmd) {
nuclear@0 144 ovrHmd_Destroy(hmd);
nuclear@0 145 ovr_Shutdown();
nuclear@0 146 }
nuclear@5 147 destroy_options(optdb);
nuclear@0 148 }
nuclear@0 149
nuclear@0 150 static int set_option(const char *opt, enum opt_type type, void *valp)
nuclear@0 151 {
nuclear@0 152 switch(type) {
nuclear@0 153 case OTYPE_INT:
nuclear@0 154 set_option_int(optdb, opt, *(int*)valp);
nuclear@0 155 break;
nuclear@0 156
nuclear@0 157 case OTYPE_FLOAT:
nuclear@0 158 set_option_float(optdb, opt, *(float*)valp);
nuclear@0 159 break;
nuclear@0 160 }
nuclear@0 161 return 0;
nuclear@0 162 }
nuclear@0 163
nuclear@0 164 static int get_option(const char *opt, enum opt_type type, void *valp)
nuclear@0 165 {
nuclear@0 166 switch(type) {
nuclear@0 167 case OTYPE_INT:
nuclear@0 168 return get_option_int(optdb, opt, valp);
nuclear@0 169 case OTYPE_FLOAT:
nuclear@0 170 return get_option_float(optdb, opt, valp);
nuclear@0 171 }
nuclear@0 172 return -1;
nuclear@0 173 }
nuclear@0 174
nuclear@7 175 static void translation(int eye, float *vec)
nuclear@0 176 {
nuclear@0 177 if(!hmd) {
nuclear@0 178 vec[0] = vec[1] = vec[2] = 0;
nuclear@9 179 return;
nuclear@0 180 }
nuclear@0 181
nuclear@0 182 pose[eye] = ovrHmd_GetEyePose(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
nuclear@0 183 vec[0] = pose[eye].Position.x + eye_render_desc[eye].ViewAdjust.x;
nuclear@0 184 vec[1] = pose[eye].Position.y + eye_render_desc[eye].ViewAdjust.y;
nuclear@0 185 vec[2] = pose[eye].Position.z + eye_render_desc[eye].ViewAdjust.z;
nuclear@0 186 }
nuclear@0 187
nuclear@7 188 static void rotation(int eye, float *quat)
nuclear@0 189 {
nuclear@0 190 if(!hmd) {
nuclear@0 191 quat[0] = quat[1] = quat[2] = 0.0f;
nuclear@0 192 quat[3] = 1.0f;
nuclear@9 193 return;
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 pose[eye] = ovrHmd_GetEyePose(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
nuclear@0 197 quat[0] = pose[eye].Orientation.x;
nuclear@0 198 quat[1] = pose[eye].Orientation.y;
nuclear@0 199 quat[2] = pose[eye].Orientation.z;
nuclear@0 200 quat[3] = pose[eye].Orientation.w;
nuclear@0 201 }
nuclear@0 202
nuclear@0 203 static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@0 204
nuclear@0 205 static void proj_matrix(int eye, float znear, float zfar, float *mat)
nuclear@0 206 {
nuclear@0 207 int i, j;
nuclear@0 208 ovrMatrix4f vmat;
nuclear@0 209
nuclear@0 210 if(!hmd) {
nuclear@0 211 memcpy(mat, idmat, sizeof idmat);
nuclear@0 212 return;
nuclear@0 213 }
nuclear@0 214
nuclear@0 215 vmat = ovrMatrix4f_Projection(eye_render_desc[eye].Fov, znear, zfar, 1);
nuclear@0 216
nuclear@0 217 for(i=0; i<4; i++) {
nuclear@0 218 for(j=0; j<4; j++) {
nuclear@0 219 *mat++ = vmat.M[j][i];
nuclear@0 220 }
nuclear@0 221 }
nuclear@0 222 }
nuclear@0 223
nuclear@0 224 static int new_frame = 1;
nuclear@0 225
nuclear@0 226 static void begin(int eye)
nuclear@0 227 {
nuclear@0 228 if(!hmd) return;
nuclear@0 229
nuclear@0 230 if(!deferred_init_done) {
nuclear@0 231 deferred_init();
nuclear@0 232 }
nuclear@0 233
nuclear@0 234 if(new_frame) {
nuclear@0 235 ovrHmd_BeginFrame(hmd, 0);
nuclear@0 236 new_frame = 0;
nuclear@0 237 }
nuclear@0 238 }
nuclear@0 239
nuclear@0 240 static int present(void)
nuclear@0 241 {
nuclear@0 242 if(!hmd) return 0;
nuclear@0 243
nuclear@0 244 ovrHmd_EndFrame(hmd, pose, &eye_tex[0].Texture);
nuclear@0 245 new_frame = 1;
nuclear@0 246
nuclear@0 247 return 1;
nuclear@0 248 }
nuclear@0 249
nuclear@0 250 static void set_eye_texture(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@0 251 {
nuclear@0 252 ovrSizei texsz;
nuclear@0 253 ovrRecti rect;
nuclear@0 254
nuclear@0 255 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@0 256 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texsz.w);
nuclear@0 257 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texsz.h);
nuclear@0 258
nuclear@0 259 rect.Pos.x = (int)(umin * texsz.w);
nuclear@0 260 rect.Pos.y = (int)((vmin + 1.0 - vmax) * texsz.h);
nuclear@0 261 rect.Size.w = (int)((umax - umin) * texsz.w);
nuclear@0 262 rect.Size.h = (int)((vmax - vmin) * texsz.h);
nuclear@0 263
nuclear@0 264 eye_tex[eye].OGL.Header.API = ovrRenderAPI_OpenGL;
nuclear@0 265 eye_tex[eye].OGL.Header.TextureSize = texsz;
nuclear@0 266 eye_tex[eye].OGL.Header.RenderViewport = rect;
nuclear@0 267 eye_tex[eye].OGL.TexId = tex;
nuclear@0 268 }
nuclear@0 269
nuclear@0 270 static void recenter(void)
nuclear@0 271 {
nuclear@0 272 if(hmd) {
nuclear@0 273 ovrHmd_RecenterPose(hmd);
nuclear@0 274 }
nuclear@0 275 }
nuclear@0 276
nuclear@0 277 struct vr_module *vr_module_libovr(void)
nuclear@0 278 {
nuclear@0 279 static struct vr_module m;
nuclear@0 280
nuclear@0 281 if(!m.init) {
nuclear@0 282 m.name = "libovr";
nuclear@0 283 m.init = init;
nuclear@0 284 m.cleanup = cleanup;
nuclear@0 285 m.set_option = set_option;
nuclear@0 286 m.get_option = get_option;
nuclear@0 287 m.translation = translation;
nuclear@0 288 m.rotation = rotation;
nuclear@0 289 m.proj_matrix = proj_matrix;
nuclear@0 290 m.begin = begin;
nuclear@0 291 m.present = present;
nuclear@0 292 m.set_eye_texture = set_eye_texture;
nuclear@0 293 m.recenter = recenter;
nuclear@0 294 }
nuclear@0 295 return &m;
nuclear@0 296 }
nuclear@0 297
nuclear@0 298 #else /* no libovr */
nuclear@0 299
nuclear@0 300 static int init(void)
nuclear@0 301 {
nuclear@0 302 return -1;
nuclear@0 303 }
nuclear@0 304
nuclear@0 305 struct vr_module *vr_module_libovr(void)
nuclear@0 306 {
nuclear@0 307 static struct vr_module m;
nuclear@0 308
nuclear@0 309 if(!m.init) {
nuclear@0 310 m.name = "libovr";
nuclear@0 311 m.init = init;
nuclear@0 312 }
nuclear@0 313 return &m;
nuclear@0 314 }
nuclear@0 315
nuclear@0 316 #endif /* USE_LIBOVR */