conworlds

view src/vr/vr.c @ 4:e6948e131526

adding a vr wrapper
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 20 Aug 2014 06:33:43 +0300
parents
children 3c36bc28c6c2
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 int vr_view_matrix(int eye, float *mat)
92 {
93 if(vrm && vrm->view_matrix) {
94 vrm->view_matrix(eye, mat);
95 return 1;
96 }
97 memcpy(mat, idmat, sizeof idmat);
98 return 0;
99 }
101 int vr_proj_matrix(int eye, float *mat)
102 {
103 if(vrm && vrm->proj_matrix) {
104 vrm->proj_matrix(eye, mat);
105 return 1;
106 }
107 memcpy(mat, idmat, sizeof idmat);
108 return 0;
109 }
111 void vr_present(unsigned int fbtex)
112 {
113 if(vrm && vrm->draw) {
114 vrm->draw(fbtex, fbtex_rect[0], fbtex_rect[2], fbtex_rect[1], fbtex_rect[3]);
115 }
116 }
118 void vr_fbrect(float u, float umax, float v, float vmax)
119 {
120 fbtex_rect[0] = u;
121 fbtex_rect[1] = v;
122 fbtex_rect[2] = umax;
123 fbtex_rect[3] = vmax;
124 }