Index: experimental/distortionNG/DistortionSetupStrategyProjectionDesigner.cpp
===================================================================
--- experimental/distortionNG/DistortionSetupStrategyProjectionDesigner.cpp	(revision 406)
+++ experimental/distortionNG/DistortionSetupStrategyProjectionDesigner.cpp	(revision 410)
@@ -24,7 +24,123 @@
 #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;i<translationValues.size(); i++)
+	{
+		size_t valuePos = data_.find(" ");
+		std::string value = data_.substr(0, valuePos);
+		data_.replace(0,valuePos+1,"");
+		translationValues[i] = atof( value.data() );
+	}
+	return true;
+}
+
 DistortionSetupStrategyProjectionDesigner::DistortionSetupStrategyProjectionDesigner()
 {
+	_blendmapFilename = "";
+	_frustumFilename = "";
+	_distortionFilename = "";
+	distortMapTexture = NULL;
 	_distortionInitialized=false;
+	parser = new CameraConfigParser();
 }
 
@@ -34,6 +150,186 @@
 }
 
+osg::Texture* DistortionSetupStrategyProjectionDesigner::loadTexture( const std::string& fileName )
+{
+	std::string foundFileName = osgDB::findDataFile(fileName);
+	if (foundFileName.length() != 0 )
+	{
+		// load distortion map texture file
+		osg::Image* image = osgDB::readImageFile(foundFileName);
+		if (image)
+		{
+			osg::Texture2D* texture = new osg::Texture2D;
+			texture->setImage(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"<<std::endl;
+		return;
+	}
+
+	if(	 _blendmapFilename.empty() || _frustumFilename.empty() ||  _distortionFilename.empty() )
+	{
+		OSG_ALWAYS<<"DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup : You have not specified the imput filenames correctly!"<<std::endl;
+		return;
+	}
+
+	if(	!osgDB::fileExists(_blendmapFilename) || !osgDB::fileExists(_frustumFilename) || !osgDB::fileExists(_distortionFilename) )
+	{
+		OSG_ALWAYS<<"DistortionSetupStrategyProjectionDesigner::delegateDistortionSetup : One of the specified input files does not exist!"<<std::endl;
+		return;
+	}
+
+	if (_distortionInitialized==false )
+	{
+		_distortionInitialized=true;
+		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."<<std::endl;
+			return;
+	    	}
+
+		osg::GraphicsContext::ScreenIdentifier si;
+	    	si.readDISPLAY();
+
+	   	if (si.displayNum<0) si.displayNum = 0;
+
+	    	si.screenNum = 0; //Fixed value, may be replaced later
+
+	   	unsigned int width=0, height=0;
+	    	wsi->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.
+			// Important: By definition of OpenGL, Left, Right, Bottom and Top must be set as tangens, multiplied by zNear!
+			projectionOffset.makeFrustum(
+				tanf((parser->getFrustumDataset())[0]) * (parser->getFrustumDataset())[4],
+				tanf((parser->getFrustumDataset())[1]) * (parser->getFrustumDataset())[4],
+				tanf((parser->getFrustumDataset())[2]) * (parser->getFrustumDataset())[4],
+				tanf((parser->getFrustumDataset())[3]) * (parser->getFrustumDataset())[4],
+				(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;i<noSteps;++i)
+				{
+					osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i;
+					for(j=0;j<noSteps;++j)
+					{
+						if (distortMapTexture)
+						{
+							osg::Vec2 imgcoord =	osg::Vec2((distortMapTexture->getImage(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;
+			}
+
+		}
+
+	}	
+}
