oculus1

view src/vr.cc @ 14:cceffea995a4

implemented wasd controls and clamping of the texcoords to [0, 1]
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 03:35:53 +0300
parents 464e1d135d68
children 402cbb6d9ce3
line source
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include "vr.h"
4 #include "vr_impl.h"
6 static const char *sdr_src =
7 "uniform sampler2D tex;\n"
8 "uniform vec2 lens_center;\n"
9 "uniform vec2 scr_center;\n"
10 "uniform vec2 scale;\n"
11 "uniform vec2 scale_in;\n"
12 "uniform vec4 warp_param;\n"
13 "\n"
14 "vec2 warp(in vec2 pt, out float dbgrad)\n"
15 "{\n"
16 " vec2 theta = (pt - lens_center) * scale_in;\n"
17 " float rsq = dot(theta, theta);\n"
18 " vec2 rvec = theta * (warp_param.x + \n"
19 " warp_param.y * rsq +\n"
20 " warp_param.z * rsq * rsq +\n"
21 " warp_param.w * rsq * rsq * rsq);\n"
22 " dbgrad = length(theta);\n"
23 " return lens_center + scale * rvec;\n"
24 "}\n"
25 "\n"
26 "void main()\n"
27 "{\n"
28 " float dbgrad;\n"
29 " vec2 tc = warp(gl_TexCoord[0].xy, dbgrad);\n"
30 " gl_FragColor.rgb = texture2D(tex, tc).rgb;\n"
31 " gl_FragColor.rgb += mix(vec3(1.0, 1.0, 0.0), vec3(0.0), smoothstep(0.01, 0.05, dbgrad));\n"
32 " gl_FragColor.a = 1.0;\n"
33 "}\n";
35 static bool init_ovr();
36 static bool init_sdr();
38 VRContext vr_ctx;
39 static unsigned int sdrprog;
41 extern "C" int vr_init(enum vr_init_mode mode)
42 {
43 glewInit();
45 if(!init_ovr()) {
46 return -1;
47 }
49 if(!init_sdr()) {
50 return -1;
51 }
53 return 0;
54 }
56 extern "C" void vr_shutdown(void)
57 {
58 //System::Destroy();
59 }
61 static bool init_ovr()
62 {
63 LogMaskConstants log_level = LogMask_All;
64 // initialize Oculus SDK
65 const char *logenv = getenv("VR_LOGLEVEL");
66 if(logenv) {
67 switch(atoi(logenv)) {
68 case 0:
69 log_level = LogMask_None;
70 break;
71 case 1:
72 log_level = LogMask_Regular;
73 break;
74 case 2:
75 default:
76 log_level = LogMask_All;
77 break;
78 }
79 }
81 System::Init(Log::ConfigureDefaultLog(log_level));
82 if(!(vr_ctx.ovr_devman = DeviceManager::Create())) {
83 fprintf(stderr, "failed to create OVR device manager\n");
84 return false;
85 }
87 // create the display device
88 HMDInfo info;
89 if(!(vr_ctx.ovr_hmd_dev = vr_ctx.ovr_devman->EnumerateDevices<HMDDevice>().CreateDevice())) {
90 fprintf(stderr, "no oculus rift devices found\n");
91 } else {
92 if(vr_ctx.ovr_hmd_dev->GetDeviceInfo(&info)) {
93 printf("oculus device info:\n");
94 printf(" name: %s\n", info.DisplayDeviceName);
95 printf(" ipd: %f\n", info.InterpupillaryDistance);
96 printf(" distortion: %f %f %f %f\n", info.DistortionK[0],
97 info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]);
98 }
100 // calculate and store viewing parameters
101 vr_ctx.info.width = info.HResolution;
102 vr_ctx.info.height = info.VResolution;
103 vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height;
105 vr_ctx.info.ipd = info.InterpupillaryDistance;
106 for(int i=0; i<4; i++) {
107 vr_ctx.info.distort[i] = info.DistortionK[i];
108 }
110 Util::Render::StereoConfig stereohelp;
111 stereohelp.SetFullViewport(Util::Render::Viewport(0, 0, vr_ctx.info.width, vr_ctx.info.height));
112 stereohelp.SetStereoMode(Util::Render::Stereo_LeftRight_Multipass);
113 stereohelp.SetHMDInfo(info);
114 stereohelp.SetDistortionFitPointVP(-1.0, 0.0);
116 vr_ctx.info.scale = stereohelp.GetDistortionScale();
118 float vhalfsz = vr_ctx.info.scale * info.VScreenSize * 0.5;
119 vr_ctx.info.fov = 2.0 * atan(vhalfsz / info.EyeToScreenDistance);
121 vr_ctx.info.lens_center_offset = 0.5 - info.LensSeparationDistance / info.HScreenSize;
123 // calculate center of projection shift to match the lens positions
124 float center_dist_meters = info.HScreenSize * 0.25;
125 float proj_shift = center_dist_meters - info.LensSeparationDistance * 0.5;
126 vr_ctx.info.proj_center_offset = 4.0 * proj_shift / info.HScreenSize;
127 }
129 // get the sensor device
130 if(vr_ctx.ovr_hmd_dev) {
131 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_hmd_dev->GetSensor())) {
132 fprintf(stderr, "failed to get oculus sensor device\n");
133 }
134 } else {
135 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_devman->EnumerateDevices<SensorDevice>().CreateDevice())) {
136 fprintf(stderr, "failed to get oculus sensor device\n");
137 }
138 }
140 if(vr_ctx.ovr_sensor_dev) {
141 SensorInfo sinfo;
142 if(vr_ctx.ovr_sensor_dev->GetDeviceInfo(&sinfo)) {
143 printf("oculus sensor device info:\n");
144 printf(" name: %s\n", sinfo.ProductName);
145 }
147 vr_ctx.ovr_sfusion.AttachToSensor(vr_ctx.ovr_sensor_dev);
148 }
149 return true;
150 }
152 #define EXTERNAL_SHADER
154 static bool init_sdr()
155 {
156 int status;
158 #ifdef EXTERNAL_SHADER
159 FILE *fp = fopen("sdr.glsl", "rb");
160 if(!fp) {
161 perror("failed to load sdr.glsl");
162 return false;
163 }
164 fseek(fp, 0, SEEK_END);
165 long sz = ftell(fp);
166 rewind(fp);
168 char *buf = (char*)alloca(sz + 1);
169 fread(buf, 1, sz, fp);
170 buf[sz] = 0;
171 sdr_src = buf;
173 fclose(fp);
174 #endif
176 unsigned int sdr = glCreateShader(GL_FRAGMENT_SHADER);
177 glShaderSource(sdr, 1, &sdr_src, 0);
178 glCompileShader(sdr);
179 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
180 if(!status) {
181 fprintf(stderr, "failed to compile distortion shader\n");
183 int loglen;
184 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &loglen);
186 if(loglen > 0) {
187 char *log = (char*)alloca(loglen);
188 glGetShaderInfoLog(sdr, loglen, &loglen, log);
189 fprintf(stderr, "%s\n", log);
190 }
192 return false;
193 }
195 sdrprog = glCreateProgram();
196 glAttachShader(sdrprog, sdr);
197 glLinkProgram(sdrprog);
198 if(!status) {
199 fprintf(stderr, "failed to link distortion shader program\n");
200 glDeleteShader(sdr);
201 return false;
202 }
204 int loc;
206 glUseProgram(sdrprog);
208 if((loc = glGetUniformLocation(sdrprog, "tex")) != -1) {
209 glUniform1i(loc, 0);
210 }
211 if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) {
212 glUniform1f(loc, 0);
213 }
214 if((loc = glGetUniformLocation(sdrprog, "scr_center")) != -1) {
215 glUniform2f(loc, 0, 0);
216 }
217 if((loc = glGetUniformLocation(sdrprog, "scale")) != -1) {
218 glUniform1f(loc, vr_ctx.info.scale);
219 }
220 if((loc = glGetUniformLocation(sdrprog, "aspect")) != -1) {
221 printf("setting aspect: %f\n", vr_ctx.info.aspect / 2.0);
222 glUniform1f(loc, vr_ctx.info.aspect / 2.0);
223 }
224 if((loc = glGetUniformLocation(sdrprog, "scale_in")) != -1) {
225 glUniform2f(loc, 1, 1);
226 }
227 if((loc = glGetUniformLocation(sdrprog, "dist_factors")) != -1) {
228 glUniform4f(loc, vr_ctx.info.distort[0], vr_ctx.info.distort[1],
229 vr_ctx.info.distort[2], vr_ctx.info.distort[3]);
230 }
232 return true;
233 }
235 extern "C" int vr_get_width(void)
236 {
237 return vr_ctx.info.width;
238 }
240 extern "C" int vr_get_height(void)
241 {
242 return vr_ctx.info.height;
243 }
245 extern "C" float vr_get_fov(void)
246 {
247 return vr_ctx.info.fov;
248 }
250 extern "C" float vr_get_aspect(void)
251 {
252 return vr_ctx.info.aspect;
253 }
255 extern "C" void vr_set_eyedist(float ipd)
256 {
257 vr_ctx.info.ipd = ipd;
258 }
260 extern "C" float vr_get_eyedist(void)
261 {
262 return vr_ctx.info.ipd;
263 }
265 extern "C" void vr_set_distort(const float *coef)
266 {
267 memcpy(vr_ctx.info.distort, coef, sizeof vr_ctx.info.distort);
268 }
270 extern "C" void vr_get_distort(float *coef)
271 {
272 memcpy(coef, vr_ctx.info.distort, sizeof vr_ctx.info.distort);
273 }
275 extern "C" void vr_set_prediction_sec(float dt)
276 {
277 vr_ctx.ovr_sfusion.SetPrediction(dt);
278 }
280 extern "C" float vr_get_prediction_sec(void)
281 {
282 return vr_ctx.ovr_sfusion.GetPredictionDelta();
283 }
285 extern "C" void vr_get_view_matrix(float *res, int eye)
286 {
287 // TODO
288 }
290 extern "C" void vr_get_proj_matrix(float *res, int eye)
291 {
292 static float eye_scale[] = {0.0, 1.0, -1.0};
294 Matrix4f proj = Matrix4f::PerspectiveRH(vr_ctx.info.fov, vr_ctx.info.aspect / 2.0, 0.3, 1000.0);
295 proj = Matrix4f::Translation(vr_ctx.info.proj_center_offset * eye_scale[eye], 0, 0) * proj;
297 memcpy(res, proj.M[0], 16 * sizeof(float));
298 }
300 extern "C" void vr_get_translation(float *offs)
301 {
302 // current oculus devkit doesn't do translation
303 offs[0] = offs[1] = offs[2] = 0.0f;
304 }
306 extern "C" void vr_get_rotation(float *quat)
307 {
308 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
309 quat[0] = oq.x;
310 quat[1] = oq.y;
311 quat[2] = oq.z;
312 quat[3] = oq.w;
313 }
315 extern "C" void vr_get_rotation_euler(float *euler)
316 {
317 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
318 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
319 }
321 extern "C" void vr_draw_eye(unsigned int tex, int eye)
322 {
323 static const float rects[3][4] = {
324 {-1, -1, 1, 1},
325 {-1, -1, 0, 1},
326 {0, -1, 1, 1}
327 };
328 static const float offs_scale[3] = {0.0, -1.0, 1.0};
330 glPushAttrib(GL_ENABLE_BIT);
331 glDisable(GL_DEPTH_TEST);
332 glDisable(GL_LIGHTING);
333 glEnable(GL_TEXTURE_2D);
335 glMatrixMode(GL_MODELVIEW);
336 glPushMatrix();
337 glLoadIdentity();
339 glMatrixMode(GL_PROJECTION);
340 glPushMatrix();
341 glLoadIdentity();
343 glUseProgram(sdrprog);
345 if(sdrprog) {
346 int loc;
347 if((loc = glGetUniformLocation(sdrprog, "lens_center_offset")) != -1) {
348 float offset = vr_ctx.info.lens_center_offset * offs_scale[eye];
349 glUniform1f(loc, offset);
350 }
351 }
353 glBindTexture(GL_TEXTURE_2D, tex);
354 glBegin(GL_QUADS);
355 glColor4f(1, 1, 1, 1);
356 glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]);
357 glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]);
358 glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]);
359 glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]);
360 glEnd();
362 glUseProgram(0);
364 glPopMatrix();
365 glMatrixMode(GL_MODELVIEW);
366 glPopMatrix();
368 glPopAttrib();
369 }