libgoatvr

annotate src/vr.c @ 15:27fcd4c2969d

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