46#include <OgreBitwise.h>
47#include <OgreDepthBuffer.h>
51 #include "OgreD3D9RenderWindow.h"
53 #include "OgreD3D11Texture.h"
54 #include "OgreD3D11RenderWindow.h"
67 mStereoMode(SO3_NONE_STEREO),
69 mIsCustomProjection(false),
70 mIsCustomOffset(false),
71 mIsCustomRatio(false),
73 mIsCustomDistortion(false),
74 mIsCustomChromAbCorrection(false),
76 mHasExternalWindow(false),
83 mLeftUVScale(
Ogre::Vector2::UNIT_SCALE),
84 mLeftUVOffset(
Ogre::Vector2::ZERO),
85 mRightUVScale(
Ogre::Vector2::UNIT_SCALE),
86 mRightUVOffset(
Ogre::Vector2::ZERO),
87 mLeftRenderTextureSize(
SO3::
SPoint<int>(1, 1)),
88 mRightRenderTextureSize(
SO3::
SPoint<int>(1, 1)),
89 mLeftCustomProjection(
Ogre::Matrix4::IDENTITY),
90 mRightCustomProjection(
Ogre::Matrix4::IDENTITY),
91 mLeftStartWarpMatrix(
Ogre::Matrix4::IDENTITY),
92 mLeftEndWarpMatrix(
Ogre::Matrix4::IDENTITY),
93 mRightStartWarpMatrix(
Ogre::Matrix4::IDENTITY),
94 mRightEndWarpMatrix(
Ogre::Matrix4::IDENTITY),
95 mRightMask(~((
Ogre::uint32)0)),
96 mLeftMask(~((
Ogre::uint32)0)),
97 mDeviceLostListener(0)
136 Ogre::Root::getSingleton().getRenderSystem()->removeListener(mDeviceLostListener);
137 SAFE_DELETE(mDeviceLostListener);
140 StereoViewports::iterator iStereoViewport = stereoViewports.begin();
141 while(iStereoViewport != stereoViewports.end())
143 ShutdownViewport(iStereoViewport->first, iStereoViewport->second);
154 if (stereoViewports.empty())
157 iStereoViewport = stereoViewports.begin();
158 while(iStereoViewport != stereoViewports.end())
160 InitializeViewport(iStereoViewport->first, iStereoViewport->second);
169 Ogre::Root::getSingleton().getRenderSystem()->addListener(mDeviceLostListener);
176 if(mStereoMode != newStereoMode)
185 Ogre::Root::getSingleton().getRenderSystem()->removeListener(mDeviceLostListener);
186 SAFE_DELETE(mDeviceLostListener);
189 StereoViewports::iterator iStereoViewport = stereoViewports.begin();
190 while(iStereoViewport != stereoViewports.end())
192 ShutdownViewport(iStereoViewport->first, iStereoViewport->second);
198 mStereoMode = newStereoMode;
208 if (stereoViewports.empty())
209 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"StereoManager::SetStereoMode : At least one viewport must be registered\n");
211 StereoViewports::iterator iStereoViewport = stereoViewports.begin();
212 while(iStereoViewport != stereoViewports.end())
214 InitializeViewport(iStereoViewport->first, iStereoViewport->second);
223 Ogre::Root::getSingleton().getRenderSystem()->addListener(mDeviceLostListener);
234 StereoViewports::iterator iOgreViewports = stereoViewports.find(ogreViewportInstance);
235 if(iOgreViewports != stereoViewports.end())
237 StereoViewportData* viewportData = iOgreViewports->second;
238 viewportData->mCompList[compname] = state;
240 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
241 if(viewportData->mLastLeftViewport && compositorManager.hasCompositorChain(viewportData->mLastLeftViewport))
243 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(viewportData->mLastLeftViewport);
244 Ogre::CompositorInstance* compInstance = chain->getCompositor(compname);
248 compInstance->setEnabled(state);
253 if(viewportData->mLastRightViewport && compositorManager.hasCompositorChain(viewportData->mLastRightViewport))
255 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(viewportData->mLastRightViewport);
256 Ogre::CompositorInstance* compInstance = chain->getCompositor(compname);
260 compInstance->setEnabled(state);
274 StereoViewports::iterator iOgreViewports = stereoViewports.find(ogreViewportInstance);
275 if(iOgreViewports != stereoViewports.end())
277 StereoViewportData* viewportData = iOgreViewports->second;
279 if (!viewportData->mCamera)
282 if(!mIsCustomProjection)
284 viewportData->mCamera->setFrustumOffset(viewportData->mOldOffset);
287 viewportData->mCamera->setCustomProjectionMatrix(viewportData->mOldUseCustomMatrix, viewportData->mOldMatrix);
288 viewportData->mCamera->setAspectRatio(viewportData->mOldRatio);
289 viewportData->mCamera->setFOVy(viewportData->mOldFOV);
292 viewportData->mCamera->getParentSceneNode()->setPosition(viewportData->mOldPos);
294 viewportData->mCamera->resetFrustumExtents();
299 ogreViewportInstance->setCamera(viewportData->mCamera);
300 ogreViewportInstance->_updateDimensions();
302 catch(Ogre::Exception&)
306 viewportData->mCamera = 0;
315 StereoViewports::iterator iOgreViewports = stereoViewports.find(ogreViewportInstance);
316 if(iOgreViewports != stereoViewports.end())
318 StereoViewportData* viewportData = iOgreViewports->second;
321 ogreViewportInstance->setCamera(viewportData->mStereoCamera);
330 viewportData->mCamera = camera;
331 if(!viewportData->mCamera)
334 viewportData->mScene = camera->getSceneManager();
335 viewportData->mOldRatio = camera->getAspectRatio();
336 viewportData->mOldOffset = camera->getFrustumOffset();
337 viewportData->mOldPos = camera->getParentSceneNode()->getPosition();
338 viewportData->mOldFOV = camera->getFOVy();
339 viewportData->mOldMatrix = camera->getProjectionMatrix();
340 viewportData->mOldUseCustomMatrix = camera->isCustomProjectionMatrixEnabled();
341 Ogre::Real offset = 0.0f;
344 offset = (viewportData->mFlipEyes ? (-0.5f) : (0.5f)) *
GetEyesSpacing();
346 if (viewportData->mLastLeftViewport)
347 viewportData->mLastLeftViewport->setCamera(viewportData->mCamera);
349 if (viewportData->mLastRightViewport)
350 viewportData->mLastRightViewport->setCamera(viewportData->mCamera);
354 viewportData->mCamera->setAspectRatio(mRatio);
359 viewportData->mCamera->setFOVy(mFOV);
362 if(!mIsCustomProjection)
365 viewportData->mBaseCameraMatrix = camera->getProjectionMatrix();
369 Ogre::Matrix4 proj = Ogre::Matrix4::IDENTITY;
370 proj.setTrans(Ogre::Vector3(-mCenterOffset * (viewportData->mFlipEyes ? (-1) : 1), 0, 0));
371 viewportData->mCamera->setCustomProjectionMatrix(
true, proj * viewportData->mBaseCameraMatrix);
374 else if (offset != 0.0f)
376 viewportData->mCamera->setFrustumOffset(viewportData->mOldOffset + Ogre::Vector2(offset, 0));
381 viewportData->mCamera->setCustomProjectionMatrix(
true, mLeftCustomProjection);
386 viewportData->mCamera->getParentSceneNode()->setPosition(viewportData->mOldPos + (offset * (viewportData->mCamera->getParentSceneNode()->getOrientation() * Ogre::Vector3::UNIT_X)));
397 StereoViewports::iterator iOgreViewports = stereoViewports.find(ogreViewportInstance);
398 if(iOgreViewports != stereoViewports.end())
400 StereoViewportData* viewportData = iOgreViewports->second;
401 viewportData->mOldMatrix = camera->getProjectionMatrix();
402 viewportData->mOldUseCustomMatrix = camera->isCustomProjectionMatrixEnabled();
408 viewportData->mCamera->setAspectRatio(mRatio);
413 viewportData->mCamera->setFOVy(mFOV);
418 viewportData->mBaseCameraMatrix = camera->getProjectionMatrix();
424 StereoViewports::iterator iOgreViewports = stereoViewports.find(viewportInstance->
GetOgreViewPortPointer());
425 if(iOgreViewports != stereoViewports.end())
428 StereoViewportData* viewportData =
new StereoViewportData();
429 stereoViewports.insert(StereoViewports::value_type(viewportInstance->
GetOgreViewPortPointer(), viewportData));
436 StereoViewports::iterator iOgreViewports = stereoViewports.find(viewportInstance->
GetOgreViewPortPointer());
437 if(iOgreViewports != stereoViewports.end())
440 SAFE_DELETE(iOgreViewports->second);
441 stereoViewports.erase(iOgreViewports);
447 StereoViewports::iterator iOgreViewports = stereoViewports.find(ogreViewportInstance);
448 if(iOgreViewports != stereoViewports.end())
457 StereoViewports::iterator iOgreViewport = stereoViewports.find(viewport);
458 if (iOgreViewport == stereoViewports.end())
461 StereoViewportData* viewportData = iOgreViewport->second;
463 Ogre::Viewport* stereoLeftViewport = 0;
464 Ogre::Viewport* stereoRightViewport = 0;
466 Ogre::uint32 mask = viewport->getVisibilityMask();
469 viewportData->mOverlayEnable = viewport->getOverlaysEnabled();
472 viewportData->mShadowEnable = viewport->getShadowsEnabled();
474 viewportData->mSkyEnable = viewport->getSkiesEnabled();
475 viewportData->mMaterialScheme = viewport->getMaterialScheme();
476 viewportData->mMask = mask;
477 viewportData->mBackgroundColor = viewport->getBackgroundColour();
478 viewportData->mOldBackgroundColor = viewport->getBackgroundColour();
482 viewport->setOverlaysEnabled(
false);
483 viewport->setShadowsEnabled(
false);
487 if (viewportData->mLeftRenderTexture)
489 stereoLeftViewport = viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->getViewport(0);
490 if (stereoLeftViewport)
492 stereoLeftViewport->setOverlaysEnabled(viewportData->mOverlayEnable);
493 stereoLeftViewport->setShadowsEnabled(viewportData->mShadowEnable);
494 stereoLeftViewport->setVisibilityMask(viewportData->mMask);
495 stereoLeftViewport->setBackgroundColour(viewportData->mBackgroundColor);
496 stereoLeftViewport->setSkiesEnabled(viewportData->mSkyEnable);
497 stereoLeftViewport->setMaterialScheme(viewportData->mMaterialScheme);
501 if (mStereoMode !=
SO3_MONO_STEREO && viewportData->mRightRenderTexture)
503 stereoRightViewport = viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->getViewport(0);
504 if (stereoRightViewport)
506 stereoRightViewport->setShadowsEnabled(viewportData->mShadowEnable);
507 stereoRightViewport->setOverlaysEnabled(viewportData->mOverlayEnable);
508 stereoRightViewport->setVisibilityMask(viewportData->mMask);
509 stereoRightViewport->setBackgroundColour(viewportData->mBackgroundColor);
510 stereoRightViewport->setSkiesEnabled(viewportData->mSkyEnable);
511 stereoRightViewport->setMaterialScheme(viewportData->mMaterialScheme);
516void StereoManager::InitializeViewport(Ogre::Viewport* viewport, StereoViewportData* viewportData)
518 viewportData->mOriginalViewport = viewport;
520 Ogre::Camera* camera = viewport->getCamera();
529 viewportData->mCamera = camera;
530 viewportData->mScene = camera->getSceneManager();
532 viewportData->mOriginalViewportSize = Ogre::Vector2(viewport->getActualWidth(), viewport->getActualHeight());
535 viewport->setDimensions(0.0f, 0.0f, 2.0f / (
float)viewport->getActualWidth(), 2.0f / (
float)viewport->getActualHeight());
539 viewport->setDimensions(0.0f, 0.0f, 2.0f / (
float)viewport->getActualWidth(), 2.0f / (
float)viewport->getActualHeight());
545 viewportData->mStereoScene = Ogre::Root::getSingletonPtr()->createSceneManager(Ogre::SMT_DEFAULT, SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/SCENE") + Ogre::StringConverter::toString(viewport->getZOrder() + 1));
546 Ogre::AxisAlignedBox octreeBox(-1, -1, -1, 1, 1, 1);
548 viewportData->mStereoScene->setOption(
"Size", &octreeBox);
549 viewportData->mStereoScene->setOption(
"Depth", &octreeDepth);
551 Ogre::RenderQueue* rq = viewportData->mStereoScene->getRenderQueue();
552 rq->getQueueGroup(Ogre::RENDER_QUEUE_WORLD_GEOMETRY_1)->setShadowsEnabled(
false);
553 rq->getQueueGroup(Ogre::RENDER_QUEUE_WORLD_GEOMETRY_2)->setShadowsEnabled(
false);
554 rq->getQueueGroup(Ogre::RENDER_QUEUE_MAIN)->setShadowsEnabled(
false);
556 viewportData->mStereoCamera = viewportData->mStereoScene->createCamera(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/CAMERA") + Ogre::StringConverter::toString(viewport->getZOrder() + 1));
557 viewportData->mStereoCamera->setFarClipDistance(100.0f);
558 viewportData->mStereoCamera->setNearClipDistance(0.1f);
559 viewportData->mStereoCamera->setProjectionType(Ogre::PT_ORTHOGRAPHIC);
560 viewportData->mStereoCamera->setOrthoWindow(2.0f, 2.0f);
562 if (mExternalWindow && (mExternalWindow->getNumViewports() == 0))
564 mExternalViewport = mExternalWindow->addViewport(viewportData->mStereoCamera);
565 mExternalViewport->setMaterialScheme(viewport->getMaterialScheme());
569 viewportData->mStereoCameraNode->roll(Ogre::Degree(-90));
571 viewportData->mStereoScene->getRootSceneNode()->attachObject(viewportData->mStereoCamera);
572 InitializeListeners(viewport, viewportData);
574 viewportData->viewportListener =
new StereoViewportListener(
this, viewportData);
575 viewportData->frameListener =
new StereoFrameListener(
this, viewportData);
578 mExternalWindow->addListener(viewportData->viewportListener);
581 Ogre::Root::getSingletonPtr()->addFrameListener(viewportData->frameListener);
584 InitializeStereo(viewport, viewportData);
586 else if (mAvailableModes[mStereoMode].empty())
588 viewportData->mFlipEyes =
true;
594 catch(Ogre::Exception& e)
596 std::string mess(e.what());
603void StereoManager::ShutdownViewport(Ogre::Viewport* viewport, StereoViewportData* viewportData)
609 ShutdownStereo(viewport, viewportData);
611 ShutdownListeners(viewport, viewportData);
614 if (mExternalViewport)
616 mExternalWindow->removeViewport(mExternalViewport->getZOrder());
617 mExternalViewport = 0;
620 if (viewportData->mStereoScene)
622 Ogre::Root::getSingletonPtr()->destroySceneManager(viewportData->mStereoScene);
625 catch(Ogre::Exception&)
630 if (viewportData->viewportListener && mBuffer)
633 mExternalWindow->removeListener(viewportData->viewportListener);
637 SAFE_DELETE(viewportData->viewportListener);
639 if (viewportData->frameListener)
640 Ogre::Root::getSingletonPtr()->removeFrameListener(viewportData->frameListener);
642 SAFE_DELETE(viewportData->frameListener);
646 if (!viewportData->mCamera)
656 for (SWidgetManager::WidgetList::const_iterator wIt = wlist.begin(); wIt != wlist.end(); wIt++)
658 SWidget* widget = (wIt->second);
659 if (widget->GetIsOverlayed() || widget->GetIs2DRect())
661 if (mIsCustomRatio || !mAvailableModes[mStereoMode].empty())
663 widget->SetScale(1.0, 1.0);
665 widget->SetLeftOffset(0.0f);
666 widget->SetTopOffset(0.0f);
675 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
676 if (!viewportData->mScene)
682 if(viewportData->mLastLeftViewport)
684 viewportData->mLastLeftViewport->getTarget()->removeListener(viewportData->leftCameraListener);
695 if (compositorManager.hasCompositorChain(viewportData->mLastLeftViewport))
697 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(viewportData->mLastLeftViewport);
700 Ogre::CompositorChain::Instances instances = chain->getCompositorInstances();
701 for(
unsigned int i = 0; i < instances.size(); i++)
703 Ogre::CompositorInstance *compositorInstance = instances.at(i);
704 Ogre::Compositor *compositor = compositorInstance->getCompositor();
707 compositorInstance->setAlive(
false);
709 chain->removeAllCompositors();
710 compositorManager.removeCompositorChain(viewportData->mLastLeftViewport);
713 viewportData->mLastLeftViewport = 0;
716 if(viewportData->mLastRightViewport)
718 viewportData->mLastRightViewport->getTarget()->removeListener(viewportData->rightCameraListener);
729 if (compositorManager.hasCompositorChain(viewportData->mLastRightViewport))
731 Ogre::CompositorChain* chain = compositorManager.getCompositorChain(viewportData->mLastRightViewport);
734 Ogre::CompositorChain::Instances instances = chain->getCompositorInstances();
735 for (
unsigned int i = 0; i < instances.size(); i++)
737 Ogre::CompositorInstance *compositorInstance = instances.at(i);
738 Ogre::Compositor *compositor = compositorInstance->getCompositor();
741 compositorInstance->setAlive(
false);
743 chain->removeAllCompositors();
744 compositorManager.removeCompositorChain(viewportData->mLastRightViewport);
747 viewportData->mLastRightViewport = 0;
752 if(viewportData->mLeftRenderTexture)
753 viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->removeAllViewports();
755 if(viewportData->mRightRenderTexture)
756 viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->removeAllViewports();
758 catch(Ogre::Exception&)
766 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
770 bool isD3D = (rendererName ==
"Direct3D9 Rendering Subsystem" || rendererName ==
"Direct3D11 Rendering Subsystem") ?
true :
false;
772 Ogre::Viewport* stereoLeftViewport = 0;
773 Ogre::Viewport* stereoRightViewport = 0;
776 if (viewportData->mLeftRenderTexture)
778 stereoLeftViewport = viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->addViewport(viewportData->mCamera);
779 stereoLeftViewport->setOverlaysEnabled(viewportData->mOverlayEnable);
780 stereoLeftViewport->setShadowsEnabled(viewportData->mShadowEnable);
781 stereoLeftViewport->setVisibilityMask(viewportData->mMask);
782 stereoLeftViewport->setBackgroundColour(viewportData->mBackgroundColor);
783 stereoLeftViewport->setSkiesEnabled(viewportData->mSkyEnable);
784 stereoLeftViewport->setMaterialScheme(viewportData->mMaterialScheme);
787 if (mStereoMode !=
SO3_MONO_STEREO && viewportData->mRightRenderTexture)
789 stereoRightViewport = viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->addViewport(viewportData->mCamera);
790 stereoRightViewport->setShadowsEnabled(viewportData->mShadowEnable);
791 stereoRightViewport->setOverlaysEnabled(viewportData->mOverlayEnable);
792 stereoRightViewport->setVisibilityMask(viewportData->mMask);
793 stereoRightViewport->setBackgroundColour(viewportData->mBackgroundColor);
794 stereoRightViewport->setSkiesEnabled(viewportData->mSkyEnable);
795 stereoRightViewport->setMaterialScheme(viewportData->mMaterialScheme);
799 if (viewportData->mCompIndexList.size() > 0)
801 CompositorIndexList::iterator compIt = viewportData->mCompIndexList.begin();
802 CompositorIndexList::iterator compItEnd = viewportData->mCompIndexList.end();
804 while (compIt != compItEnd)
806 if (stereoLeftViewport)
808 compositorManager.addCompositor(stereoLeftViewport, *compIt);
809 compositorManager.setCompositorEnabled(stereoLeftViewport, *compIt, viewportData->mCompList[*compIt]);
814 compositorManager.addCompositor(stereoRightViewport, *compIt);
815 compositorManager.setCompositorEnabled(stereoRightViewport, *compIt, viewportData->mCompList[*compIt]);
824 compositorManager.addCompositor(stereoLeftViewport,
"FlipVertically");
825 compositorManager.setCompositorEnabled(stereoLeftViewport,
"FlipVertically",
true);
826 compositorManager.addCompositor(stereoRightViewport,
"FlipVertically");
827 compositorManager.setCompositorEnabled(stereoRightViewport,
"FlipVertically",
true);
832 if (stereoLeftViewport)
841 if (stereoRightViewport)
852 if (stereoLeftViewport)
854 stereoLeftViewport->getTarget()->insertListener(viewportData->leftCameraListener);
855 viewportData->mLastLeftViewport = stereoLeftViewport;
860 stereoRightViewport->getTarget()->insertListener(viewportData->rightCameraListener);
861 viewportData->mLastRightViewport = stereoRightViewport;
865void StereoManager::InitializeStereo(Ogre::Viewport* viewport, StereoViewportData* viewportData)
868 bool isD3D = (rendererName ==
"Direct3D9 Rendering Subsystem" || rendererName ==
"Direct3D11 Rendering Subsystem") ?
true : false;
882 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
883 Ogre::CompositorChain* oldChain = 0;
884 if(compositorManager.hasCompositorChain(viewport))
885 oldChain = compositorManager.getCompositorChain(viewport);
890 Ogre::CompositorChain::Instances instances = oldChain->getCompositorInstances();
891 for (
unsigned int i = 0; i < instances.size(); i++)
893 Ogre::CompositorInstance *compositorInstance = instances.at(i);
894 Ogre::Compositor *compositor = compositorInstance->getCompositor();
897 viewportData->mCompList.insert(viewportData->mCompList.begin(), std::pair<std::string, bool>(compositor->getName(), compositorInstance->getEnabled()));
898 viewportData->mCompIndexList.push_back(compositor->getName());
901 oldChain->setCompositorEnabled(i,
false);
905 compositorManager.getCompositorChain(viewport)->_markDirty();
909 Ogre::uint32 mask = viewport->getVisibilityMask();
910 viewportData->mOverlayEnable = viewport->getOverlaysEnabled();
911 viewportData->mShadowEnable = viewport->getShadowsEnabled();
912 viewportData->mSkyEnable = viewport->getSkiesEnabled();
913 viewportData->mMaterialScheme = viewport->getMaterialScheme();
914 viewportData->mMask = mask;
915 viewportData->mBackgroundColor = viewport->getBackgroundColour();
916 viewportData->mOldBackgroundColor = viewport->getBackgroundColour();
921 viewport->setOverlaysEnabled(
false);
922 viewport->setShadowsEnabled(
false);
926 Ogre::MaterialPtr mat =
static_cast<Ogre::MaterialPtr
>(Ogre::MaterialManager::getSingleton().getByName(mAvailableModes[mStereoMode], SO3_INTERNAL_RESOURCE_GROUP));
928 OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, mAvailableModes[mStereoMode] +
" not found, missing StereoManager resources",
"StereoManager::InitializeStereo");
930 viewportData->rttListener =
new StereoRTTListener(
this, viewportData);
932 int leftWidth = (mStereoMode ==
SO3_CARDBOARD_STEREO) ? viewport->getActualWidth() / 2 : viewport->getActualWidth();
933 int leftHeight = viewport->getActualHeight();
934 int rightWidth = (mStereoMode ==
SO3_CARDBOARD_STEREO) ? viewport->getActualWidth() / 2 : viewport->getActualWidth();
935 int rightHeight = viewport->getActualHeight();
946 if (mLeftRenderTextureSize.
x != 1 && mRightRenderTextureSize.
x != 1)
948 leftWidth = (mStereoMode ==
SO3_CARDBOARD_STEREO) ? mLeftRenderTextureSize.
x / 2 : mLeftRenderTextureSize.x;
949 leftHeight = mLeftRenderTextureSize.
y;
950 rightWidth = (mStereoMode ==
SO3_CARDBOARD_STEREO) ? mRightRenderTextureSize.
x / 2 : mRightRenderTextureSize.x;
951 rightHeight = mRightRenderTextureSize.
y;
957 viewportData->mLeftRenderTexture = Ogre::TextureManager::getSingleton().createManual(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/LEFT/RTT") + Ogre::StringConverter::toString(viewport->getZOrder() + 1), SO3_INTERNAL_RESOURCE_GROUP, Ogre::TEX_TYPE_2D, leftWidth, leftHeight, 0, rttFormat, Ogre::TU_RENDERTARGET);
958 viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->setAutoUpdated(
false);
961 viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->setDepthBufferPool(Ogre::DepthBuffer::POOL_NO_DEPTH);
963 viewportData->mLeftRenderTexture->addListener(viewportData->rttListener);
967 viewportData->mRightRenderTexture = Ogre::TextureManager::getSingleton().createManual(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/RIGHT/RTT") + Ogre::StringConverter::toString(viewport->getZOrder() + 1), SO3_INTERNAL_RESOURCE_GROUP, Ogre::TEX_TYPE_2D, rightWidth, rightHeight, 0, rttFormat, Ogre::TU_RENDERTARGET);
968 viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->setAutoUpdated(
false);
971 viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->setDepthBufferPool(Ogre::DepthBuffer::POOL_NO_DEPTH);
973 viewportData->mRightRenderTexture->addListener(viewportData->rttListener);
977 mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTexture(viewportData->mLeftRenderTexture);
980 mat->getTechnique(0)->getPass(0)->getTextureUnitState(1)->setTexture(viewportData->mRightRenderTexture);
983 viewportData->mStereoCameraNode = viewportData->mStereoScene->createSceneNode(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/CAMERA/NODE") + Ogre::StringConverter::toString(viewport->getZOrder() + 1));
984 viewportData->mStereoScene->getRootSceneNode()->addChild(viewportData->mStereoCameraNode);
986 viewportData->mRect =
new Ogre::Rectangle2D(
true);
987 viewportData->mRect->setCastShadows(
false);
988 viewportData->mRect->setMaterial(mat);
989 viewportData->mRect->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
990 viewportData->mStereoScene->getRootSceneNode()->attachObject(viewportData->mRect);
991 viewportData->mRect->setCorners(-1.0f, 1.0f, 1.0f, -1.0f);
995 viewportData->mRect->setUVs(Ogre::Vector2::UNIT_Y, Ogre::Vector2::ZERO, Ogre::Vector2::UNIT_SCALE, Ogre::Vector2::UNIT_X);
998 Ogre::AxisAlignedBox aabInf;
999 aabInf.setInfinite();
1000 viewportData->mRect->setBoundingBox(aabInf);
1001 viewportData->mRect->setVisible(
true);
1024 if (!mLeftMesh || !mRightMesh)
1029 Ogre::MaterialPtr leftmat =
static_cast<Ogre::MaterialPtr
>(Ogre::MaterialManager::getSingleton().getByName(
"Stereo/Oculus/Distortion/Left", SO3_INTERNAL_RESOURCE_GROUP));
1031 OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, mAvailableModes[mStereoMode] +
" not found, missing StereoManager resources",
"StereoManager::InitializeStereo");
1033 Ogre::MaterialPtr rightmat =
static_cast<Ogre::MaterialPtr
>(Ogre::MaterialManager::getSingleton().getByName(
"Stereo/Oculus/Distortion/Right", SO3_INTERNAL_RESOURCE_GROUP));
1035 OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, mAvailableModes[mStereoMode] +
" not found, missing StereoManager resources",
"StereoManager::InitializeStereo");
1037 Ogre::GpuProgramParametersSharedPtr leftShaderParams = leftmat->getTechnique(0)->getPass(0)->getVertexProgramParameters();
1038 leftShaderParams->setNamedConstant(
"eyeToSourceUVScale", mLeftUVScale);
1039 leftShaderParams->setNamedConstant(
"eyeToSourceUVOffset", mLeftUVOffset);
1041 Ogre::GpuProgramParametersSharedPtr rightShaderParams = rightmat->getTechnique(0)->getPass(0)->getVertexProgramParameters();
1042 rightShaderParams->setNamedConstant(
"eyeToSourceUVScale", mRightUVScale);
1043 rightShaderParams->setNamedConstant(
"eyeToSourceUVOffset", mRightUVOffset);
1045 viewportData->rttListener =
new StereoRTTListener(
this, viewportData);
1048 Ogre::PixelFormat rttFormat = Ogre::PF_R8G8B8;
1050 viewportData->mLeftRenderTexture = Ogre::TextureManager::getSingleton().createManual(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/LEFT/RTT") + Ogre::StringConverter::toString(viewport->getZOrder() + 1), SO3_INTERNAL_RESOURCE_GROUP, Ogre::TEX_TYPE_2D, mLeftRenderTextureSize.
x, mLeftRenderTextureSize.
y, 0, rttFormat, Ogre::TU_RENDERTARGET);
1051 viewportData->mRightRenderTexture = Ogre::TextureManager::getSingleton().createManual(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/RIGHT/RTT") + Ogre::StringConverter::toString(viewport->getZOrder() + 1), SO3_INTERNAL_RESOURCE_GROUP, Ogre::TEX_TYPE_2D, mRightRenderTextureSize.
x, mRightRenderTextureSize.
y, 0, rttFormat, Ogre::TU_RENDERTARGET);
1052 viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->setAutoUpdated(
false);
1053 viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->setAutoUpdated(
false);
1056 viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->setDepthBufferPool(Ogre::DepthBuffer::POOL_NO_DEPTH);
1057 viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->setDepthBufferPool(Ogre::DepthBuffer::POOL_NO_DEPTH);
1060 viewportData->mLeftRenderTexture->addListener(viewportData->rttListener);
1061 viewportData->mRightRenderTexture->addListener(viewportData->rttListener);
1064 leftmat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTexture(viewportData->mLeftRenderTexture);
1065 rightmat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTexture(viewportData->mRightRenderTexture);
1068 viewportData->mStereoCameraNode = viewportData->mStereoScene->createSceneNode(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/CAMERA/NODE") + Ogre::StringConverter::toString(viewport->getZOrder() + 1));
1069 viewportData->mStereoScene->getRootSceneNode()->addChild(viewportData->mStereoCameraNode);
1071 viewportData->mLeftEntity = viewportData->mStereoScene->createEntity(mLeftMesh);
1072 viewportData->mRightEntity = viewportData->mStereoScene->createEntity(mRightMesh);
1073 viewportData->mStereoCameraNode->attachObject(viewportData->mLeftEntity);
1074 viewportData->mStereoCameraNode->attachObject(viewportData->mRightEntity);
1076 viewportData->mStereoCameraNode->setPosition(0.0f, 0.0f, -1.0f);
1077 viewportData->mStereoCameraNode->setScale(1.0f, 1.0f, -1.0f);
1084void StereoManager::ShutdownStereo(Ogre::Viewport* viewport, StereoViewportData* viewportData)
1087 viewport->setDimensions(0.0f, 0.0f, 1.0f, 1.0f);
1091 viewport->setDimensions(0.0f, 0.0f, 1.0f, 1.0f);
1096 Ogre::CompositorManager &compositorManager = Ogre::CompositorManager::getSingleton();
1100 Ogre::CompositorChain* mainChain = compositorManager.getCompositorChain(viewport);
1103 CompositorIndexList::iterator compIt = viewportData->mCompIndexList.begin();
1104 CompositorIndexList::iterator compItEnd = viewportData->mCompIndexList.end();
1106 while (compIt != compItEnd)
1108 Ogre::CompositorInstance* inst = mainChain->getCompositor(*compIt);
1110 inst->setEnabled(viewportData->mCompList[*compIt]);
1114 mainChain->_markDirty();
1117 viewport->setOverlaysEnabled(viewportData->mOverlayEnable);
1118 viewport->setShadowsEnabled(viewportData->mShadowEnable);
1119 viewport->setBackgroundColour(viewportData->mBackgroundColor);
1121 if (scene && scene->GetEnvironment() && scene->GetEnvironment()->IsRegisteredViewport(viewport))
1123 if (scene->GetEnvironment()->GetWater() && scene->GetEnvironment()->GetWater()->GetEnable())
1126 if (scene->GetEnvironment()->GetSky() && scene->GetEnvironment()->GetSky()->GetEnable())
1127 scene->GetEnvironment()->GetSky()->AddViewport(viewport);
1130 if (scene && scene->GetEnvironment())
1132 scene->GetEnvironment()->UnregisterCamera(viewportData->mCamera);
1133 scene->GetEnvironment()->UnregisterCamera(viewportData->mStereoCamera);
1137 if (viewportData->mStereoScene)
1139 if (viewportData->mLeftEntity)
1140 viewportData->mStereoScene->destroyEntity(viewportData->mLeftEntity);
1141 if (viewportData->mRightEntity)
1142 viewportData->mStereoScene->destroyEntity(viewportData->mRightEntity);
1145 viewportData->mLeftEntity = 0;
1146 viewportData->mRightEntity = 0;
1148 if(viewportData->mLeftRenderTexture)
1150 viewportData->mLeftRenderTexture->removeListener(viewportData->rttListener);
1151 viewportData->mLeftRenderTexture->unload();
1152 Ogre::TextureManager::getSingleton().remove(viewportData->mLeftRenderTexture->getHandle());
1153 viewportData->mLeftRenderTexture.reset();
1156 if(viewportData->mRightRenderTexture)
1158 viewportData->mRightRenderTexture->removeListener(viewportData->rttListener);
1159 viewportData->mRightRenderTexture->unload();
1160 Ogre::TextureManager::getSingleton().remove(viewportData->mRightRenderTexture->getHandle());
1161 viewportData->mRightRenderTexture.reset();
1164 if (viewportData->mStereoCameraNode)
1166 viewportData->mStereoCameraNode->detachAllObjects();
1167 viewportData->mStereoCameraNode = 0;
1170 if (viewportData->mRect)
1172 viewportData->mRect->detachFromParent();
1173 delete(viewportData->mRect);
1174 viewportData->mRect = 0;
1177 SAFE_DELETE(viewportData->rttListener);
1180 viewportData->mCompList.clear();
1181 viewportData->mCompIndexList.clear();
1184void StereoManager::InitializeListeners(Ogre::Viewport* viewport, StereoViewportData* viewportData)
1186 viewportData->leftCameraListener =
new StereoCameraListener(
this, viewportData->mFlipEyes ?
false : true, viewport, viewportData);
1189 viewportData->rightCameraListener =
new StereoCameraListener(
this, viewportData->mFlipEyes ?
true : false, viewport, viewportData);
1191 viewportData->rightCameraListener = 0;
1194void StereoManager::ShutdownListeners(Ogre::Viewport* viewport, StereoViewportData* viewportData)
1196 SAFE_DELETE(viewportData->leftCameraListener);
1197 SAFE_DELETE(viewportData->rightCameraListener);
1203 return mEyesSpacing;
1213 mIsCustomProjection = enable;
1214 mLeftCustomProjection = leftMatrix;
1215 mRightCustomProjection = rightMatrix;
1218void StereoManager::SetTimeWarpMatrix(
const Ogre::Matrix4 &leftStartMatrix,
const Ogre::Matrix4 &leftEndMatrix,
const Ogre::Matrix4 &rightStartMatrix,
const Ogre::Matrix4 &rightEndMatrix)
1220 mLeftStartWarpMatrix = leftStartMatrix;
1221 mLeftEndWarpMatrix = leftEndMatrix;
1222 mRightStartWarpMatrix = rightStartMatrix;
1223 mRightEndWarpMatrix = rightEndMatrix;
1228 enabled = mIsCustomProjection;
1229 leftMatrix = mLeftCustomProjection;
1230 rightMatrix = mRightCustomProjection;
1235 mLeftUVScale = leftuvscale;
1236 mLeftUVOffset = leftuvoffset;
1238 mRightUVScale = rightuvscale;
1239 mRightUVOffset = rightuvoffset;
1244 if (leftsize.
x <= 0 || leftsize.
y <= 0 || rightsize.
x <= 0 || rightsize.
y <= 0)
1266 mLeftRenderTextureSize = leftsize;
1267 mRightRenderTextureSize = rightsize;
1275 std::array <SCOL_PTR_TYPE, 2> glid = { 0, 0 };
1276 StereoViewports::iterator iStereoViewport = stereoViewports.begin();
1277 if (iStereoViewport != stereoViewports.end())
1279 StereoViewportData* viewportData = iStereoViewport->second;
1281 if (viewportData->mLeftRenderTexture)
1283#if defined(_WIN32) && defined(SO3_USE_DX11)
1285 glid[0] = (SCOL_PTR_TYPE)(
static_cast<Ogre::D3D11Texture*
>(viewportData->mLeftRenderTexture.get())->GetTex2D());
1289 viewportData->mLeftRenderTexture->getCustomAttribute(
"GLID", &glid[0]);
1293 if (viewportData->mRightRenderTexture)
1295#if defined(_WIN32) && defined(SO3_USE_DX11)
1297 glid[1] = (SCOL_PTR_TYPE)(
static_cast<Ogre::D3D11Texture*
>(viewportData->mRightRenderTexture.get())->GetTex2D());
1301 viewportData->mRightRenderTexture->getCustomAttribute(
"GLID", &glid[1]);
1311 StereoViewports::iterator iStereoViewport = stereoViewports.begin();
1312 if (iStereoViewport != stereoViewports.end())
1314 StereoViewportData* viewportData = iStereoViewport->second;
1316 if (viewportData->mLeftRenderTexture)
1318 vpsize[0].x = viewportData->mLeftRenderTexture->getWidth();
1319 vpsize[0].y = viewportData->mLeftRenderTexture->getHeight();
1322 if (viewportData->mRightRenderTexture)
1324 vpsize[1].x = viewportData->mRightRenderTexture->getWidth();
1325 vpsize[1].y = viewportData->mRightRenderTexture->getHeight();
1333 mHasExternalWindow = state;
1336 mWindowMonitorIndex = index;
1338 if(mHasExternalWindow && !mExternalWindow)
1342 Ogre::NameValuePairList viewConfig;
1343 viewConfig[
"Resource Creation Policy"] =
"Create on all devices";
1344 viewConfig[
"Multi device memory hint"] =
"Auto hardware buffers management";
1345 viewConfig[
"vsync"] =
"Yes";
1346 viewConfig[
"Use Multihead"] =
"Auto";
1347 viewConfig[
"FSAAHint"] =
"Quality";
1348 viewConfig[
"FSAA"] =
"2";
1351 viewConfig[
"left"] = Ogre::StringConverter::toString(mWindowPos.
x);
1352 viewConfig[
"top"] = Ogre::StringConverter::toString(mWindowPos.
y);
1356 viewConfig[
"monitorIndex"] = Ogre::StringConverter::toString(mWindowMonitorIndex);
1358 mExternalWindow = Ogre::Root::getSingleton().createRenderWindow(
"STEREO", mWindowSise.
x, mWindowSise.
y,
true, &viewConfig);
1360 catch(Ogre::Exception&)
1365 else if (!mHasExternalWindow && mExternalWindow)
1367 if (mExternalViewport)
1368 mExternalWindow->removeViewport(mExternalViewport->getZOrder());
1371 mExternalWindow = 0;
1372 mExternalViewport = 0;
1376void StereoManager::SetStereoMeshLeft(std::vector<Ogre::Vector3> vertices, std::vector<std::vector<Ogre::Vector2> > uvs, std::vector<Ogre::Real> vignetteColor, std::vector<Ogre::Real> warp, std::vector<int> indexs)
1380 Ogre::MeshManager::getSingleton().remove(mLeftMesh->getHandle());
1384 if (vertices.size() <= 0)
1387 unsigned int nbUvs = (uvs.size() > 0) ? uvs[0].size() : 0;
1389 mLeftMesh = Ogre::MeshManager::getSingleton().createManual(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/LEFT/MESH"), SO3_INTERNAL_RESOURCE_GROUP);
1390 mLeftMesh->sharedVertexData =
new Ogre::VertexData();
1391 mLeftMesh->sharedVertexData->vertexCount = vertices.size();
1392 Ogre::VertexData* vdata = mLeftMesh->sharedVertexData;
1395 Ogre::VertexDeclaration* decl = vdata->vertexDeclaration;
1398 Ogre::VertexBufferBinding* bind = vdata->vertexBufferBinding;
1400 size_t vBufSegmentSize = 0;
1401 size_t texSegmentSize = 0;
1405 decl->addElement(0, vBufSegmentSize,Ogre::VET_FLOAT3,Ogre::VES_POSITION);
1406 vBufSegmentSize += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
1409 decl->addElement(0, vBufSegmentSize, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
1410 vBufSegmentSize += Ogre::VertexElement::getTypeSize(Ogre::VET_COLOUR);
1415 for (
unsigned int i = 0; i < nbUvs; i++)
1417 decl->addElement(1, texSegmentSize, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, countTex);
1418 texSegmentSize += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
1423 Ogre::HardwareVertexBufferSharedPtr vbuf = Ogre::HardwareBufferManager::getSingletonPtr()
1424 ->createVertexBuffer(vBufSegmentSize, vdata->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
1425 Ogre::HardwareVertexBufferSharedPtr texBuf = Ogre::HardwareBufferManager::getSingletonPtr()
1426 ->createVertexBuffer(texSegmentSize, vdata->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
1429 bind->setBinding(0, vbuf);
1430 bind->setBinding(1, texBuf);
1433 unsigned char* pVert =
static_cast<unsigned char*
>(vbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
1434 unsigned char* pTexVert =
static_cast<unsigned char*
>(texBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
1439 Ogre::VertexDeclaration::VertexElementList elems = decl->findElementsBySource(0);
1440 Ogre::VertexDeclaration::VertexElementList texElems = decl->findElementsBySource(1);
1444 for (
unsigned int i = 0; i < vertices.size(); ++i)
1446 Ogre::Vector3 v = vertices[i];
1447 Ogre::Real vignette = (i < vignetteColor.size()) ? vignetteColor[i] : 1.0f;
1448 Ogre::Real vwarp = (i < warp.size()) ? warp[i] : 0.0f;
1449 Ogre::ColourValue vc(vignette, vwarp, 1.0f, 1.0f);
1450 std::vector<Ogre::Vector2> vuvs;
1454 Ogre::VertexDeclaration::VertexElementList::const_iterator elemItr, elemEnd;
1455 elemEnd = elems.end();
1456 for (elemItr = elems.begin(); elemItr != elemEnd; ++elemItr)
1459 const Ogre::VertexElement& elem = *elemItr;
1460 switch (elem.getSemantic())
1462 case Ogre::VES_POSITION:
1464 elem.baseVertexPointerToElement(pVert, &pFloat);
1471 case Ogre::VES_DIFFUSE:
1473 elem.baseVertexPointerToElement(pVert, &pCol);
1474 *pCol = Ogre::VertexElement::convertColourValue(vc, Ogre::VertexElement::getBestColourVertexElementType());
1483 elemEnd = texElems.end();
1484 for (elemItr = texElems.begin(); elemItr != elemEnd; ++elemItr)
1487 const Ogre::VertexElement& elem = *elemItr;
1488 switch (elem.getSemantic())
1490 case Ogre::VES_TEXTURE_COORDINATES:
1492 elem.baseVertexPointerToElement(pTexVert, &pFloat);
1493 *pFloat++ = vuvs[iTexCoord].x;
1494 *pFloat++ = vuvs[iTexCoord].y;
1504 pVert += vbuf->getVertexSize();
1505 pTexVert += texBuf->getVertexSize();
1511 Ogre::SubMesh* pSubmesh = mLeftMesh->createSubMesh();
1512 pSubmesh->operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST;
1515 pSubmesh->setMaterialName(mAvailableModes[mStereoMode] +
"/Left");
1518 pSubmesh->useSharedVertices =
true;
1520 pSubmesh->vertexData =
new Ogre::VertexData();
1521 pSubmesh->vertexData->vertexCount = vertices.size();
1524 pSubmesh->indexData->indexCount = indexs.size();
1525 pSubmesh->indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(
1526 Ogre::HardwareIndexBuffer::IT_32BIT,
1527 pSubmesh->indexData->indexCount,
1528 Ogre::HardwareBuffer::HBU_STATIC);
1531 Ogre::uint32* pIdx =
static_cast<Ogre::uint32*
>(pSubmesh->indexData->indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD));
1532 for (
unsigned int i = 0; i < indexs.size(); i++)
1534 *pIdx++ =
static_cast<Ogre::uint32
>(indexs[i]);
1536 pSubmesh->indexData->indexBuffer->unlock();
1538 mLeftMesh->_setBounds(Ogre::AxisAlignedBox::BOX_INFINITE,
false);
1539 mLeftMesh->_setBoundingSphereRadius(100000.0f);
1544void StereoManager::SetStereoMeshRight(std::vector<Ogre::Vector3> vertices, std::vector<std::vector<Ogre::Vector2> > uvs, std::vector<Ogre::Real> vignetteColor, std::vector<Ogre::Real> warp, std::vector<int> indexs)
1548 Ogre::MeshManager::getSingleton().remove(mRightMesh->getHandle());
1552 if (vertices.size() <= 0)
1555 unsigned int nbUvs = (uvs.size() > 0) ? uvs[0].size() : 0;
1557 mRightMesh = Ogre::MeshManager::getSingleton().createManual(SO3_INTERNAL_RESOURCE_GROUP + Ogre::String(
"/STEREO/RIGHT/MESH"), SO3_INTERNAL_RESOURCE_GROUP);
1558 mRightMesh->sharedVertexData =
new Ogre::VertexData();
1559 mRightMesh->sharedVertexData->vertexCount = vertices.size();
1560 Ogre::VertexData* vdata = mRightMesh->sharedVertexData;
1563 Ogre::VertexDeclaration* decl = vdata->vertexDeclaration;
1566 Ogre::VertexBufferBinding* bind = vdata->vertexBufferBinding;
1568 size_t vBufSegmentSize = 0;
1569 size_t texSegmentSize = 0;
1573 decl->addElement(0, vBufSegmentSize,Ogre::VET_FLOAT3,Ogre::VES_POSITION);
1574 vBufSegmentSize += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
1577 decl->addElement(0, vBufSegmentSize, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
1578 vBufSegmentSize += Ogre::VertexElement::getTypeSize(Ogre::VET_COLOUR);
1583 for (
unsigned int i = 0; i < nbUvs; i++)
1585 decl->addElement(1, texSegmentSize, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, countTex);
1586 texSegmentSize += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2);
1591 Ogre::HardwareVertexBufferSharedPtr vbuf = Ogre::HardwareBufferManager::getSingletonPtr()
1592 ->createVertexBuffer(vBufSegmentSize, vdata->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
1593 Ogre::HardwareVertexBufferSharedPtr texBuf = Ogre::HardwareBufferManager::getSingletonPtr()
1594 ->createVertexBuffer(texSegmentSize, vdata->vertexCount, Ogre::HardwareBuffer::HBU_STATIC,
false);
1597 bind->setBinding(0, vbuf);
1598 bind->setBinding(1, texBuf);
1601 unsigned char* pVert =
static_cast<unsigned char*
>(vbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
1602 unsigned char* pTexVert =
static_cast<unsigned char*
>(texBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
1607 Ogre::VertexDeclaration::VertexElementList elems = decl->findElementsBySource(0);
1608 Ogre::VertexDeclaration::VertexElementList texElems = decl->findElementsBySource(1);
1612 for (
unsigned int i = 0; i < vertices.size(); ++i)
1614 Ogre::Vector3 v = vertices[i];
1615 Ogre::Real vignette = (i < vignetteColor.size()) ? vignetteColor[i] : 1.0f;
1616 Ogre::Real vwarp = (i < warp.size()) ? warp[i] : 0.0f;
1617 Ogre::ColourValue vc(vignette, vwarp, 1.0f, 1.0f);
1618 std::vector<Ogre::Vector2> vuvs;
1622 Ogre::VertexDeclaration::VertexElementList::const_iterator elemItr, elemEnd;
1623 elemEnd = elems.end();
1624 for (elemItr = elems.begin(); elemItr != elemEnd; ++elemItr)
1627 const Ogre::VertexElement& elem = *elemItr;
1628 switch (elem.getSemantic())
1630 case Ogre::VES_POSITION:
1632 elem.baseVertexPointerToElement(pVert, &pFloat);
1639 case Ogre::VES_DIFFUSE:
1641 elem.baseVertexPointerToElement(pVert, &pCol);
1642 *pCol = Ogre::VertexElement::convertColourValue(vc, Ogre::VertexElement::getBestColourVertexElementType());
1651 elemEnd = texElems.end();
1652 for (elemItr = texElems.begin(); elemItr != elemEnd; ++elemItr)
1655 const Ogre::VertexElement& elem = *elemItr;
1656 switch (elem.getSemantic())
1658 case Ogre::VES_TEXTURE_COORDINATES:
1660 elem.baseVertexPointerToElement(pTexVert, &pFloat);
1661 *pFloat++ = vuvs[iTexCoord].x;
1662 *pFloat++ = vuvs[iTexCoord].y;
1672 pVert += vbuf->getVertexSize();
1673 pTexVert += texBuf->getVertexSize();
1679 Ogre::SubMesh* pSubmesh = mRightMesh->createSubMesh();
1680 pSubmesh->operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST;
1683 pSubmesh->setMaterialName(mAvailableModes[mStereoMode] +
"/Right");
1686 pSubmesh->useSharedVertices =
true;
1688 pSubmesh->vertexData =
new Ogre::VertexData();
1689 pSubmesh->vertexData->vertexCount = vertices.size();
1692 pSubmesh->indexData->indexCount = indexs.size();
1693 pSubmesh->indexData->indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(
1694 Ogre::HardwareIndexBuffer::IT_32BIT,
1695 pSubmesh->indexData->indexCount,
1696 Ogre::HardwareBuffer::HBU_STATIC);
1699 Ogre::uint32* pIdx =
static_cast<Ogre::uint32*
>(pSubmesh->indexData->indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD));
1700 for (
unsigned int i = 0; i < indexs.size(); i++)
1702 *pIdx++ =
static_cast<Ogre::uint32
>(indexs[i]);
1704 pSubmesh->indexData->indexBuffer->unlock();
1706 mRightMesh->_setBounds(Ogre::AxisAlignedBox::BOX_INFINITE,
false);
1707 mRightMesh->_setBoundingSphereRadius(100000.0f);
1718 mIsCustomOffset = (offset == 0.0) ?
false :
true;
1719 mCenterOffset = offset;
1731 mIsCustomRatio = (ratio == 0.0) ?
false :
true;
1749 mIsCustomFOV = (fov == 0.0) ?
false :
true;
1762 mRotateView = state;
1779 mChromAbCorrection = chromAbC;
1781 mIsCustomChromAbCorrection = enable;
1794 mIsCustomDistortion = enable;
1801StereoManager::DeviceLostListener::DeviceLostListener(
StereoManager* stereoMgr) : mStereoMgr(stereoMgr)
1805void StereoManager::DeviceLostListener::eventOccurred (
const Ogre::String& eventName,
const Ogre::NameValuePairList* parameters)
1807 if(eventName ==
"DeviceLost")
1810 else if(eventName ==
"DeviceRestored")
1813 else if(eventName ==
"BeforeDevicePresent")
1822 else if(eventName ==
"AfterDevicePresent")
1830StereoManager::StereoCameraListener::StereoCameraListener(StereoManager* stereoMgr,
bool isLeftEye, Ogre::Viewport* originalViewport, StereoManager::StereoViewportData* stereoViewportData) : mStereoMgr(stereoMgr),
1831 mIsLeftEye(isLeftEye),
1832 viewportData(stereoViewportData),
1833 mOriginalViewport(originalViewport)
1837void StereoManager::StereoCameraListener::preRenderTargetUpdate(
const Ogre::RenderTargetEvent& evt)
1839 if (!viewportData->mCamera)
1842 int texwidth = (mIsLeftEye) ? viewportData->mLeftRenderTexture->getWidth() : viewportData->mRightRenderTexture->getWidth();
1843 int texheight = (mIsLeftEye) ? viewportData->mLeftRenderTexture->getHeight() : viewportData->mRightRenderTexture->getHeight();
1846 if (viewportData->mCamera->getAutoAspectRatio())
1848 viewportData->mCamera->setAspectRatio((
float)texwidth / (float)texheight);
1850 if (mStereoMgr->GetStereoMode() == SO3_MONO_STEREO)
1851 mStereoMgr->mIsCustomRatio =
false;
1853 else if (mStereoMgr->mIsCustomRatio)
1855 viewportData->mCamera->setAspectRatio(mStereoMgr->mRatio);
1858 Ogre::Real offset = (mIsLeftEye ? (-0.5f) : (0.5f)) * mStereoMgr->GetEyesSpacing();
1859 Ogre::Matrix4 baseMat = viewportData->mBaseCameraMatrix;
1860 Ogre::Real nearClip = viewportData->mCamera->getNearClipDistance();
1861 Ogre::Real farClip = viewportData->mCamera->getFarClipDistance();
1862 Ogre::Real focalLength = viewportData->mCamera->getFocalLength();
1865 Ogre::Radian fovY = viewportData->mCamera->getFOVy();
1866 Ogre::Real camspect = viewportData->mCamera->getAspectRatio();
1868 if (viewportData->mOldUseCustomMatrix && offset != 0.0f)
1870 fovY = 2.0 * atan(1.0 / viewportData->mOldMatrix[1][1]);
1871 camspect = viewportData->mOldMatrix[1][1] / viewportData->mOldMatrix[0][0];
1872 focalLength = viewportData->mOldMatrix[1][1];
1874 else if (mStereoMgr->mIsCustomProjection)
1876 Ogre::Matrix4 customMat = (mIsLeftEye) ? mStereoMgr->mLeftCustomProjection : mStereoMgr->mRightCustomProjection;
1877 fovY = 2.0 * atan(1.0 / customMat[1][1]);
1878 camspect = customMat[1][1] / customMat[0][0];
1879 focalLength = customMat[1][1];
1882 if (mStereoMgr->GetStereoMode() != SO3_MONO_STEREO)
1887 for (SWidgetManager::WidgetList::const_iterator wIt = wlist.begin(); wIt != wlist.end(); wIt++)
1889 SWidget* widget = (wIt->second);
1890 if (widget->GetVisible())
1892 if ((widget->GetIsOverlayed() || widget->GetIs2DRect()))
1894 widget->SetCurrentViewport((mIsLeftEye) ? viewportData->mLastLeftViewport : viewportData->mLastRightViewport);
1896 Ogre::Real dist = (widget->GetZOrder() == 0) ? farClip : nearClip + 10.0f - (10.0f * ((float)widget->GetZOrder() / 1000.0f));
1897 Ogre::Real hv = 2.0f * tanf (fovY.valueRadians() * 0.5f) * dist;
1898 Ogre::Real wv = hv * ((float)texwidth / (
float)texheight);
1899 Ogre::Real woffset = (offset * wv * focalLength) / dist;
1901 widget->SetLeftOffset((viewportData->mFlipEyes) ? woffset : -woffset);
1904 widget->SetCurrentViewport(viewportData->mOriginalViewport);
1907 widget->SetStereoEye(mIsLeftEye);
1912 if (viewportData->mOldUseCustomMatrix && offset != 0.0f)
1914 Ogre::Matrix4 proj = Ogre::Matrix4::IDENTITY;
1915 proj.setTrans(Ogre::Vector3(-offset, 0, 0));
1916 viewportData->mCamera->setCustomProjectionMatrix(
true, proj * viewportData->mOldMatrix);
1918 else if (mStereoMgr->mIsCustomProjection)
1920 Ogre::Matrix4 customMat = (mIsLeftEye) ? mStereoMgr->mLeftCustomProjection : mStereoMgr->mRightCustomProjection;
1923 viewportData->mCamera->setCustomProjectionMatrix(
true, customMat);
1925 else if (mStereoMgr->mIsCustomOffset)
1927 Ogre::Matrix4 proj = Ogre::Matrix4::IDENTITY;
1928 proj.setTrans(Ogre::Vector3(-mStereoMgr->mCenterOffset * (mIsLeftEye ? (-1) : 1), 0, 0));
1929 viewportData->mCamera->setCustomProjectionMatrix(
true, proj * baseMat);
1934 else if (offset != 0.0f)
1935 viewportData->mCamera->setFrustumOffset(viewportData->mOldOffset + Ogre::Vector2(offset, 0));
1938 if((mStereoMgr->mStereoMode != SO3_MONO_STEREO) && (!mStereoMgr->mAvailableModes[mStereoMgr->mStereoMode].empty() || (mStereoMgr->mStereoMode == SO3_OCULUS2_STEREO)))
1941 viewportData->mCamera->getParentSceneNode()->setPosition(viewportData->mOldPos + (offset * (viewportData->mCamera->getParentSceneNode()->getOrientation() * Ogre::Vector3::UNIT_X)));
1943 viewportData->mCamera->resetFrustumExtents();
1946void StereoManager::StereoCameraListener::postRenderTargetUpdate(
const Ogre::RenderTargetEvent& evt)
1949 if (!viewportData->mCamera)
1952 Ogre::Matrix4 baseMat = viewportData->mBaseCameraMatrix;
1953 Ogre::Real offset = 0.0f;
1954 if(!mStereoMgr->mIsCustomProjection)
1956 if (mStereoMgr->mIsCustomOffset)
1958 Ogre::Matrix4 proj = Ogre::Matrix4::IDENTITY;
1959 proj.setTrans(Ogre::Vector3(0.0, 0.0f, 0.0f));
1960 viewportData->mCamera->setCustomProjectionMatrix(
true, proj * baseMat);
1962 else if (offset != 0.0f)
1964 viewportData->mCamera->setFrustumOffset(viewportData->mOldOffset + Ogre::Vector2(offset, 0));
1968 viewportData->mCamera->setCustomProjectionMatrix(
true, mStereoMgr->mLeftCustomProjection);
1971 if((mStereoMgr->mStereoMode != SO3_MONO_STEREO) && (!mStereoMgr->mAvailableModes[mStereoMgr->mStereoMode].empty() || (mStereoMgr->mStereoMode == SO3_OCULUS2_STEREO) || (mStereoMgr->mStereoMode == SO3_OCULUS_STEREO)))
1974 viewportData->mCamera->getParentSceneNode()->setPosition(viewportData->mOldPos);
1979StereoManager::StereoViewportListener::StereoViewportListener(StereoManager* stereoMgr, StereoManager::StereoViewportData* stereoViewportData) : mStereoMgr(stereoMgr),
1980 viewportData(stereoViewportData)
1984void StereoManager::StereoViewportListener::preRenderTargetUpdate(
const Ogre::RenderTargetEvent& evt)
1991 if (viewportData->mLeftRenderTexture)
1993 viewportData->mLeftRenderTexture->getBuffer()->getRenderTarget()->update();
1996 if (viewportData->mRightRenderTexture)
1998 viewportData->mRightRenderTexture->getBuffer()->getRenderTarget()->update();
2001 catch(Ogre::Exception&)
2008 if ((mStereoMgr->mStereoMode == SO3_OCULUS2_STEREO) || (mStereoMgr->mStereoMode == SO3_OCULUS_STEREO))
2012 Ogre::MaterialPtr leftmat =
static_cast<Ogre::MaterialPtr
>(Ogre::MaterialManager::getSingleton().getByName(mStereoMgr->mAvailableModes[mStereoMgr->mStereoMode] +
"/Left", SO3_INTERNAL_RESOURCE_GROUP));
2015 Ogre::GpuProgramParametersSharedPtr leftShaderParams = leftmat->getTechnique(0)->getPass(0)->getVertexProgramParameters();
2016 leftShaderParams->setNamedConstant(
"eyeToSourceUVScale", mStereoMgr->mLeftUVScale);
2017 leftShaderParams->setNamedConstant(
"eyeToSourceUVOffset", mStereoMgr->mLeftUVOffset);
2018 leftShaderParams->setNamedConstant(
"eyeRotationStart", mStereoMgr->mLeftStartWarpMatrix);
2019 leftShaderParams->setNamedConstant(
"eyeRotationEnd", mStereoMgr->mLeftEndWarpMatrix);
2022 Ogre::MaterialPtr rightmat =
static_cast<Ogre::MaterialPtr
>(Ogre::MaterialManager::getSingleton().getByName(mStereoMgr->mAvailableModes[mStereoMgr->mStereoMode] +
"/Right", SO3_INTERNAL_RESOURCE_GROUP));
2025 Ogre::GpuProgramParametersSharedPtr rightShaderParams = rightmat->getTechnique(0)->getPass(0)->getVertexProgramParameters();
2026 rightShaderParams->setNamedConstant(
"eyeToSourceUVScale", mStereoMgr->mRightUVScale);
2027 rightShaderParams->setNamedConstant(
"eyeToSourceUVOffset", mStereoMgr->mRightUVOffset);
2028 rightShaderParams->setNamedConstant(
"eyeRotationStart", mStereoMgr->mRightStartWarpMatrix);
2029 rightShaderParams->setNamedConstant(
"eyeRotationEnd", mStereoMgr->mRightEndWarpMatrix);
2032 catch(Ogre::Exception&)
2038void StereoManager::StereoViewportListener::postRenderTargetUpdate(
const Ogre::RenderTargetEvent& evt)
2060StereoManager::StereoFrameListener::StereoFrameListener(StereoManager* stereoMgr, StereoManager::StereoViewportData* stereoViewportData) : mStereoMgr(stereoMgr),
2061 viewportData(stereoViewportData)
2065bool StereoManager::StereoFrameListener::frameStarted(
const Ogre::FrameEvent& evt)
2067 viewportData->frameTime = evt.timeSinceLastFrame;
2072bool StereoManager::StereoFrameListener::frameRenderingQueued(
const Ogre::FrameEvent& evt)
2077bool StereoManager::StereoFrameListener::frameEnded(
const Ogre::FrameEvent& evt)
2084StereoManager::StereoRTTListener::StereoRTTListener(StereoManager* stereoMgr, StereoManager::StereoViewportData* stereoViewportData) : mStereoMgr(stereoMgr),
2085 viewportData(stereoViewportData)
2089void StereoManager::StereoRTTListener::unloadingComplete(Ogre::Resource* resource)
2091 mStereoMgr->ReleaseRttTarget(viewportData);
2094void StereoManager::StereoRTTListener::loadingComplete(Ogre::Resource* resource)
2096 mStereoMgr->ReleaseRttTarget(viewportData);
2097 mStereoMgr->ConstructRttTarget(viewportData);
int getBufferStereoBeforeUpdateEvent(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE param)
int getBufferStereoUpdatedEvent(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE param)
int getBufferStereoUpdateParametersEvent(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE param)
int getBufferStereoUpdatedEvent(struct Mmachine *, SCOL_PTR_TYPE, SCOL_PTR_TYPE)
int getBufferStereoUpdateParametersEvent(struct Mmachine *, SCOL_PTR_TYPE, SCOL_PTR_TYPE)
int getBufferStereoBeforeUpdateEvent(struct Mmachine *, SCOL_PTR_TYPE, SCOL_PTR_TYPE)
void UnregisterCamera(Ogre::Camera *cam)
bool IsRegisteredViewport(SViewPort *targetViewport)
SScene * GetScene(const std::string &sceneName)
RenderSystem GetRenderSystem()
Ogre::RenderSystem * GetOgreRenderSystem()
static SRoot & getSingleton()
static SRoot * getSingletonPtr()
SEnvironment * GetEnvironment() const
void RemoveViewport(Ogre::Viewport *viewport)
void AddViewport(Ogre::Viewport *viewport)
Ogre::Viewport * GetOgreViewPortPointer()
void AddViewport(Ogre::Viewport *viewport)
void RemoveViewport(Ogre::Viewport *viewport)
Ogre::RenderWindow * GetOgreRenderWindowPointer()
void SetStereoMeshLeft(std::vector< Ogre::Vector3 > vertices, std::vector< std::vector< Ogre::Vector2 > > uvs, std::vector< Ogre::Real > vignetteColor, std::vector< Ogre::Real > warp, std::vector< int > indexs)
StereoMode GetStereoMode() const
void SetStereoFOVy(Ogre::Real fov)
std::array< SCOL_PTR_TYPE, 2 > GetStereoTextures()
void SetCustomProjectonMatrices(bool enable, const Ogre::Matrix4 &leftMatrix, const Ogre::Matrix4 &rightMatrix)
bool UpdateStereoCompositorState(Ogre::Viewport *ogreViewportInstance, const Ogre::String compname, bool state)
void SetStereoProjectionOffset(Ogre::Real offset)
friend class StereoCameraListener
void SetStereoDistortion(bool enable, Ogre::Vector4 dist)
void GetCustomProjectonMatrices(bool &enabled, Ogre::Matrix4 &leftMatrix, Ogre::Matrix4 &rightMatrix) const
Ogre::Real GetStereoAspectRatio()
void SetStereoMeshRttSize(SO3::SPoint< int > leftsize, SO3::SPoint< int > rightsize)
bool IsViewportRegistered(Ogre::Viewport *ogreViewportInstance)
void SetStereoChromaticAbCorrection(bool enable, Ogre::Vector4 chromAbC)
void SetStereoMode(const StereoMode &newStereoMode)
void SetStereoWindow(SO3::SPoint< int > pos, SO3::SPoint< int > size, int index, bool state)
Ogre::Real GetEyesSpacing() const
void UnregisterViewport(SViewPort *viewportInstance)
void ConstructRttTarget(StereoViewportData *viewportData)
void SetTimeWarpMatrix(const Ogre::Matrix4 &leftStartMatrix, const Ogre::Matrix4 &leftEndMatrix, const Ogre::Matrix4 &rightStartMatrix, const Ogre::Matrix4 &rightEndMatrix)
std::array< SO3::SPoint< int >, 2 > GetStereoViewportSize()
void ReleaseRttTarget(StereoViewportData *viewportData)
void SetStereoMeshRight(std::vector< Ogre::Vector3 > vertices, std::vector< std::vector< Ogre::Vector2 > > uvs, std::vector< Ogre::Real > vignetteColor, std::vector< Ogre::Real > warp, std::vector< int > indexs)
@ SO3_OPENVR_STEREO
OpenVR.
@ SO3_CARDBOARD_STEREO
Google card board.
@ SO3_QUAD_BUFFER_STEREO
OpenGL QuadBuffer.
@ SO3_OCULUSM_STEREO
OculusM.
@ SO3_INTERLACED_V_STEREO
Verticaly interlaced mode.
@ SO3_SIDE_BY_SIDE_STEREO
Side by side image.
@ SO3_ANAGLYPH_YB_STEREO
Anaglyph yellow/blue.
@ SO3_OPENXR_STEREO
OculusM.
@ SO3_ANAGLYPH_RC_STEREO
Anaglyph red/cyan.
@ SO3_INTERLACED_CB_STEREO
Interlaced mode with a checkerboard pattern.
@ SO3_OCULUS2_STEREO
Oculus rift DK2.
@ SO3_OCULUS_STEREO
Oculus rift.
@ SO3_INTERLACED_H_STEREO
Horizontaly interlaced mode.
@ SO3_UP_DOWN_STEREO
Up/down image.
friend class DeviceLostListener
StereoManager(SWindow *buffer)
void SetStereoMeshUVConfig(Ogre::Vector2 leftuvscale, Ogre::Vector2 leftuvoffset, Ogre::Vector2 rightuvscale, Ogre::Vector2 rightuvoffset)
void UpdateStereoCamera(Ogre::Viewport *ogreViewportInstance, Ogre::Camera *camera)
void RestoreStereoCamera(Ogre::Viewport *ogreViewportInstance)
void RegisterViewport(SViewPort *viewportInstance)
void SynchViewportSetup(SViewPort *viewportInstance, bool overlay=false, bool shadow=false)
void SetEyesSpacing(Ogre::Real l)
void SetStereoAspectRatio(Ogre::Real ratio)
void UpdateStereoCameraMatrix(Ogre::Viewport *ogreViewportInstance, Ogre::Camera *camera)
void SetRotateView(bool state)