vrchess

view src/vr/vr.c @ 9:c2eecf764daa

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 18:48:25 +0300
parents 90abf4b93cc9
children e3f0ca1d008a
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 /* TODO combine vr_view_translation and vr_view_rotation */
156 return 0;
157 }
159 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
160 {
161 if(vrm && vrm->proj_matrix) {
162 vrm->proj_matrix(eye, znear, zfar, mat);
163 return 1;
164 }
165 memcpy(mat, idmat, sizeof idmat);
166 return 0;
167 }
169 void vr_begin(int eye)
170 {
171 if(vrm && vrm->begin) {
172 vrm->begin(eye);
173 }
174 }
176 void vr_end(void)
177 {
178 if(vrm && vrm->end) {
179 vrm->end();
180 }
181 }
183 int vr_swap_buffers(void)
184 {
185 int res = 0;
187 if(vrm && vrm->present) {
188 res = vrm->present();
189 }
191 if(!res) {
192 swap_buffers();
193 }
194 return 0;
195 }
197 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
198 {
199 float halfu = (umax + umin) * 0.5f;
201 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
202 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
203 }
205 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
206 {
207 if(vrm && vrm->set_eye_texture) {
208 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
209 }
210 }
212 void vr_recenter(void)
213 {
214 if(vrm && vrm->recenter) {
215 vrm->recenter();
216 }
217 }
220 #ifdef __unix__
221 #include <GL/glx.h>
223 static void swap_buffers(void)
224 {
225 glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable());
226 }
228 #endif
230 #ifdef WIN32
231 #include <windows.h>
233 static void swap_buffers(void)
234 {
235 SwapBuffers(wglGetCurrentDC());
236 }
237 #endif