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