goat3dgfx

annotate src/vr/vr.cc @ 22:92bfb0206969

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