conworlds

annotate src/vr/vr.c @ 11:5dc4e2b8f6f5

LibOVR is broken
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 23 Aug 2014 00:24:20 +0300
parents e3f0ca1d008a
children
rev   line source
nuclear@4 1 #include <stdio.h>
nuclear@4 2 #include <string.h>
nuclear@4 3 #include "vr.h"
nuclear@4 4 #include "vr_impl.h"
nuclear@11 5 #include "mathutil.h"
nuclear@4 6
nuclear@4 7
nuclear@8 8 static void swap_buffers(void);
nuclear@8 9
nuclear@8 10
nuclear@4 11 static struct vr_module *vrm;
nuclear@4 12 static float idmat[] = {
nuclear@4 13 1, 0, 0, 0,
nuclear@4 14 0, 1, 0, 0,
nuclear@4 15 0, 0, 1, 0,
nuclear@4 16 0, 0, 0, 1
nuclear@4 17 };
nuclear@4 18 static float fbtex_rect[] = {
nuclear@4 19 0, 0, 1, 1
nuclear@4 20 };
nuclear@4 21
nuclear@4 22 int vr_init(void)
nuclear@4 23 {
nuclear@4 24 int i, nmodules;
nuclear@4 25
nuclear@4 26 if(vrm) {
nuclear@4 27 vr_shutdown();
nuclear@4 28 }
nuclear@4 29
nuclear@4 30 vr_init_modules();
nuclear@4 31
nuclear@4 32 nmodules = vr_get_num_modules();
nuclear@4 33 for(i=0; i<nmodules; i++) {
nuclear@4 34 struct vr_module *m = vr_get_module(i);
nuclear@4 35 if(m->init() != -1) {
nuclear@4 36 /* add to the active modules array */
nuclear@4 37 vr_activate_module(i);
nuclear@4 38 if(!vrm) {
nuclear@4 39 vr_use_module(0);
nuclear@4 40 }
nuclear@4 41 }
nuclear@4 42 }
nuclear@4 43
nuclear@4 44 if(!vrm) {
nuclear@4 45 return -1;
nuclear@4 46 }
nuclear@4 47 return 0;
nuclear@4 48 }
nuclear@4 49
nuclear@4 50 void vr_shutdown(void)
nuclear@4 51 {
nuclear@4 52 vr_clear_modules();
nuclear@4 53 vrm = 0;
nuclear@4 54 fbtex_rect[0] = fbtex_rect[1] = 0;
nuclear@4 55 fbtex_rect[2] = fbtex_rect[3] = 1;
nuclear@4 56 }
nuclear@4 57
nuclear@4 58 int vr_module_count(void)
nuclear@4 59 {
nuclear@4 60 return vr_get_num_active_modules();
nuclear@4 61 }
nuclear@4 62
nuclear@4 63 const char *vr_module_name(int idx)
nuclear@4 64 {
nuclear@4 65 struct vr_module *m = vr_get_active_module(idx);
nuclear@4 66 if(!m) {
nuclear@4 67 return 0;
nuclear@4 68 }
nuclear@4 69 return m->name;
nuclear@4 70 }
nuclear@4 71
nuclear@4 72 int vr_use_module(int idx)
nuclear@4 73 {
nuclear@4 74 if(idx >= 0 && idx < vr_get_num_active_modules()) {
nuclear@9 75 struct vr_module *m = vr_get_active_module(idx);
nuclear@9 76 if(m != vrm) {
nuclear@9 77 vrm = m;
nuclear@9 78 printf("using vr module: %s\n", vrm->name);
nuclear@9 79 }
nuclear@4 80 return 0;
nuclear@4 81 }
nuclear@4 82 return -1;
nuclear@4 83 }
nuclear@4 84
nuclear@4 85 int vr_use_module_named(const char *name)
nuclear@4 86 {
nuclear@4 87 int i, count = vr_get_num_active_modules();
nuclear@4 88
nuclear@4 89 for(i=0; i<count; i++) {
nuclear@4 90 struct vr_module *m = vr_get_active_module(i);
nuclear@4 91 if(strcmp(m->name, name) == 0) {
nuclear@4 92 return vr_use_module(i);
nuclear@4 93 }
nuclear@4 94 }
nuclear@4 95 return -1;
nuclear@4 96 }
nuclear@4 97
nuclear@7 98 void vr_set_opti(const char *optname, int val)
nuclear@7 99 {
nuclear@7 100 if(vrm && vrm->set_option) {
nuclear@7 101 vrm->set_option(optname, OTYPE_INT, &val);
nuclear@7 102 }
nuclear@7 103 }
nuclear@7 104
nuclear@7 105 void vr_set_optf(const char *optname, float val)
nuclear@7 106 {
nuclear@7 107 if(vrm && vrm->set_option) {
nuclear@7 108 vrm->set_option(optname, OTYPE_FLOAT, &val);
nuclear@7 109 }
nuclear@7 110 }
nuclear@7 111
nuclear@7 112 int vr_get_opti(const char *optname)
nuclear@7 113 {
nuclear@7 114 int res = 0;
nuclear@7 115
nuclear@7 116 if(vrm && vrm->get_option) {
nuclear@7 117 vrm->get_option(optname, OTYPE_INT, &res);
nuclear@7 118 }
nuclear@7 119 return res;
nuclear@7 120 }
nuclear@7 121
nuclear@7 122 float vr_get_optf(const char *optname)
nuclear@7 123 {
nuclear@7 124 float res = 0.0f;
nuclear@7 125
nuclear@7 126 if(vrm && vrm->get_option) {
nuclear@7 127 vrm->get_option(optname, OTYPE_FLOAT, &res);
nuclear@7 128 }
nuclear@7 129 return res;
nuclear@7 130 }
nuclear@7 131
nuclear@7 132
nuclear@7 133 int vr_view_translation(int eye, float *vec)
nuclear@7 134 {
nuclear@7 135 if(vrm && vrm->translation) {
nuclear@7 136 vrm->translation(eye, vec);
nuclear@11 137 return 1;
nuclear@7 138 }
nuclear@7 139 vec[0] = vec[1] = vec[2] = 0.0f;
nuclear@11 140 return 0;
nuclear@7 141 }
nuclear@7 142
nuclear@7 143 int vr_view_rotation(int eye, float *quat)
nuclear@7 144 {
nuclear@7 145 if(vrm && vrm->rotation) {
nuclear@7 146 vrm->rotation(eye, quat);
nuclear@11 147 return 1;
nuclear@7 148 }
nuclear@7 149 quat[0] = quat[1] = quat[2] = 0.0f;
nuclear@7 150 quat[3] = 1.0f;
nuclear@11 151 return 0;
nuclear@7 152 }
nuclear@7 153
nuclear@4 154 int vr_view_matrix(int eye, float *mat)
nuclear@4 155 {
nuclear@11 156 int have_trans, have_rot;
nuclear@10 157 float offs[3], quat[4];
nuclear@11 158 float rmat[16], tmat[16];
nuclear@10 159
nuclear@10 160 if(vrm && vrm->view_matrix) {
nuclear@10 161 vrm->view_matrix(eye, mat);
nuclear@10 162 return 1;
nuclear@10 163 }
nuclear@10 164
nuclear@11 165 have_trans = vr_view_translation(eye, offs);
nuclear@11 166 have_rot = vr_view_rotation(eye, quat);
nuclear@11 167
nuclear@11 168 if(!have_trans && !have_rot) {
nuclear@10 169 return 0;
nuclear@10 170 }
nuclear@11 171
nuclear@11 172 offs[0] = -offs[0];
nuclear@11 173 offs[1] = -offs[1];
nuclear@11 174 offs[2] = -offs[2];
nuclear@11 175
nuclear@11 176 translation_matrix(offs, tmat);
nuclear@11 177 rotation_matrix(quat, rmat);
nuclear@11 178 mult_matrix(mat, tmat, rmat);
nuclear@11 179 return 1;
nuclear@4 180 }
nuclear@4 181
nuclear@7 182 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
nuclear@4 183 {
nuclear@4 184 if(vrm && vrm->proj_matrix) {
nuclear@7 185 vrm->proj_matrix(eye, znear, zfar, mat);
nuclear@4 186 return 1;
nuclear@4 187 }
nuclear@4 188 memcpy(mat, idmat, sizeof idmat);
nuclear@4 189 return 0;
nuclear@4 190 }
nuclear@4 191
nuclear@6 192 void vr_begin(int eye)
nuclear@4 193 {
nuclear@6 194 if(vrm && vrm->begin) {
nuclear@6 195 vrm->begin(eye);
nuclear@4 196 }
nuclear@4 197 }
nuclear@4 198
nuclear@6 199 void vr_end(void)
nuclear@4 200 {
nuclear@6 201 if(vrm && vrm->end) {
nuclear@6 202 vrm->end();
nuclear@6 203 }
nuclear@4 204 }
nuclear@6 205
nuclear@6 206 int vr_swap_buffers(void)
nuclear@6 207 {
nuclear@8 208 int res = 0;
nuclear@8 209
nuclear@6 210 if(vrm && vrm->present) {
nuclear@8 211 res = vrm->present();
nuclear@8 212 }
nuclear@8 213
nuclear@8 214 if(!res) {
nuclear@8 215 swap_buffers();
nuclear@6 216 }
nuclear@6 217 return 0;
nuclear@6 218 }
nuclear@8 219
nuclear@7 220 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@7 221 {
nuclear@7 222 float halfu = (umax + umin) * 0.5f;
nuclear@7 223
nuclear@7 224 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
nuclear@7 225 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
nuclear@7 226 }
nuclear@7 227
nuclear@7 228 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
nuclear@7 229 {
nuclear@7 230 if(vrm && vrm->set_eye_texture) {
nuclear@7 231 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
nuclear@7 232 }
nuclear@7 233 }
nuclear@7 234
nuclear@7 235 void vr_recenter(void)
nuclear@7 236 {
nuclear@7 237 if(vrm && vrm->recenter) {
nuclear@7 238 vrm->recenter();
nuclear@7 239 }
nuclear@8 240 }
nuclear@8 241
nuclear@8 242
nuclear@8 243 #ifdef __unix__
nuclear@8 244 #include <GL/glx.h>
nuclear@8 245
nuclear@8 246 static void swap_buffers(void)
nuclear@8 247 {
nuclear@8 248 glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable());
nuclear@8 249 }
nuclear@8 250
nuclear@8 251 #endif
nuclear@8 252
nuclear@8 253 #ifdef WIN32
nuclear@8 254 #include <windows.h>
nuclear@8 255
nuclear@8 256 static void swap_buffers(void)
nuclear@8 257 {
nuclear@8 258 SwapBuffers(wglGetCurrentDC());
nuclear@8 259 }
nuclear@8 260 #endif