vrchess

annotate src/vr/vr.c @ 10:e3f0ca1d008a

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