libgoatvr
diff src/vr.c @ 0:ded3d0a74e19
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 29 Aug 2014 03:45:25 +0300 |
parents | |
children | e63cb28fc644 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/vr.c Fri Aug 29 03:45:25 2014 +0300 1.3 @@ -0,0 +1,260 @@ 1.4 +#include <stdio.h> 1.5 +#include <string.h> 1.6 +#include "vr.h" 1.7 +#include "vr_impl.h" 1.8 +#include "mathutil.h" 1.9 + 1.10 + 1.11 +static void swap_buffers(void); 1.12 + 1.13 + 1.14 +static struct vr_module *vrm; 1.15 +static float idmat[] = { 1.16 + 1, 0, 0, 0, 1.17 + 0, 1, 0, 0, 1.18 + 0, 0, 1, 0, 1.19 + 0, 0, 0, 1 1.20 +}; 1.21 +static float fbtex_rect[] = { 1.22 + 0, 0, 1, 1 1.23 +}; 1.24 + 1.25 +int vr_init(void) 1.26 +{ 1.27 + int i, nmodules; 1.28 + 1.29 + if(vrm) { 1.30 + vr_shutdown(); 1.31 + } 1.32 + 1.33 + vr_init_modules(); 1.34 + 1.35 + nmodules = vr_get_num_modules(); 1.36 + for(i=0; i<nmodules; i++) { 1.37 + struct vr_module *m = vr_get_module(i); 1.38 + if(m->init() != -1) { 1.39 + /* add to the active modules array */ 1.40 + vr_activate_module(i); 1.41 + if(!vrm) { 1.42 + vr_use_module(0); 1.43 + } 1.44 + } 1.45 + } 1.46 + 1.47 + if(!vrm) { 1.48 + return -1; 1.49 + } 1.50 + return 0; 1.51 +} 1.52 + 1.53 +void vr_shutdown(void) 1.54 +{ 1.55 + vr_clear_modules(); 1.56 + vrm = 0; 1.57 + fbtex_rect[0] = fbtex_rect[1] = 0; 1.58 + fbtex_rect[2] = fbtex_rect[3] = 1; 1.59 +} 1.60 + 1.61 +int vr_module_count(void) 1.62 +{ 1.63 + return vr_get_num_active_modules(); 1.64 +} 1.65 + 1.66 +const char *vr_module_name(int idx) 1.67 +{ 1.68 + struct vr_module *m = vr_get_active_module(idx); 1.69 + if(!m) { 1.70 + return 0; 1.71 + } 1.72 + return m->name; 1.73 +} 1.74 + 1.75 +int vr_use_module(int idx) 1.76 +{ 1.77 + if(idx >= 0 && idx < vr_get_num_active_modules()) { 1.78 + struct vr_module *m = vr_get_active_module(idx); 1.79 + if(m != vrm) { 1.80 + vrm = m; 1.81 + printf("using vr module: %s\n", vrm->name); 1.82 + } 1.83 + return 0; 1.84 + } 1.85 + return -1; 1.86 +} 1.87 + 1.88 +int vr_use_module_named(const char *name) 1.89 +{ 1.90 + int i, count = vr_get_num_active_modules(); 1.91 + 1.92 + for(i=0; i<count; i++) { 1.93 + struct vr_module *m = vr_get_active_module(i); 1.94 + if(strcmp(m->name, name) == 0) { 1.95 + return vr_use_module(i); 1.96 + } 1.97 + } 1.98 + return -1; 1.99 +} 1.100 + 1.101 +void vr_set_opti(const char *optname, int val) 1.102 +{ 1.103 + if(vrm && vrm->set_option) { 1.104 + vrm->set_option(optname, OTYPE_INT, &val); 1.105 + } 1.106 +} 1.107 + 1.108 +void vr_set_optf(const char *optname, float val) 1.109 +{ 1.110 + if(vrm && vrm->set_option) { 1.111 + vrm->set_option(optname, OTYPE_FLOAT, &val); 1.112 + } 1.113 +} 1.114 + 1.115 +int vr_get_opti(const char *optname) 1.116 +{ 1.117 + int res = 0; 1.118 + 1.119 + if(vrm && vrm->get_option) { 1.120 + vrm->get_option(optname, OTYPE_INT, &res); 1.121 + } 1.122 + return res; 1.123 +} 1.124 + 1.125 +float vr_get_optf(const char *optname) 1.126 +{ 1.127 + float res = 0.0f; 1.128 + 1.129 + if(vrm && vrm->get_option) { 1.130 + vrm->get_option(optname, OTYPE_FLOAT, &res); 1.131 + } 1.132 + return res; 1.133 +} 1.134 + 1.135 + 1.136 +int vr_view_translation(int eye, float *vec) 1.137 +{ 1.138 + if(vrm && vrm->translation) { 1.139 + vrm->translation(eye, vec); 1.140 + return 1; 1.141 + } 1.142 + vec[0] = vec[1] = vec[2] = 0.0f; 1.143 + return 0; 1.144 +} 1.145 + 1.146 +int vr_view_rotation(int eye, float *quat) 1.147 +{ 1.148 + if(vrm && vrm->rotation) { 1.149 + vrm->rotation(eye, quat); 1.150 + return 1; 1.151 + } 1.152 + quat[0] = quat[1] = quat[2] = 0.0f; 1.153 + quat[3] = 1.0f; 1.154 + return 0; 1.155 +} 1.156 + 1.157 +int vr_view_matrix(int eye, float *mat) 1.158 +{ 1.159 + int have_trans, have_rot; 1.160 + float offs[3], quat[4]; 1.161 + float rmat[16], tmat[16]; 1.162 + 1.163 + if(vrm && vrm->view_matrix) { 1.164 + vrm->view_matrix(eye, mat); 1.165 + return 1; 1.166 + } 1.167 + 1.168 + have_trans = vr_view_translation(eye, offs); 1.169 + have_rot = vr_view_rotation(eye, quat); 1.170 + 1.171 + if(!have_trans && !have_rot) { 1.172 + return 0; 1.173 + } 1.174 + 1.175 + offs[0] = -offs[0]; 1.176 + offs[1] = -offs[1]; 1.177 + offs[2] = -offs[2]; 1.178 + 1.179 + translation_matrix(offs, tmat); 1.180 + rotation_matrix(quat, rmat); 1.181 + mult_matrix(mat, tmat, rmat); 1.182 + return 1; 1.183 +} 1.184 + 1.185 +int vr_proj_matrix(int eye, float znear, float zfar, float *mat) 1.186 +{ 1.187 + if(vrm && vrm->proj_matrix) { 1.188 + vrm->proj_matrix(eye, znear, zfar, mat); 1.189 + return 1; 1.190 + } 1.191 + memcpy(mat, idmat, sizeof idmat); 1.192 + return 0; 1.193 +} 1.194 + 1.195 +void vr_begin(int eye) 1.196 +{ 1.197 + if(vrm && vrm->begin) { 1.198 + vrm->begin(eye); 1.199 + } 1.200 +} 1.201 + 1.202 +void vr_end(void) 1.203 +{ 1.204 + if(vrm && vrm->end) { 1.205 + vrm->end(); 1.206 + } 1.207 +} 1.208 + 1.209 +int vr_swap_buffers(void) 1.210 +{ 1.211 + int res = 0; 1.212 + 1.213 + if(vrm && vrm->present) { 1.214 + res = vrm->present(); 1.215 + } 1.216 + 1.217 + if(!res) { 1.218 + swap_buffers(); 1.219 + } 1.220 + return 0; 1.221 +} 1.222 + 1.223 +void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax) 1.224 +{ 1.225 + float halfu = (umax + umin) * 0.5f; 1.226 + 1.227 + vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax); 1.228 + vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax); 1.229 +} 1.230 + 1.231 +void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax) 1.232 +{ 1.233 + if(vrm && vrm->set_eye_texture) { 1.234 + vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax); 1.235 + } 1.236 +} 1.237 + 1.238 +void vr_recenter(void) 1.239 +{ 1.240 + if(vrm && vrm->recenter) { 1.241 + vrm->recenter(); 1.242 + } 1.243 +} 1.244 + 1.245 + 1.246 +#ifdef __unix__ 1.247 +#include <GL/glx.h> 1.248 + 1.249 +static void swap_buffers(void) 1.250 +{ 1.251 + glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable()); 1.252 +} 1.253 + 1.254 +#endif 1.255 + 1.256 +#ifdef WIN32 1.257 +#include <windows.h> 1.258 + 1.259 +static void swap_buffers(void) 1.260 +{ 1.261 + SwapBuffers(wglGetCurrentDC()); 1.262 +} 1.263 +#endif