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