/* osgVisual test. distortionNG, experimental. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include "DistortionSetupStrategyProjectionDesigner.h" CameraConfigParser::CameraConfigParser() { configParsed = false; frustumValues.clear(); rotationValues.clear(); rotationValues.resize(3,0); translationValues.clear(); translationValues.resize(3,0); } bool CameraConfigParser::parseConfigFile(const char * filename) { if (! osgDB::fileExists(filename) ) { OSG_NOTIFY( osg::FATAL ) << "ERROR: File '" << filename << "' does not exist." << std::endl; return false; } frustumValues.clear(); rotationValues.clear(); rotationValues.resize(3,0); translationValues.clear(); translationValues.resize(3,0); std::string line; std::ifstream configFile (filename); if (configFile.is_open()) { while (! configFile.eof() ) { getline (configFile,line); // Auf Frustum untersuchen if( line.find("Frustum") != std::string::npos ) { extractFrustum(line); } // Auf Rotation untersuchen if( line.find("Rotate") != std::string::npos ) { extractRotation(line); } // Auf Translation untersuchens if( line.find("Translate") != std::string::npos ) { extractTranslation(line); } } configFile.close(); configParsed = true; return true; } else { OSG_NOTIFY( osg::FATAL ) << "Unable to open file:" << filename << std::endl; frustumValues.clear(); configParsed = false; return false; } } bool CameraConfigParser::extractFrustum(std::string data_) { data_.replace(0,16,""); // Delete leading spaces and "Frustum" data_.replace(data_.length()-1,1,""); // Delete trailing ";" for( int i=0; i<6; i++) { size_t pos = data_.find(" "); // Position of first value in string if( pos == -1) pos = data_.length(); frustumValues.push_back( atof( data_.substr(0, pos).data() ) ); data_.replace(0,pos+1,""); } return true; } bool CameraConfigParser::extractRotation(std::string data_) { data_.replace(0,15,""); // Delete leading spaces and "Rotate" data_.replace(data_.length()-1,1,""); // Delete trailing ";" size_t valuePos = data_.find(" "); // First Value is RotationValue std::string axis = data_.substr( valuePos+1); switch(axis.find("1")) { case 0: rotationValues[0] = atof( data_.substr(0, valuePos).data() ); // X-Axis break; case 2: rotationValues[1] = atof( data_.substr(0, valuePos).data() ); // Y-Axis break; case 4: rotationValues[2] = atof( data_.substr(0, valuePos).data() ); // Z-Axis break; }; return true; } bool CameraConfigParser::extractTranslation(std::string data_) { data_.replace(0,18,""); // Delete leading spaces and "Translate" data_.replace(data_.length()-1,1,""); // Delete trailing ";" for( unsigned int i=0;isetImage(image); texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE); texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE); return texture; } } OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl; return NULL; } void DistortionSetupStrategyProjectionDesigner::setDistortionInputFiles(std::string distortionFile, std::string blendmapFile, std::string frustumFile) { _blendmapFilename = blendmapFile; _frustumFilename = frustumFile; _distortionFilename = distortionFile; } void DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup(osgViewer::DistortionSet* distortionSet) { if(distortionSet == NULL) { OSG_ALWAYS<<"DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup : Invalid DistortionSet"<dirtyMesh(); distortionSet->dirtyMatrix(); // ******************** // // *** IntensityMap *** // // ******************** // osg::Image* intensityMap=NULL; intensityMap=osgDB::readImageFile(_blendmapFilename); osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); if (!wsi) { OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<getScreenResolution(si, width, height); if(intensityMap->s()!=width || intensityMap->t()!=height) intensityMap->scaleImage(width, height, intensityMap->r()); // Copy image data and flag as dirty to update texture. osg::copyImage(intensityMap, 0, 0, 0, intensityMap->s(), intensityMap->t(), intensityMap->r(), distortionSet->getIntensityMap(), 0, 0, 0, true); distortionSet->getIntensityMap()->dirty(); // ******************************* // // *** Frustum and View Offset *** // // ******************************* // parser->parseConfigFile((_frustumFilename).data()); if( parser->isConfigParsed()) { osg::Matrixd viewOffset=osg::Matrix::identity(); osg::Matrixd projectionOffset=osg::Matrixd(); osg::Matrixd rotViewOffset=osg::Matrixd(); rotViewOffset.makeRotate( osg::DegreesToRadians((parser->getRotationDataset())[0]), osg::Vec3(0,1,0), // heading osg::DegreesToRadians(-(parser->getRotationDataset())[1]), osg::Vec3(1,0,0), // pitch osg::DegreesToRadians((parser->getRotationDataset())[2]), osg::Vec3(0,0,1)); // roll osg::Matrixd transViewOffset=osg::Matrixd(); transViewOffset.makeTranslate( (parser->getTranslationDataset())[0], (parser->getTranslationDataset())[1], (parser->getTranslationDataset())[2]); viewOffset = viewOffset * rotViewOffset; // * transViewOffset; //Frustum Parameters: Left, Right, Bottom, Top, zNear, zFar. projectionOffset.makeFrustum( (parser->getFrustumDataset())[0], (parser->getFrustumDataset())[1], (parser->getFrustumDataset())[2], (parser->getFrustumDataset())[3], (parser->getFrustumDataset())[4], (parser->getFrustumDataset())[5]); distortionSet->setViewOffset(viewOffset); distortionSet->setProjectionOffset(projectionOffset); } else { OSG_NOTIFY(osg::WARN) << "WARNING: Unable to parse Frustum values from '" << _frustumFilename << "' -- continue without valid frustum values." << std::endl; } // ******************************* // // ******* Distortion Mesh ******* // // ******************************* // osg::Geometry* distortionMeshGeometry = distortionSet->getDistortionInternals()->getChild(osgViewer::DistortionSet::MESH)->asGeode()->getDrawable(0)->asGeometry(); if(distortionMeshGeometry) { if ( osgDB::fileExists( _distortionFilename ) ) { distortMapTexture = loadTexture(_distortionFilename); osg::Vec4Array* distortionMesh = new osg::Vec4Array; osg::Vec3 origin(0.0f,0.0f,0.0f); osg::Vec3 xAxis(1.0f,0.0f,0.0f); osg::Vec3 yAxis(0.0f,1.0f,0.0f); int noSteps = 128; osg::Vec3 bottom = origin; osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); osg::Vec2 bottom_texcoord(0.0f,0.0f); osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f); osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1)); osg::Vec2 texcoord = bottom_texcoord; int i,j; for(i=0;igetImage(0)->s()-1) * texcoord.x(), (distortMapTexture->getImage(0)->t()-1) * texcoord.y()); unsigned char* pPixel = &distortMapTexture->getImage(0)->data()[(int)imgcoord.y()*distortMapTexture->getImage(0)->getRowSizeInBytes()+ (int)imgcoord.x()*distortMapTexture->getImage(0)->getPixelSizeInBits()/8]; distortionMesh->push_back(osg::Vec4( texcoord.x(), texcoord.y(), ((float)pPixel[2] / 255.0f + (pPixel[0] % 16)) / 16.0f, 1.0f-((float)pPixel[1] / 255.0f + (pPixel[0] / 16)) / 16.0f)); } texcoord += dx_texcoord; } } distortionSet->setDistortionMesh(distortionMesh); distortionSet->setDistortionMeshDimensions(noSteps, noSteps); } else { OSG_NOTIFY(osg::WARN) << "WARNING: Unable to load Distortionmap '" << _distortionFilename << "' -- continue without distortion." << std::endl; } } } }