libgoatvr

annotate src/vr.c @ 14:604a9a0a8c37

fixed call to ovrHmd_ConfigureTracking to stop setting the ovrTrackingCap_Idle bit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Sep 2014 07:52:49 +0300
parents 34d4643d61f9
children 27fcd4c2969d
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@7 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@5 4 #include "opengl.h"
nuclear@0 5 #include "vr.h"
nuclear@0 6 #include "vr_impl.h"
nuclear@0 7 #include "mathutil.h"
nuclear@0 8
nuclear@0 9
nuclear@5 10 static void fallback_present(void);
nuclear@0 11
nuclear@0 12
nuclear@0 13 static struct vr_module *vrm;
nuclear@0 14 static float idmat[] = {
nuclear@0 15 1, 0, 0, 0,
nuclear@0 16 0, 1, 0, 0,
nuclear@0 17 0, 0, 1, 0,
nuclear@0 18 0, 0, 0, 1
nuclear@0 19 };
nuclear@0 20 static float fbtex_rect[] = {
nuclear@0 21 0, 0, 1, 1
nuclear@0 22 };
nuclear@0 23
nuclear@5 24 static void *defopt; /* default options db */
nuclear@5 25
nuclear@5 26 static struct {
nuclear@5 27 float umin, umax, vmin, vmax;
nuclear@5 28 int tex;
nuclear@5 29 } rtarg[2];
nuclear@5 30
nuclear@5 31
nuclear@0 32 int vr_init(void)
nuclear@0 33 {
nuclear@0 34 int i, nmodules;
nuclear@7 35 char *vrmod_env;
nuclear@0 36
nuclear@5 37 /* create the default options database */
nuclear@5 38 if(!defopt && (defopt = create_options())) {
nuclear@11 39 set_option_float(defopt, VR_EYE_HEIGHT, 1.675);
nuclear@11 40 set_option_float(defopt, VR_IPD, 0.064);
nuclear@5 41 }
nuclear@5 42
nuclear@0 43 if(vrm) {
nuclear@0 44 vr_shutdown();
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 vr_init_modules();
nuclear@0 48
nuclear@0 49 nmodules = vr_get_num_modules();
nuclear@0 50 for(i=0; i<nmodules; i++) {
nuclear@0 51 struct vr_module *m = vr_get_module(i);
nuclear@0 52 if(m->init() != -1) {
nuclear@0 53 /* add to the active modules array */
nuclear@0 54 vr_activate_module(i);
nuclear@0 55 }
nuclear@0 56 }
nuclear@0 57
nuclear@7 58 if(!vr_get_num_active_modules()) {
nuclear@0 59 return -1;
nuclear@0 60 }
nuclear@7 61
nuclear@7 62 if((vrmod_env = getenv("VR_MODULE"))) {
nuclear@7 63 vr_use_module_named(vrmod_env);
nuclear@7 64 } else {
nuclear@7 65 vr_use_module(0);
nuclear@7 66 }
nuclear@0 67 return 0;
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 void vr_shutdown(void)
nuclear@0 71 {
nuclear@0 72 vr_clear_modules();
nuclear@0 73 vrm = 0;
nuclear@0 74 fbtex_rect[0] = fbtex_rect[1] = 0;
nuclear@0 75 fbtex_rect[2] = fbtex_rect[3] = 1;
nuclear@0 76 }
nuclear@0 77
nuclear@0 78 int vr_module_count(void)
nuclear@0 79 {
nuclear@0 80 return vr_get_num_active_modules();
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 const char *vr_module_name(int idx)
nuclear@0 84 {
nuclear@0 85 struct vr_module *m = vr_get_active_module(idx);
nuclear@0 86 if(!m) {
nuclear@0 87 return 0;
nuclear@0 88 }
nuclear@0 89 return m->name;
nuclear@0 90 }
nuclear@0 91
nuclear@0 92 int vr_use_module(int idx)
nuclear@0 93 {
nuclear@0 94 if(idx >= 0 && idx < vr_get_num_active_modules()) {
nuclear@0 95 struct vr_module *m = vr_get_active_module(idx);
nuclear@0 96 if(m != vrm) {
nuclear@0 97 vrm = m;
nuclear@0 98 printf("using vr module: %s\n", vrm->name);
nuclear@0 99 }
nuclear@0 100 return 0;
nuclear@0 101 }
nuclear@0 102 return -1;
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 int vr_use_module_named(const char *name)
nuclear@0 106 {
nuclear@0 107 int i, count = vr_get_num_active_modules();
nuclear@0 108
nuclear@0 109 for(i=0; i<count; i++) {
nuclear@0 110 struct vr_module *m = vr_get_active_module(i);
nuclear@0 111 if(strcmp(m->name, name) == 0) {
nuclear@0 112 return vr_use_module(i);
nuclear@0 113 }
nuclear@0 114 }
nuclear@0 115 return -1;
nuclear@0 116 }
nuclear@0 117
nuclear@11 118 void vr_seti(const char *optname, int val)
nuclear@0 119 {
nuclear@0 120 if(vrm && vrm->set_option) {
nuclear@0 121 vrm->set_option(optname, OTYPE_INT, &val);
nuclear@5 122 } else {
nuclear@5 123 set_option_int(defopt, optname, val);
nuclear@0 124 }
nuclear@0 125 }
nuclear@0 126
nuclear@11 127 void vr_setf(const char *optname, float val)
nuclear@0 128 {
nuclear@0 129 if(vrm && vrm->set_option) {
nuclear@0 130 vrm->set_option(optname, OTYPE_FLOAT, &val);
nuclear@5 131 } else {
nuclear@5 132 set_option_float(defopt, optname, val);
nuclear@0 133 }
nuclear@0 134 }
nuclear@0 135
nuclear@11 136 int vr_geti(const char *optname)
nuclear@0 137 {
nuclear@0 138 int res = 0;
nuclear@0 139
nuclear@5 140 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
nuclear@5 141 get_option_int(defopt, optname, &res); /* fallback */
nuclear@0 142 }
nuclear@0 143 return res;
nuclear@0 144 }
nuclear@0 145
nuclear@11 146 float vr_getf(const char *optname)
nuclear@0 147 {
nuclear@0 148 float res = 0.0f;
nuclear@0 149
nuclear@5 150 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
nuclear@5 151 get_option_float(defopt, optname, &res); /* fallback */
nuclear@0 152 }
nuclear@0 153 return res;
nuclear@0 154 }
nuclear@0 155
nuclear@12 156 int vr_geti_def(const char *optname, int def_val)
nuclear@12 157 {
nuclear@12 158 int res = 0;
nuclear@12 159
nuclear@12 160 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
nuclear@12 161 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
nuclear@12 162 return def_val;
nuclear@12 163 }
nuclear@12 164 }
nuclear@12 165 return res;
nuclear@12 166 }
nuclear@12 167
nuclear@12 168 float vr_getf_def(const char *optname, float def_val)
nuclear@12 169 {
nuclear@12 170 float res = 0.0f;
nuclear@12 171
nuclear@12 172 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
nuclear@12 173 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
nuclear@12 174 return def_val;
nuclear@12 175 }
nuclear@12 176 }
nuclear@12 177 return res;
nuclear@12 178 }
nuclear@0 179
nuclear@0 180 int vr_view_translation(int eye, float *vec)
nuclear@0 181 {
nuclear@0 182 if(vrm && vrm->translation) {
nuclear@0 183 vrm->translation(eye, vec);
nuclear@0 184 return 1;
nuclear@0 185 }
nuclear@0 186 vec[0] = vec[1] = vec[2] = 0.0f;
nuclear@0 187 return 0;
nuclear@0 188 }
nuclear@0 189
nuclear@0 190 int vr_view_rotation(int eye, float *quat)
nuclear@0 191 {
nuclear@0 192 if(vrm && vrm->rotation) {
nuclear@0 193 vrm->rotation(eye, quat);
nuclear@0 194 return 1;
nuclear@0 195 }
nuclear@0 196 quat[0] = quat[1] = quat[2] = 0.0f;
nuclear@0 197 quat[3] = 1.0f;
nuclear@0 198 return 0;
nuclear@0 199 }
nuclear@0 200
nuclear@0 201 int vr_view_matrix(int eye, float *mat)
nuclear@0 202 {
nuclear@0 203 int have_trans, have_rot;
nuclear@0 204 float offs[3], quat[4];
nuclear@0 205 float rmat[16], tmat[16];
nuclear@0 206
nuclear@0 207 if(vrm && vrm->view_matrix) {
nuclear@0 208 vrm->view_matrix(eye, mat);
nuclear@0 209 return 1;
nuclear@0 210 }
nuclear@0 211
nuclear@0 212 have_trans = vr_view_translation(eye, offs);
nuclear@0 213 have_rot = vr_view_rotation(eye, quat);
nuclear@0 214
nuclear@0 215 if(!have_trans && !have_rot) {
nuclear@5 216 memcpy(mat, idmat, sizeof idmat);
nuclear@0 217 return 0;
nuclear@0 218 }
nuclear@0 219
nuclear@0 220 offs[0] = -offs[0];
nuclear@0 221 offs[1] = -offs[1];
nuclear@0 222 offs[2] = -offs[2];
nuclear@0 223
nuclear@8 224 vrimp_translation_matrix(offs, tmat);
nuclear@8 225 vrimp_rotation_matrix(quat, rmat);
nuclear@8 226 vrimp_mult_matrix(mat, tmat, rmat);
nuclear@0 227 return 1;
nuclear@0 228 }
nuclear@0 229
nuclear@0 230 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
nuclear@0 231 {
nuclear@0 232 if(vrm && vrm->proj_matrix) {
nuclear@0 233 vrm->proj_matrix(eye, znear, zfar, mat);
nuclear@0 234 return 1;
nuclear@0 235 }
nuclear@0 236 memcpy(mat, idmat, sizeof idmat);
nuclear@0 237 return 0;
nuclear@0 238 }
nuclear@0 239
nuclear@0 240 void vr_begin(int eye)
nuclear@0 241 {
nuclear@0 242 if(vrm && vrm->begin) {
nuclear@0 243 vrm->begin(eye);
nuclear@0 244 }
nuclear@0 245 }
nuclear@0 246
nuclear@0 247 void vr_end(void)
nuclear@0 248 {
nuclear@0 249 if(vrm && vrm->end) {
nuclear@0 250 vrm->end();
nuclear@0 251 }
nuclear@0 252 }
nuclear@0 253
nuclear@0 254 int vr_swap_buffers(void)
nuclear@0 255 {
nuclear@0 256 int res = 0;
nuclear@0 257
nuclear@0 258 if(vrm && vrm->present) {
nuclear@0 259 res = vrm->present();
nuclear@0 260 }
nuclear@0 261
nuclear@0 262 if(!res) {
nuclear@5 263 fallback_present();
nuclear@8 264 vrimp_swap_buffers();
nuclear@0 265 }
nuclear@0 266 return 0;
nuclear@0 267 }
nuclear@0 268
nuclear@0 269 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@0 270 {
nuclear@0 271 float halfu = (umax + umin) * 0.5f;
nuclear@0 272
nuclear@0 273 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
nuclear@0 274 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
nuclear@0 275 }
nuclear@0 276
nuclear@0 277 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@0 278 {
nuclear@0 279 if(vrm && vrm->set_eye_texture) {
nuclear@0 280 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
nuclear@5 281 } else {
nuclear@5 282 rtarg[eye].tex = tex;
nuclear@5 283 rtarg[eye].umin = umin;
nuclear@5 284 rtarg[eye].umax = umax;
nuclear@5 285 rtarg[eye].vmin = vmin;
nuclear@5 286 rtarg[eye].vmax = vmax;
nuclear@0 287 }
nuclear@0 288 }
nuclear@0 289
nuclear@0 290 void vr_recenter(void)
nuclear@0 291 {
nuclear@0 292 if(vrm && vrm->recenter) {
nuclear@0 293 vrm->recenter();
nuclear@0 294 }
nuclear@0 295 }
nuclear@0 296
nuclear@5 297 static void fallback_present(void)
nuclear@5 298 {
nuclear@5 299 int i;
nuclear@0 300
nuclear@5 301 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
nuclear@0 302
nuclear@5 303 glDisable(GL_LIGHTING);
nuclear@5 304 glDisable(GL_DEPTH_TEST);
nuclear@5 305 glDisable(GL_ALPHA_TEST);
nuclear@5 306 glDisable(GL_STENCIL_TEST);
nuclear@5 307 glDisable(GL_FOG);
nuclear@5 308
nuclear@5 309 glEnable(GL_TEXTURE_2D);
nuclear@5 310
nuclear@5 311 glMatrixMode(GL_MODELVIEW);
nuclear@5 312 glPushMatrix();
nuclear@5 313 glLoadIdentity();
nuclear@5 314 glMatrixMode(GL_PROJECTION);
nuclear@5 315 glPushMatrix();
nuclear@5 316 glLoadIdentity();
nuclear@5 317
nuclear@5 318 for(i=0; i<2; i++) {
nuclear@5 319 float x0 = i == 0 ? -1 : 0;
nuclear@5 320 float x1 = i == 0 ? 0 : 1;
nuclear@5 321
nuclear@5 322 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
nuclear@5 323
nuclear@5 324 glBegin(GL_QUADS);
nuclear@5 325 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
nuclear@5 326 glVertex2f(x0, -1);
nuclear@5 327 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
nuclear@5 328 glVertex2f(x1, -1);
nuclear@5 329 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
nuclear@5 330 glVertex2f(x1, 1);
nuclear@5 331 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
nuclear@5 332 glVertex2f(x0, 1);
nuclear@5 333 glEnd();
nuclear@5 334 }
nuclear@5 335
nuclear@5 336 glPopMatrix();
nuclear@5 337 glMatrixMode(GL_MODELVIEW);
nuclear@5 338 glPopMatrix();
nuclear@5 339
nuclear@5 340 glPopAttrib();
nuclear@0 341 }