Index: experimental/distortionNG/distortionNG.cpp
===================================================================
--- experimental/distortionNG/distortionNG.cpp	(revision 311)
+++ experimental/distortionNG/distortionNG.cpp	(revision 313)
@@ -1,13 +1,8 @@
 #include "distortionNG.h"
 
-#include<osg/Texture2D>
 #include<osg/Point>
-#include<osg/ComputeBoundsVisitor>
+
 
 #include<osgViewer/Viewer>
-
-#include<osgDB/ReadFile>
-
-#include <osgUtil/SmoothingVisitor>
 
 
@@ -21,53 +16,5 @@
 }
 
-osg::Geometry* distortionNG::createMesh( unsigned int column, unsigned int row )
-{
-	osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(column * row);
-	osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(column * row);
-	for ( unsigned int i=0; i<row; ++i )
-	{
-		for ( unsigned int j=0; j<column; ++j )
-		{
-			(*vertices)[i*column + j].set( (float)i, (float)j, 0.0f );
-			(*texcoords)[i*column + j].set( (float)i/(float)row, (float)j/(float)column );
-		}
-	}
 
-	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
-	geom->setUseDisplayList( false );
-	geom->setUseVertexBufferObjects( true );
-	geom->setVertexArray( vertices.get() );
-	geom->setTexCoordArray( 0, texcoords.get() );
-	for ( unsigned int i=0; i<row-1; ++i )
-	{
-		osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2);
-		for ( unsigned int j=0; j<column; ++j )
-		{
-			(*de)[j*2 + 0] = i*column + j;
-			(*de)[j*2 + 1] = (i+1)*column + j;
-		}
-		geom->addPrimitiveSet( de.get() );
-	}
-
-
-
-	osg::ComputeBoundsVisitor cbv;
-	cbv.applyDrawable(geom);
-	osg::BoundingBox box = cbv.getBoundingBox();
-	if (!box.valid()) 
-		std::cout << "Invalid bounding box!"; 
-	geom->setInitialBound(box);
-
-	osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
-	texture->setImage( osgDB::readImageFile("Images/osg256.png") );
-	texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
-	texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
-	geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );
-
-	// Create normals
-	osgUtil::SmoothingVisitor::smooth( *geom );
-
-	return geom.release();
-}
 
 osg::Geode* distortionHandler::createVertexHighlighter()
Index: experimental/distortionNG/distortionNG.h
===================================================================
--- experimental/distortionNG/distortionNG.h	(revision 311)
+++ experimental/distortionNG/distortionNG.h	(revision 313)
@@ -38,5 +38,3 @@
 	distortionNG();
 	virtual ~distortionNG();
-
-	static osg::Geometry* createMesh(unsigned int column, unsigned int row);
 };
Index: experimental/distortionNG/extViewer.cpp
===================================================================
--- experimental/distortionNG/extViewer.cpp	(revision 311)
+++ experimental/distortionNG/extViewer.cpp	(revision 313)
@@ -1,4 +1,13 @@
 #include "extViewer.h"
 
+#include<osg/Texture2D>
+#include<osg/TextureRectangle>
+#include<osg/TexMat>
+#include<osg/ComputeBoundsVisitor>
+
+#include<osgDB/ReadFile>
+
+#include <osgUtil/SmoothingVisitor>
+
 extViewer::extViewer() : Viewer()
 {
@@ -21,7 +30,8 @@
 }
 
