| 1 |
/************************************************************************** |
| 2 |
* Copyright (c) 2014 Bastiaan Veelo <Bastiaan a_t Veelo d_o_t net> * |
| 3 |
* Copyright (c) 2014 Jürgen Riegel <[email protected]> * |
| 4 |
* * |
| 5 |
* All rights reserved. Contact me if the below is too restrictive for you.* |
| 6 |
* * |
| 7 |
* Redistribution and use in source and binary forms, with or without * |
| 8 |
* modification, are permitted provided that the following conditions are * |
| 9 |
* met: * |
| 10 |
* * |
| 11 |
* Redistributions of source code must retain the above copyright notice, * |
| 12 |
* this list of conditions and the following disclaimer. * |
| 13 |
* * |
| 14 |
* Redistributions in binary form must reproduce the above copyright * |
| 15 |
* notice, this list of conditions and the following disclaimer in the * |
| 16 |
* documentation and/or other materials provided with the distribution. * |
| 17 |
* * |
| 18 |
* Neither the name of the copyright holder nor the names of its * |
| 19 |
* contributors may be used to endorse or promote products derived from * |
| 20 |
* this software without specific prior written permission. * |
| 21 |
* * |
| 22 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * |
| 23 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * |
| 24 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * |
| 25 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * |
| 26 |
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * |
| 27 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 28 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * |
| 29 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * |
| 30 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * |
| 31 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * |
| 32 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 33 |
\**************************************************************************/ |
| 34 |
|
| 35 |
|
| 36 |
#include "CoinRiftWidget.h" |
| 37 |
|
| 38 |
#include <Base/Console.h> |
| 39 |
|
| 40 |
#if BUILD_VR |
| 41 |
|
| 42 |
|
| 43 |
# undef max |
| 44 |
|
| 45 |
|
| 46 |
CoinRiftWidget::CoinRiftWidget() |
| 47 |
: QGLWidget() |
| 48 |
{ |
| 49 |
for (int eye = 0; eye < 2; eye++) { |
| 50 |
reinterpret_cast<ovrGLTextureData*>(&eyeTexture[eye])->TexId = 0; |
| 51 |
# ifdef USE_FRAMEBUFFER |
| 52 |
frameBufferID[eye] = 0; |
| 53 |
depthBufferID[eye] = 0; |
| 54 |
# endif |
| 55 |
} |
| 56 |
|
| 57 |
// OVR will do the swapping. |
| 58 |
setAutoBufferSwap(false); |
| 59 |
|
| 60 |
hmd = ovrHmd_Create(0); |
| 61 |
if (!hmd) { |
| 62 |
qDebug() << "Could not find Rift device."; |
| 63 |
throw; |
| 64 |
} |
| 65 |
|
| 66 |
if (!ovrHmd_ConfigureTracking( |
| 67 |
hmd, |
| 68 |
ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, |
| 69 |
ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position |
| 70 |
)) { // Capabilities we require. |
| 71 |
qDebug() << "Could not start Rift motion sensor."; |
| 72 |
throw; |
| 73 |
} |
| 74 |
|
| 75 |
resize(hmd->Resolution.w, hmd->Resolution.h); |
| 76 |
|
| 77 |
// Configure stereo settings. |
| 78 |
ovrSizei recommenedTex0Size |
| 79 |
= ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0f); |
| 80 |
ovrSizei recommenedTex1Size |
| 81 |
= ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0f); |
| 82 |
|
| 83 |
# ifdef USE_SO_OFFSCREEN_RENDERER |
| 84 |
renderer = new SoOffscreenRenderer(SbViewportRegion( |
| 85 |
std::max(recommenedTex0Size.w, recommenedTex0Size.w), |
| 86 |
std::max(recommenedTex1Size.h, recommenedTex1Size.h) |
| 87 |
)); |
| 88 |
renderer->setComponents(SoOffscreenRenderer::RGB_TRANSPARENCY); |
| 89 |
BackgroundColor = SbColor(.0f, .0f, .8f); |
| 90 |
renderer->setBackgroundColor(BackgroundColor); |
| 91 |
# endif |
| 92 |
# ifdef USE_FRAMEBUFFER |
| 93 |
m_sceneManager = new SoSceneManager(); |
| 94 |
m_sceneManager->setViewportRegion(SbViewportRegion( |
| 95 |
std::max(recommenedTex0Size.w, recommenedTex0Size.w), |
| 96 |
std::max(recommenedTex1Size.h, recommenedTex1Size.h) |
| 97 |
)); |
| 98 |
m_sceneManager->setBackgroundColor(SbColor(.0f, .0f, .8f)); |
| 99 |
# endif |
| 100 |
basePosition = SbVec3f(0.0f, 0.0f, -2.0f); |
| 101 |
|
| 102 |
// light handling |
| 103 |
SoDirectionalLight* light = new SoDirectionalLight(); |
| 104 |
light->direction.setValue(1, -1, -1); |
| 105 |
|
| 106 |
SoDirectionalLight* light2 = new SoDirectionalLight(); |
| 107 |
light2->direction.setValue(-1, -1, -1); |
| 108 |
light2->intensity.setValue(0.6); |
| 109 |
light2->color.setValue(0.8, 0.8, 1); |
| 110 |
|
| 111 |
|
| 112 |
scene = new SoSeparator(0); // Placeholder. |
| 113 |
for (int eye = 0; eye < 2; eye++) { |
| 114 |
rootScene[eye] = new SoSeparator(); |
| 115 |
rootScene[eye]->ref(); |
| 116 |
camera[eye] = new SoFrustumCamera(); |
| 117 |
camera[eye]->position.setValue(basePosition); |
| 118 |
camera[eye]->focalDistance.setValue(5.0f); |
| 119 |
camera[eye]->viewportMapping.setValue(SoCamera::LEAVE_ALONE); |
| 120 |
rootScene[eye]->addChild(camera[eye]); |
| 121 |
rootScene[eye]->addChild(light); |
| 122 |
rootScene[eye]->addChild(light2); |
| 123 |
rootScene[eye]->addChild(scene); |
| 124 |
} |
| 125 |
|
| 126 |
// Populate ovrEyeDesc[2]. |
| 127 |
eyeRenderDesc[0].Eye = ovrEye_Left; |
| 128 |
eyeRenderDesc[1].Eye = ovrEye_Right; |
| 129 |
eyeRenderDesc[0].Fov = hmd->DefaultEyeFov[0]; |
| 130 |
eyeRenderDesc[1].Fov = hmd->DefaultEyeFov[1]; |
| 131 |
# ifdef USE_SO_OFFSCREEN_RENDERER |
| 132 |
eyeTexture[0].Header.TextureSize.w |
| 133 |
= renderer->getViewportRegion().getViewportSizePixels().getValue()[0]; |
| 134 |
eyeTexture[0].Header.TextureSize.h |
| 135 |
= renderer->getViewportRegion().getViewportSizePixels().getValue()[1]; |
| 136 |
eyeTexture[1].Header.TextureSize = eyeTexture[0].Header.TextureSize; |
| 137 |
# endif |
| 138 |
# ifdef USE_FRAMEBUFFER |
| 139 |
eyeTexture[0].Header.TextureSize = recommenedTex0Size; |
| 140 |
eyeTexture[1].Header.TextureSize = recommenedTex1Size; |
| 141 |
# endif |
| 142 |
eyeTexture[0].Header.RenderViewport.Pos.x = 0; |
| 143 |
eyeTexture[0].Header.RenderViewport.Pos.y = 0; |
| 144 |
eyeTexture[0].Header.RenderViewport.Size = eyeTexture[0].Header.TextureSize; |
| 145 |
eyeTexture[1].Header.RenderViewport.Pos = eyeTexture[0].Header.RenderViewport.Pos; |
| 146 |
eyeTexture[1].Header.RenderViewport.Size = eyeTexture[1].Header.TextureSize; |
| 147 |
|
| 148 |
const int backBufferMultisample = 0; // TODO This is a guess? |
| 149 |
ovrGLConfig cfg; |
| 150 |
cfg.OGL.Header.API = ovrRenderAPI_OpenGL; |
| 151 |
cfg.OGL.Header.RTSize = hmd->Resolution; |
| 152 |
cfg.OGL.Header.Multisample = backBufferMultisample; |
| 153 |
cfg.OGL.Window = reinterpret_cast<HWND>(winId()); |
| 154 |
makeCurrent(); |
| 155 |
// cfg.OGL.WglContext = wglGetCurrentContext(); // |
| 156 |
// http://stackoverflow.com/questions/17532033/qglwidget-get-gl-contextes-for-windows |
| 157 |
cfg.OGL.DC = wglGetCurrentDC(); |
| 158 |
qDebug() << "Window:" << cfg.OGL.Window; |
| 159 |
// qDebug() << "Context:" << cfg.OGL.WglContext; |
| 160 |
qDebug() << "DC:" << cfg.OGL.DC; |
| 161 |
|
| 162 |
int DistortionCaps = 0; |
| 163 |
DistortionCaps |= ovrDistortionCap_Chromatic; |
| 164 |
// DistortionCaps |= ovrDistortionCap_TimeWarp; // Produces black screen... |
| 165 |
DistortionCaps |= ovrDistortionCap_Vignette; |
| 166 |
DistortionCaps |= ovrDistortionCap_HqDistortion; |
| 167 |
|
| 168 |
bool VSyncEnabled(false); // TODO This is a guess. |
| 169 |
if (!ovrHmd_ConfigureRendering( |
| 170 |
hmd, |
| 171 |
&cfg.Config, |
| 172 |
/*(VSyncEnabled ? 0 : ovrHmdCap_NoVSync),*/ |
| 173 |
DistortionCaps, |
| 174 |
hmd->DefaultEyeFov, // eyes, |
| 175 |
eyeRenderDesc |
| 176 |
)) { |
| 177 |
qDebug() << "Could not configure OVR rendering."; |
| 178 |
throw; |
| 179 |
} |
| 180 |
static const float nearPlane = 0.01; |
| 181 |
|
| 182 |
for (int eye = 0; eye < 2; eye++) { |
| 183 |
camera[eye]->aspectRatio.setValue( |
| 184 |
(eyeRenderDesc[eye].Fov.LeftTan + eyeRenderDesc[eye].Fov.RightTan) |
| 185 |
/ (eyeRenderDesc[eye].Fov.UpTan + eyeRenderDesc[eye].Fov.DownTan) |
| 186 |
); |
| 187 |
camera[eye]->nearDistance.setValue(nearPlane); |
| 188 |
camera[eye]->farDistance.setValue(10000.0f); |
| 189 |
camera[eye]->left.setValue(-eyeRenderDesc[eye].Fov.LeftTan * nearPlane); |
| 190 |
camera[eye]->right.setValue(eyeRenderDesc[eye].Fov.RightTan * nearPlane); |
| 191 |
camera[eye]->top.setValue(eyeRenderDesc[eye].Fov.UpTan * nearPlane); |
| 192 |
camera[eye]->bottom.setValue(-eyeRenderDesc[eye].Fov.DownTan * nearPlane); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
CoinRiftWidget::~CoinRiftWidget() |
| 198 |
{ |
| 199 |
# ifdef USE_SO_OFFSCREEN_RENDERER |
| 200 |
delete renderer; |
| 201 |
# endif |
| 202 |
for (int eye = 0; eye < 2; eye++) { |
| 203 |
rootScene[eye]->unref(); |
| 204 |
ovrGLTextureData* texData = reinterpret_cast<ovrGLTextureData*>(&eyeTexture[eye]); |
| 205 |
if (texData->TexId) { |
| 206 |
glDeleteTextures(1, &texData->TexId); |
| 207 |
texData->TexId = 0; |
| 208 |
} |
| 209 |
# ifdef USE_FRAMEBUFFER |
| 210 |
if (frameBufferID[eye] != 0) { |
| 211 |
// OVR::CAPI::GL::glDeleteFramebuffersExt(1, &frameBufferID[eye]); // TODO |
| 212 |
frameBufferID[eye] = 0; |
| 213 |
} |
| 214 |
if (depthBufferID[eye] != 0) { |
| 215 |
// OVR::CAPI::GL::glDeleteRenderbuffersExt(1, &depthBufferID[eye]); // TODO |
| 216 |
depthBufferID[eye] = 0; |
| 217 |
} |
| 218 |
# endif |
| 219 |
} |
| 220 |
scene = 0; |
| 221 |
// ovrHmd_StopSensor(hmd); |
| 222 |
ovrHmd_Destroy(hmd); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
void CoinRiftWidget::setBackgroundColor(const SbColor& Col) |
| 227 |
{ |
| 228 |
BackgroundColor = Col; |
| 229 |
renderer->setBackgroundColor(BackgroundColor); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
void CoinRiftWidget::setSceneGraph(SoNode* sceneGraph) |
| 234 |
{ |
| 235 |
rootScene[0]->replaceChild(scene, sceneGraph); |
| 236 |
rootScene[1]->replaceChild(scene, sceneGraph); |
| 237 |
scene = sceneGraph; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
void CoinRiftWidget::resizeGL(int width, int height) |
| 242 |
{ |
| 243 |
int side = qMin(width, height); |
| 244 |
glViewport((width - side) / 2, (height - side) / 2, side, side); |
| 245 |
|
| 246 |
glMatrixMode(GL_PROJECTION); |
| 247 |
glLoadIdentity(); |
| 248 |
glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 1000.0); |
| 249 |
glMatrixMode(GL_MODELVIEW); |
| 250 |
} |
| 251 |
|
| 252 |
void CoinRiftWidget::initializeGL() |
| 253 |
{ |
| 254 |
makeCurrent(); |
| 255 |
// Infer hardware capabilities. |
| 256 |
# ifdef USE_FRAMEBUFFER |
| 257 |
OVR::CAPI::GL::InitGLExtensions(); |
| 258 |
if (OVR::CAPI::GL::glBindFramebuffer == NULL) { |
| 259 |
qDebug() << "No GL extensions found."; |
| 260 |
exit(4); |
| 261 |
} |
| 262 |
|
| 263 |
// Store old framebuffer. |
| 264 |
GLint oldfb; |
| 265 |
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &oldfb); |
| 266 |
# endif |
| 267 |
|
| 268 |
// Create rendering target textures. |
| 269 |
glEnable(GL_TEXTURE_2D); |
| 270 |
for (int eye = 0; eye < 2; eye++) { |
| 271 |
# ifdef USE_FRAMEBUFFER |
| 272 |
OVR::CAPI::GL::glGenFramebuffers(1, &frameBufferID[eye]); |
| 273 |
OVR::CAPI::GL::glBindFramebuffer(GL_FRAMEBUFFER_EXT, frameBufferID[eye]); |
| 274 |
// Create the render buffer. |
| 275 |
// TODO: need to check for OpenGl 3 or higher and load the functions JR 2014 |
| 276 |
/*OVR::CAPI::GL::*/ glGenRenderbuffers(1, &depthBufferID[eye]); |
| 277 |
/*OVR::CAPI::GL::*/ glBindRenderbuffer(GL_RENDERBUFFER_EXT, depthBufferID[eye]); |
| 278 |
/*OVR::CAPI::GL::*/ glRenderbufferStorage( |
| 279 |
GL_RENDERBUFFER_EXT, |
| 280 |
GL_DEPTH_COMPONENT16, |
| 281 |
eyeTexture[eye].Header.TextureSize.w, |
| 282 |
eyeTexture[eye].Header.TextureSize.h |
| 283 |
); |
| 284 |
// Attach renderbuffer to framebuffer. |
| 285 |
OVR::CAPI::GL::glFramebufferRenderbuffer( |
| 286 |
GL_FRAMEBUFFER_EXT, |
| 287 |
GL_DEPTH_ATTACHMENT_EXT, |
| 288 |
GL_RENDERBUFFER_EXT, |
| 289 |
depthBufferID[eye] |
| 290 |
); |
| 291 |
# endif |
| 292 |
ovrGLTextureData* texData = reinterpret_cast<ovrGLTextureData*>(&eyeTexture[eye]); |
| 293 |
texData->Header.API = ovrRenderAPI_OpenGL; |
| 294 |
texData->Header.TextureSize = eyeTexture[eye].Header.TextureSize; |
| 295 |
texData->Header.RenderViewport = eyeTexture[eye].Header.RenderViewport; |
| 296 |
glGenTextures(1, &texData->TexId); |
| 297 |
glBindTexture(GL_TEXTURE_2D, texData->TexId); |
| 298 |
Q_ASSERT(!glGetError()); |
| 299 |
// Allocate storage for the texture. |
| 300 |
glTexImage2D( |
| 301 |
GL_TEXTURE_2D, |
| 302 |
0, |
| 303 |
GL_RGBA, |
| 304 |
eyeTexture[eye].Header.TextureSize.w, |
| 305 |
eyeTexture[eye].Header.TextureSize.h, |
| 306 |
0, |
| 307 |
GL_BGRA, |
| 308 |
GL_UNSIGNED_BYTE, |
| 309 |
NULL |
| 310 |
); |
| 311 |
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 312 |
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 313 |
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 314 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 315 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 316 |
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); |
| 317 |
Q_ASSERT(!glGetError()); |
| 318 |
# ifdef USE_FRAMEBUFFER |
| 319 |
// Attach texture to framebuffer color object. |
| 320 |
OVR::CAPI::GL::glFramebufferTexture2D( |
| 321 |
GL_FRAMEBUFFER_EXT, |
| 322 |
GL_COLOR_ATTACHMENT0_EXT, |
| 323 |
GL_TEXTURE_2D, |
| 324 |
texData->TexId, |
| 325 |
0 |
| 326 |
); |
| 327 |
if (OVR::CAPI::GL::glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 328 |
qDebug() << "ERROR: FrameBuffer is not operational!"; |
| 329 |
} |
| 330 |
# endif |
| 331 |
} |
| 332 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 333 |
glDisable(GL_TEXTURE_2D); |
| 334 |
|
| 335 |
# ifdef USE_FRAMEBUFFER |
| 336 |
// Continue rendering to the original frame buffer (likely 0, the onscreen buffer). |
| 337 |
OVR::CAPI::GL::glBindFramebuffer(GL_FRAMEBUFFER_EXT, oldfb); |
| 338 |
# endif |
| 339 |
doneCurrent(); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
void CoinRiftWidget::paintGL() |
| 344 |
{ |
| 345 |
const int ms(1000 / 75 /*fps*/); |
| 346 |
QTimer::singleShot(ms, this, &CoinRiftWidget::updateGL); |
| 347 |
|
| 348 |
// handling the safety warning |
| 349 |
handlingSafetyWarning(); |
| 350 |
|
| 351 |
makeCurrent(); |
| 352 |
|
| 353 |
ovrPosef eyePose[2]; |
| 354 |
|
| 355 |
glEnable(GL_TEXTURE_2D); |
| 356 |
|
| 357 |
ovrFrameTiming hmdFrameTiming = ovrHmd_BeginFrame(hmd, 0); |
| 358 |
for (int eyeIndex = 0; eyeIndex < ovrEye_Count; eyeIndex++) { |
| 359 |
ovrEyeType eye = hmd->EyeRenderOrder[eyeIndex]; |
| 360 |
eyePose[eye] = ovrHmd_GetEyePose(hmd, eye); |
| 361 |
|
| 362 |
|
| 363 |
SbRotation riftOrientation( |
| 364 |
eyePose[eye].Orientation.x, |
| 365 |
eyePose[eye].Orientation.y, |
| 366 |
eyePose[eye].Orientation.z, |
| 367 |
eyePose[eye].Orientation.w |
| 368 |
); |
| 369 |
|
| 370 |
camera[eye]->orientation.setValue(riftOrientation); |
| 371 |
|
| 372 |
SbVec3f riftPosition |
| 373 |
= SbVec3f(eyePose[eye].Position.x, eyePose[eye].Position.y, eyePose[eye].Position.z); |
| 374 |
|
| 375 |
|
| 376 |
// SbVec3f originalPosition(camera[eye]->position.getValue()); |
| 377 |
SbVec3f viewAdjust( |
| 378 |
eyeRenderDesc[eye].ViewAdjust.x, |
| 379 |
eyeRenderDesc[eye].ViewAdjust.y, |
| 380 |
eyeRenderDesc[eye].ViewAdjust.z |
| 381 |
); |
| 382 |
|
| 383 |
riftOrientation.multVec(viewAdjust, viewAdjust); |
| 384 |
|
| 385 |
camera[eye]->position.setValue(basePosition - viewAdjust + riftPosition); |
| 386 |
|
| 387 |
// Base::Console().log("Eye(%d) Pos: %f, %f, %f ViewAdjust: %f, %f, %f \n",eye, |
| 388 |
// eyePose[eye].Position.x, |
| 389 |
// eyePose[eye].Position.y, |
| 390 |
// eyePose[eye].Position.z, |
| 391 |
// eyeRenderDesc[eye].ViewAdjust.x, |
| 392 |
// eyeRenderDesc[eye].ViewAdjust.y, |
| 393 |
// eyeRenderDesc[eye].ViewAdjust.z); |
| 394 |
|
| 395 |
# ifdef USE_SO_OFFSCREEN_RENDERER |
| 396 |
ovrGLTextureData* texData = reinterpret_cast<ovrGLTextureData*>(&eyeTexture[eye]); |
| 397 |
glBindTexture(GL_TEXTURE_2D, texData->TexId); |
| 398 |
renderer->render(rootScene[eye]); |
| 399 |
Q_ASSERT(!glGetError()); |
| 400 |
glTexImage2D( |
| 401 |
GL_TEXTURE_2D, |
| 402 |
0, |
| 403 |
GL_RGBA, |
| 404 |
eyeTexture[eye].Header.TextureSize.w, |
| 405 |
eyeTexture[eye].Header.TextureSize.h, |
| 406 |
0, |
| 407 |
GL_RGBA /*GL_BGRA*/, |
| 408 |
GL_UNSIGNED_BYTE, |
| 409 |
renderer->getBuffer() |
| 410 |
); |
| 411 |
Q_ASSERT(!glGetError()); |
| 412 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 413 |
# endif |
| 414 |
# ifdef USE_FRAMEBUFFER |
| 415 |
// Clear state pollution from OVR SDK. |
| 416 |
glBindTexture(GL_TEXTURE_2D, 0); // You need this, at least if (hmdDesc.DistortionCaps & |
| 417 |
// ovrDistortion_Chromatic). |
| 418 |
OVR::CAPI::GL::glUseProgram(0); // You need this even more. |
| 419 |
|
| 420 |
GLint oldfb; |
| 421 |
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &oldfb); |
| 422 |
// Set up framebuffer for rendering. |
| 423 |
OVR::CAPI::GL::glBindFramebuffer(GL_FRAMEBUFFER_EXT, frameBufferID[eye]); |
| 424 |
|
| 425 |
m_sceneManager->setSceneGraph(rootScene[eye]); |
| 426 |
// m_sceneManager->setCamera(camera[eye]); // SoSceneManager does this implicitly. |
| 427 |
m_sceneManager->render(); |
| 428 |
|
| 429 |
// Continue rendering to the original frame buffer (likely 0, the onscreen buffer). |
| 430 |
OVR::CAPI::GL::glBindFramebuffer(GL_FRAMEBUFFER_EXT, oldfb); |
| 431 |
Q_ASSERT(!glGetError()); |
| 432 |
# endif |
| 433 |
|
| 434 |
// camera[eye]->position.setValue(originalPosition); |
| 435 |
} |
| 436 |
|
| 437 |
// Submit the texture for distortion. |
| 438 |
ovrHmd_EndFrame(hmd, eyePose, eyeTexture); |
| 439 |
|
| 440 |
// Swap buffers. |
| 441 |
glDisable(GL_CULL_FACE); |
| 442 |
glDisable(GL_DEPTH_TEST); |
| 443 |
// ovrHmd_EndFrame(hmd); |
| 444 |
glEnable(GL_CULL_FACE); |
| 445 |
glEnable(GL_DEPTH_TEST); |
| 446 |
glClearDepth(1.0); |
| 447 |
|
| 448 |
doneCurrent(); |
| 449 |
} |
| 450 |
|
| 451 |
void CoinRiftWidget::handlingSafetyWarning(void) |
| 452 |
{ |
| 453 |
// Health and Safety Warning display state. |
| 454 |
ovrHSWDisplayState hswDisplayState; |
| 455 |
ovrHmd_GetHSWDisplayState(hmd, &hswDisplayState); |
| 456 |
if (hswDisplayState.Displayed) { |
| 457 |
// Dismiss the warning if the user pressed the appropriate key or if the user |
| 458 |
// is tapping the side of the HMD. |
| 459 |
// If the user has requested to dismiss the warning via keyboard or controller input... |
| 460 |
// if (Util_GetAndResetHSWDismissedState()) |
| 461 |
ovrHmd_DismissHSWDisplay(hmd); |
| 462 |
// else |
| 463 |
//{ |
| 464 |
// // Detect a moderate tap on the side of the HMD. |
| 465 |
// ovrTrackingState ts = ovrHmd_GetTrackingState(hmd, ovr_GetTimeInSeconds()); |
| 466 |
// if (ts.StatusFlags & ovrStatus_OrientationTracked) |
| 467 |
// { |
| 468 |
// const OVR::Vector3f v(ts.RawSensorData.Accelerometer.x, |
| 469 |
// ts.RawSensorData.Accelerometer.y, |
| 470 |
// ts.RawSensorData.Accelerometer.z); |
| 471 |
// // Arbitrary value and representing moderate tap on the side of the DK2 Rift. |
| 472 |
// if (v.LengthSq() > 250.f) |
| 473 |
// ovrHmd_DismissHSWDisplay(hmd); |
| 474 |
// } |
| 475 |
// } |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
# ifdef BUILD_RIFT_TEST_MAIN |
| 481 |
|
| 482 |
int main(int argc, char* argv[]) |
| 483 |
{ |
| 484 |
SoDB::init(); |
| 485 |
|
| 486 |
QApplication app(argc, argv); |
| 487 |
qAddPostRoutine(cleanup); |
| 488 |
|
| 489 |
// Moved here because of |
| 490 |
// https://developer.oculusvr.com/forums/viewtopic.php?f=17&t=7915&p=108503#p108503 Init libovr. |
| 491 |
if (!ovr_Initialize()) { |
| 492 |
qDebug() << "Could not initialize Oculus SDK."; |
| 493 |
exit(1); |
| 494 |
} |
| 495 |
|
| 496 |
CoinRiftWidget window; |
| 497 |
window.show(); |
| 498 |
|
| 499 |
// An example scene. |
| 500 |
static const char* inlineSceneGraph[] = { |
| 501 |
"#Inventor V2.1 ascii\n", |
| 502 |
"\n", |
| 503 |
"Separator {\n", |
| 504 |
" Rotation { rotation 1 0 0 0.3 }\n", |
| 505 |
" Cone { }\n", |
| 506 |
" BaseColor { rgb 1 0 0 }\n", |
| 507 |
" Scale { scaleFactor .7 .7 .7 }\n", |
| 508 |
" Cube { }\n", |
| 509 |
"\n", |
| 510 |
" DrawStyle { style LINES }\n", |
| 511 |
" ShapeHints { vertexOrdering COUNTERCLOCKWISE }\n", |
| 512 |
" Coordinate3 {\n", |
| 513 |
" point [\n", |
| 514 |
" -2 -2 1.1, -2 -1 1.1, -2 1 1.1, -2 2 1.1,\n", |
| 515 |
" -1 -2 1.1, -1 -1 1.1, -1 1 1.1, -1 2 1.1\n", |
| 516 |
" 1 -2 1.1, 1 -1 1.1, 1 1 1.1, 1 2 1.1\n", |
| 517 |
" 2 -2 1.1, 2 -1 1.1, 2 1 1.1, 2 2 1.1\n", |
| 518 |
" ]\n", |
| 519 |
" }\n", |
| 520 |
"\n", |
| 521 |
" Complexity { value 0.7 }\n", |
| 522 |
" NurbsSurface {\n", |
| 523 |
" numUControlPoints 4\n", |
| 524 |
" numVControlPoints 4\n", |
| 525 |
" uKnotVector [ 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 ]\n", |
| 526 |
" vKnotVector [ 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 ]\n", |
| 527 |
" }\n", |
| 528 |
"}\n", |
| 529 |
NULL |
| 530 |
}; |
| 531 |
|
| 532 |
SoInput in; |
| 533 |
in.setStringArray(inlineSceneGraph); |
| 534 |
|
| 535 |
window.setSceneGraph(SoDB::readAll(&in)); |
| 536 |
|
| 537 |
return app.exec(); |
| 538 |
} |
| 539 |
|
| 540 |
# endif // BUILD_RIFT_TEST_MAIN |
| 541 |
|
| 542 |
#endif // BUILD_VR |