libgoatvr

view src/vr_libovr.c @ 29:ddaa9c764030

minor fix
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 08 Apr 2015 02:32:22 +0300
parents 5136dfcea7b1
children 1a8343ea54ce
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 #undef 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(0)) {
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_int(optdb, VR_RENDER_XRES, eye_res[0].w + eye_res[1].w);
106 set_option_int(optdb, VR_RENDER_YRES, eye_res[0].h > eye_res[1].h ? eye_res[0].h : eye_res[1].h);
107 set_option_float(optdb, VR_RENDER_RES_SCALE, 1.0);
108 set_option_float(optdb, VR_EYE_HEIGHT, ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, OVR_DEFAULT_EYE_HEIGHT));
109 set_option_float(optdb, VR_IPD, ovrHmd_GetFloat(hmd, OVR_KEY_IPD, OVR_DEFAULT_IPD));
110 set_option_int(optdb, VR_WIN_XOFFS, hmd->WindowsPos.x);
111 set_option_int(optdb, VR_WIN_YOFFS, hmd->WindowsPos.y);
112 }
114 deferred_init_done = 0;
115 return 0;
116 }
118 static void deferred_init(void)
119 {
120 union ovrGLConfig glcfg;
121 unsigned int dcaps;
122 void *win = 0;
123 float leye_offs[3], reye_offs[3];
125 deferred_init_done = 1;
127 memset(&glcfg, 0, sizeof glcfg);
128 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
129 glcfg.OGL.Header.BackBufferSize = hmd->Resolution;
130 glcfg.OGL.Header.Multisample = 1;
131 #ifdef WIN32
132 win = GetActiveWindow();
133 glcfg.OGL.Window = win;
134 glcfg.OGL.DC = wglGetCurrentDC();
135 #else
136 glcfg.OGL.Disp = glXGetCurrentDisplay();
137 win = (void*)glXGetCurrentDrawable();
139 /* on linux the Oculus SDK docs are instructing users to leave the DK2 screen in
140 * portrait mode. So we'll have to flip width and height
141 */
142 glcfg.OGL.Header.BackBufferSize.w = hmd->Resolution.h;
143 glcfg.OGL.Header.BackBufferSize.h = hmd->Resolution.w;
144 #endif
146 if(!(hmd->HmdCaps & ovrHmdCap_ExtendDesktop)) {
147 ovrHmd_AttachToWindow(hmd, win, 0, 0);
148 printf("running in \"direct-to-rift\" mode\n");
149 } else {
150 printf("running in \"extended desktop\" mode\n");
151 }
152 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
154 dcaps = ovrDistortionCap_TimeWarp | ovrDistortionCap_Overdrive;
155 #ifdef OVR_OS_LINUX
156 dcaps |= ovrDistortionCap_LinuxDevFullscreen;
157 #endif
159 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, eye_fov, eye_render_desc)) {
160 fprintf(stderr, "failed to configure LibOVR distortion renderer\n");
161 }
163 /* set the eye offset options */
164 leye_offs[0] = eye_render_desc[ovrEye_Left].HmdToEyeViewOffset.x;
165 leye_offs[1] = eye_render_desc[ovrEye_Left].HmdToEyeViewOffset.y;
166 leye_offs[2] = eye_render_desc[ovrEye_Left].HmdToEyeViewOffset.z;
167 reye_offs[0] = eye_render_desc[ovrEye_Right].HmdToEyeViewOffset.x;
168 reye_offs[1] = eye_render_desc[ovrEye_Right].HmdToEyeViewOffset.y;
169 reye_offs[2] = eye_render_desc[ovrEye_Right].HmdToEyeViewOffset.z;
171 /* sanity check ... on linux it seems I'm getting the eye offsets reversed for some reason */
172 if(leye_offs[0] > reye_offs[0]) {
173 fprintf(stderr, "BUG %s:%d: eye offset reversed?! fixing but wtf...\n", __FILE__, __LINE__);
174 set_option_vec(optdb, VR_LEYE_OFFSET, reye_offs);
175 set_option_vec(optdb, VR_REYE_OFFSET, leye_offs);
176 } else {
177 set_option_vec(optdb, VR_LEYE_OFFSET, leye_offs);
178 set_option_vec(optdb, VR_REYE_OFFSET, reye_offs);
179 }
182 #ifdef DISABLE_RETARDED_HEALTH_WARNING
183 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
184 #endif
185 }
187 static void cleanup(void)
188 {
189 if(hmd) {
190 ovrHmd_Destroy(hmd);
191 ovr_Shutdown();
192 }
193 destroy_options(optdb);
194 }
196 static int set_option(const char *opt, enum opt_type type, void *valp)
197 {
198 float fval;
200 switch(type) {
201 case OTYPE_INT:
202 fval = (float)*(int*)valp;
203 set_option_int(optdb, opt, *(int*)valp);
204 break;
206 case OTYPE_FLOAT:
207 fval = *(float*)valp;
208 set_option_float(optdb, opt, fval);
209 break;
211 case OTYPE_VEC:
212 set_option_vec(optdb, opt, valp);
213 break;
214 }
216 if(hmd && strcmp(opt, VR_RENDER_RES_SCALE) == 0) {
217 eye_res[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, eye_fov[0], fval);
218 eye_res[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, eye_fov[1], fval);
220 set_option_int(optdb, VR_LEYE_XRES, eye_res[0].w);
221 set_option_int(optdb, VR_LEYE_YRES, eye_res[0].h);
222 set_option_int(optdb, VR_REYE_XRES, eye_res[1].w);
223 set_option_int(optdb, VR_REYE_YRES, eye_res[1].h);
224 }
226 return 0;
227 }
229 static int get_option(const char *opt, enum opt_type type, void *valp)
230 {
231 switch(type) {
232 case OTYPE_INT:
233 return get_option_int(optdb, opt, valp);
234 case OTYPE_FLOAT:
235 return get_option_float(optdb, opt, valp);
236 case OTYPE_VEC:
237 return get_option_vec(optdb, opt, valp);
238 }
239 return -1;
240 }
242 static void translation(int eye, float *vec)
243 {
244 if(!hmd) {
245 vec[0] = vec[1] = vec[2] = 0;
246 return;
247 }
249 /* if we're inside the begin-end block we can get a fresh pose, otherwise we'll just
250 * reuse the one we got last frame.
251 */
252 if(inside_begin_end) {
253 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
254 }
256 vec[0] = pose[eye].Position.x;
257 vec[1] = pose[eye].Position.y;
258 vec[2] = pose[eye].Position.z;
259 }
261 static void rotation(int eye, float *quat)
262 {
263 if(!hmd) {
264 quat[0] = quat[1] = quat[2] = 0.0f;
265 quat[3] = 1.0f;
266 return;
267 }
269 /* same as above (translation) */
270 if(inside_begin_end) {
271 pose[eye] = ovrHmd_GetHmdPosePerEye(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
272 }
274 quat[0] = pose[eye].Orientation.x;
275 quat[1] = pose[eye].Orientation.y;
276 quat[2] = pose[eye].Orientation.z;
277 quat[3] = pose[eye].Orientation.w;
278 }
280 static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
282 static void proj_matrix(int eye, float znear, float zfar, float *mat)
283 {
284 int i, j;
285 ovrMatrix4f vmat;
287 if(!hmd) {
288 memcpy(mat, idmat, sizeof idmat);
289 return;
290 }
292 vmat = ovrMatrix4f_Projection(eye_render_desc[eye].Fov, znear, zfar, 1);
294 for(i=0; i<4; i++) {
295 for(j=0; j<4; j++) {
296 *mat++ = vmat.M[j][i];
297 }
298 }
299 }
301 static void begin(int eye)
302 {
303 if(!hmd) return;
305 if(!deferred_init_done) {
306 deferred_init();
307 }
309 if(!inside_begin_end) {
310 ovrHmd_BeginFrame(hmd, 0);
311 inside_begin_end = 1;
312 }
313 }
315 static int present(void)
316 {
317 int cur_prog;
319 if(!hmd) return 0;
321 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
323 ovrHmd_EndFrame(hmd, pose, &eye_tex[0].Texture);
324 inside_begin_end = 0;
326 if(cur_prog) {
327 /*glUseProgram(0);*/
328 }
330 return 1;
331 }
333 static void set_eye_texture(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
334 {
335 ovrSizei texsz;
336 ovrRecti rect;
338 glBindTexture(GL_TEXTURE_2D, tex);
339 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texsz.w);
340 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texsz.h);
342 rect.Pos.x = (int)(umin * texsz.w);
343 rect.Pos.y = (int)(vmin * texsz.h);
344 rect.Size.w = (int)((umax - umin) * texsz.w);
345 rect.Size.h = (int)((vmax - vmin) * texsz.h);
347 eye_tex[eye].OGL.Header.API = ovrRenderAPI_OpenGL;
348 eye_tex[eye].OGL.Header.TextureSize = texsz;
349 eye_tex[eye].OGL.Header.RenderViewport = rect;
350 eye_tex[eye].OGL.TexId = tex;
351 }
353 static void recenter(void)
354 {
355 if(hmd) {
356 ovrHmd_RecenterPose(hmd);
357 }
358 }
360 struct vr_module *vr_module_libovr(void)
361 {
362 static struct vr_module m;
364 if(!m.init) {
365 m.name = "libovr";
366 m.init = init;
367 m.cleanup = cleanup;
368 m.set_option = set_option;
369 m.get_option = get_option;
370 m.translation = translation;
371 m.rotation = rotation;
372 m.proj_matrix = proj_matrix;
373 m.begin = begin;
374 m.present = present;
375 m.set_eye_texture = set_eye_texture;
376 m.recenter = recenter;
377 }
378 return &m;
379 }
381 #else /* no libovr */
383 static int init(void)
384 {
385 return -1;
386 }
388 struct vr_module *vr_module_libovr(void)
389 {
390 static struct vr_module m;
392 if(!m.init) {
393 m.name = "libovr";
394 m.init = init;
395 }
396 return &m;
397 }
399 #endif /* USE_LIBOVR */