-void extViewer::setUpViewForDistortion()
-{
-    OSG_INFO<<"View::setUpViewForDistortion"<<std::endl;
+void extViewer::setUpViewForManualDistortion(osg::Image* intensityMap, unsigned int screenNum)
+{
+	OSG_INFO<<"View::setUpViewForManualDistortion(sn="<<screenNum<<", im="<<intensityMap<<")"<<std::endl;
+
     osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
     if (!wsi)
@@ -31,5 +41,5 @@
     }
 
-    osg::GraphicsContext::ScreenIdentifier si;
+	osg::GraphicsContext::ScreenIdentifier si;
     si.readDISPLAY();
 
@@ -42,5 +52,4 @@
     wsi->getScreenResolution(si, width, height);
 
-	   
 	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
     traits->hostName = si.hostName;
@@ -55,6 +64,7 @@
     traits->sharedContext = 0;
 
-
-    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
+	bool applyIntensityMapAsColours = true;
+
+	osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
     if (!gc)
     {
@@ -62,3 +72,140 @@
         return;
     }
-}
+
+    int tex_width = width;
+    int tex_height = height;
+
+    int camera_width = tex_width;
+    int camera_height = tex_height;
+
+    osg::TextureRectangle* texture = new osg::TextureRectangle;
+
+    texture->setTextureSize(tex_width, tex_height);
+    texture->setInternalFormat(GL_RGB);
+    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
+    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
+    texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
+    texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
+
+
+#if 0
+    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW;
+    GLenum buffer = GL_FRONT;
+#else
+    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
+    GLenum buffer = GL_FRONT;
+#endif
+
+	// Scene camera
+    {
+        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
+        camera->setName("Scene camera");
+        camera->setGraphicsContext(gc.get());
+        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
+        camera->setDrawBuffer(buffer);
+        camera->setReadBuffer(buffer);
+        camera->setAllowEventFocus(false);
+        // tell the camera to use OpenGL frame buffer object where supported.
+        camera->setRenderTargetImplementation(renderTargetImplementation);
+
+        // attach the texture and use it as the color buffer.
+        camera->attach(osg::Camera::COLOR_BUFFER, texture);
+
+        addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
+    }
+
+    // distortion correction set up.
+    {
+        osg::Geode* geode = new osg::Geode();
+        //geode->addDrawable(createParoramicSphericalDisplayDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), radius, collar, applyIntensityMapAsColours ? intensityMap : 0, projectorMatrix));
+		geode->addDrawable(createMesh(16, 19));
+
+        // new we need to add the texture to the mesh, we do so by creating a
+        // StateSet to contain the Texture StateAttribute.
+        osg::StateSet* stateset = geode->getOrCreateStateSet();
+        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
+        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
+
+        osg::TexMat* texmat = new osg::TexMat;
+        texmat->setScaleByTextureRectangleSize(true);
+        stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
+
+        if (!applyIntensityMapAsColours && intensityMap)
+        {
+            stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON);
+        }
+
+        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
+        camera->setGraphicsContext(gc.get());
+        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
+        camera->setClearColor( osg::Vec4(0.0,0.0,0.0,1.0) );
+        camera->setViewport(new osg::Viewport(0, 0, width, height));
+        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
+        camera->setDrawBuffer(buffer);
+        camera->setReadBuffer(buffer);
+        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
+        camera->setAllowEventFocus(false);
+        camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE);
+        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
+
+        camera->setProjectionMatrixAsOrtho2D(0,width,0,height);
+        camera->setViewMatrix(osg::Matrix::identity());
+
+        // add subgraph to render
+        camera->addChild(geode);
+
+        camera->setName("DistortionCorrectionCamera");
+
+        addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false);
+    }
+}
+
+osg::Geometry* extViewer::createMesh( unsigned int column, unsigned int row )
+{
+	osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(column * row);
+	osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(column * row);
+	for ( unsigned int i=0; i<row; ++i )
+	{
+		for ( unsigned int j=0; j<column; ++j )
+		{
+			(*vertices)[i*column + j].set( (float)i, (float)j, 0.0f );
+			(*texcoords)[i*column + j].set( (float)i/(float)row, (float)j/(float)column );
+		}
+	}
+
+	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
+	geom->setUseDisplayList( false );
+	geom->setUseVertexBufferObjects( true );
+	geom->setVertexArray( vertices.get() );
+	geom->setTexCoordArray( 0, texcoords.get() );
+	for ( unsigned int i=0; i<row-1; ++i )
+	{
+		osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2);
+		for ( unsigned int j=0; j<column; ++j )
+		{
+			(*de)[j*2 + 0] = i*column + j;
+			(*de)[j*2 + 1] = (i+1)*column + j;
+		}
+		geom->addPrimitiveSet( de.get() );
+	}
+
+
+
+	osg::ComputeBoundsVisitor cbv;
+	cbv.applyDrawable(geom);
+	osg::BoundingBox box = cbv.getBoundingBox();
+	if (!box.valid()) 
+		std::cout << "Invalid bounding box!"; 
+	geom->setInitialBound(box);
+
+	osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
+	texture->setImage( osgDB::readImageFile("Images/osg256.png") );
+	texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
+	texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
+	geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );
+
+	// Create normals
+	osgUtil::SmoothingVisitor::smooth( *geom );
+
+	return geom.release();
+}
Index: experimental/distortionNG/extViewer.h
===================================================================
--- experimental/distortionNG/extViewer.h	(revision 311)
+++ experimental/distortionNG/extViewer.h	(revision 313)
@@ -14,4 +14,6 @@
 
 		/** Convenience method for projection on curved screens using a slave camera rendering scene and a second camera doing distortion correction to present on a nonplaner display.*/
-        void setUpViewForDistortion();
+        void setUpViewForManualDistortion(osg::Image* intensityMap, unsigned int screenNum=0);
+
+		static osg::Geometry* createMesh(unsigned int column, unsigned int row);
 };
Index: experimental/distortionNG/main.cpp
===================================================================
--- experimental/distortionNG/main.cpp	(revision 311)
+++ experimental/distortionNG/main.cpp	(revision 313)
@@ -107,5 +107,5 @@
 
 	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
-	geode->addDrawable( distortionNG::createMesh(16, 9) );
+	geode->addDrawable( extViewer::createMesh(16, 9) );
 	geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
 	geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) );
