libgoatvr

view src/vr.c @ 5:e63cb28fc644

working on the linux side a bit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Sep 2014 10:56:45 +0300
parents ded3d0a74e19
children 6896f9cf9621
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "opengl.h"
4 #include "vr.h"
5 #include "vr_impl.h"
6 #include "mathutil.h"
9 static void fallback_present(void);
12 static struct vr_module *vrm;
13 static float idmat[] = {
14 1, 0, 0, 0,
15 0, 1, 0, 0,
16 0, 0, 1, 0,
17 0, 0, 0, 1
18 };
19 static float fbtex_rect[] = {
20 0, 0, 1, 1
21 };
23 static void *defopt; /* default options db */
25 static struct {
26 float umin, umax, vmin, vmax;
27 int tex;
28 } rtarg[2];
31 int vr_init(void)
32 {
33 int i, nmodules;
35 /* create the default options database */
36 if(!defopt && (defopt = create_options())) {
37 set_option_float(defopt, VR_OPT_EYE_HEIGHT, 1.675);
38 set_option_float(defopt, VR_OPT_IPD, 0.064);
39 }
41 if(vrm) {
42 vr_shutdown();
43 }
45 vr_init_modules();
47 nmodules = vr_get_num_modules();
48 for(i=0; i<nmodules; i++) {
49 struct vr_module *m = vr_get_module(i);
50 if(m->init() != -1) {
51 /* add to the active modules array */
52 vr_activate_module(i);
53 if(!vrm) {
54 vr_use_module(0);
55 }
56 }
57 }
59 if(!vrm) {
60 return -1;
61 }
62 return 0;
63 }
65 void vr_shutdown(void)
66 {
67 vr_clear_modules();
68 vrm = 0;
69 fbtex_rect[0] = fbtex_rect[1] = 0;
70 fbtex_rect[2] = fbtex_rect[3] = 1;
71 }
73 int vr_module_count(void)
74 {
75 return vr_get_num_active_modules();
76 }
78 const char *vr_module_name(int idx)
79 {
80 struct vr_module *m = vr_get_active_module(idx);
81 if(!m) {
82 return 0;
83 }
84 return m->name;
85 }
87 int vr_use_module(int idx)
88 {
89 if(idx >= 0 && idx < vr_get_num_active_modules()) {
90 struct vr_module *m = vr_get_active_module(idx);
91 if(m != vrm) {
92 vrm = m;
93 printf("using vr module: %s\n", vrm->name);
94 }
95 return 0;
96 }
97 return -1;
98 }
100 int vr_use_module_named(const char *name)
101 {
102 int i, count = vr_get_num_active_modules();
104 for(i=0; i<count; i++) {
105 struct vr_module *m = vr_get_active_module(i);
106 if(strcmp(m->name, name) == 0) {
107 return vr_use_module(i);
108 }
109 }
110 return -1;
111 }
113 void vr_set_opti(const char *optname, int val)
114 {
115 if(vrm && vrm->set_option) {
116 vrm->set_option(optname, OTYPE_INT, &val);
117 } else {
118 set_option_int(defopt, optname, val);
119 }
120 }
122 void vr_set_optf(const char *optname, float val)
123 {
124 if(vrm && vrm->set_option) {
125 vrm->set_option(optname, OTYPE_FLOAT, &val);
126 } else {
127 set_option_float(defopt, optname, val);
128 }
129 }
131 int vr_get_opti(const char *optname)
132 {
133 int res = 0;
135 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
136 get_option_int(defopt, optname, &res); /* fallback */
137 }
138 return res;
139 }
141 float vr_get_optf(const char *optname)
142 {
143 float res = 0.0f;
145 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
146 get_option_float(defopt, optname, &res); /* fallback */
147 }
148 return res;
149 }
152 int vr_view_translation(int eye, float *vec)
153 {
154 if(vrm && vrm->translation) {
155 vrm->translation(eye, vec);
156 return 1;
157 }
158 vec[0] = vec[1] = vec[2] = 0.0f;
159 return 0;
160 }
162 int vr_view_rotation(int eye, float *quat)
163 {
164 if(vrm && vrm->rotation) {
165 vrm->rotation(eye, quat);
166 return 1;
167 }
168 quat[0] = quat[1] = quat[2] = 0.0f;
169 quat[3] = 1.0f;
170 return 0;
171 }
173 int vr_view_matrix(int eye, float *mat)
174 {
175 int have_trans, have_rot;
176 float offs[3], quat[4];
177 float rmat[16], tmat[16];
179 if(vrm && vrm->view_matrix) {
180 vrm->view_matrix(eye, mat);
181 return 1;
182 }
184 have_trans = vr_view_translation(eye, offs);
185 have_rot = vr_view_rotation(eye, quat);
187 if(!have_trans && !have_rot) {
188 memcpy(mat, idmat, sizeof idmat);
189 return 0;
190 }
192 offs[0] = -offs[0];
193 offs[1] = -offs[1];
194 offs[2] = -offs[2];
196 translation_matrix(offs, tmat);
197 rotation_matrix(quat, rmat);
198 mult_matrix(mat, tmat, rmat);
199 return 1;
200 }
202 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
203 {
204 if(vrm && vrm->proj_matrix) {
205 vrm->proj_matrix(eye, znear, zfar, mat);
206 return 1;
207 }
208 memcpy(mat, idmat, sizeof idmat);
209 return 0;
210 }
212 void vr_begin(int eye)
213 {
214 if(vrm && vrm->begin) {
215 vrm->begin(eye);
216 }
217 }
219 void vr_end(void)
220 {
221 if(vrm && vrm->end) {
222 vrm->end();
223 }
224 }
226 int vr_swap_buffers(void)
227 {
228 int res = 0;
230 if(vrm && vrm->present) {
231 res = vrm->present();
232 }
234 if(!res) {
235 fallback_present();
236 vr_gl_swap_buffers();
237 }
238 return 0;
239 }
241 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
242 {
243 float halfu = (umax + umin) * 0.5f;
245 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
246 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
247 }
249 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
250 {
251 if(vrm && vrm->set_eye_texture) {
252 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
253 } else {
254 rtarg[eye].tex = tex;
255 rtarg[eye].umin = umin;
256 rtarg[eye].umax = umax;
257 rtarg[eye].vmin = vmin;
258 rtarg[eye].vmax = vmax;
259 }
260 }
262 void vr_recenter(void)
263 {
264 if(vrm && vrm->recenter) {
265 vrm->recenter();
266 }
267 }
269 static void fallback_present(void)
270 {
271 int i;
273 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
275 glDisable(GL_LIGHTING);
276 glDisable(GL_DEPTH_TEST);
277 glDisable(GL_ALPHA_TEST);
278 glDisable(GL_STENCIL_TEST);
279 glDisable(GL_FOG);
281 glEnable(GL_TEXTURE_2D);
283 glMatrixMode(GL_MODELVIEW);
284 glPushMatrix();
285 glLoadIdentity();
286 glMatrixMode(GL_PROJECTION);
287 glPushMatrix();
288 glLoadIdentity();
290 for(i=0; i<2; i++) {
291 float x0 = i == 0 ? -1 : 0;
292 float x1 = i == 0 ? 0 : 1;
294 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
296 glBegin(GL_QUADS);
297 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
298 glVertex2f(x0, -1);
299 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
300 glVertex2f(x1, -1);
301 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
302 glVertex2f(x1, 1);
303 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
304 glVertex2f(x0, 1);
305 glEnd();
306 }
308 glPopMatrix();
309 glMatrixMode(GL_MODELVIEW);
310 glPopMatrix();
312 glPopAttrib();
313 }