libgoatvr

annotate src/vr.c @ 30:1a8343ea54ce

fixed on windows
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Apr 2015 04:01:47 +0300
parents 437fe32ac633
children 7eea82cea9d2
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@19 9 #define DEF_IPD 0.064f
nuclear@0 10
nuclear@5 11 static void fallback_present(void);
nuclear@0 12
nuclear@0 13
nuclear@0 14 static struct vr_module *vrm;
nuclear@0 15 static float idmat[] = {
nuclear@0 16 1, 0, 0, 0,
nuclear@0 17 0, 1, 0, 0,
nuclear@0 18 0, 0, 1, 0,
nuclear@0 19 0, 0, 0, 1
nuclear@0 20 };
nuclear@0 21 static float fbtex_rect[] = {
nuclear@0 22 0, 0, 1, 1
nuclear@0 23 };
nuclear@0 24
nuclear@5 25 static void *defopt; /* default options db */
nuclear@5 26
nuclear@5 27 static struct {
nuclear@5 28 float umin, umax, vmin, vmax;
nuclear@5 29 int tex;
nuclear@5 30 } rtarg[2];
nuclear@5 31
nuclear@5 32
nuclear@0 33 int vr_init(void)
nuclear@0 34 {
nuclear@0 35 int i, nmodules;
nuclear@24 36 char *env;
nuclear@0 37
nuclear@5 38 /* create the default options database */
nuclear@5 39 if(!defopt && (defopt = create_options())) {
nuclear@19 40 set_option_float(defopt, VR_RENDER_RES_SCALE, 1.0f);
nuclear@19 41 set_option_float(defopt, VR_EYE_HEIGHT, 1.675f);
nuclear@19 42 set_option_float(defopt, VR_IPD, DEF_IPD);
nuclear@19 43 set_option_vec3f(defopt, VR_LEYE_OFFSET, -DEF_IPD * 0.5f, 0.0f, 0.0f);
nuclear@19 44 set_option_vec3f(defopt, VR_REYE_OFFSET, DEF_IPD * 0.5f, 0.0f, 0.0f);
nuclear@24 45
nuclear@24 46 if((env = getenv("VR_NULL_STEREO")) && atoi(env)) {
nuclear@24 47 set_option_int(defopt, VR_NULL_STEREO, 1);
nuclear@24 48 } else {
nuclear@24 49 set_option_int(defopt, VR_NULL_STEREO, 0);
nuclear@24 50 }
nuclear@5 51 }
nuclear@5 52
nuclear@0 53 if(vrm) {
nuclear@0 54 vr_shutdown();
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 vr_init_modules();
nuclear@0 58
nuclear@0 59 nmodules = vr_get_num_modules();
nuclear@0 60 for(i=0; i<nmodules; i++) {
nuclear@0 61 struct vr_module *m = vr_get_module(i);
nuclear@0 62 if(m->init() != -1) {
nuclear@0 63 /* add to the active modules array */
nuclear@0 64 vr_activate_module(i);
nuclear@0 65 }
nuclear@0 66 }
nuclear@0 67
nuclear@7 68 if(!vr_get_num_active_modules()) {
nuclear@0 69 return -1;
nuclear@0 70 }
nuclear@7 71
nuclear@24 72 if((env = getenv("VR_MODULE"))) {
nuclear@24 73 vr_use_module_named(env);
nuclear@7 74 } else {
nuclear@7 75 vr_use_module(0);
nuclear@7 76 }
nuclear@0 77 return 0;
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 void vr_shutdown(void)
nuclear@0 81 {
nuclear@0 82 vr_clear_modules();
nuclear@0 83 vrm = 0;
nuclear@0 84 fbtex_rect[0] = fbtex_rect[1] = 0;
nuclear@0 85 fbtex_rect[2] = fbtex_rect[3] = 1;
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 int vr_module_count(void)
nuclear@0 89 {
nuclear@0 90 return vr_get_num_active_modules();
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 const char *vr_module_name(int idx)
nuclear@0 94 {
nuclear@0 95 struct vr_module *m = vr_get_active_module(idx);
nuclear@0 96 if(!m) {
nuclear@0 97 return 0;
nuclear@0 98 }
nuclear@0 99 return m->name;
nuclear@0 100 }
nuclear@0 101
nuclear@0 102 int vr_use_module(int idx)
nuclear@0 103 {
nuclear@0 104 if(idx >= 0 && idx < vr_get_num_active_modules()) {
nuclear@0 105 struct vr_module *m = vr_get_active_module(idx);
nuclear@0 106 if(m != vrm) {
nuclear@0 107 vrm = m;
nuclear@0 108 printf("using vr module: %s\n", vrm->name);
nuclear@0 109 }
nuclear@0 110 return 0;
nuclear@0 111 }
nuclear@0 112 return -1;
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 int vr_use_module_named(const char *name)
nuclear@0 116 {
nuclear@0 117 int i, count = vr_get_num_active_modules();
nuclear@0 118
nuclear@0 119 for(i=0; i<count; i++) {
nuclear@0 120 struct vr_module *m = vr_get_active_module(i);
nuclear@0 121 if(strcmp(m->name, name) == 0) {
nuclear@0 122 return vr_use_module(i);
nuclear@0 123 }
nuclear@0 124 }
nuclear@0 125 return -1;
nuclear@0 126 }
nuclear@0 127
nuclear@11 128 void vr_seti(const char *optname, int val)
nuclear@0 129 {
nuclear@0 130 if(vrm && vrm->set_option) {
nuclear@0 131 vrm->set_option(optname, OTYPE_INT, &val);
nuclear@5 132 } else {
nuclear@5 133 set_option_int(defopt, optname, val);
nuclear@0 134 }
nuclear@0 135 }
nuclear@0 136
nuclear@11 137 void vr_setf(const char *optname, float val)
nuclear@0 138 {
nuclear@0 139 if(vrm && vrm->set_option) {
nuclear@0 140 vrm->set_option(optname, OTYPE_FLOAT, &val);
nuclear@5 141 } else {
nuclear@5 142 set_option_float(defopt, optname, val);
nuclear@0 143 }
nuclear@0 144 }
nuclear@0 145
nuclear@16 146 static int def_option_int(const char *optname)
nuclear@16 147 {
nuclear@16 148 int res = 0;
nuclear@16 149 int left, right;
nuclear@16 150
nuclear@16 151 if(strcmp(optname, VR_RENDER_XRES) == 0) {
nuclear@16 152 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
nuclear@16 153 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
nuclear@16 154 return left + right;
nuclear@16 155 }
nuclear@16 156 } else if(strcmp(optname, VR_RENDER_YRES) == 0) {
nuclear@16 157 if(vrm && vrm->get_option && vrm->get_option(optname, OTYPE_INT, &left) != -1 &&
nuclear@16 158 vrm->get_option(optname, OTYPE_INT, &right) != -1) {
nuclear@16 159 return left > right ? left : right;
nuclear@16 160 }
nuclear@16 161 }
nuclear@16 162
nuclear@16 163 get_option_int(defopt, optname, &res);
nuclear@16 164 return res;
nuclear@16 165 }
nuclear@16 166
nuclear@16 167 static float def_option_float(const char *optname)
nuclear@16 168 {
nuclear@18 169 float res = 0.0f;
nuclear@16 170
nuclear@16 171 if(strcmp(optname, VR_RENDER_XRES) == 0 || strcmp(optname, VR_RENDER_YRES) == 0) {
nuclear@16 172 return (float)def_option_int(optname);
nuclear@16 173 }
nuclear@16 174
nuclear@16 175 get_option_float(defopt, optname, &res);
nuclear@16 176 return res;
nuclear@16 177 }
nuclear@16 178
nuclear@19 179 static float *def_option_vec(const char *optname, float *res)
nuclear@19 180 {
nuclear@19 181 res[0] = res[1] = res[2] = res[3] = 0.0f;
nuclear@19 182
nuclear@19 183 get_option_vec(defopt, optname, res);
nuclear@19 184 return res;
nuclear@19 185 }
nuclear@19 186
nuclear@11 187 int vr_geti(const char *optname)
nuclear@0 188 {
nuclear@0 189 int res = 0;
nuclear@0 190
nuclear@5 191 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
nuclear@16 192 res = def_option_int(optname);
nuclear@0 193 }
nuclear@0 194 return res;
nuclear@0 195 }
nuclear@0 196
nuclear@11 197 float vr_getf(const char *optname)
nuclear@0 198 {
nuclear@0 199 float res = 0.0f;
nuclear@0 200
nuclear@5 201 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
nuclear@16 202 res = def_option_float(optname);
nuclear@0 203 }
nuclear@0 204 return res;
nuclear@0 205 }
nuclear@0 206
nuclear@19 207 float *vr_getfv(const char *optname, float *res)
nuclear@19 208 {
nuclear@19 209 static float sres[4];
nuclear@19 210 if(!res) res = sres;
nuclear@19 211
nuclear@19 212 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_VEC, res) == -1) {
nuclear@19 213 def_option_vec(optname, res);
nuclear@19 214 }
nuclear@19 215 return res;
nuclear@19 216 }
nuclear@19 217
nuclear@12 218 int vr_geti_def(const char *optname, int def_val)
nuclear@12 219 {
nuclear@12 220 int res = 0;
nuclear@12 221
nuclear@12 222 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
nuclear@12 223 if(get_option_int(defopt, optname, &res) == -1) { /* fallback */
nuclear@12 224 return def_val;
nuclear@12 225 }
nuclear@12 226 }
nuclear@12 227 return res;
nuclear@12 228 }
nuclear@12 229
nuclear@12 230 float vr_getf_def(const char *optname, float def_val)
nuclear@12 231 {
nuclear@12 232 float res = 0.0f;
nuclear@12 233
nuclear@12 234 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
nuclear@12 235 if(get_option_float(defopt, optname, &res) == -1) { /* fallback */
nuclear@12 236 return def_val;
nuclear@12 237 }
nuclear@12 238 }
nuclear@12 239 return res;
nuclear@12 240 }
nuclear@0 241
nuclear@0 242 int vr_view_translation(int eye, float *vec)
nuclear@0 243 {
nuclear@0 244 if(vrm && vrm->translation) {
nuclear@0 245 vrm->translation(eye, vec);
nuclear@0 246 return 1;
nuclear@0 247 }
nuclear@19 248 vec[0] = vec[1] = vec[2] = 0.0f;
nuclear@19 249 return 0;
nuclear@0 250 }
nuclear@0 251
nuclear@0 252 int vr_view_rotation(int eye, float *quat)
nuclear@0 253 {
nuclear@0 254 if(vrm && vrm->rotation) {
nuclear@0 255 vrm->rotation(eye, quat);
nuclear@0 256 return 1;
nuclear@0 257 }
nuclear@0 258 quat[0] = quat[1] = quat[2] = 0.0f;
nuclear@0 259 quat[3] = 1.0f;
nuclear@0 260 return 0;
nuclear@0 261 }
nuclear@0 262
nuclear@0 263 int vr_view_matrix(int eye, float *mat)
nuclear@0 264 {
nuclear@19 265 float offs[3], quat[4], eye_offs[4];
nuclear@19 266 float rmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@19 267 float tmat[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@0 268
nuclear@0 269 if(vrm && vrm->view_matrix) {
nuclear@0 270 vrm->view_matrix(eye, mat);
nuclear@0 271 return 1;
nuclear@0 272 }
nuclear@0 273
nuclear@19 274 memcpy(mat, idmat, sizeof idmat);
nuclear@19 275
nuclear@19 276 if(vr_view_translation(eye, offs)) {
nuclear@19 277 offs[0] = -offs[0];
nuclear@19 278 offs[1] = -offs[1];
nuclear@19 279 offs[2] = -offs[2];
nuclear@19 280
nuclear@19 281 vrimp_translation_matrix(offs, tmat);
nuclear@19 282 vrimp_mult_matrix(mat, mat, tmat);
nuclear@18 283 }
nuclear@19 284 if(vr_view_rotation(eye, quat)) {
nuclear@19 285 vrimp_rotation_matrix(quat, rmat);
nuclear@19 286 vrimp_mult_matrix(mat, mat, rmat);
nuclear@0 287 }
nuclear@0 288
nuclear@19 289 vr_getfv(eye == VR_EYE_LEFT ? VR_LEYE_OFFSET : VR_REYE_OFFSET, eye_offs);
nuclear@19 290 eye_offs[0] = -eye_offs[0];
nuclear@19 291 eye_offs[1] = -eye_offs[1];
nuclear@19 292 eye_offs[2] = -eye_offs[2];
nuclear@19 293 vrimp_translation_matrix(eye_offs, tmat);
nuclear@19 294 vrimp_mult_matrix(mat, mat, tmat);
nuclear@0 295 return 1;
nuclear@0 296 }
nuclear@0 297
nuclear@0 298 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
nuclear@0 299 {
nuclear@0 300 if(vrm && vrm->proj_matrix) {
nuclear@0 301 vrm->proj_matrix(eye, znear, zfar, mat);
nuclear@0 302 return 1;
nuclear@0 303 }
nuclear@0 304 memcpy(mat, idmat, sizeof idmat);
nuclear@0 305 return 0;
nuclear@0 306 }
nuclear@0 307
nuclear@0 308 void vr_begin(int eye)
nuclear@0 309 {
nuclear@0 310 if(vrm && vrm->begin) {
nuclear@0 311 vrm->begin(eye);
nuclear@0 312 }
nuclear@0 313 }
nuclear@0 314
nuclear@0 315 void vr_end(void)
nuclear@0 316 {
nuclear@0 317 if(vrm && vrm->end) {
nuclear@0 318 vrm->end();
nuclear@0 319 }
nuclear@0 320 }
nuclear@0 321
nuclear@0 322 int vr_swap_buffers(void)
nuclear@0 323 {
nuclear@0 324 int res = 0;
nuclear@0 325
nuclear@0 326 if(vrm && vrm->present) {
nuclear@0 327 res = vrm->present();
nuclear@0 328 }
nuclear@0 329
nuclear@0 330 if(!res) {
nuclear@5 331 fallback_present();
nuclear@8 332 vrimp_swap_buffers();
nuclear@0 333 }
nuclear@0 334 return 0;
nuclear@0 335 }
nuclear@0 336
nuclear@0 337 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@0 338 {
nuclear@0 339 float halfu = (umax + umin) * 0.5f;
nuclear@0 340
nuclear@0 341 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
nuclear@0 342 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
nuclear@0 343 }
nuclear@0 344
nuclear@0 345 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@0 346 {
nuclear@0 347 if(vrm && vrm->set_eye_texture) {
nuclear@0 348 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
nuclear@5 349 } else {
nuclear@5 350 rtarg[eye].tex = tex;
nuclear@5 351 rtarg[eye].umin = umin;
nuclear@5 352 rtarg[eye].umax = umax;
nuclear@5 353 rtarg[eye].vmin = vmin;
nuclear@5 354 rtarg[eye].vmax = vmax;
nuclear@0 355 }
nuclear@0 356 }
nuclear@0 357
nuclear@0 358 void vr_recenter(void)
nuclear@0 359 {
nuclear@0 360 if(vrm && vrm->recenter) {
nuclear@0 361 vrm->recenter();
nuclear@0 362 }
nuclear@0 363 }
nuclear@0 364
nuclear@5 365 static void fallback_present(void)
nuclear@5 366 {
nuclear@17 367 int i, show_both = vr_geti(VR_NULL_STEREO);
nuclear@0 368
nuclear@5 369 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
nuclear@0 370
nuclear@5 371 glDisable(GL_LIGHTING);
nuclear@5 372 glDisable(GL_DEPTH_TEST);
nuclear@5 373 glDisable(GL_ALPHA_TEST);
nuclear@5 374 glDisable(GL_STENCIL_TEST);
nuclear@5 375 glDisable(GL_FOG);
nuclear@5 376
nuclear@5 377 glEnable(GL_TEXTURE_2D);
nuclear@5 378
nuclear@5 379 glMatrixMode(GL_MODELVIEW);
nuclear@5 380 glPushMatrix();
nuclear@5 381 glLoadIdentity();
nuclear@5 382 glMatrixMode(GL_PROJECTION);
nuclear@5 383 glPushMatrix();
nuclear@5 384 glLoadIdentity();
nuclear@5 385
nuclear@5 386 for(i=0; i<2; i++) {
nuclear@17 387 float x0, x1;
nuclear@17 388
nuclear@17 389 if(show_both) {
nuclear@17 390 x0 = i == 0 ? -1 : 0;
nuclear@17 391 x1 = i == 0 ? 0 : 1;
nuclear@17 392 } else {
nuclear@17 393 x0 = -1;
nuclear@17 394 x1 = 1;
nuclear@17 395 }
nuclear@5 396
nuclear@5 397 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
nuclear@5 398
nuclear@5 399 glBegin(GL_QUADS);
nuclear@5 400 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
nuclear@5 401 glVertex2f(x0, -1);
nuclear@5 402 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
nuclear@5 403 glVertex2f(x1, -1);
nuclear@5 404 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
nuclear@5 405 glVertex2f(x1, 1);
nuclear@5 406 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
nuclear@5 407 glVertex2f(x0, 1);
nuclear@5 408 glEnd();
nuclear@17 409
nuclear@17 410 if(!show_both) break;
nuclear@5 411 }
nuclear@5 412
nuclear@5 413 glPopMatrix();
nuclear@5 414 glMatrixMode(GL_MODELVIEW);
nuclear@5 415 glPopMatrix();
nuclear@5 416
nuclear@5 417 glPopAttrib();
nuclear@0 418 }