libgoatvr
view src/vr.c @ 18:1067274dc780
fixed the fallback vr_view_translation and vr_view_matrix to include stereo separation
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 03 Oct 2014 23:07:27 +0300 |
parents | 3de7339841d0 |
children | 437fe32ac633 |
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 float res = 0.0f;
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 float eye_offset;
219 if(vrm && vrm->translation) {
220 vrm->translation(eye, vec);
221 return 1;
222 }
224 eye_offset = vr_getf(VR_IPD) / 2.0;
225 vec[0] = eye == VR_EYE_LEFT ? -eye_offset : eye_offset;
226 vec[1] = vec[2] = 0.0f;
227 return 1;
228 }
230 int vr_view_rotation(int eye, float *quat)
231 {
232 if(vrm && vrm->rotation) {
233 vrm->rotation(eye, quat);
234 return 1;
235 }
236 quat[0] = quat[1] = quat[2] = 0.0f;
237 quat[3] = 1.0f;
238 return 0;
239 }
241 int vr_view_matrix(int eye, float *mat)
242 {
243 float offs[3], quat[4];
244 float rmat[16], tmat[16];
246 if(vrm && vrm->view_matrix) {
247 vrm->view_matrix(eye, mat);
248 return 1;
249 }
251 if(!vr_view_translation(eye, offs)) {
252 float eye_offset = vr_getf(VR_IPD) / 2.0;
253 offs[0] = eye == VR_EYE_LEFT ? -eye_offset : eye_offset;
254 offs[1] = offs[2];
255 }
256 if(!vr_view_rotation(eye, quat)) {
257 quat[0] = quat[1] = quat[2] = 0.0f;
258 quat[3] = 1.0f;
259 }
261 offs[0] = -offs[0];
262 offs[1] = -offs[1];
263 offs[2] = -offs[2];
265 vrimp_translation_matrix(offs, tmat);
266 vrimp_rotation_matrix(quat, rmat);
267 vrimp_mult_matrix(mat, tmat, rmat);
268 return 1;
269 }
271 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
272 {
273 if(vrm && vrm->proj_matrix) {
274 vrm->proj_matrix(eye, znear, zfar, mat);
275 return 1;
276 }
277 memcpy(mat, idmat, sizeof idmat);
278 return 0;
279 }
281 void vr_begin(int eye)
282 {
283 if(vrm && vrm->begin) {
284 vrm->begin(eye);
285 }
286 }
288 void vr_end(void)
289 {
290 if(vrm && vrm->end) {
291 vrm->end();
292 }
293 }
295 int vr_swap_buffers(void)
296 {
297 int res = 0;
299 if(vrm && vrm->present) {
300 res = vrm->present();
301 }
303 if(!res) {
304 fallback_present();
305 vrimp_swap_buffers();
306 }
307 return 0;
308 }
310 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
311 {
312 float halfu = (umax + umin) * 0.5f;
314 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
315 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
316 }
318 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
319 {
320 if(vrm && vrm->set_eye_texture) {
321 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
322 } else {
323 rtarg[eye].tex = tex;
324 rtarg[eye].umin = umin;
325 rtarg[eye].umax = umax;
326 rtarg[eye].vmin = vmin;
327 rtarg[eye].vmax = vmax;
328 }
329 }
331 void vr_recenter(void)
332 {
333 if(vrm && vrm->recenter) {
334 vrm->recenter();
335 }
336 }
338 static void fallback_present(void)
339 {
340 int i, show_both = vr_geti(VR_NULL_STEREO);
342 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
344 glDisable(GL_LIGHTING);
345 glDisable(GL_DEPTH_TEST);
346 glDisable(GL_ALPHA_TEST);
347 glDisable(GL_STENCIL_TEST);
348 glDisable(GL_FOG);
350 glEnable(GL_TEXTURE_2D);
352 glMatrixMode(GL_MODELVIEW);
353 glPushMatrix();
354 glLoadIdentity();
355 glMatrixMode(GL_PROJECTION);
356 glPushMatrix();
357 glLoadIdentity();
359 for(i=0; i<2; i++) {
360 float x0, x1;
362 if(show_both) {
363 x0 = i == 0 ? -1 : 0;
364 x1 = i == 0 ? 0 : 1;
365 } else {
366 x0 = -1;
367 x1 = 1;
368 }
370 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
372 glBegin(GL_QUADS);
373 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
374 glVertex2f(x0, -1);
375 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
376 glVertex2f(x1, -1);
377 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
378 glVertex2f(x1, 1);
379 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
380 glVertex2f(x0, 1);
381 glEnd();
383 if(!show_both) break;
384 }
386 glPopMatrix();
387 glMatrixMode(GL_MODELVIEW);
388 glPopMatrix();
390 glPopAttrib();
391 }