libgoatvr
view src/vr.c @ 16:b2d902fff68d
renamed VR_EYE_RES_SCALE to VR_RENDER_RES_SCALE
added convenience options: VR_RENDER_XRES & VR_RENDER_YRES, which are derived
from VR_LEYE_XRES, VR_REYE_XRES, etc.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 25 Sep 2014 09:06:57 +0300 |
parents | 27fcd4c2969d |
children | 3de7339841d0 |
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 }
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 static int def_option_int(const char *optname)
138 {
139 int res = 0;
140 int left, right;
142 if(strcmp(optname, VR_RENDER_XRES) == 0) {
143 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
144 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
145 return left + right;
146 }
147 } else if(strcmp(optname, VR_RENDER_YRES) == 0) {
148 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
149 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
150 return left > right ? left : right;
151 }
152 }
154 get_option_int(defopt, optname, &res);
155 return res;
156 }
158 static float def_option_float(const char *optname)
159 {
160 int res = 0;
162 if(strcmp(optname, VR_RENDER_XRES) == 0 || strcmp(optname, VR_RENDER_YRES) == 0) {
163 return (float)def_option_int(optname);
164 }
166 get_option_float(defopt, optname, &res);
167 return res;
168 }
170 int vr_geti(const char *optname)
171 {
172 int res = 0;
174 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
175 res = def_option_int(optname);
176 }
177 return res;
178 }
180 float vr_getf(const char *optname)
181 {
182 float res = 0.0f;
184 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
185 res = def_option_float(optname);
186 }
187 return res;
188 }
190 int vr_geti_def(const char *optname, int def_val)
191 {
192 int res = 0;
194 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
195 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
196 return def_val;
197 }
198 }
199 return res;
200 }
202 float vr_getf_def(const char *optname, float def_val)
203 {
204 float res = 0.0f;
206 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
207 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
208 return def_val;
209 }
210 }
211 return res;
212 }
214 int vr_view_translation(int eye, float *vec)
215 {
216 if(vrm && vrm->translation) {
217 vrm->translation(eye, vec);
218 return 1;
219 }
220 vec[0] = vec[1] = vec[2] = 0.0f;
221 return 0;
222 }
224 int vr_view_rotation(int eye, float *quat)
225 {
226 if(vrm && vrm->rotation) {
227 vrm->rotation(eye, quat);
228 return 1;
229 }
230 quat[0] = quat[1] = quat[2] = 0.0f;
231 quat[3] = 1.0f;
232 return 0;
233 }
235 int vr_view_matrix(int eye, float *mat)
236 {
237 int have_trans, have_rot;
238 float offs[3], quat[4];
239 float rmat[16], tmat[16];
241 if(vrm && vrm->view_matrix) {
242 vrm->view_matrix(eye, mat);
243 return 1;
244 }
246 have_trans = vr_view_translation(eye, offs);
247 have_rot = vr_view_rotation(eye, quat);
249 if(!have_trans && !have_rot) {
250 memcpy(mat, idmat, sizeof idmat);
251 return 0;
252 }
254 offs[0] = -offs[0];
255 offs[1] = -offs[1];
256 offs[2] = -offs[2];
258 vrimp_translation_matrix(offs, tmat);
259 vrimp_rotation_matrix(quat, rmat);
260 vrimp_mult_matrix(mat, tmat, rmat);
261 return 1;
262 }
264 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
265 {
266 if(vrm && vrm->proj_matrix) {
267 vrm->proj_matrix(eye, znear, zfar, mat);
268 return 1;
269 }
270 memcpy(mat, idmat, sizeof idmat);
271 return 0;
272 }
274 void vr_begin(int eye)
275 {
276 if(vrm && vrm->begin) {
277 vrm->begin(eye);
278 }
279 }
281 void vr_end(void)
282 {
283 if(vrm && vrm->end) {
284 vrm->end();
285 }
286 }
288 int vr_swap_buffers(void)
289 {
290 int res = 0;
292 if(vrm && vrm->present) {
293 res = vrm->present();
294 }
296 if(!res) {
297 fallback_present();
298 vrimp_swap_buffers();
299 }
300 return 0;
301 }
303 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
304 {
305 float halfu = (umax + umin) * 0.5f;
307 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
308 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
309 }
311 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
312 {
313 if(vrm && vrm->set_eye_texture) {
314 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
315 } else {
316 rtarg[eye].tex = tex;
317 rtarg[eye].umin = umin;
318 rtarg[eye].umax = umax;
319 rtarg[eye].vmin = vmin;
320 rtarg[eye].vmax = vmax;
321 }
322 }
324 void vr_recenter(void)
325 {
326 if(vrm && vrm->recenter) {
327 vrm->recenter();
328 }
329 }
331 static void fallback_present(void)
332 {
333 int i;
335 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
337 glDisable(GL_LIGHTING);
338 glDisable(GL_DEPTH_TEST);
339 glDisable(GL_ALPHA_TEST);
340 glDisable(GL_STENCIL_TEST);
341 glDisable(GL_FOG);
343 glEnable(GL_TEXTURE_2D);
345 glMatrixMode(GL_MODELVIEW);
346 glPushMatrix();
347 glLoadIdentity();
348 glMatrixMode(GL_PROJECTION);
349 glPushMatrix();
350 glLoadIdentity();
352 for(i=0; i<2; i++) {
353 float x0 = i == 0 ? -1 : 0;
354 float x1 = i == 0 ? 0 : 1;
356 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
358 glBegin(GL_QUADS);
359 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
360 glVertex2f(x0, -1);
361 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
362 glVertex2f(x1, -1);
363 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
364 glVertex2f(x1, 1);
365 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
366 glVertex2f(x0, 1);
367 glEnd();
368 }
370 glPopMatrix();
371 glMatrixMode(GL_MODELVIEW);
372 glPopMatrix();
374 glPopAttrib();
375 }