vrchess

view src/vr/vr.c @ 8:90abf4b93cc9

fixed line endings fixed viewport when returning to original framebuffer
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 17:24:43 +0300
parents bd8202d6d28d
children c2eecf764daa
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 vrm = vr_get_active_module(idx);
75 printf("using vr module: %s\n", vrm->name);
76 return 0;
77 }
78 return -1;
79 }
81 int vr_use_module_named(const char *name)
82 {
83 int i, count = vr_get_num_active_modules();
85 for(i=0; i<count; i++) {
86 struct vr_module *m = vr_get_active_module(i);
87 if(strcmp(m->name, name) == 0) {
88 return vr_use_module(i);
89 }
90 }
91 return -1;
92 }
94 void vr_set_opti(const char *optname, int val)
95 {
96 if(vrm && vrm->set_option) {
97 vrm->set_option(optname, OTYPE_INT, &val);
98 }
99 }
101 void vr_set_optf(const char *optname, float val)
102 {
103 if(vrm && vrm->set_option) {
104 vrm->set_option(optname, OTYPE_FLOAT, &val);
105 }
106 }
108 int vr_get_opti(const char *optname)
109 {
110 int res = 0;
112 if(vrm && vrm->get_option) {
113 vrm->get_option(optname, OTYPE_INT, &res);
114 }
115 return res;
116 }
118 float vr_get_optf(const char *optname)
119 {
120 float res = 0.0f;
122 if(vrm && vrm->get_option) {
123 vrm->get_option(optname, OTYPE_FLOAT, &res);
124 }
125 return res;
126 }
129 int vr_view_translation(int eye, float *vec)
130 {
131 if(vrm && vrm->translation) {
132 vrm->translation(eye, vec);
133 return 0;
134 }
135 vec[0] = vec[1] = vec[2] = 0.0f;
136 return -1;
137 }
139 int vr_view_rotation(int eye, float *quat)
140 {
141 if(vrm && vrm->rotation) {
142 vrm->rotation(eye, quat);
143 return 0;
144 }
145 quat[0] = quat[1] = quat[2] = 0.0f;
146 quat[3] = 1.0f;
147 return -1;
148 }
150 int vr_view_matrix(int eye, float *mat)
151 {
152 /* TODO combine vr_view_translation and vr_view_rotation */
153 return 0;
154 }
156 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
157 {
158 if(vrm && vrm->proj_matrix) {
159 vrm->proj_matrix(eye, znear, zfar, mat);
160 return 1;
161 }
162 memcpy(mat, idmat, sizeof idmat);
163 return 0;
164 }
166 void vr_begin(int eye)
167 {
168 if(vrm && vrm->begin) {
169 vrm->begin(eye);
170 }
171 }
173 void vr_end(void)
174 {
175 if(vrm && vrm->end) {
176 vrm->end();
177 }
178 }
180 int vr_swap_buffers(void)
181 {
182 int res = 0;
184 if(vrm && vrm->present) {
185 res = vrm->present();
186 }
188 if(!res) {
189 swap_buffers();
190 }
191 return 0;
192 }
194 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
195 {
196 float halfu = (umax + umin) * 0.5f;
198 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
199 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
200 }
202 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
203 {
204 if(vrm && vrm->set_eye_texture) {
205 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
206 }
207 }
209 void vr_recenter(void)
210 {
211 if(vrm && vrm->recenter) {
212 vrm->recenter();
213 }
214 }
217 #ifdef __unix__
218 #include <GL/glx.h>
220 static void swap_buffers(void)
221 {
222 glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable());
223 }
225 #endif
227 #ifdef WIN32
228 #include <windows.h>
230 static void swap_buffers(void)
231 {
232 SwapBuffers(wglGetCurrentDC());
233 }
234 #endif