libgoatvr

view src/vr.c @ 24:d659cbedde1d

works on linux with 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 08:03:27 +0200
parents 437fe32ac633
children 7eea82cea9d2
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")) && atoi(env)) {
47 set_option_int(defopt, VR_NULL_STEREO, 1);
48 } else {
49 set_option_int(defopt, VR_NULL_STEREO, 0);
50 }
51 }
53 if(vrm) {
54 vr_shutdown();
55 }
57 vr_init_modules();
59 nmodules = vr_get_num_modules();
60 for(i=0; i<nmodules; i++) {
61 struct vr_module *m = vr_get_module(i);
62 if(m->init() != -1) {
63 /* add to the active modules array */
64 vr_activate_module(i);
65 }
66 }
68 if(!vr_get_num_active_modules()) {
69 return -1;
70 }
72 if((env = getenv("VR_MODULE"))) {
73 vr_use_module_named(env);
74 } else {
75 vr_use_module(0);
76 }
77 return 0;
78 }
80 void vr_shutdown(void)
81 {
82 vr_clear_modules();
83 vrm = 0;
84 fbtex_rect[0] = fbtex_rect[1] = 0;
85 fbtex_rect[2] = fbtex_rect[3] = 1;
86 }
88 int vr_module_count(void)
89 {
90 return vr_get_num_active_modules();
91 }
93 const char *vr_module_name(int idx)
94 {
95 struct vr_module *m = vr_get_active_module(idx);
96 if(!m) {
97 return 0;
98 }
99 return m->name;
100 }
102 int vr_use_module(int idx)
103 {
104 if(idx >= 0 && idx < vr_get_num_active_modules()) {
105 struct vr_module *m = vr_get_active_module(idx);
106 if(m != vrm) {
107 vrm = m;
108 printf("using vr module: %s\n", vrm->name);
109 }
110 return 0;
111 }
112 return -1;
113 }
115 int vr_use_module_named(const char *name)
116 {
117 int i, count = vr_get_num_active_modules();
119 for(i=0; i<count; i++) {
120 struct vr_module *m = vr_get_active_module(i);
121 if(strcmp(m->name, name) == 0) {
122 return vr_use_module(i);
123 }
124 }
125 return -1;
126 }
128 void vr_seti(const char *optname, int val)
129 {
130 if(vrm && vrm->set_option) {
131 vrm->set_option(optname, OTYPE_INT, &val);
132 } else {
133 set_option_int(defopt, optname, val);
134 }
135 }
137 void vr_setf(const char *optname, float val)
138 {
139 if(vrm && vrm->set_option) {
140 vrm->set_option(optname, OTYPE_FLOAT, &val);
141 } else {
142 set_option_float(defopt, optname, val);
143 }
144 }
146 static int def_option_int(const char *optname)
147 {
148 int res = 0;
149 int left, right;
151 if(strcmp(optname, VR_RENDER_XRES) == 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;
155 }
156 } else if(strcmp(optname, VR_RENDER_YRES) == 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 ? left : right;
160 }
161 }
163 get_option_int(defopt, optname, &res);
164 return res;
165 }
167 static float def_option_float(const char *optname)
168 {
169 float res = 0.0f;
171 if(strcmp(optname, VR_RENDER_XRES) == 0 || strcmp(optname, VR_RENDER_YRES) == 0) {
172 return (float)def_option_int(optname);
173 }
175 get_option_float(defopt, optname, &res);
176 return res;
177 }
179 static float *def_option_vec(const char *optname, float *res)
180 {
181 res[0] = res[1] = res[2] = res[3] = 0.0f;
183 get_option_vec(defopt, optname, res);
184 return res;
185 }
187 int vr_geti(const char *optname)
188 {
189 int res = 0;
191 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
192 res = def_option_int(optname);
193 }
194 return res;
195 }
197 float vr_getf(const char *optname)
198 {
199 float res = 0.0f;
201 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
202 res = def_option_float(optname);
203 }
204 return res;
205 }
207 float *vr_getfv(const char *optname, float *res)
208 {
209 static float sres[4];
210 if(!res) res = sres;
212 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_VEC, res) == -1) {
213 def_option_vec(optname, res);
214 }
215 return res;
216 }
218 int vr_geti_def(const char *optname, int def_val)
219 {
220 int res = 0;
222 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
223 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
224 return def_val;
225 }
226 }
227 return res;
228 }
230 float vr_getf_def(const char *optname, float def_val)
231 {
232 float res = 0.0f;
234 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
235 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
236 return def_val;
237 }
238 }
239 return res;
240 }
242 int vr_view_translation(int eye, float *vec)
243 {
244 if(vrm && vrm->translation) {
245 vrm->translation(eye, vec);
246 return 1;
247 }
248 vec[0] = vec[1] = vec[2] = 0.0f;
249 return 0;
250 }
252 int vr_view_rotation(int eye, float *quat)
253 {
254 if(vrm && vrm->rotation) {
255 vrm->rotation(eye, quat);
256 return 1;
257 }
258 quat[0] = quat[1] = quat[2] = 0.0f;
259 quat[3] = 1.0f;
260 return 0;
261 }
263 int vr_view_matrix(int eye, float *mat)
264 {
265 float offs[3], quat[4], eye_offs[4];
266 float rmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
267 float tmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
269 if(vrm && vrm->view_matrix) {
270 vrm->view_matrix(eye, mat);
271 return 1;
272 }
274 memcpy(mat, idmat, sizeof idmat);
276 if(vr_view_translation(eye, offs)) {
277 offs[0] = -offs[0];
278 offs[1] = -offs[1];
279 offs[2] = -offs[2];
281 vrimp_translation_matrix(offs, tmat);
282 vrimp_mult_matrix(mat, mat, tmat);
283 }
284 if(vr_view_rotation(eye, quat)) {
285 vrimp_rotation_matrix(quat, rmat);
286 vrimp_mult_matrix(mat, mat, rmat);
287 }
289 vr_getfv(eye == VR_EYE_LEFT ? VR_LEYE_OFFSET : VR_REYE_OFFSET, eye_offs);
290 eye_offs[0] = -eye_offs[0];
291 eye_offs[1] = -eye_offs[1];
292 eye_offs[2] = -eye_offs[2];
293 vrimp_translation_matrix(eye_offs, tmat);
294 vrimp_mult_matrix(mat, mat, tmat);
295 return 1;
296 }
298 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
299 {
300 if(vrm && vrm->proj_matrix) {
301 vrm->proj_matrix(eye, znear, zfar, mat);
302 return 1;
303 }
304 memcpy(mat, idmat, sizeof idmat);
305 return 0;
306 }
308 void vr_begin(int eye)
309 {
310 if(vrm && vrm->begin) {
311 vrm->begin(eye);
312 }
313 }
315 void vr_end(void)
316 {
317 if(vrm && vrm->end) {
318 vrm->end();
319 }
320 }
322 int vr_swap_buffers(void)
323 {
324 int res = 0;
326 if(vrm && vrm->present) {
327 res = vrm->present();
328 }
330 if(!res) {
331 fallback_present();
332 vrimp_swap_buffers();
333 }
334 return 0;
335 }
337 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
338 {
339 float halfu = (umax + umin) * 0.5f;
341 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
342 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
343 }
345 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
346 {
347 if(vrm && vrm->set_eye_texture) {
348 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
349 } else {
350 rtarg[eye].tex = tex;
351 rtarg[eye].umin = umin;
352 rtarg[eye].umax = umax;
353 rtarg[eye].vmin = vmin;
354 rtarg[eye].vmax = vmax;
355 }
356 }
358 void vr_recenter(void)
359 {
360 if(vrm && vrm->recenter) {
361 vrm->recenter();
362 }
363 }
365 static void fallback_present(void)
366 {
367 int i, show_both = vr_geti(VR_NULL_STEREO);
369 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
371 glDisable(GL_LIGHTING);
372 glDisable(GL_DEPTH_TEST);
373 glDisable(GL_ALPHA_TEST);
374 glDisable(GL_STENCIL_TEST);
375 glDisable(GL_FOG);
377 glEnable(GL_TEXTURE_2D);
379 glMatrixMode(GL_MODELVIEW);
380 glPushMatrix();
381 glLoadIdentity();
382 glMatrixMode(GL_PROJECTION);
383 glPushMatrix();
384 glLoadIdentity();
386 for(i=0; i<2; i++) {
387 float x0, x1;
389 if(show_both) {
390 x0 = i == 0 ? -1 : 0;
391 x1 = i == 0 ? 0 : 1;
392 } else {
393 x0 = -1;
394 x1 = 1;
395 }
397 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
399 glBegin(GL_QUADS);
400 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
401 glVertex2f(x0, -1);
402 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
403 glVertex2f(x1, -1);
404 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
405 glVertex2f(x1, 1);
406 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
407 glVertex2f(x0, 1);
408 glEnd();
410 if(!show_both) break;
411 }
413 glPopMatrix();
414 glMatrixMode(GL_MODELVIEW);
415 glPopMatrix();
417 glPopAttrib();
418 }