libgoatvr

view src/vr.c @ 19:437fe32ac633

ops... wasn't handling the stereo eye separation correctly. also fixed a bug in vr_libovr.c causing an assertion inside LibOVR when ovrHmd_GetEyePose was called as a result of calls to view_rotation or view_translation outside of vr_begin/vr_end
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 04 Oct 2014 03:39:14 +0300
parents 1067274dc780
children d659cbedde1d
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "opengl.h"
5 #include "vr.h"
6 #include "vr_impl.h"
7 #include "mathutil.h"
9 #define DEF_IPD 0.064f
11 static void fallback_present(void);
14 static struct vr_module *vrm;
15 static float idmat[] = {
16 1, 0, 0, 0,
17 0, 1, 0, 0,
18 0, 0, 1, 0,
19 0, 0, 0, 1
20 };
21 static float fbtex_rect[] = {
22 0, 0, 1, 1
23 };
25 static void *defopt; /* default options db */
27 static struct {
28 float umin, umax, vmin, vmax;
29 int tex;
30 } rtarg[2];
33 int vr_init(void)
34 {
35 int i, nmodules;
36 char *vrmod_env;
38 /* create the default options database */
39 if(!defopt && (defopt = create_options())) {
40 set_option_float(defopt, VR_RENDER_RES_SCALE, 1.0f);
41 set_option_float(defopt, VR_EYE_HEIGHT, 1.675f);
42 set_option_float(defopt, VR_IPD, DEF_IPD);
43 set_option_vec3f(defopt, VR_LEYE_OFFSET, -DEF_IPD * 0.5f, 0.0f, 0.0f);
44 set_option_vec3f(defopt, VR_REYE_OFFSET, DEF_IPD * 0.5f, 0.0f, 0.0f);
45 set_option_int(defopt, VR_NULL_STEREO, 0);
46 }
48 if(vrm) {
49 vr_shutdown();
50 }
52 vr_init_modules();
54 nmodules = vr_get_num_modules();
55 for(i=0; i<nmodules; i++) {
56 struct vr_module *m = vr_get_module(i);
57 if(m->init() != -1) {
58 /* add to the active modules array */
59 vr_activate_module(i);
60 }
61 }
63 if(!vr_get_num_active_modules()) {
64 return -1;
65 }
67 if((vrmod_env = getenv("VR_MODULE"))) {
68 vr_use_module_named(vrmod_env);
69 } else {
70 vr_use_module(0);
71 }
72 return 0;
73 }
75 void vr_shutdown(void)
76 {
77 vr_clear_modules();
78 vrm = 0;
79 fbtex_rect[0] = fbtex_rect[1] = 0;
80 fbtex_rect[2] = fbtex_rect[3] = 1;
81 }
83 int vr_module_count(void)
84 {
85 return vr_get_num_active_modules();
86 }
88 const char *vr_module_name(int idx)
89 {
90 struct vr_module *m = vr_get_active_module(idx);
91 if(!m) {
92 return 0;
93 }
94 return m->name;
95 }
97 int vr_use_module(int idx)
98 {
99 if(idx >= 0 && idx < vr_get_num_active_modules()) {
100 struct vr_module *m = vr_get_active_module(idx);
101 if(m != vrm) {
102 vrm = m;
103 printf("using vr module: %s\n", vrm->name);
104 }
105 return 0;
106 }
107 return -1;
108 }
110 int vr_use_module_named(const char *name)
111 {
112 int i, count = vr_get_num_active_modules();
114 for(i=0; i<count; i++) {
115 struct vr_module *m = vr_get_active_module(i);
116 if(strcmp(m->name, name) == 0) {
117 return vr_use_module(i);
118 }
119 }
120 return -1;
121 }
123 void vr_seti(const char *optname, int val)
124 {
125 if(vrm && vrm->set_option) {
126 vrm->set_option(optname, OTYPE_INT, &val);
127 } else {
128 set_option_int(defopt, optname, val);
129 }
130 }
132 void vr_setf(const char *optname, float val)
133 {
134 if(vrm && vrm->set_option) {
135 vrm->set_option(optname, OTYPE_FLOAT, &val);
136 } else {
137 set_option_float(defopt, optname, val);
138 }
139 }
141 static int def_option_int(const char *optname)
142 {
143 int res = 0;
144 int left, right;
146 if(strcmp(optname, VR_RENDER_XRES) == 0) {
147 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
148 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
149 return left + right;
150 }
151 } else if(strcmp(optname, VR_RENDER_YRES) == 0) {
152 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
153 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
154 return left > right ? left : right;
155 }
156 }
158 get_option_int(defopt, optname, &res);
159 return res;
160 }
162 static float def_option_float(const char *optname)
163 {
164 float res = 0.0f;
166 if(strcmp(optname, VR_RENDER_XRES) == 0 || strcmp(optname, VR_RENDER_YRES) == 0) {
167 return (float)def_option_int(optname);
168 }
170 get_option_float(defopt, optname, &res);
171 return res;
172 }
174 static float *def_option_vec(const char *optname, float *res)
175 {
176 res[0] = res[1] = res[2] = res[3] = 0.0f;
178 get_option_vec(defopt, optname, res);
179 return res;
180 }
182 int vr_geti(const char *optname)
183 {
184 int res = 0;
186 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
187 res = def_option_int(optname);
188 }
189 return res;
190 }
192 float vr_getf(const char *optname)
193 {
194 float res = 0.0f;
196 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
197 res = def_option_float(optname);
198 }
199 return res;
200 }
202 float *vr_getfv(const char *optname, float *res)
203 {
204 static float sres[4];
205 if(!res) res = sres;
207 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_VEC, res) == -1) {
208 def_option_vec(optname, res);
209 }
210 return res;
211 }
213 int vr_geti_def(const char *optname, int def_val)
214 {
215 int res = 0;
217 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
218 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
219 return def_val;
220 }
221 }
222 return res;
223 }
225 float vr_getf_def(const char *optname, float def_val)
226 {
227 float res = 0.0f;
229 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
230 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
231 return def_val;
232 }
233 }
234 return res;
235 }
237 int vr_view_translation(int eye, float *vec)
238 {
239 if(vrm && vrm->translation) {
240 vrm->translation(eye, vec);
241 return 1;
242 }
243 vec[0] = vec[1] = vec[2] = 0.0f;
244 return 0;
245 }
247 int vr_view_rotation(int eye, float *quat)
248 {
249 if(vrm && vrm->rotation) {
250 vrm->rotation(eye, quat);
251 return 1;
252 }
253 quat[0] = quat[1] = quat[2] = 0.0f;
254 quat[3] = 1.0f;
255 return 0;
256 }
258 int vr_view_matrix(int eye, float *mat)
259 {
260 float offs[3], quat[4], eye_offs[4];
261 float rmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
262 float tmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
264 if(vrm && vrm->view_matrix) {
265 vrm->view_matrix(eye, mat);
266 return 1;
267 }
269 memcpy(mat, idmat, sizeof idmat);
271 if(vr_view_translation(eye, offs)) {
272 offs[0] = -offs[0];
273 offs[1] = -offs[1];
274 offs[2] = -offs[2];
276 vrimp_translation_matrix(offs, tmat);
277 vrimp_mult_matrix(mat, mat, tmat);
278 }
279 if(vr_view_rotation(eye, quat)) {
280 vrimp_rotation_matrix(quat, rmat);
281 vrimp_mult_matrix(mat, mat, rmat);
282 }
284 vr_getfv(eye == VR_EYE_LEFT ? VR_LEYE_OFFSET : VR_REYE_OFFSET, eye_offs);
285 eye_offs[0] = -eye_offs[0];
286 eye_offs[1] = -eye_offs[1];
287 eye_offs[2] = -eye_offs[2];
288 vrimp_translation_matrix(eye_offs, tmat);
289 vrimp_mult_matrix(mat, mat, tmat);
290 return 1;
291 }
293 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
294 {
295 if(vrm && vrm->proj_matrix) {
296 vrm->proj_matrix(eye, znear, zfar, mat);
297 return 1;
298 }
299 memcpy(mat, idmat, sizeof idmat);
300 return 0;
301 }
303 void vr_begin(int eye)
304 {
305 if(vrm && vrm->begin) {
306 vrm->begin(eye);
307 }
308 }
310 void vr_end(void)
311 {
312 if(vrm && vrm->end) {
313 vrm->end();
314 }
315 }
317 int vr_swap_buffers(void)
318 {
319 int res = 0;
321 if(vrm && vrm->present) {
322 res = vrm->present();
323 }
325 if(!res) {
326 fallback_present();
327 vrimp_swap_buffers();
328 }
329 return 0;
330 }
332 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
333 {
334 float halfu = (umax + umin) * 0.5f;
336 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
337 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
338 }
340 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
341 {
342 if(vrm && vrm->set_eye_texture) {
343 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
344 } else {
345 rtarg[eye].tex = tex;
346 rtarg[eye].umin = umin;
347 rtarg[eye].umax = umax;
348 rtarg[eye].vmin = vmin;
349 rtarg[eye].vmax = vmax;
350 }
351 }
353 void vr_recenter(void)
354 {
355 if(vrm && vrm->recenter) {
356 vrm->recenter();
357 }
358 }
360 static void fallback_present(void)
361 {
362 int i, show_both = vr_geti(VR_NULL_STEREO);
364 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
366 glDisable(GL_LIGHTING);
367 glDisable(GL_DEPTH_TEST);
368 glDisable(GL_ALPHA_TEST);
369 glDisable(GL_STENCIL_TEST);
370 glDisable(GL_FOG);
372 glEnable(GL_TEXTURE_2D);
374 glMatrixMode(GL_MODELVIEW);
375 glPushMatrix();
376 glLoadIdentity();
377 glMatrixMode(GL_PROJECTION);
378 glPushMatrix();
379 glLoadIdentity();
381 for(i=0; i<2; i++) {
382 float x0, x1;
384 if(show_both) {
385 x0 = i == 0 ? -1 : 0;
386 x1 = i == 0 ? 0 : 1;
387 } else {
388 x0 = -1;
389 x1 = 1;
390 }
392 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
394 glBegin(GL_QUADS);
395 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
396 glVertex2f(x0, -1);
397 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
398 glVertex2f(x1, -1);
399 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
400 glVertex2f(x1, 1);
401 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
402 glVertex2f(x0, 1);
403 glEnd();
405 if(!show_both) break;
406 }
408 glPopMatrix();
409 glMatrixMode(GL_MODELVIEW);
410 glPopMatrix();
412 glPopAttrib();
413 }