conworlds

view src/vr/vr.c @ 20:782ff06817fb

merged ...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Aug 2014 18:42:53 +0300
parents e3f0ca1d008a
children
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "vr.h"
4 #include "vr_impl.h"
5 #include "mathutil.h"
8 static void swap_buffers(void);
11 static struct vr_module *vrm;
12 static float idmat[] = {
13 1, 0, 0, 0,
14 0, 1, 0, 0,
15 0, 0, 1, 0,
16 0, 0, 0, 1
17 };
18 static float fbtex_rect[] = {
19 0, 0, 1, 1
20 };
22 int vr_init(void)
23 {
24 int i, nmodules;
26 if(vrm) {
27 vr_shutdown();
28 }
30 vr_init_modules();
32 nmodules = vr_get_num_modules();
33 for(i=0; i<nmodules; i++) {
34 struct vr_module *m = vr_get_module(i);
35 if(m->init() != -1) {
36 /* add to the active modules array */
37 vr_activate_module(i);
38 if(!vrm) {
39 vr_use_module(0);
40 }
41 }
42 }
44 if(!vrm) {
45 return -1;
46 }
47 return 0;
48 }
50 void vr_shutdown(void)
51 {
52 vr_clear_modules();
53 vrm = 0;
54 fbtex_rect[0] = fbtex_rect[1] = 0;
55 fbtex_rect[2] = fbtex_rect[3] = 1;
56 }
58 int vr_module_count(void)
59 {
60 return vr_get_num_active_modules();
61 }
63 const char *vr_module_name(int idx)
64 {
65 struct vr_module *m = vr_get_active_module(idx);
66 if(!m) {
67 return 0;
68 }
69 return m->name;
70 }
72 int vr_use_module(int idx)
73 {
74 if(idx >= 0 && idx < vr_get_num_active_modules()) {
75 struct vr_module *m = vr_get_active_module(idx);
76 if(m != vrm) {
77 vrm = m;
78 printf("using vr module: %s\n", vrm->name);
79 }
80 return 0;
81 }
82 return -1;
83 }
85 int vr_use_module_named(const char *name)
86 {
87 int i, count = vr_get_num_active_modules();
89 for(i=0; i<count; i++) {
90 struct vr_module *m = vr_get_active_module(i);
91 if(strcmp(m->name, name) == 0) {
92 return vr_use_module(i);
93 }
94 }
95 return -1;
96 }
98 void vr_set_opti(const char *optname, int val)
99 {
100 if(vrm && vrm->set_option) {
101 vrm->set_option(optname, OTYPE_INT, &val);
102 }
103 }
105 void vr_set_optf(const char *optname, float val)
106 {
107 if(vrm && vrm->set_option) {
108 vrm->set_option(optname, OTYPE_FLOAT, &val);
109 }
110 }
112 int vr_get_opti(const char *optname)
113 {
114 int res = 0;
116 if(vrm && vrm->get_option) {
117 vrm->get_option(optname, OTYPE_INT, &res);
118 }
119 return res;
120 }
122 float vr_get_optf(const char *optname)
123 {
124 float res = 0.0f;
126 if(vrm && vrm->get_option) {
127 vrm->get_option(optname, OTYPE_FLOAT, &res);
128 }
129 return res;
130 }
133 int vr_view_translation(int eye, float *vec)
134 {
135 if(vrm && vrm->translation) {
136 vrm->translation(eye, vec);
137 return 1;
138 }
139 vec[0] = vec[1] = vec[2] = 0.0f;
140 return 0;
141 }
143 int vr_view_rotation(int eye, float *quat)
144 {
145 if(vrm && vrm->rotation) {
146 vrm->rotation(eye, quat);
147 return 1;
148 }
149 quat[0] = quat[1] = quat[2] = 0.0f;
150 quat[3] = 1.0f;
151 return 0;
152 }
154 int vr_view_matrix(int eye, float *mat)
155 {
156 int have_trans, have_rot;
157 float offs[3], quat[4];
158 float rmat[16], tmat[16];
160 if(vrm && vrm->view_matrix) {
161 vrm->view_matrix(eye, mat);
162 return 1;
163 }
165 have_trans = vr_view_translation(eye, offs);
166 have_rot = vr_view_rotation(eye, quat);
168 if(!have_trans && !have_rot) {
169 return 0;
170 }
172 offs[0] = -offs[0];
173 offs[1] = -offs[1];
174 offs[2] = -offs[2];
176 translation_matrix(offs, tmat);
177 rotation_matrix(quat, rmat);
178 mult_matrix(mat, tmat, rmat);
179 return 1;
180 }
182 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
183 {
184 if(vrm && vrm->proj_matrix) {
185 vrm->proj_matrix(eye, znear, zfar, mat);
186 return 1;
187 }
188 memcpy(mat, idmat, sizeof idmat);
189 return 0;
190 }
192 void vr_begin(int eye)
193 {
194 if(vrm && vrm->begin) {
195 vrm->begin(eye);
196 }
197 }
199 void vr_end(void)
200 {
201 if(vrm && vrm->end) {
202 vrm->end();
203 }
204 }
206 int vr_swap_buffers(void)
207 {
208 int res = 0;
210 if(vrm && vrm->present) {
211 res = vrm->present();
212 }
214 if(!res) {
215 swap_buffers();
216 }
217 return 0;
218 }
220 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
221 {
222 float halfu = (umax + umin) * 0.5f;
224 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
225 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
226 }
228 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
229 {
230 if(vrm && vrm->set_eye_texture) {
231 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
232 }
233 }
235 void vr_recenter(void)
236 {
237 if(vrm && vrm->recenter) {
238 vrm->recenter();
239 }
240 }
243 #ifdef __unix__
244 #include <GL/glx.h>
246 static void swap_buffers(void)
247 {
248 glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable());
249 }
251 #endif
253 #ifdef WIN32
254 #include <windows.h>
256 static void swap_buffers(void)
257 {
258 SwapBuffers(wglGetCurrentDC());
259 }
260 #endif