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