libgoatvr

view src/vr.c @ 17:3de7339841d0

option for the null module fallback presentation to show one or both eye views
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Sep 2014 17:14:04 +0300
parents b2d902fff68d
children 1067274dc780
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"
10 static void fallback_present(void);
13 static struct vr_module *vrm;
14 static float idmat[] = {
15 1, 0, 0, 0,
16 0, 1, 0, 0,
17 0, 0, 1, 0,
18 0, 0, 0, 1
19 };
20 static float fbtex_rect[] = {
21 0, 0, 1, 1
22 };
24 static void *defopt; /* default options db */
26 static struct {
27 float umin, umax, vmin, vmax;
28 int tex;
29 } rtarg[2];
32 int vr_init(void)
33 {
34 int i, nmodules;
35 char *vrmod_env;
37 /* create the default options database */
38 if(!defopt && (defopt = create_options())) {
39 set_option_float(defopt, VR_RENDER_RES_SCALE, 1.0);
40 set_option_float(defopt, VR_EYE_HEIGHT, 1.675);
41 set_option_float(defopt, VR_IPD, 0.064);
42 set_option_int(defopt, VR_NULL_STEREO, 0);
43 }
45 if(vrm) {
46 vr_shutdown();
47 }
49 vr_init_modules();
51 nmodules = vr_get_num_modules();
52 for(i=0; i<nmodules; i++) {
53 struct vr_module *m = vr_get_module(i);
54 if(m->init() != -1) {
55 /* add to the active modules array */
56 vr_activate_module(i);
57 }
58 }
60 if(!vr_get_num_active_modules()) {
61 return -1;
62 }
64 if((vrmod_env = getenv("VR_MODULE"))) {
65 vr_use_module_named(vrmod_env);
66 } else {
67 vr_use_module(0);
68 }
69 return 0;
70 }
72 void vr_shutdown(void)
73 {
74 vr_clear_modules();
75 vrm = 0;
76 fbtex_rect[0] = fbtex_rect[1] = 0;
77 fbtex_rect[2] = fbtex_rect[3] = 1;
78 }
80 int vr_module_count(void)
81 {
82 return vr_get_num_active_modules();
83 }
85 const char *vr_module_name(int idx)
86 {
87 struct vr_module *m = vr_get_active_module(idx);
88 if(!m) {
89 return 0;
90 }
91 return m->name;
92 }
94 int vr_use_module(int idx)
95 {
96 if(idx >= 0 && idx < vr_get_num_active_modules()) {
97 struct vr_module *m = vr_get_active_module(idx);
98 if(m != vrm) {
99 vrm = m;
100 printf("using vr module: %s\n", vrm->name);
101 }
102 return 0;
103 }
104 return -1;
105 }
107 int vr_use_module_named(const char *name)
108 {
109 int i, count = vr_get_num_active_modules();
111 for(i=0; i<count; i++) {
112 struct vr_module *m = vr_get_active_module(i);
113 if(strcmp(m->name, name) == 0) {
114 return vr_use_module(i);
115 }
116 }
117 return -1;
118 }
120 void vr_seti(const char *optname, int val)
121 {
122 if(vrm && vrm->set_option) {
123 vrm->set_option(optname, OTYPE_INT, &val);
124 } else {
125 set_option_int(defopt, optname, val);
126 }
127 }
129 void vr_setf(const char *optname, float val)
130 {
131 if(vrm && vrm->set_option) {
132 vrm->set_option(optname, OTYPE_FLOAT, &val);
133 } else {
134 set_option_float(defopt, optname, val);
135 }
136 }
138 static int def_option_int(const char *optname)
139 {
140 int res = 0;
141 int left, right;
143 if(strcmp(optname, VR_RENDER_XRES) == 0) {
144 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
145 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
146 return left + right;
147 }
148 } else if(strcmp(optname, VR_RENDER_YRES) == 0) {
149 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
150 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
151 return left > right ? left : right;
152 }
153 }
155 get_option_int(defopt, optname, &res);
156 return res;
157 }
159 static float def_option_float(const char *optname)
160 {
161 int res = 0;
163 if(strcmp(optname, VR_RENDER_XRES) == 0 || strcmp(optname, VR_RENDER_YRES) == 0) {
164 return (float)def_option_int(optname);
165 }
167 get_option_float(defopt, optname, &res);
168 return res;
169 }
171 int vr_geti(const char *optname)
172 {
173 int res = 0;
175 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
176 res = def_option_int(optname);
177 }
178 return res;
179 }
181 float vr_getf(const char *optname)
182 {
183 float res = 0.0f;
185 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
186 res = def_option_float(optname);
187 }
188 return res;
189 }
191 int vr_geti_def(const char *optname, int def_val)
192 {
193 int res = 0;
195 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
196 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
197 return def_val;
198 }
199 }
200 return res;
201 }
203 float vr_getf_def(const char *optname, float def_val)
204 {
205 float res = 0.0f;
207 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
208 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
209 return def_val;
210 }
211 }
212 return res;
213 }
215 int vr_view_translation(int eye, float *vec)
216 {
217 if(vrm && vrm->translation) {
218 vrm->translation(eye, vec);
219 return 1;
220 }
221 vec[0] = vec[1] = vec[2] = 0.0f;
222 return 0;
223 }
225 int vr_view_rotation(int eye, float *quat)
226 {
227 if(vrm && vrm->rotation) {
228 vrm->rotation(eye, quat);
229 return 1;
230 }
231 quat[0] = quat[1] = quat[2] = 0.0f;
232 quat[3] = 1.0f;
233 return 0;
234 }
236 int vr_view_matrix(int eye, float *mat)
237 {
238 int have_trans, have_rot;
239 float offs[3], quat[4];
240 float rmat[16], tmat[16];
242 if(vrm && vrm->view_matrix) {
243 vrm->view_matrix(eye, mat);
244 return 1;
245 }
247 have_trans = vr_view_translation(eye, offs);
248 have_rot = vr_view_rotation(eye, quat);
250 if(!have_trans && !have_rot) {
251 memcpy(mat, idmat, sizeof idmat);
252 return 0;
253 }
255 offs[0] = -offs[0];
256 offs[1] = -offs[1];
257 offs[2] = -offs[2];
259 vrimp_translation_matrix(offs, tmat);
260 vrimp_rotation_matrix(quat, rmat);
261 vrimp_mult_matrix(mat, tmat, rmat);
262 return 1;
263 }
265 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
266 {
267 if(vrm && vrm->proj_matrix) {
268 vrm->proj_matrix(eye, znear, zfar, mat);
269 return 1;
270 }
271 memcpy(mat, idmat, sizeof idmat);
272 return 0;
273 }
275 void vr_begin(int eye)
276 {
277 if(vrm && vrm->begin) {
278 vrm->begin(eye);
279 }
280 }
282 void vr_end(void)
283 {
284 if(vrm && vrm->end) {
285 vrm->end();
286 }
287 }
289 int vr_swap_buffers(void)
290 {
291 int res = 0;
293 if(vrm && vrm->present) {
294 res = vrm->present();
295 }
297 if(!res) {
298 fallback_present();
299 vrimp_swap_buffers();
300 }
301 return 0;
302 }
304 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
305 {
306 float halfu = (umax + umin) * 0.5f;
308 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
309 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
310 }
312 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
313 {
314 if(vrm && vrm->set_eye_texture) {
315 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
316 } else {
317 rtarg[eye].tex = tex;
318 rtarg[eye].umin = umin;
319 rtarg[eye].umax = umax;
320 rtarg[eye].vmin = vmin;
321 rtarg[eye].vmax = vmax;
322 }
323 }
325 void vr_recenter(void)
326 {
327 if(vrm && vrm->recenter) {
328 vrm->recenter();
329 }
330 }
332 static void fallback_present(void)
333 {
334 int i, show_both = vr_geti(VR_NULL_STEREO);
336 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
338 glDisable(GL_LIGHTING);
339 glDisable(GL_DEPTH_TEST);
340 glDisable(GL_ALPHA_TEST);
341 glDisable(GL_STENCIL_TEST);
342 glDisable(GL_FOG);
344 glEnable(GL_TEXTURE_2D);
346 glMatrixMode(GL_MODELVIEW);
347 glPushMatrix();
348 glLoadIdentity();
349 glMatrixMode(GL_PROJECTION);
350 glPushMatrix();
351 glLoadIdentity();
353 for(i=0; i<2; i++) {
354 float x0, x1;
356 if(show_both) {
357 x0 = i == 0 ? -1 : 0;
358 x1 = i == 0 ? 0 : 1;
359 } else {
360 x0 = -1;
361 x1 = 1;
362 }
364 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
366 glBegin(GL_QUADS);
367 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
368 glVertex2f(x0, -1);
369 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
370 glVertex2f(x1, -1);
371 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
372 glVertex2f(x1, 1);
373 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
374 glVertex2f(x0, 1);
375 glEnd();
377 if(!show_both) break;
378 }
380 glPopMatrix();
381 glMatrixMode(GL_MODELVIEW);
382 glPopMatrix();
384 glPopAttrib();
385 }