goat3dgfx

annotate src/vr/vr.cc @ 13:25bf39105c82

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