conworlds

view src/vr/vr.c @ 7:bd8202d6d28d

some progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 16:55:16 +0300
parents 3c36bc28c6c2
children 90abf4b93cc9
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "vr.h"
4 #include "vr_impl.h"
7 static struct vr_module *vrm;
8 static float idmat[] = {
9 1, 0, 0, 0,
10 0, 1, 0, 0,
11 0, 0, 1, 0,
12 0, 0, 0, 1
13 };
14 static float fbtex_rect[] = {
15 0, 0, 1, 1
16 };
18 int vr_init(void)
19 {
20 int i, nmodules;
22 if(vrm) {
23 vr_shutdown();
24 }
26 vr_init_modules();
28 nmodules = vr_get_num_modules();
29 for(i=0; i<nmodules; i++) {
30 struct vr_module *m = vr_get_module(i);
31 if(m->init() != -1) {
32 /* add to the active modules array */
33 vr_activate_module(i);
34 if(!vrm) {
35 vr_use_module(0);
36 }
37 }
38 }
40 if(!vrm) {
41 return -1;
42 }
43 return 0;
44 }
46 void vr_shutdown(void)
47 {
48 vr_clear_modules();
49 vrm = 0;
50 fbtex_rect[0] = fbtex_rect[1] = 0;
51 fbtex_rect[2] = fbtex_rect[3] = 1;
52 }
54 int vr_module_count(void)
55 {
56 return vr_get_num_active_modules();
57 }
59 const char *vr_module_name(int idx)
60 {
61 struct vr_module *m = vr_get_active_module(idx);
62 if(!m) {
63 return 0;
64 }
65 return m->name;
66 }
68 int vr_use_module(int idx)
69 {
70 if(idx >= 0 && idx < vr_get_num_active_modules()) {
71 vrm = vr_get_active_module(idx);
72 printf("using vr module: %s\n", vrm->name);
73 return 0;
74 }
75 return -1;
76 }
78 int vr_use_module_named(const char *name)
79 {
80 int i, count = vr_get_num_active_modules();
82 for(i=0; i<count; i++) {
83 struct vr_module *m = vr_get_active_module(i);
84 if(strcmp(m->name, name) == 0) {
85 return vr_use_module(i);
86 }
87 }
88 return -1;
89 }
91 void vr_set_opti(const char *optname, int val)
92 {
93 if(vrm && vrm->set_option) {
94 vrm->set_option(optname, OTYPE_INT, &val);
95 }
96 }
98 void vr_set_optf(const char *optname, float val)
99 {
100 if(vrm && vrm->set_option) {
101 vrm->set_option(optname, OTYPE_FLOAT, &val);
102 }
103 }
105 int vr_get_opti(const char *optname)
106 {
107 int res = 0;
109 if(vrm && vrm->get_option) {
110 vrm->get_option(optname, OTYPE_INT, &res);
111 }
112 return res;
113 }
115 float vr_get_optf(const char *optname)
116 {
117 float res = 0.0f;
119 if(vrm && vrm->get_option) {
120 vrm->get_option(optname, OTYPE_FLOAT, &res);
121 }
122 return res;
123 }
126 int vr_view_translation(int eye, float *vec)
127 {
128 if(vrm && vrm->translation) {
129 vrm->translation(eye, vec);
130 return 0;
131 }
132 vec[0] = vec[1] = vec[2] = 0.0f;
133 return -1;
134 }
136 int vr_view_rotation(int eye, float *quat)
137 {
138 if(vrm && vrm->rotation) {
139 vrm->rotation(eye, quat);
140 return 0;
141 }
142 quat[0] = quat[1] = quat[2] = 0.0f;
143 quat[3] = 1.0f;
144 return -1;
145 }
147 int vr_view_matrix(int eye, float *mat)
148 {
149 /* TODO combine vr_view_translation and vr_view_rotation */
150 return 0;
151 }
153 int vr_proj_matrix(int eye, float znear, float zfar, float *mat)
154 {
155 if(vrm && vrm->proj_matrix) {
156 vrm->proj_matrix(eye, znear, zfar, mat);
157 return 1;
158 }
159 memcpy(mat, idmat, sizeof idmat);
160 return 0;
161 }
163 void vr_begin(int eye)
164 {
165 if(vrm && vrm->begin) {
166 vrm->begin(eye);
167 }
168 }
170 void vr_end(void)
171 {
172 if(vrm && vrm->end) {
173 vrm->end();
174 }
175 }
177 int vr_swap_buffers(void)
178 {
179 if(vrm && vrm->present) {
180 vrm->present();
181 return 1;
182 }
183 return 0;
184 }
186 void vr_output_texture(unsigned int tex, float umin, float vmin, float umax, float vmax)
187 {
188 float halfu = (umax + umin) * 0.5f;
190 vr_output_texture_eye(VR_EYE_LEFT, tex, umin, vmin, halfu, vmax);
191 vr_output_texture_eye(VR_EYE_RIGHT, tex, halfu, vmin, umax, vmax);
192 }
194 void vr_output_texture_eye(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
195 {
196 if(vrm && vrm->set_eye_texture) {
197 vrm->set_eye_texture(eye, tex, umin, vmin, umax, vmax);
198 }
199 }
201 void vr_recenter(void)
202 {
203 if(vrm && vrm->recenter) {
204 vrm->recenter();
205 }