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