libgoatvr

view src/vr.c @ 8:3d9ec6fe97d7

- added distortion mesh generation for the OpenHMD module (unfinished) - changed internal implementation function naming to use the vrimp_ prefix - added an opengl helper function to load extension entry points
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 20 Sep 2014 13:22:53 +0300
parents 6896f9cf9621
children 34d4643d61f9
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "opengl.h"
5 #include "vr.h"
6 #include "vr_impl.h"
7 #include "mathutil.h"
10 static void fallback_present(void);
13 static struct vr_module *vrm;
14 static float idmat[] = {
15 1, 0, 0, 0,
16 0, 1, 0, 0,
17 0, 0, 1, 0,
18 0, 0, 0, 1
19 };
20 static float fbtex_rect[] = {
21 0, 0, 1, 1
22 };
24 static void *defopt; /* default options db */
26 static struct {
27 float umin, umax, vmin, vmax;
28 int tex;
29 } rtarg[2];
32 int vr_init(void)
33 {
34 int i, nmodules;
35 char *vrmod_env;
37 /* create the default options database */
38 if(!defopt && (defopt = create_options())) {
39 set_option_float(defopt, VR_OPT_EYE_HEIGHT, 1.675);
40 set_option_float(defopt, VR_OPT_IPD, 0.064);
41 }
43 if(vrm) {
44 vr_shutdown();
45 }
47 vr_init_modules();
49 nmodules = vr_get_num_modules();
50 for(i=0; i<nmodules; i++) {
51 struct vr_module *m = vr_get_module(i);
52 if(m->init() != -1) {
53 /* add to the active modules array */
54 vr_activate_module(i);
55 }
56 }
58 if(!vr_get_num_active_modules()) {
59 return -1;
60 }
62 if((vrmod_env = getenv("VR_MODULE"))) {
63 vr_use_module_named(vrmod_env);
64 } else {
65 vr_use_module(0);
66 }
67 return 0;
68 }
70 void vr_shutdown(void)
71 {
72 vr_clear_modules();
73 vrm = 0;
74 fbtex_rect[0] = fbtex_rect[1] = 0;
75 fbtex_rect[2] = fbtex_rect[3] = 1;
76 }
78 int vr_module_count(void)
79 {
80 return vr_get_num_active_modules();
81 }
83 const char *vr_module_name(int idx)
84 {
85 struct vr_module *m = vr_get_active_module(idx);
86 if(!m) {
87 return 0;
88 }
89 return m->name;
90 }
92 int vr_use_module(int idx)
93 {
94 if(idx >= 0 && idx < vr_get_num_active_modules()) {
95 struct vr_module *m = vr_get_active_module(idx);
96 if(m != vrm) {
97 vrm = m;
98 printf("using vr module: %s\n", vrm->name);
99 }
100 return 0;
101 }
102 return -1;
103 }
105 int vr_use_module_named(const char *name)
106 {
107 int i, count = vr_get_num_active_modules();
109 for(i=0; i<count; i++) {
110 struct vr_module *m = vr_get_active_module(i);
111 if(strcmp(m->name, name) == 0) {
112 return vr_use_module(i);
113 }
114 }
115 return -1;
116 }
118 void vr_set_opti(const char *optname, int val)
119 {
120 if(vrm && vrm->set_option) {
121 vrm->set_option(optname, OTYPE_INT, &val);
122 } else {
123 set_option_int(defopt, optname, val);
124 }
125 }
127 void vr_set_optf(const char *optname, float val)
128 {
129 if(vrm && vrm->set_option) {
130 vrm->set_option(optname, OTYPE_FLOAT, &val);
131 } else {
132 set_option_float(defopt, optname, val);
133 }
134 }
136 int vr_get_opti(const char *optname)
137 {
138 int res = 0;
140 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_INT, &res) == -1) {
141 get_option_int(defopt, optname, &res); /* fallback */
142 }
143 return res;
144 }
146 float vr_get_optf(const char *optname)
147 {
148 float res = 0.0f;
150 if(!vrm || !vrm->get_option || vrm->get_option(optname, OTYPE_FLOAT, &res) == -1) {
151 get_option_float(defopt, optname, &res); /* fallback */
152 }
153 return res;
154 }
157 int vr_view_translation(int eye, float *vec)
158 {
159 if(vrm && vrm->translation) {
160 vrm->translation(eye, vec);
161 return 1;
162 }
163 vec[0] = vec[1] = vec[2] = 0.0f;
164 return 0;
165 }
167 int vr_view_rotation(int eye, float *quat)
168 {
169 if(vrm && vrm->rotation) {
170 vrm->rotation(eye, quat);
171 return 1;
172 }
173 quat[0] = quat[1] = quat[2] = 0.0f;
174 quat[3] = 1.0f;
175 return 0;
176 }
178 int vr_view_matrix(int eye, float *mat)
179 {
180 int have_trans, have_rot;
181 float offs[3], quat[4];
182 float rmat[16], tmat[16];
184 if(vrm && vrm->view_matrix) {
185 vrm->view_matrix(eye, mat);
186 return 1;
187 }
189 have_trans = vr_view_translation(eye, offs);
190 have_rot = vr_view_rotation(eye, quat);
192 if(!have_trans && !have_rot) {
193 memcpy(mat, idmat, sizeof idmat);
194 return 0;
195 }
197 offs[0] = -offs[0];
198 offs[1] = -offs[1];
199 offs[2] = -offs[2];
201 vrimp_translation_matrix(offs, tmat);
202 vrimp_rotation_matrix(quat, rmat);
203 vrimp_mult_matrix(mat, tmat, rmat);
204 return 1;
205 }
207 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
208 {
209 if(vrm && vrm->proj_matrix) {
210 vrm->proj_matrix(eye, znear, zfar, mat);
211 return 1;
212 }
213 memcpy(mat, idmat, sizeof idmat);
214 return 0;
215 }
217 void vr_begin(int eye)
218 {
219 if(vrm && vrm->begin) {
220 vrm->begin(eye);
221 }
222 }
224 void vr_end(void)
225 {
226 if(vrm && vrm->end) {
227 vrm->end();
228 }
229 }
231 int vr_swap_buffers(void)
232 {
233 int res = 0;
235 if(vrm && vrm->present) {
236 res = vrm->present();
237 }
239 if(!res) {
240 fallback_present();
241 vrimp_swap_buffers();
242 }
243 return 0;
244 }
246 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
247 {
248 float halfu = (umax + umin) * 0.5f;
250 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
251 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
252 }
254 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
255 {
256 if(vrm && vrm->set_eye_texture) {
257 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
258 } else {
259 rtarg[eye].tex = tex;
260 rtarg[eye].umin = umin;
261 rtarg[eye].umax = umax;
262 rtarg[eye].vmin = vmin;
263 rtarg[eye].vmax = vmax;
264 }
265 }
267 void vr_recenter(void)
268 {
269 if(vrm && vrm->recenter) {
270 vrm->recenter();
271 }
272 }
274 static void fallback_present(void)
275 {
276 int i;
278 glPushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT);
280 glDisable(GL_LIGHTING);
281 glDisable(GL_DEPTH_TEST);
282 glDisable(GL_ALPHA_TEST);
283 glDisable(GL_STENCIL_TEST);
284 glDisable(GL_FOG);
286 glEnable(GL_TEXTURE_2D);
288 glMatrixMode(GL_MODELVIEW);
289 glPushMatrix();
290 glLoadIdentity();
291 glMatrixMode(GL_PROJECTION);
292 glPushMatrix();
293 glLoadIdentity();
295 for(i=0; i<2; i++) {
296 float x0 = i == 0 ? -1 : 0;
297 float x1 = i == 0 ? 0 : 1;
299 glBindTexture(GL_TEXTURE_2D, rtarg[i].tex);
301 glBegin(GL_QUADS);
302 glTexCoord2f(rtarg[i].umin, rtarg[i].vmin);
303 glVertex2f(x0, -1);
304 glTexCoord2f(rtarg[i].umax, rtarg[i].vmin);
305 glVertex2f(x1, -1);
306 glTexCoord2f(rtarg[i].umax, rtarg[i].vmax);
307 glVertex2f(x1, 1);
308 glTexCoord2f(rtarg[i].umin, rtarg[i].vmax);
309 glVertex2f(x0, 1);
310 glEnd();
311 }
313 glPopMatrix();
314 glMatrixMode(GL_MODELVIEW);
315 glPopMatrix();
317 glPopAttrib();
318 }