libgoatvr

annotate src/vr.c @ 33:1102327fe85f

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