vrchess

view src/vr/vr.c @ 10:e3f0ca1d008a

added preliminary OpenHMD module
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 20:11:15 +0300
parents c2eecf764daa
children 5dc4e2b8f6f5
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "vr.h"
4 #include "vr_impl.h"
7 static void swap_buffers(void);
10 static struct vr_module *vrm;
11 static float idmat[] = {
12 1, 0, 0, 0,
13 0, 1, 0, 0,
14 0, 0, 1, 0,
15 0, 0, 0, 1
16 };
17 static float fbtex_rect[] = {
18 0, 0, 1, 1
19 };
21 int vr_init(void)
22 {
23 int i, nmodules;
25 if(vrm) {
26 vr_shutdown();
27 }
29 vr_init_modules();
31 nmodules = vr_get_num_modules();
32 for(i=0; i<nmodules; i++) {
33 struct vr_module *m = vr_get_module(i);
34 if(m->init() != -1) {
35 /* add to the active modules array */
36 vr_activate_module(i);
37 if(!vrm) {
38 vr_use_module(0);
39 }
40 }
41 }
43 if(!vrm) {
44 return -1;
45 }
46 return 0;
47 }
49 void vr_shutdown(void)
50 {
51 vr_clear_modules();
52 vrm = 0;
53 fbtex_rect[0] = fbtex_rect[1] = 0;
54 fbtex_rect[2] = fbtex_rect[3] = 1;
55 }
57 int vr_module_count(void)
58 {
59 return vr_get_num_active_modules();
60 }
62 const char *vr_module_name(int idx)
63 {
64 struct vr_module *m = vr_get_active_module(idx);
65 if(!m) {
66 return 0;
67 }
68 return m->name;
69 }
71 int vr_use_module(int idx)
72 {
73 if(idx >= 0 && idx < vr_get_num_active_modules()) {
74 struct vr_module *m = vr_get_active_module(idx);
75 if(m != vrm) {
76 vrm = m;
77 printf("using vr module: %s\n", vrm->name);
78 }
79 return 0;
80 }
81 return -1;
82 }
84 int vr_use_module_named(const char *name)
85 {
86 int i, count = vr_get_num_active_modules();
88 for(i=0; i<count; i++) {
89 struct vr_module *m = vr_get_active_module(i);
90 if(strcmp(m->name, name) == 0) {
91 return vr_use_module(i);
92 }
93 }
94 return -1;
95 }
97 void vr_set_opti(const char *optname, int val)
98 {
99 if(vrm && vrm->set_option) {
100 vrm->set_option(optname, OTYPE_INT, &val);
101 }
102 }
104 void vr_set_optf(const char *optname, float val)
105 {
106 if(vrm && vrm->set_option) {
107 vrm->set_option(optname, OTYPE_FLOAT, &val);
108 }
109 }
111 int vr_get_opti(const char *optname)
112 {
113 int res = 0;
115 if(vrm && vrm->get_option) {
116 vrm->get_option(optname, OTYPE_INT, &res);
117 }
118 return res;
119 }
121 float vr_get_optf(const char *optname)
122 {
123 float res = 0.0f;
125 if(vrm && vrm->get_option) {
126 vrm->get_option(optname, OTYPE_FLOAT, &res);
127 }
128 return res;
129 }
132 int vr_view_translation(int eye, float *vec)
133 {
134 if(vrm && vrm->translation) {
135 vrm->translation(eye, vec);
136 return 0;
137 }
138 vec[0] = vec[1] = vec[2] = 0.0f;
139 return -1;
140 }
142 int vr_view_rotation(int eye, float *quat)
143 {
144 if(vrm && vrm->rotation) {
145 vrm->rotation(eye, quat);
146 return 0;
147 }
148 quat[0] = quat[1] = quat[2] = 0.0f;
149 quat[3] = 1.0f;
150 return -1;
151 }
153 int vr_view_matrix(int eye, float *mat)
154 {
155 float offs[3], quat[4];
157 if(vrm && vrm->view_matrix) {
158 vrm->view_matrix(eye, mat);
159 return 1;
160 }
162 if(!vr_view_translation(eye, offs) && !vr_view_rotation(eye, quat)) {
163 return 0;
164 }
165 /* TODO construct matrix */
166 return 0;
167 }
169 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
170 {
171 if(vrm && vrm->proj_matrix) {
172 vrm->proj_matrix(eye, znear, zfar, mat);
173 return 1;
174 }
175 memcpy(mat, idmat, sizeof idmat);
176 return 0;
177 }
179 void vr_begin(int eye)
180 {
181 if(vrm && vrm->begin) {
182 vrm->begin(eye);
183 }
184 }
186 void vr_end(void)
187 {
188 if(vrm && vrm->end) {
189 vrm->end();
190 }
191 }
193 int vr_swap_buffers(void)
194 {
195 int res = 0;
197 if(vrm && vrm->present) {
198 res = vrm->present();
199 }
201 if(!res) {
202 swap_buffers();
203 }
204 return 0;
205 }
207 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
208 {
209 float halfu = (umax + umin) * 0.5f;
211 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
212 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
213 }
215 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
216 {
217 if(vrm && vrm->set_eye_texture) {
218 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
219 }
220 }
222 void vr_recenter(void)
223 {
224 if(vrm && vrm->recenter) {
225 vrm->recenter();
226 }
227 }
230 #ifdef __unix__
231 #include <GL/glx.h>
233 static void swap_buffers(void)
234 {
235 glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable());
236 }
238 #endif
240 #ifdef WIN32
241 #include <windows.h>
243 static void swap_buffers(void)
244 {
245 SwapBuffers(wglGetCurrentDC());
246 }
247 #endif