@ -9,6 +9,8 @@
# include <osgViewer/Viewer>
# include <iostream>
# include <map>
# include <string>
# include <components/debug/debuglog.hpp>
@ -225,11 +227,18 @@ namespace Misc
StereoView * stereoView ;
} ;
StereoView * sInstance = nullptr ;
StereoView & StereoView : : instance ( )
{
return * sInstance ;
}
StereoView : : StereoView ( osgViewer : : Viewer * viewer , Technique technique , osg : : Node : : NodeMask geometryShaderMask , osg : : Node : : NodeMask noShaderMask )
: osg : : Group ( )
, mViewer ( viewer )
, mMainCamera ( mViewer - > getCamera ( ) )
, mRoot ( viewer - > getSceneData ( ) - > asGroup ( ) )
, mRoot ( mV iewer- > getSceneData ( ) - > asGroup ( ) )
, mTechnique ( technique )
, mGeometryShaderMask ( geometryShaderMask )
, mNoShaderMask ( noShaderMask )
@ -241,19 +250,14 @@ namespace Misc
// Do nothing
return ;
mRoot - > setDataVariance ( osg : : Object : : STATIC ) ;
mMasterConfig - > _id = " STEREO " ;
mMasterConfig - > _master = true ;
mSlaveConfig - > _id = " STEREO " ;
mSlaveConfig - > _master = false ;
SceneUtil : : FindByNameVisitor findScene ( " Scene Root " ) ;
mRoot - > accept ( findScene ) ;
mScene = findScene . mFoundNode ;
if ( ! mScene )
throw std : : logic_error ( " Couldn't find scene root " ) ;
setName ( " Stereo Root " ) ;
mRoot - > setDataVariance ( osg : : Object : : STATIC ) ;
setDataVariance ( osg : : Object : : STATIC ) ;
mLeftCamera - > setReferenceFrame ( osg : : Transform : : ABSOLUTE_RF ) ;
mLeftCamera - > setProjectionResizePolicy ( osg : : Camera : : FIXED ) ;
@ -287,6 +291,10 @@ namespace Misc
{
setupBruteForceTechnique ( ) ;
}
if ( sInstance )
throw std : : logic_error ( " Double instance og StereoView " ) ;
sInstance = this ;
}
void StereoView : : setupBruteForceTechnique ( )
@ -313,8 +321,8 @@ namespace Misc
mRightCamera - > setViewport ( width / 2 , 0 , width / 2 , height ) ;
mViewer - > stopThreading ( ) ;
mViewer - > addSlave ( mLeftCamera , true ) ;
mViewer - > addSlave ( mRightCamera , true ) ;
mViewer - > addSlave ( mLeftCamera , true ) ;
mRightCamera - > setGraphicsContext ( mViewer - > getCamera ( ) - > getGraphicsContext ( ) ) ;
mLeftCamera - > setGraphicsContext ( mViewer - > getCamera ( ) - > getGraphicsContext ( ) ) ;
mViewer - > getCamera ( ) - > setGraphicsContext ( nullptr ) ;
@ -332,16 +340,14 @@ namespace Misc
mMainCamera - > setCullMask ( mGeometryShaderMask ) ;
addChild ( mStereoGeometryShaderRoot ) ;
mStereoGeometryShaderRoot - > addChild ( mRoot ) ;
addChild ( mStereoBruteForceRoot ) ;
mStereoBruteForceRoot - > addChild ( mLeftCamera ) ;
mLeftCamera - > addChild ( mScene ) ; // Use scene directly to avoid redundant shadow computation.
mStereoBruteForceRoot - > addChild ( mRightCamera ) ;
mRightCamera - > addChild ( mScene ) ;
addCullCallback ( new StereoStatesetUpdateCallback ( this ) ) ;
// Inject self as the root of the scene graph
mStereoGeometryShaderRoot - > addChild ( mRoot ) ;
mViewer - > setSceneData ( this ) ;
}
@ -494,6 +500,21 @@ namespace Misc
cb = cb_ ;
}
void StereoView : : initializeScene ( )
{
SceneUtil : : FindByNameVisitor findScene ( " Scene Root " ) ;
mRoot - > accept ( findScene ) ;
mScene = findScene . mFoundNode ;
if ( ! mScene )
throw std : : logic_error ( " Couldn't find scene root " ) ;
if ( mTechnique = = Technique : : GeometryShader_IndexedViewports )
{
mLeftCamera - > addChild ( mScene ) ; // Use scene directly to avoid redundant shadow computation.
mRightCamera - > addChild ( mScene ) ;
}
}
void disableStereoForCamera ( osg : : Camera * camera )
{
auto * viewport = camera - > getViewport ( ) ;
@ -553,4 +574,33 @@ namespace Misc
near = 1 ;
far = 6656 ;
}
void StereoView : : setInitialDrawCallback ( osg : : ref_ptr < osg : : Camera : : DrawCallback > cb )
{
mMainCamera - > setInitialDrawCallback ( cb ) ;
}
void StereoView : : setPredrawCallback ( osg : : ref_ptr < osg : : Camera : : DrawCallback > cb )
{
if ( mTechnique = = Technique : : GeometryShader_IndexedViewports )
{
mMainCamera - > setPreDrawCallback ( cb ) ;
}
else
{
mLeftCamera - > setPreDrawCallback ( cb ) ;
}
}
void StereoView : : setPostdrawCallback ( osg : : ref_ptr < osg : : Camera : : DrawCallback > cb )
{
if ( mTechnique = = Technique : : GeometryShader_IndexedViewports )
{
mMainCamera - > setPostDrawCallback ( cb ) ;
}
else
{
mRightCamera - > setPostDrawCallback ( cb ) ;
}
}
}