oculus1

view src/vr.cc @ 17:cfe4979ab3eb

ops, minor error in the last commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 07:09:48 +0300
parents f3672317e5c2
children 1b107de821c1
line source
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include "vr.h"
4 #include "vr_impl.h"
5 #include "vr_sdr.h"
7 static bool init_ovr();
8 static bool init_sdr();
10 VRContext vr_ctx;
11 static unsigned int sdrprog;
13 extern "C" int vr_init(enum vr_init_mode mode)
14 {
15 glewInit();
17 if(!init_ovr()) {
18 return -1;
19 }
21 if(!init_sdr()) {
22 return -1;
23 }
25 return 0;
26 }
28 extern "C" void vr_shutdown(void)
29 {
30 delete [] vr_ctx.info.display;
31 //System::Destroy();
32 }
34 static bool init_ovr()
35 {
36 LogMaskConstants log_level = LogMask_All;
37 // initialize Oculus SDK
38 const char *logenv = getenv("VR_LOGLEVEL");
39 if(logenv) {
40 switch(atoi(logenv)) {
41 case 0:
42 log_level = LogMask_None;
43 break;
44 case 1:
45 log_level = LogMask_Regular;
46 break;
47 case 2:
48 default:
49 log_level = LogMask_All;
50 break;
51 }
52 }
54 System::Init(Log::ConfigureDefaultLog(log_level));
55 if(!(vr_ctx.ovr_devman = DeviceManager::Create())) {
56 fprintf(stderr, "failed to create OVR device manager\n");
57 return false;
58 }
60 // create the display device
61 HMDInfo info;
62 if(!(vr_ctx.ovr_hmd_dev = vr_ctx.ovr_devman->EnumerateDevices<HMDDevice>().CreateDevice())) {
63 fprintf(stderr, "no oculus rift devices found\n");
64 } else {
65 if(vr_ctx.ovr_hmd_dev->GetDeviceInfo(&info)) {
66 printf("oculus device info:\n");
67 printf(" name: %s\n", info.DisplayDeviceName);
68 printf(" ipd: %f\n", info.InterpupillaryDistance);
69 printf(" distortion: %f %f %f %f\n", info.DistortionK[0],
70 info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]);
71 }
73 // calculate and store viewing parameters
74 vr_ctx.info.width = info.HResolution;
75 vr_ctx.info.height = info.VResolution;
76 vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height;
78 vr_ctx.info.ipd = info.InterpupillaryDistance;
79 for(int i=0; i<4; i++) {
80 vr_ctx.info.distort[i] = info.DistortionK[i];
81 }
83 Util::Render::StereoConfig stereohelp;
84 stereohelp.SetFullViewport(Util::Render::Viewport(0, 0, vr_ctx.info.width, vr_ctx.info.height));
85 stereohelp.SetStereoMode(Util::Render::Stereo_LeftRight_Multipass);
86 stereohelp.SetHMDInfo(info);
87 stereohelp.SetDistortionFitPointVP(-1.0, 0.0);
89 vr_ctx.info.scale = stereohelp.GetDistortionScale();
91 float vhalfsz = vr_ctx.info.scale * info.VScreenSize * 0.5;
92 vr_ctx.info.fov = 2.0 * atan(vhalfsz / info.EyeToScreenDistance);
94 vr_ctx.info.lens_center_offset = 0.5 - info.LensSeparationDistance / info.HScreenSize;
96 // calculate center of projection shift to match the lens positions
97 float center_dist_meters = info.HScreenSize * 0.25;
98 float proj_shift = center_dist_meters - info.LensSeparationDistance * 0.5;
99 vr_ctx.info.proj_center_offset = 4.0 * proj_shift / info.HScreenSize;
101 // grab the display info
102 vr_ctx.info.display = new char[strlen(info.DisplayDeviceName) + 1];
103 strcpy(vr_ctx.info.display, info.DisplayDeviceName);
105 vr_ctx.info.display_xoffs = info.DesktopX;
106 vr_ctx.info.display_yoffs = info.DesktopY;
108 printf("display: \"%s\" offset: %+d %+d\n", vr_ctx.info.display,
109 vr_ctx.info.display_xoffs, vr_ctx.info.display_yoffs);
110 }
112 // get the sensor device
113 if(vr_ctx.ovr_hmd_dev) {
114 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_hmd_dev->GetSensor())) {
115 fprintf(stderr, "failed to get oculus sensor device\n");
116 }
117 } else {
118 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_devman->EnumerateDevices<SensorDevice>().CreateDevice())) {
119 fprintf(stderr, "failed to get oculus sensor device\n");
120 }
121 }
123 if(vr_ctx.ovr_sensor_dev) {
124 SensorInfo sinfo;
125 if(vr_ctx.ovr_sensor_dev->GetDeviceInfo(&sinfo)) {
126 printf("oculus sensor device info:\n");
127 printf(" name: %s\n", sinfo.ProductName);
128 }
130 vr_ctx.ovr_sfusion.AttachToSensor(vr_ctx.ovr_sensor_dev);
131 }
132 return true;
133 }
135 #define EXTERNAL_SHADER
137 static bool init_sdr()
138 {
139 int status;
141 #ifdef EXTERNAL_SHADER
142 FILE *fp = fopen("sdr/sdr.glsl", "rb");
143 if(!fp) {
144 perror("failed to load sdr.glsl");
145 return false;
146 }
147 fseek(fp, 0, SEEK_END);
148 long sz = ftell(fp);
149 rewind(fp);
151 char *buf = (char*)alloca(sz + 1);
152 fread(buf, 1, sz, fp);
153 buf[sz] = 0;
154 sdr_src = buf;
156 fclose(fp);
157 #endif
159 unsigned int sdr = glCreateShader(GL_FRAGMENT_SHADER);
160 glShaderSource(sdr, 1, &sdr_src, 0);
161 glCompileShader(sdr);
162 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
163 if(!status) {
164 fprintf(stderr, "failed to compile distortion shader\n");
166 int loglen;
167 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &loglen);
169 if(loglen > 0) {
170 char *log = (char*)alloca(loglen);
171 glGetShaderInfoLog(sdr, loglen, &loglen, log);
172 fprintf(stderr, "%s\n", log);
173 }
175 return false;
176 }
178 sdrprog = glCreateProgram();
179 glAttachShader(sdrprog, sdr);
180 glLinkProgram(sdrprog);
181 if(!status) {
182 fprintf(stderr, "failed to link distortion shader program\n");
183 glDeleteShader(sdr);
184 return false;
185 }
187 int loc;
189 glUseProgram(sdrprog);
191 if((loc = glGetUniformLocation(sdrprog, "tex")) != -1) {
192 glUniform1i(loc, 0);
193 }
194 if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) {
195 glUniform1f(loc, 0);
196 }
197 if((loc = glGetUniformLocation(sdrprog, "scr_center")) != -1) {
198 glUniform2f(loc, 0, 0);
199 }
200 if((loc = glGetUniformLocation(sdrprog, "scale")) != -1) {
201 glUniform1f(loc, vr_ctx.info.scale);
202 }
203 if((loc = glGetUniformLocation(sdrprog, "aspect")) != -1) {
204 printf("setting aspect: %f\n", vr_ctx.info.aspect / 2.0);
205 glUniform1f(loc, vr_ctx.info.aspect / 2.0);
206 }
207 if((loc = glGetUniformLocation(sdrprog, "scale_in")) != -1) {
208 glUniform2f(loc, 1, 1);
209 }
210 if((loc = glGetUniformLocation(sdrprog, "dist_factors")) != -1) {
211 glUniform4f(loc, vr_ctx.info.distort[0], vr_ctx.info.distort[1],
212 vr_ctx.info.distort[2], vr_ctx.info.distort[3]);
213 }
215 return true;
216 }
218 extern "C" const char *vr_get_display_name(void)
219 {
220 return vr_ctx.info.display;
221 }
223 extern "C" void vr_get_display_pos(int *xptr, int *yptr)
224 {
225 *xptr = vr_ctx.info.display_xoffs;
226 *yptr = vr_ctx.info.display_yoffs;
227 }
229 extern "C" int vr_get_width(void)
230 {
231 return vr_ctx.info.width;
232 }
234 extern "C" int vr_get_height(void)
235 {
236 return vr_ctx.info.height;
237 }
239 extern "C" float vr_get_fov(void)
240 {
241 return vr_ctx.info.fov;
242 }
244 extern "C" float vr_get_aspect(void)
245 {
246 return vr_ctx.info.aspect;
247 }
249 extern "C" void vr_set_eyedist(float ipd)
250 {
251 vr_ctx.info.ipd = ipd;
252 }
254 extern "C" float vr_get_eyedist(void)
255 {
256 return vr_ctx.info.ipd;
257 }
259 extern "C" void vr_set_distort(const float *coef)
260 {
261 memcpy(vr_ctx.info.distort, coef, sizeof vr_ctx.info.distort);
262 }
264 extern "C" void vr_get_distort(float *coef)
265 {
266 memcpy(coef, vr_ctx.info.distort, sizeof vr_ctx.info.distort);
267 }
269 extern "C" void vr_set_prediction_sec(float dt)
270 {
271 vr_ctx.ovr_sfusion.SetPrediction(dt);
272 }
274 extern "C" float vr_get_prediction_sec(void)
275 {
276 return vr_ctx.ovr_sfusion.GetPredictionDelta();
277 }
279 extern "C" void vr_get_view_matrix(float *res, int eye)
280 {
281 // TODO
282 }
284 extern "C" void vr_get_proj_matrix(float *res, int eye)
285 {
286 static float eye_scale[] = {0.0, 1.0, -1.0};
288 Matrix4f proj = Matrix4f::PerspectiveRH(vr_ctx.info.fov, vr_ctx.info.aspect / 2.0, 0.3, 1000.0);
289 proj = Matrix4f::Translation(vr_ctx.info.proj_center_offset * eye_scale[eye], 0, 0) * proj;
291 memcpy(res, proj.M[0], 16 * sizeof(float));
292 }
294 extern "C" void vr_get_translation(float *offs)
295 {
296 // current oculus devkit doesn't do translation
297 offs[0] = offs[1] = offs[2] = 0.0f;
298 }
300 extern "C" void vr_get_rotation(float *quat)
301 {
302 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
303 quat[0] = oq.x;
304 quat[1] = oq.y;
305 quat[2] = oq.z;
306 quat[3] = oq.w;
307 }
309 extern "C" void vr_get_rotation_euler(float *euler)
310 {
311 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
312 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
313 }
315 extern "C" void vr_draw_eye(unsigned int tex, int eye)
316 {
317 static const float rects[3][4] = {
318 {-1, -1, 1, 1},
319 {-1, -1, 0, 1},
320 {0, -1, 1, 1}
321 };
322 static const float offs_scale[3] = {0.0, -1.0, 1.0};
324 glPushAttrib(GL_ENABLE_BIT);
325 glDisable(GL_DEPTH_TEST);
326 glDisable(GL_LIGHTING);
327 glEnable(GL_TEXTURE_2D);
329 glMatrixMode(GL_MODELVIEW);
330 glPushMatrix();
331 glLoadIdentity();
333 glMatrixMode(GL_PROJECTION);
334 glPushMatrix();
335 glLoadIdentity();
337 glUseProgram(sdrprog);
339 if(sdrprog) {
340 int loc;
341 if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) {
342 float offset = vr_ctx.info.lens_center_offset * offs_scale[eye];
343 glUniform1f(loc, offset);
344 }
345 }
347 glBindTexture(GL_TEXTURE_2D, tex);
348 glBegin(GL_QUADS);
349 glColor4f(1, 1, 1, 1);
350 glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]);
351 glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]);
352 glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]);
353 glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]);
354 glEnd();
356 glUseProgram(0);
358 glPopMatrix();
359 glMatrixMode(GL_MODELVIEW);
360 glPopMatrix();
362 glPopAttrib();
363 }