Ignore:
Timestamp:
Aug 13, 2012, 8:36:29 PM (12 years ago)
Author:
Torben Dannhauer
Message:

New Distortion setup strategy added: distortionNG can now handle configuration files of "Projection Designer"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/DistortionSetupStrategyProjectionDesigner.cpp

    r406 r410  
    2424#include "DistortionSetupStrategyProjectionDesigner.h"
    2525
     26CameraConfigParser::CameraConfigParser()
     27{
     28        configParsed = false;
     29        frustumValues.clear();
     30        rotationValues.clear();
     31        rotationValues.resize(3,0);
     32        translationValues.clear();
     33        translationValues.resize(3,0);
     34}
     35
     36bool CameraConfigParser::parseConfigFile(const char * filename)
     37{
     38        if (! osgDB::fileExists(filename) )
     39        {
     40                OSG_NOTIFY( osg::FATAL ) << "ERROR: File '" << filename << "' does not exist." << std::endl;
     41                return false;
     42        }
     43
     44        frustumValues.clear();
     45        rotationValues.clear();
     46        rotationValues.resize(3,0);
     47        translationValues.clear();
     48        translationValues.resize(3,0);
     49
     50        std::string line;
     51        std::ifstream configFile (filename);
     52
     53        if (configFile.is_open())
     54        {
     55                while (! configFile.eof() )
     56                {
     57                        getline (configFile,line);
     58
     59                        // Auf Frustum untersuchen
     60                        if( line.find("Frustum") != std::string::npos )
     61                        {
     62                                extractFrustum(line);
     63                        }
     64
     65                        // Auf Rotation untersuchen
     66                        if( line.find("Rotate") != std::string::npos )
     67                        {
     68                                extractRotation(line);
     69                        }
     70
     71                        // Auf Translation untersuchens
     72                        if( line.find("Translate") != std::string::npos )
     73                        {
     74                                extractTranslation(line);
     75                        }
     76                }
     77                configFile.close();
     78                configParsed = true;
     79                return true;
     80        }
     81        else
     82        {
     83                OSG_NOTIFY( osg::FATAL ) << "Unable to open file:" << filename << std::endl;
     84                frustumValues.clear();
     85                configParsed = false;
     86                return false;
     87        }
     88}
     89
     90bool CameraConfigParser::extractFrustum(std::string data_)
     91{
     92        data_.replace(0,16,"");         // Delete leading spaces and "Frustum"
     93        data_.replace(data_.length()-1,1,"");   // Delete trailing ";"
     94        for( int i=0; i<6; i++)
     95        {
     96                size_t pos = data_.find(" ");    // Position of first value in string
     97                if( pos == -1) pos = data_.length();
     98                frustumValues.push_back( atof( data_.substr(0, pos).data() ) );
     99                data_.replace(0,pos+1,"");
     100        }
     101        return true;
     102}
     103
     104bool CameraConfigParser::extractRotation(std::string data_)
     105{
     106        data_.replace(0,15,"");         // Delete leading spaces and "Rotate"
     107        data_.replace(data_.length()-1,1,"");   // Delete trailing ";"
     108
     109        size_t valuePos = data_.find(" ");    // First Value is RotationValue
     110        std::string axis = data_.substr( valuePos+1);
     111        switch(axis.find("1"))
     112        {
     113                case 0: rotationValues[0] = atof( data_.substr(0, valuePos).data() ); // X-Axis
     114                        break;
     115                case 2: rotationValues[1] = atof( data_.substr(0, valuePos).data() ); // Y-Axis
     116                        break;
     117                case 4: rotationValues[2] = atof( data_.substr(0, valuePos).data() ); // Z-Axis
     118                        break;
     119        };
     120        return true;
     121}
     122
     123bool CameraConfigParser::extractTranslation(std::string data_)
     124{
     125        data_.replace(0,18,"");         // Delete leading spaces and "Translate"
     126        data_.replace(data_.length()-1,1,"");   // Delete trailing ";"
     127        for( unsigned int i=0;i<translationValues.size(); i++)
     128        {
     129                size_t valuePos = data_.find(" ");
     130                std::string value = data_.substr(0, valuePos);
     131                data_.replace(0,valuePos+1,"");
     132                translationValues[i] = atof( value.data() );
     133        }
     134        return true;
     135}
     136
    26137DistortionSetupStrategyProjectionDesigner::DistortionSetupStrategyProjectionDesigner()
    27138{
     139        _blendmapFilename = "";
     140        _frustumFilename = "";
     141        _distortionFilename = "";
     142        distortMapTexture = NULL;
    28143        _distortionInitialized=false;
     144        parser = new CameraConfigParser();
    29145}
    30146
     
    34150}
    35151
     152osg::Texture* DistortionSetupStrategyProjectionDesigner::loadTexture( const std::string& fileName )
     153{
     154        std::string foundFileName = osgDB::findDataFile(fileName);
     155        if (foundFileName.length() != 0 )
     156        {
     157                // load distortion map texture file
     158                osg::Image* image = osgDB::readImageFile(foundFileName);
     159                if (image)
     160                {
     161                        osg::Texture2D* texture = new osg::Texture2D;
     162                        texture->setImage(image);
     163                        texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
     164                        texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
     165                        texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE);
     166                        texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE);
     167                        return texture;
     168                }
     169        }
     170
     171        OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl;
     172
     173        return NULL;
     174}
     175
     176void DistortionSetupStrategyProjectionDesigner::setDistortionInputFiles(std::string distortionFile, std::string blendmapFile, std::string frustumFile)
     177{
     178        _blendmapFilename = blendmapFile;
     179        _frustumFilename = frustumFile;
     180        _distortionFilename = distortionFile;
     181}
     182
    36183void DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet)
    37184{
    38        
    39 }
     185        if(distortionSet == NULL)
     186        {
     187                OSG_ALWAYS<<"DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup : Invalid DistortionSet"<<std::endl;
     188                return;
     189        }
     190
     191        if(      _blendmapFilename.empty() || _frustumFilename.empty() ||  _distortionFilename.empty() )
     192        {
     193                OSG_ALWAYS<<"DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup : You have not specified the imput filenames correctly!"<<std::endl;
     194                return;
     195        }
     196
     197        if(     !osgDB::fileExists(_blendmapFilename) || !osgDB::fileExists(_frustumFilename) || !osgDB::fileExists(_distortionFilename) )
     198        {
     199                OSG_ALWAYS<<"DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup : One of the specified input files does not exist!"<<std::endl;
     200                return;
     201        }
     202
     203        if (_distortionInitialized==false )
     204        {
     205                _distortionInitialized=true;
     206                distortionSet->dirtyMesh();
     207                distortionSet->dirtyMatrix();
     208
     209                // ******************** //
     210                // *** IntensityMap *** //
     211                // ******************** //
     212
     213                osg::Image* intensityMap=NULL;
     214                intensityMap=osgDB::readImageFile(_blendmapFilename);
     215                osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
     216                if (!wsi)
     217                {
     218                        OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
     219                        return;
     220                }
     221
     222                osg::GraphicsContext::ScreenIdentifier si;
     223                si.readDISPLAY();
     224
     225                if (si.displayNum<0) si.displayNum = 0;
     226
     227                si.screenNum = 0; //Fixed value, may be replaced later
     228
     229                unsigned int width=0, height=0;
     230                wsi->getScreenResolution(si, width, height);
     231
     232                if(intensityMap->s()!=width || intensityMap->t()!=height)
     233                        intensityMap->scaleImage(width, height, intensityMap->r());
     234
     235                // Copy image data and flag as dirty to update texture.
     236                osg::copyImage(intensityMap, 0, 0, 0, intensityMap->s(), intensityMap->t(), intensityMap->r(), distortionSet->getIntensityMap(), 0, 0, 0, true);
     237                distortionSet->getIntensityMap()->dirty();
     238
     239                // ******************************* //
     240                // *** Frustum and View Offset *** //
     241                // ******************************* //
     242                parser->parseConfigFile((_frustumFilename).data());
     243                if( parser->isConfigParsed())
     244                {
     245                        osg::Matrixd viewOffset=osg::Matrix::identity();
     246                        osg::Matrixd projectionOffset=osg::Matrixd();
     247
     248                        osg::Matrixd rotViewOffset=osg::Matrixd();
     249
     250                        rotViewOffset.makeRotate(
     251                                osg::DegreesToRadians((parser->getRotationDataset())[0]), osg::Vec3(0,1,0),     // heading
     252                                osg::DegreesToRadians(-(parser->getRotationDataset())[1]), osg::Vec3(1,0,0),    // pitch
     253                                osg::DegreesToRadians((parser->getRotationDataset())[2]), osg::Vec3(0,0,1));    // roll
     254
     255                        osg::Matrixd transViewOffset=osg::Matrixd();
     256                        transViewOffset.makeTranslate( (parser->getTranslationDataset())[0], (parser->getTranslationDataset())[1], (parser->getTranslationDataset())[2]);
     257
     258                        viewOffset = viewOffset * rotViewOffset; // * transViewOffset;
     259
     260                        //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar.
     261                        // Important: By definition of OpenGL, Left, Right, Bottom and Top must be set as tangens, multiplied by zNear!
     262                        projectionOffset.makeFrustum(
     263                                tanf((parser->getFrustumDataset())[0]) * (parser->getFrustumDataset())[4],
     264                                tanf((parser->getFrustumDataset())[1]) * (parser->getFrustumDataset())[4],
     265                                tanf((parser->getFrustumDataset())[2]) * (parser->getFrustumDataset())[4],
     266                                tanf((parser->getFrustumDataset())[3]) * (parser->getFrustumDataset())[4],
     267                                (parser->getFrustumDataset())[4],
     268                                (parser->getFrustumDataset())[5]);
     269
     270                        distortionSet->setViewOffset(viewOffset);
     271                        distortionSet->setProjectionOffset(projectionOffset);
     272                }
     273                else
     274                {
     275                        OSG_NOTIFY(osg::WARN) << "WARNING: Unable to parse Frustum values from '" << _frustumFilename << "' -- continue without valid frustum values." << std::endl;
     276                }
     277                // ******************************* //
     278                // ******* Distortion Mesh ******* //
     279                // ******************************* //
     280
     281                osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry();
     282                if(distortionMeshGeometry)
     283                {
     284
     285                        if ( osgDB::fileExists( _distortionFilename ) )
     286                        {
     287                                distortMapTexture = loadTexture(_distortionFilename);
     288                                osg::Vec4Array* distortionMesh = new osg::Vec4Array;
     289
     290                                osg::Vec3 origin(0.0f,0.0f,0.0f);
     291                                osg::Vec3 xAxis(1.0f,0.0f,0.0f);
     292                                osg::Vec3 yAxis(0.0f,1.0f,0.0f);
     293                                int noSteps = 128;
     294
     295                                osg::Vec3 bottom = origin;
     296                                osg::Vec3 dx = xAxis*(width/((float)(noSteps-1)));
     297                                osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
     298
     299                                osg::Vec2 bottom_texcoord(0.0f,0.0f);
     300                                osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f);
     301                                osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1));
     302
     303                                osg::Vec2 texcoord = bottom_texcoord;
     304                                int i,j;
     305
     306                                for(i=0;i<noSteps;++i)
     307                                {
     308                                        osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i;
     309                                        for(j=0;j<noSteps;++j)
     310                                        {
     311                                                if (distortMapTexture)
     312                                                {
     313                                                        osg::Vec2 imgcoord =    osg::Vec2((distortMapTexture->getImage(0)->s()-1) * texcoord.x(),
     314                                                                                (distortMapTexture->getImage(0)->t()-1) * texcoord.y());
     315                                                        unsigned char* pPixel = &distortMapTexture->getImage(0)->data()[(int)imgcoord.y()*distortMapTexture->getImage(0)->getRowSizeInBytes()+
     316                                                                                (int)imgcoord.x()*distortMapTexture->getImage(0)->getPixelSizeInBits()/8];
     317                                                        distortionMesh->push_back(osg::Vec4( texcoord.x(), texcoord.y(), ((float)pPixel[2] / 255.0f + (pPixel[0] % 16)) / 16.0f,
     318                                                                                1.0f-((float)pPixel[1] / 255.0f + (pPixel[0] / 16)) / 16.0f));
     319                                                }
     320                                                texcoord += dx_texcoord;
     321                                        }
     322                                }
     323
     324                                distortionSet->setDistortionMesh(distortionMesh);
     325                                distortionSet->setDistortionMeshDimensions(noSteps, noSteps);
     326                        }
     327                        else
     328                        {
     329                                OSG_NOTIFY(osg::WARN) << "WARNING: Unable to load Distortionmap '" << _distortionFilename << "' -- continue without distortion." << std::endl;
     330                        }
     331
     332                }
     333
     334        }       
     335}
Note: See TracChangeset for help on using the changeset viewer.