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