libgoatvr

annotate src/vr.c @ 20:827ec716c275

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