conworlds
view src/vr/vr_libovr.c @ 20:782ff06817fb
merged ...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 26 Aug 2014 18:42:53 +0300 |
parents | 5dc4e2b8f6f5 |
children |
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 printf("running in \"direct-to-rift\" mode\n");
114 } else {
115 printf("running in \"extended desktop\" mode\n");
116 }
117 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
119 dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
120 ovrDistortionCap_Overdrive | ovrDistortionCap_NoRestore;
122 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, eye_fov, eye_render_desc)) {
123 fprintf(stderr, "failed to configure LibOVR distortion renderer\n");
124 }
126 #ifdef DISABLE_RETARDED_HEALTH_WARNING
127 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
128 #endif
129 }
131 static void cleanup(void)
132 {
133 if(hmd) {
134 ovrHmd_Destroy(hmd);
135 ovr_Shutdown();
136 }
137 }
139 static int set_option(const char *opt, enum opt_type type, void *valp)
140 {
141 switch(type) {
142 case OTYPE_INT:
143 set_option_int(optdb, opt, *(int*)valp);
144 break;
146 case OTYPE_FLOAT:
147 set_option_float(optdb, opt, *(float*)valp);
148 break;
149 }
150 return 0;
151 }
153 static int get_option(const char *opt, enum opt_type type, void *valp)
154 {
155 switch(type) {
156 case OTYPE_INT:
157 return get_option_int(optdb, opt, valp);
158 case OTYPE_FLOAT:
159 return get_option_float(optdb, opt, valp);
160 }
161 return -1;
162 }
164 static int translation(int eye, float *vec)
165 {
166 if(!hmd) {
167 vec[0] = vec[1] = vec[2] = 0;
168 return 0;
169 }
171 pose[eye] = ovrHmd_GetEyePose(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
172 vec[0] = pose[eye].Position.x;
173 vec[1] = pose[eye].Position.y;
174 vec[2] = pose[eye].Position.z;
175 return 1;
176 }
178 static int rotation(int eye, float *quat)
179 {
180 if(!hmd) {
181 quat[0] = quat[1] = quat[2] = 0.0f;
182 quat[3] = 1.0f;
183 return 0;
184 }
186 pose[eye] = ovrHmd_GetEyePose(hmd, eye == VR_EYE_LEFT ? ovrEye_Left : ovrEye_Right);
187 quat[0] = pose[eye].Orientation.x;
188 quat[1] = pose[eye].Orientation.y;
189 quat[2] = pose[eye].Orientation.z;
190 quat[3] = pose[eye].Orientation.w;
191 return 1;
192 }
194 static const float idmat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
196 static void proj_matrix(int eye, float znear, float zfar, float *mat)
197 {
198 int i, j;
199 ovrMatrix4f vmat;
201 if(!hmd) {
202 memcpy(mat, idmat, sizeof idmat);
203 return;
204 }
206 vmat = ovrMatrix4f_Projection(eye_render_desc[eye].Fov, znear, zfar, 1);
208 for(i=0; i<4; i++) {
209 for(j=0; j<4; j++) {
210 *mat++ = vmat.M[j][i];
211 }
212 }
213 }
215 static int new_frame = 1;
217 static void begin(int eye)
218 {
219 if(!hmd) return;
221 if(!deferred_init_done) {
222 deferred_init();
223 }
225 if(new_frame) {
226 ovrHmd_BeginFrame(hmd, 0);
227 new_frame = 0;
228 }
229 }
231 static int present(void)
232 {
233 if(!hmd) return 0;
235 ovrHmd_EndFrame(hmd, pose, &eye_tex[0].Texture);
236 new_frame = 1;
238 return 1;
239 }
241 static void set_eye_texture(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
242 {
243 ovrSizei texsz;
244 ovrRecti rect;
246 glBindTexture(GL_TEXTURE_2D, tex);
247 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texsz.w);
248 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texsz.h);
250 rect.Pos.x = (int)(umin * texsz.w);
251 rect.Pos.y = (int)((vmin + 1.0 - vmax) * texsz.h);
252 rect.Size.w = (int)((umax - umin) * texsz.w);
253 rect.Size.h = (int)((vmax - vmin) * texsz.h);
255 eye_tex[eye].OGL.Header.API = ovrRenderAPI_OpenGL;
256 eye_tex[eye].OGL.Header.TextureSize = texsz;
257 eye_tex[eye].OGL.Header.RenderViewport = rect;
258 eye_tex[eye].OGL.TexId = tex;
259 }
261 static void recenter(void)
262 {
263 if(hmd) {
264 ovrHmd_RecenterPose(hmd);
265 }
266 }
268 struct vr_module *vr_module_libovr(void)
269 {
270 static struct vr_module m;
272 if(!m.init) {
273 m.name = "libovr";
274 m.init = init;
275 m.cleanup = cleanup;
276 m.set_option = set_option;
277 m.get_option = get_option;
278 m.translation = translation;
279 m.rotation = rotation;
280 m.proj_matrix = proj_matrix;
281 m.begin = begin;
282 m.present = present;
283 m.set_eye_texture = set_eye_texture;
284 m.recenter = recenter;
285 }
286 return &m;
287 }
289 #else /* no libovr */
291 static int init(void)
292 {
293 return -1;
294 }
296 struct vr_module *vr_module_libovr(void)
297 {
298 static struct vr_module m;
300 if(!m.init) {
301 m.name = "libovr";
302 m.init = init;
303 }
304 return &m;
305 }
307 #endif /* USE_LIBOVR */