libgoatvr

view src/vr.c @ 15:27fcd4c2969d

added VR_EYE_RES_SCALE option
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Sep 2014 08:50:50 +0300
parents b536bd21b37f
children b2d902fff68d
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_RES_SCALE, 1.0);
40 set_option_float(defopt, VR_EYE_HEIGHT, 1.675);
41 set_option_float(defopt, VR_IPD, 0.064);
42 }
44 if(vrm) {
45 vr_shutdown();
46 }
48 vr_init_modules();
50 nmodules = vr_get_num_modules();
51 for(i=0; i<nmodules; i++) {
52 struct vr_module *m = vr_get_module(i);
53 if(m->init() != -1) {
54 /* add to the active modules array */
55 vr_activate_module(i);
56 }
57 }
59 if(!vr_get_num_active_modules()) {
60 return -1;
61 }
63 if((vrmod_env = getenv("VR_MODULE"))) {
64 vr_use_module_named(vrmod_env);
65 } else {
66 vr_use_module(0);
67 }
68 return 0;
69 }
71 void vr_shutdown(void)
72 {
73 vr_clear_modules();
74 vrm = 0;
75 fbtex_rect[0] = fbtex_rect[1] = 0;
76 fbtex_rect[2] = fbtex_rect[3] = 1;
77 }
79 int vr_module_count(void)
80 {
81 return vr_get_num_active_modules();
82 }
84 const char *vr_module_name(int idx)
85 {
86 struct vr_module *m = vr_get_active_module(idx);
87 if(!m) {
88 return 0;
89 }
90 return m->name;
91 }
93 int vr_use_module(int idx)
94 {
95 if(idx >= 0 && idx < vr_get_num_active_modules()) {
96 struct vr_module *m = vr_get_active_module(idx);
97 if(m != vrm) {
98 vrm = m;
99 printf("using vr module: %s\n", vrm->name);
100 }
101 return 0;
102 }
103 return -1;
104 }
106 int vr_use_module_named(const char *name)
107 {
108 int i, count = vr_get_num_active_modules();
110 for(i=0; i<count; i++) {
111 struct vr_module *m = vr_get_active_module(i);
112 if(strcmp(m->name, name) == 0) {
113 return vr_use_module(i);
114 }
115 }
116 return -1;
117 }
119 void vr_seti(const char *optname, int val)
120 {
121 if(vrm && vrm->set_option) {
122 vrm->set_option(optname, OTYPE_INT, &val);
123 } else {
124 set_option_int(defopt, optname, val);
125 }
126 }
128 void vr_setf(const char *optname, float val)
129 {
130 if(vrm && vrm->set_option) {
131 vrm->set_option(optname, OTYPE_FLOAT, &val);
132 } else {
133 set_option_float(defopt, optname, val);
134 }
135 }
137 int vr_geti(const char *optname)
138 {
139 int res = 0;
141 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
142 get_option_int(defopt, optname, &res); /* fallback */
143 }
144 return res;
145 }
147 float vr_getf(const char *optname)
148 {
149 float res = 0.0f;
151 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
152 get_option_float(defopt, optname, &res); /* fallback */
153 }
154 return res;
155 }
157 int vr_geti_def(const char *optname, int def_val)
158 {
159 int res = 0;
161 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
162 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
163 return def_val;
164 }
165 }
166 return res;
167 }
169 float vr_getf_def(const char *optname, float def_val)
170 {
171 float res = 0.0f;
173 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
174 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
175 return def_val;
176 }
177 }
178 return res;
179 }
181 int vr_view_translation(int eye, float *vec)
182 {
183 if(vrm && vrm->translation) {
184 vrm->translation(eye, vec);
185 return 1;
186 }
187 vec[0] = vec[1] = vec[2] = 0.0f;
188 return 0;
189 }
191 int vr_view_rotation(int eye, float *quat)
192 {
193 if(vrm && vrm->rotation) {
194 vrm->rotation(eye, quat);
195 return 1;
196 }
197 quat[0] = quat[1] = quat[2] = 0.0f;
198 quat[3] = 1.0f;
199 return 0;
200 }
202 int vr_view_matrix(int eye, float *mat)
203 {
204 int have_trans, have_rot;
205 float offs[3], quat[4];
206 float rmat[16], tmat[16];
208 if(vrm && vrm->view_matrix) {
209 vrm->view_matrix(eye, mat);
210 return 1;
211 }
213 have_trans = vr_view_translation(eye, offs);
214 have_rot = vr_view_rotation(eye, quat);
216 if(!have_trans && !have_rot) {
217 memcpy(mat, idmat, sizeof idmat);
218 return 0;
219 }
221 offs[0] = -offs[0];
222 offs[1] = -offs[1];
223 offs[2] = -offs[2];
225 vrimp_translation_matrix(offs, tmat);
226 vrimp_rotation_matrix(quat, rmat);
227 vrimp_mult_matrix(mat, tmat, rmat);
228 return 1;
229 }
231 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
232 {
233 if(vrm && vrm->proj_matrix) {
234 vrm->proj_matrix(eye, znear, zfar, mat);
235 return 1;
236 }
237 memcpy(mat, idmat, sizeof idmat);
238 return 0;
239 }
241 void vr_begin(int eye)
242 {
243 if(vrm && vrm->begin) {
244 vrm->begin(eye);
245 }
246 }
248 void vr_end(void)
249 {
250 if(vrm && vrm->end) {
251 vrm->end();
252 }
253 }
255 int vr_swap_buffers(void)
256 {
257 int res = 0;
259 if(vrm && vrm->present) {
260 res = vrm->present();
261 }
263 if(!res) {
264 fallback_present();
265 vrimp_swap_buffers();
266 }
267 return 0;
268 }
270 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
271 {
272 float halfu = (umax + umin) * 0.5f;
274 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
275 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
276 }
278 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
279 {
280 if(vrm && vrm->set_eye_texture) {
281 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
282 } else {
283 rtarg[eye].tex = tex;
284 rtarg[eye].umin = umin;
285 rtarg[eye].umax = umax;
286 rtarg[eye].vmin = vmin;
287 rtarg[eye].vmax = vmax;
288 }
289 }
291 void vr_recenter(void)
292 {
293 if(vrm && vrm->recenter) {
294 vrm->recenter();
295 }
296 }
298 static void fallback_present(void)
299 {
300 int i;
302 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
304 glDisable(GL_LIGHTING);
305 glDisable(GL_DEPTH_TEST);
306 glDisable(GL_ALPHA_TEST);
307 glDisable(GL_STENCIL_TEST);
308 glDisable(GL_FOG);
310 glEnable(GL_TEXTURE_2D);
312 glMatrixMode(GL_MODELVIEW);
313 glPushMatrix();
314 glLoadIdentity();
315 glMatrixMode(GL_PROJECTION);
316 glPushMatrix();
317 glLoadIdentity();
319 for(i=0; i<2; i++) {
320 float x0 = i == 0 ? -1 : 0;
321 float x1 = i == 0 ? 0 : 1;
323 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
325 glBegin(GL_QUADS);
326 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
327 glVertex2f(x0, -1);
328 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
329 glVertex2f(x1, -1);
330 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
331 glVertex2f(x1, 1);
332 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
333 glVertex2f(x0, 1);
334 glEnd();
335 }
337 glPopMatrix();
338 glMatrixMode(GL_MODELVIEW);
339 glPopMatrix();
341 glPopAttrib();
342 }