Ignore:
Timestamp:
Apr 21, 2012, 4:53:52 PM (12 years ago)
Author:
Torben Dannhauer
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/extViewer.cpp

    r349 r350  
    5757}
    5858
    59 //static osg::Texture2D* loadTexture( const std::string& fileName )
    60 //{
    61 //    std::string foundFileName = osgDB::findDataFile(fileName);
    62 //    if (foundFileName.length() != 0 )
    63 //    {
    64 //        // load distortion map texture file
    65 //        osg::Image* image = osgDB::readImageFile(foundFileName);
    66 //        if (image)
    67 //        {
    68 //                      osg::Texture2D* texture = new osg::Texture2D;
    69 //            texture->setImage(image);
    70 //                      texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
    71 //                      texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
    72 //                      texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
    73 //                      texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
    74 //            return texture;
    75 //        }
    76 //    }
    77 //
    78 //    OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl;
    79 //
    80 //    return NULL;
    81 //}
    82 
    83 //static osg::TextureRectangle* loadTextureRect( const std::string& fileName )
    84 //{
    85 //    std::string foundFileName = osgDB::findDataFile(fileName);
    86 //    if (foundFileName.length() != 0 )
    87 //    {
    88 //        // load distortion map texture file
    89 //        osg::Image* image = osgDB::readImageFile(foundFileName);
    90 //        if (image)
    91 //        {
    92 //                      osg::TextureRectangle* texture = new osg::TextureRectangle;
    93 //            texture->setImage(image);
    94 //            texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
    95 //            texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
    96 //            texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE);
    97 //            texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE);
    98 //            return texture;
    99 //        }
    100 //    }
    101 //
    102 //    OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl;
    103 //
    104 //    return NULL;
    105 //}
    106 
    10759static osg::Geometry* createMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, unsigned int columns, unsigned int rows, osg::Image* intensityMap, bool applyIntensityMapAsColours, const osg::Matrix& projectorMatrix)
    10860{
     
    199151}
    200152
     153
    201154void extViewer::setUpViewForManualDistortion(unsigned int screenNum, osg::Image* intensityMap, const osg::Matrixd& projectorMatrix)
    202155{
    203         OSG_INFO<<"View::setUpViewForManualDistortion(sn="<<screenNum<<", im="<<intensityMap<<")"<<std::endl;
     156        OSG_INFO<<"View::setUpViewForManualDistortion(sn="<<screenNum<<")"<<std::endl;
     157
     158        // Create DistortionSet
     159        _distortionSet = new osgViewer::DistortionSet();
     160        _distortionSet->setIntensityMap( intensityMap );
     161        _distortionSet->setDistortionMeshRows( 20 );
     162        _distortionSet->setDistortionMeshColumns( 20 );
     163
     164
     165
    204166
    205167    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
     
    233195    traits->sharedContext = 0;
    234196
    235         bool applyIntensityMapAsColours = false;
    236 
    237197        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    238198    if (!gc)
     
    302262        // StateSet to contain the Texture StateAttribute.
    303263            osg::StateSet* stateset = geode->getOrCreateStateSet();
    304         stateset->setTextureAttributeAndModes(0, sceneTexture,osg::StateAttribute::ON);
     264        stateset->setTextureAttributeAndModes(_distortionSet->getTexUnitScene(), sceneTexture,osg::StateAttribute::ON);
    305265        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
    306266
     
    309269        stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
    310270
    311                 // If the intensityMap is used but not applyIntensityMapAsColours: Apply intensityMap as intensityMapTexture on unit 1
    312                 if (!applyIntensityMapAsColours && intensityMap)
    313                         setUpIntensityMapBlending(stateset, intensityMap, screenNum, 0, 1);     
    314 
    315                 osg::Geometry* distortionMesh = createMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), 20, 20, intensityMap, applyIntensityMapAsColours, projectorMatrix);
     271                setUpIntensityMapBlending(_distortionSet, stateset, screenNum);
     272
     273                osg::Geometry* distortionMesh = createMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), 20, 20, _distortionSet->getIntensityMap(), false, projectorMatrix);
    316274                geode->addDrawable(distortionMesh);
    317275
     
    347305                camera->setCullingMode( mode & (~osg::CullSettings::SMALL_FEATURE_CULLING) );
    348306
    349                 // Create DistortionSet
    350                 _distortionSet = new osgViewer::DistortionSet();
    351                 _distortionSet->setIntensityMap( intensityMap );
    352 
    353 
    354307                // Add the distortionHandler
    355308                osgViewer::DistortionManipulator* distManip = new osgViewer::DistortionManipulator();
     
    364317}
    365318
    366 void extViewer::setUpIntensityMapBlending(osg::StateSet* stateset, osg::Image* intensityMap, unsigned int screenNum, int rttSceneTextureUnit, int intensityMapTextureUnit)
    367 {
    368         if(stateset == NULL || intensityMap == NULL)
     319void extViewer::setUpIntensityMapBlending(osgViewer::DistortionSet* distSet, osg::StateSet* stateset, unsigned int screenNum)
     320{
     321        osg::Image* intensityMap = _distortionSet->getIntensityMap();
     322        if(intensityMap == NULL)        return;
     323
     324        if(stateset == NULL)
    369325        {
    370326                OSG_NOTICE<<"Error, no intensityMap or stateset for intensityMapBlending specified."<<std::endl;
     
    400356        intensityMapTexture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
    401357
    402     stateset->setTextureAttributeAndModes(intensityMapTextureUnit, intensityMapTexture, osg::StateAttribute::ON);
     358    stateset->setTextureAttributeAndModes(_distortionSet->getTexUnitIntensityMap(), intensityMapTexture, osg::StateAttribute::ON);
    403359
    404360        // create shaders for blending
     
    411367        {
    412368                distortProgram->addShader( fShader );
    413                 stateset->addUniform( new osg::Uniform("sceneTexture", rttSceneTextureUnit) );
    414                 stateset->addUniform( new osg::Uniform("intensityMapTexture", intensityMapTextureUnit) );
     369                //stateset->addUniform( new osg::Uniform("sceneTexture", _distortionSet->getTexUnitScene()) );
     370                //stateset->addUniform( new osg::Uniform("intensityMapTexture", _distortionSet->getTexUnitIntensityMap()) );
     371                stateset->addUniform( new osg::Uniform("sceneTexture", 0 ) );
     372                stateset->addUniform( new osg::Uniform("intensityMapTexture", 1 ) );
    415373                stateset->setAttributeAndModes(distortProgram, osg::StateAttribute::ON);
    416374        }
    417375}
     376
     377void extViewer::setUpIntensityMapBlending(std::string intensityMap)
     378{
     379        // Try to load intensityMap
     380        osg::Image* intMap = osgDB::readImageFile(intensityMap);
     381        if (!intMap)
     382        {
     383                osg::notify(osg::WARN) << "Quitting, couldn't find intensity map: " << intensityMap << std::endl;
     384                return;
     385        }
     386
     387        // Create DistortionSet
     388        _distortionSet = new osgViewer::DistortionSet();
     389        _distortionSet->setIntensityMap( intMap );
     390        _distortionSet->setDistortionMeshRows( 1 );
     391        _distortionSet->setDistortionMeshColumns( 1 );
     392        _distortionSet->setTexUnitScene( 0 );
     393        _distortionSet->setTexUnitIntensityMap( 1 );
     394
     395        // Todo: call setupDistortion/Blendung to perform the blending
     396}
Note: See TracChangeset for help on using the changeset viewer.