libgoatvr

view src/vr.c @ 12:b536bd21b37f

added vr_get_def functions
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 24 Sep 2014 23:50:40 +0300
parents 34d4643d61f9
children 27fcd4c2969d
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_EYE_HEIGHT, 1.675);
40 set_option_float(defopt, VR_IPD, 0.064);
41 }
43 if(vrm) {
44 vr_shutdown();
45 }
47 vr_init_modules();
49 nmodules = vr_get_num_modules();
50 for(i=0; i<nmodules; i++) {
51 struct vr_module *m = vr_get_module(i);
52 if(m->init() != -1) {
53 /* add to the active modules array */
54 vr_activate_module(i);
55 }
56 }
58 if(!vr_get_num_active_modules()) {
59 return -1;
60 }
62 if((vrmod_env = getenv("VR_MODULE"))) {
63 vr_use_module_named(vrmod_env);
64 } else {
65 vr_use_module(0);
66 }
67 return 0;
68 }
70 void vr_shutdown(void)
71 {
72 vr_clear_modules();
73 vrm = 0;
74 fbtex_rect[0] = fbtex_rect[1] = 0;
75 fbtex_rect[2] = fbtex_rect[3] = 1;
76 }
78 int vr_module_count(void)
79 {
80 return vr_get_num_active_modules();
81 }
83 const char *vr_module_name(int idx)
84 {
85 struct vr_module *m = vr_get_active_module(idx);
86 if(!m) {
87 return 0;
88 }
89 return m->name;
90 }
92 int vr_use_module(int idx)
93 {
94 if(idx >= 0 && idx < vr_get_num_active_modules()) {
95 struct vr_module *m = vr_get_active_module(idx);
96 if(m != vrm) {
97 vrm = m;
98 printf("using vr module: %s\n", vrm->name);
99 }
100 return 0;
101 }
102 return -1;
103 }
105 int vr_use_module_named(const char *name)
106 {
107 int i, count = vr_get_num_active_modules();
109 for(i=0; i<count; i++) {
110 struct vr_module *m = vr_get_active_module(i);
111 if(strcmp(m->name, name) == 0) {
112 return vr_use_module(i);
113 }
114 }
115 return -1;
116 }
118 void vr_seti(const char *optname, int val)
119 {
120 if(vrm && vrm->set_option) {
121 vrm->set_option(optname, OTYPE_INT, &val);
122 } else {
123 set_option_int(defopt, optname, val);
124 }
125 }
127 void vr_setf(const char *optname, float val)
128 {
129 if(vrm && vrm->set_option) {
130 vrm->set_option(optname, OTYPE_FLOAT, &val);
131 } else {
132 set_option_float(defopt, optname, val);
133 }
134 }
136 int vr_geti(const char *optname)
137 {
138 int res = 0;
140 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
141 get_option_int(defopt, optname, &res); /* fallback */
142 }
143 return res;
144 }
146 float vr_getf(const char *optname)
147 {
148 float res = 0.0f;
150 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
151 get_option_float(defopt, optname, &res); /* fallback */
152 }
153 return res;
154 }
156 int vr_geti_def(const char *optname, int def_val)
157 {
158 int res = 0;
160 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
161 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
162 return def_val;
163 }
164 }
165 return res;
166 }
168 float vr_getf_def(const char *optname, float def_val)
169 {
170 float res = 0.0f;
172 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
173 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
174 return def_val;
175 }
176 }
177 return res;
178 }
180 int vr_view_translation(int eye, float *vec)
181 {
182 if(vrm && vrm->translation) {
183 vrm->translation(eye, vec);
184 return 1;
185 }
186 vec[0] = vec[1] = vec[2] = 0.0f;
187 return 0;
188 }
190 int vr_view_rotation(int eye, float *quat)
191 {
192 if(vrm && vrm->rotation) {
193 vrm->rotation(eye, quat);
194 return 1;
195 }
196 quat[0] = quat[1] = quat[2] = 0.0f;
197 quat[3] = 1.0f;
198 return 0;
199 }
201 int vr_view_matrix(int eye, float *mat)
202 {
203 int have_trans, have_rot;
204 float offs[3], quat[4];
205 float rmat[16], tmat[16];
207 if(vrm && vrm->view_matrix) {
208 vrm->view_matrix(eye, mat);
209 return 1;
210 }
212 have_trans = vr_view_translation(eye, offs);
213 have_rot = vr_view_rotation(eye, quat);
215 if(!have_trans && !have_rot) {
216 memcpy(mat, idmat, sizeof idmat);
217 return 0;
218 }
220 offs[0] = -offs[0];
221 offs[1] = -offs[1];
222 offs[2] = -offs[2];
224 vrimp_translation_matrix(offs, tmat);
225 vrimp_rotation_matrix(quat, rmat);
226 vrimp_mult_matrix(mat, tmat, rmat);
227 return 1;
228 }
230 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
231 {
232 if(vrm && vrm->proj_matrix) {
233 vrm->proj_matrix(eye, znear, zfar, mat);
234 return 1;
235 }
236 memcpy(mat, idmat, sizeof idmat);
237 return 0;
238 }
240 void vr_begin(int eye)
241 {
242 if(vrm && vrm->begin) {
243 vrm->begin(eye);
244 }
245 }
247 void vr_end(void)
248 {
249 if(vrm && vrm->end) {
250 vrm->end();
251 }
252 }
254 int vr_swap_buffers(void)
255 {
256 int res = 0;
258 if(vrm && vrm->present) {
259 res = vrm->present();
260 }
262 if(!res) {
263 fallback_present();
264 vrimp_swap_buffers();
265 }
266 return 0;
267 }
269 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
270 {
271 float halfu = (umax + umin) * 0.5f;
273 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
274 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
275 }
277 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
278 {
279 if(vrm && vrm->set_eye_texture) {
280 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
281 } else {
282 rtarg[eye].tex = tex;
283 rtarg[eye].umin = umin;
284 rtarg[eye].umax = umax;
285 rtarg[eye].vmin = vmin;
286 rtarg[eye].vmax = vmax;
287 }
288 }
290 void vr_recenter(void)
291 {
292 if(vrm && vrm->recenter) {
293 vrm->recenter();
294 }
295 }
297 static void fallback_present(void)
298 {
299 int i;
301 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
303 glDisable(GL_LIGHTING);
304 glDisable(GL_DEPTH_TEST);
305 glDisable(GL_ALPHA_TEST);
306 glDisable(GL_STENCIL_TEST);
307 glDisable(GL_FOG);
309 glEnable(GL_TEXTURE_2D);
311 glMatrixMode(GL_MODELVIEW);
312 glPushMatrix();
313 glLoadIdentity();
314 glMatrixMode(GL_PROJECTION);
315 glPushMatrix();
316 glLoadIdentity();
318 for(i=0; i<2; i++) {
319 float x0 = i == 0 ? -1 : 0;
320 float x1 = i == 0 ? 0 : 1;
322 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
324 glBegin(GL_QUADS);
325 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
326 glVertex2f(x0, -1);
327 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
328 glVertex2f(x1, -1);
329 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
330 glVertex2f(x1, 1);
331 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
332 glVertex2f(x0, 1);
333 glEnd();
334 }
336 glPopMatrix();
337 glMatrixMode(GL_MODELVIEW);
338 glPopMatrix();
340 glPopAttrib();
341 }