libgoatvr

view src/vr.c @ 32:7eea82cea9d2

added preliminary support for quad-buffer stereo fallback drawing
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 30 Oct 2015 06:12:48 +0200
parents d659cbedde1d
children 1102327fe85f
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 *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);
46 if((env = getenv("VR_NULL_STEREO_SBS")) && atoi(env)) {
47 set_option_int(defopt, VR_NULL_STEREO_SBS, 1);
48 } else {
49 set_option_int(defopt, VR_NULL_STEREO_SBS, 0);
50 }
51 if((env = getenv("VR_NULL_STEREO_GL")) && atoi(env)) {
52 set_option_int(defopt, VR_NULL_STEREO_GL, 1);
53 } else {
54 set_option_int(defopt, VR_NULL_STEREO_GL, 0);
55 }
56 }
58 if(vrm) {
59 vr_shutdown();
60 }
62 vr_init_modules();
64 nmodules = vr_get_num_modules();
65 for(i=0; i<nmodules; i++) {
66 struct vr_module *m = vr_get_module(i);
67 if(m->init() != -1) {
68 /* add to the active modules array */
69 vr_activate_module(i);
70 }
71 }
73 if(!vr_get_num_active_modules()) {
74 return -1;
75 }
77 if((env = getenv("VR_MODULE"))) {
78 vr_use_module_named(env);
79 } else {
80 vr_use_module(0);
81 }
82 return 0;
83 }
85 void vr_shutdown(void)
86 {
87 vr_clear_modules();
88 vrm = 0;
89 fbtex_rect[0] = fbtex_rect[1] = 0;
90 fbtex_rect[2] = fbtex_rect[3] = 1;
91 }
93 int vr_module_count(void)
94 {
95 return vr_get_num_active_modules();
96 }
98 const char *vr_module_name(int idx)
99 {
100 struct vr_module *m = vr_get_active_module(idx);
101 if(!m) {
102 return 0;
103 }
104 return m->name;
105 }
107 int vr_use_module(int idx)
108 {
109 if(idx >= 0 && idx < vr_get_num_active_modules()) {
110 struct vr_module *m = vr_get_active_module(idx);
111 if(m != vrm) {
112 vrm = m;
113 printf("using vr module: %s\n", vrm->name);
114 }
115 return 0;
116 }
117 return -1;
118 }
120 int vr_use_module_named(const char *name)
121 {
122 int i, count = vr_get_num_active_modules();
124 for(i=0; i<count; i++) {
125 struct vr_module *m = vr_get_active_module(i);
126 if(strcmp(m->name, name) == 0) {
127 return vr_use_module(i);
128 }
129 }
130 return -1;
131 }
133 void vr_seti(const char *optname, int val)
134 {
135 if(vrm && vrm->set_option) {
136 vrm->set_option(optname, OTYPE_INT, &val);
137 } else {
138 set_option_int(defopt, optname, val);
139 }
140 }
142 void vr_setf(const char *optname, float val)
143 {
144 if(vrm && vrm->set_option) {
145 vrm->set_option(optname, OTYPE_FLOAT, &val);
146 } else {
147 set_option_float(defopt, optname, val);
148 }
149 }
151 static int def_option_int(const char *optname)
152 {
153 int res = 0;
154 int left, right;
156 if(strcmp(optname, VR_RENDER_XRES) == 0) {
157 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
158 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
159 return left + right;
160 }
161 } else if(strcmp(optname, VR_RENDER_YRES) == 0) {
162 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
163 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
164 return left > right ? left : right;
165 }
166 }
168 get_option_int(defopt, optname, &res);
169 return res;
170 }
172 static float def_option_float(const char *optname)
173 {
174 float res = 0.0f;
176 if(strcmp(optname, VR_RENDER_XRES) == 0 || strcmp(optname, VR_RENDER_YRES) == 0) {
177 return (float)def_option_int(optname);
178 }
180 get_option_float(defopt, optname, &res);
181 return res;
182 }
184 static float *def_option_vec(const char *optname, float *res)
185 {
186 res[0] = res[1] = res[2] = res[3] = 0.0f;
188 get_option_vec(defopt, optname, res);
189 return res;
190 }
192 int vr_geti(const char *optname)
193 {
194 int res = 0;
196 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
197 res = def_option_int(optname);
198 }
199 return res;
200 }
202 float vr_getf(const char *optname)
203 {
204 float res = 0.0f;
206 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
207 res = def_option_float(optname);
208 }
209 return res;
210 }
212 float *vr_getfv(const char *optname, float *res)
213 {
214 static float sres[4];
215 if(!res) res = sres;
217 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_VEC, res) == -1) {
218 def_option_vec(optname, res);
219 }
220 return res;
221 }
223 int vr_geti_def(const char *optname, int def_val)
224 {
225 int res = 0;
227 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
228 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
229 return def_val;
230 }
231 }
232 return res;
233 }
235 float vr_getf_def(const char *optname, float def_val)
236 {
237 float res = 0.0f;
239 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
240 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
241 return def_val;
242 }
243 }
244 return res;
245 }
247 int vr_view_translation(int eye, float *vec)
248 {
249 if(vrm && vrm->translation) {
250 vrm->translation(eye, vec);
251 return 1;
252 }
253 vec[0] = vec[1] = vec[2] = 0.0f;
254 return 0;
255 }
257 int vr_view_rotation(int eye, float *quat)
258 {
259 if(vrm && vrm->rotation) {
260 vrm->rotation(eye, quat);
261 return 1;
262 }
263 quat[0] = quat[1] = quat[2] = 0.0f;
264 quat[3] = 1.0f;
265 return 0;
266 }
268 int vr_view_matrix(int eye, float *mat)
269 {
270 float offs[3], quat[4], eye_offs[4];
271 float rmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
272 float tmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
274 if(vrm && vrm->view_matrix) {
275 vrm->view_matrix(eye, mat);
276 return 1;
277 }
279 memcpy(mat, idmat, sizeof idmat);
281 if(vr_view_translation(eye, offs)) {
282 offs[0] = -offs[0];
283 offs[1] = -offs[1];
284 offs[2] = -offs[2];
286 vrimp_translation_matrix(offs, tmat);
287 vrimp_mult_matrix(mat, mat, tmat);
288 }
289 if(vr_view_rotation(eye, quat)) {
290 vrimp_rotation_matrix(quat, rmat);
291 vrimp_mult_matrix(mat, mat, rmat);
292 }
294 vr_getfv(eye == VR_EYE_LEFT ? VR_LEYE_OFFSET : VR_REYE_OFFSET, eye_offs);
295 eye_offs[0] = -eye_offs[0];
296 eye_offs[1] = -eye_offs[1];
297 eye_offs[2] = -eye_offs[2];
298 vrimp_translation_matrix(eye_offs, tmat);
299 vrimp_mult_matrix(mat, mat, tmat);
300 return 1;
301 }
303 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
304 {
305 if(vrm && vrm->proj_matrix) {
306 vrm->proj_matrix(eye, znear, zfar, mat);
307 return 1;
308 }
309 memcpy(mat, idmat, sizeof idmat);
310 return 0;
311 }
313 void vr_begin(int eye)
314 {
315 if(vrm && vrm->begin) {
316 vrm->begin(eye);
317 }
318 }
320 void vr_end(void)
321 {
322 if(vrm && vrm->end) {
323 vrm->end();
324 }
325 }
327 int vr_swap_buffers(void)
328 {
329 int res = 0;
331 if(vrm && vrm->present) {
332 res = vrm->present();
333 }
335 if(!res) {
336 fallback_present();
337 vrimp_swap_buffers();
338 }
339 return 0;
340 }
342 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
343 {
344 float halfu = (umax + umin) * 0.5f;
346 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
347 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
348 }
350 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
351 {
352 if(vrm && vrm->set_eye_texture) {
353 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
354 } else {
355 rtarg[eye].tex = tex;
356 rtarg[eye].umin = umin;
357 rtarg[eye].umax = umax;
358 rtarg[eye].vmin = vmin;
359 rtarg[eye].vmax = vmax;
360 }
361 }
363 void vr_recenter(void)
364 {
365 if(vrm && vrm->recenter) {
366 vrm->recenter();
367 }
368 }
370 static void fallback_present(void)
371 {
372 int i, show_sbs = vr_geti(VR_NULL_STEREO_SBS);
373 int use_quadbuf = vr_geti(VR_NULL_STEREO_GL);
375 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
377 glDisable(GL_LIGHTING);
378 glDisable(GL_DEPTH_TEST);
379 glDisable(GL_ALPHA_TEST);
380 glDisable(GL_STENCIL_TEST);
381 glDisable(GL_FOG);
383 glEnable(GL_TEXTURE_2D);
385 glMatrixMode(GL_MODELVIEW);
386 glPushMatrix();
387 glLoadIdentity();
388 glMatrixMode(GL_PROJECTION);
389 glPushMatrix();
390 glLoadIdentity();
392 for(i=0; i<2; i++) {
393 float x0, x1;
395 if(show_sbs && !use_quadbuf) {
396 x0 = i == 0 ? -1 : 0;
397 x1 = i == 0 ? 0 : 1;
398 } else {
399 x0 = -1;
400 x1 = 1;
401 }
403 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
405 if(use_quadbuf) {
406 glDrawBuffer(GL_BACK_LEFT + i);
407 }
409 glBegin(GL_QUADS);
410 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
411 glVertex2f(x0, -1);
412 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
413 glVertex2f(x1, -1);
414 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
415 glVertex2f(x1, 1);
416 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
417 glVertex2f(x0, 1);
418 glEnd();
420 if(!show_sbs && !use_quadbuf) break;
421 }
423 if(use_quadbuf) {
424 glDrawBuffer(GL_BACK);
425 }
427 glPopMatrix();
428 glMatrixMode(GL_MODELVIEW);
429 glPopMatrix();
431 glPopAttrib();
432 }