libgoatvr

annotate src/vr.c @ 32:7eea82cea9d2

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