libgoatvr

view src/vr_openhmd.c @ 6:b71314e80654

fixed the null plugin
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 19 Sep 2014 05:30:20 +0300
parents ded3d0a74e19
children 6896f9cf9621
line source
1 #include "vr_impl.h"
3 #ifdef USE_OPENHMD
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <openhmd/openhmd.h>
7 #include "opt.h"
9 /* a noble spirit embiggens the framebuffer to avoid aliasing in the middle */
10 #define FB_EMBIGGEN 1.5
12 static ohmd_context *ctx;
13 static ohmd_device *dev;
14 static void *optdb;
15 static int new_frame = 1;
17 static int disp_width, disp_height;
18 static float ipd;
20 static struct {
21 unsigned int id;
22 float umin, umax;
23 float vmin, vmax;
24 } eye_tex[2];
27 static int init(void)
28 {
29 int i, num_hmds;
31 if(!(ctx = ohmd_ctx_create())) {
32 fprintf(stderr, "failed to create OpenHMD context\n");
33 ohmd_ctx_destroy(ctx);
34 return -1;
35 }
36 if(!(num_hmds = ohmd_ctx_probe(ctx))) {
37 fprintf(stderr, "no HMDs detected\n");
38 return -1;
39 }
41 for(i=0; i<num_hmds; i++) {
42 const char *vendor = ohmd_list_gets(ctx, i, OHMD_VENDOR);
43 const char *product = ohmd_list_gets(ctx, i, OHMD_PRODUCT);
44 const char *devpath = ohmd_list_gets(ctx, i, OHMD_PATH);
46 printf("[%d] %s - %s (path: %s)\n", i, vendor, product, devpath);
47 }
49 printf("opening device 0\n");
51 if(!(dev = ohmd_list_open_device(ctx, 0))) {
52 fprintf(stderr, "failed to open device 0: %s\n", ohmd_ctx_get_error(ctx));
53 return -1;
54 }
56 ohmd_device_geti(dev, OHMD_SCREEN_HORIZONTAL_SIZE, &disp_width);
57 ohmd_device_geti(dev, OHMD_SCREEN_VERTICAL_SIZE, &disp_height);
58 ohmd_device_getf(dev, OHMD_EYE_IPD, &ipd);
60 if((optdb = create_options())) {
61 set_option_int(optdb, VR_OPT_DISPLAY_WIDTH, disp_width);
62 set_option_int(optdb, VR_OPT_DISPLAY_HEIGHT, disp_height);
63 set_option_float(optdb, VR_OPT_IPD, ipd);
65 set_option_int(optdb, VR_OPT_LEYE_XRES, (int)(disp_width / 2.0 * FB_EMBIGGEN));
66 set_option_int(optdb, VR_OPT_LEYE_YRES, (int)(disp_height * FB_EMBIGGEN));
67 set_option_int(optdb, VR_OPT_REYE_XRES, (int)(disp_width / 2.0 * FB_EMBIGGEN));
68 set_option_int(optdb, VR_OPT_REYE_YRES, (int)(disp_height * FB_EMBIGGEN));
69 }
71 return 0;
72 }
74 static void cleanup(void)
75 {
76 if(ctx) {
77 ohmd_ctx_destroy(ctx);
78 }
79 }
82 static int set_option(const char *opt, enum opt_type type, void *valp)
83 {
84 switch(type) {
85 case OTYPE_INT:
86 set_option_int(optdb, opt, *(int*)valp);
87 break;
89 case OTYPE_FLOAT:
90 set_option_float(optdb, opt, *(float*)valp);
91 break;
92 }
93 return 0;
94 }
96 static int get_option(const char *opt, enum opt_type type, void *valp)
97 {
98 switch(type) {
99 case OTYPE_INT:
100 return get_option_int(optdb, opt, valp);
101 case OTYPE_FLOAT:
102 return get_option_float(optdb, opt, valp);
103 }
104 return -1;
105 }
107 static int translation(int eye, float *vec)
108 {
109 float xform[16];
111 view_matrix(eye, xform);
113 vec[0] = xform[3];
114 vec[1] = xform[7];
115 vec[2] = xform[11];
116 return 0;
117 }
119 static int rotation(int eye, float *quat)
120 {
121 if(!dev) {
122 quat[0] = quat[1] = quat[2] = 0.0f;
123 quat[3] = 1.0f;
124 return -1;
125 }
127 ohmd_device_getf(dev, OHMD_ROTATION_QUAT, quat);
128 return 0;
129 }
132 static void view_matrix(int eye, float *mat)
133 {
134 ohmd_device_getf(dev, OHMD_LEFT_EYE_GL_MODELVIEW_MATRIX + eye, mat);
135 }
137 static void proj_matrix(int eye, float znear, float zfar, float *mat)
138 {
139 ohmd_device_setf(dev, OHMD_PROJECTION_ZNEAR, znear);
140 ohmd_device_setf(dev, OHMD_PROJECTION_ZFAR, zfar);
141 ohmd_device_getf(dev, OHMD_LEFT_EYE_GL_PROJECTION_MATRIX + eye, mat);
142 }
144 static void begin(int eye)
145 {
146 if(new_frame) {
147 ohmd_ctx_update(ctx);
148 new_frame = 0;
149 }
150 }
152 static int present(void)
153 {
154 new_frame = 1;
155 return 0;
156 }
158 static void set_eye_texture(int eye, unsigned int tex, float umin, float vmin, float umax, float vmax)
159 {
160 eye_tex[eye].id = tex;
161 eye_tex[eye].umin = umin;
162 eye_tex[eye].umax = umax;
163 eye_tex[eye].vmin = vmin;
164 eye_tex[eye].vmax = vmax;
165 }
167 static void recenter(void)
168 {
169 /* TODO does OHMD support the magnetometer? */
170 }
173 struct vr_module *vr_module_openhmd(void)
174 {
175 static struct vr_module m;
177 if(!m.init) {
178 m.name = "openhmd";
179 m.init = init;
180 m.cleanup = cleanup;
181 m.set_option = set_option;
182 m.get_option = get_option;
183 m.view_matrix = view_matrix;
184 m.proj_matrix = proj_matrix;
185 m.begin = begin;
186 m.present = present;
187 m.set_eye_texture = set_eye_texture;
188 m.recenter = recenter;
189 }
190 }
192 #else /* don't use OpenHMD */
194 static int init(void)
195 {
196 return -1;
197 }
199 struct vr_module *vr_module_openhmd(void)
200 {
201 static struct vr_module m;
203 if(!m.init) {
204 m.name = "openhmd";
205 m.init = init;
206 }
207 return &m;
208 }
210 #endif /* USE_OPENHMD */