Index: experimental/distortionNG/extViewer.cpp
===================================================================
--- experimental/distortionNG/extViewer.cpp	(revision 340)
+++ experimental/distortionNG/extViewer.cpp	(revision 341)
@@ -287,34 +287,5 @@
 		// If the intensityMap is used but not applyIntensityMapAsColours: Apply intensityMap as intensityMapTexture on unit 1
 		if (!applyIntensityMapAsColours && intensityMap)
-        {
-			// Resize intensityMap if the dimensions are wrong
-			if(intensityMap->s()!=tex_width || intensityMap->t()!=tex_height)
-				intensityMap->scaleImage(tex_width, tex_height, intensityMap->r());
-
-			osg::TextureRectangle* intensityMapTexture = new osg::TextureRectangle(intensityMap);
-			intensityMapTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
-			intensityMapTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
-			intensityMapTexture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
-			intensityMapTexture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
-
-            stateset->setTextureAttributeAndModes(1, intensityMapTexture, osg::StateAttribute::ON);
-
-			// create shaders for distortion
-			osg::Program* distortProgram = new osg::Program;
-			distortProgram->setName( "distortion" );
-			osg::Shader* vShader = osg::Shader::readShaderFile( osg::Shader::VERTEX, "shader.vert" ); 
-			vShader->setName("intensityMapVertShader");
-			osg::Shader* fShader = osg::Shader::readShaderFile( osg::Shader::FRAGMENT, "shader.frag" ); 
-			fShader->setName("intensityMapFragShader");
-
-			if ( vShader && fShader )
-			{
-				//distortProgram->addShader( vShader );
-				distortProgram->addShader( fShader );
-				stateset->addUniform( new osg::Uniform("sceneTexture", 0) );
-				stateset->addUniform( new osg::Uniform("intensityMapTexture", 1) );
-				stateset->setAttributeAndModes(distortProgram, osg::StateAttribute::ON);
-			}
-        }
+			setUpIntensityMapBlending(stateset, intensityMap, screenNum, 0, 1);	
 
 		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);
@@ -358,2 +329,55 @@
     }
 }
+
+void extViewer::setUpIntensityMapBlending(osg::StateSet* stateset, osg::Image* intensityMap, unsigned int screenNum, int rttSceneTextureUnit, int intensityMapTextureUnit)
+{
+	if(stateset == NULL || intensityMap == NULL)
+	{
+		OSG_NOTICE<<"Error, no intensityMap or stateset for intensityMapBlending specified."<<std::endl;
+		return;
+	}
+
+	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();
+
+    // displayNum has not been set so reset it to 0.
+    if (si.displayNum<0) si.displayNum = 0;
+
+    si.screenNum = screenNum;
+
+    unsigned int width, height;
+    wsi->getScreenResolution(si, width, height);
+
+	// Resize intensityMap if the dimensions are wrong
+	if(intensityMap->s()!=width || intensityMap->t()!=height)
+		intensityMap->scaleImage(width, height, intensityMap->r());
+
+	osg::TextureRectangle* intensityMapTexture = new osg::TextureRectangle(intensityMap);
+	intensityMapTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
+	intensityMapTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
+	intensityMapTexture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
+	intensityMapTexture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
+
+    stateset->setTextureAttributeAndModes(intensityMapTextureUnit, intensityMapTexture, osg::StateAttribute::ON);
+
+	// create shaders for blending
+	osg::Program* distortProgram = new osg::Program;
+	distortProgram->setName( "intensityMapBlending" );
+	osg::Shader* fShader = osg::Shader::readShaderFile( osg::Shader::FRAGMENT, "shader.frag" ); 
+	fShader->setName("intensityMapFragShader");
+
+	if ( fShader )
+	{
+		distortProgram->addShader( fShader );
+		stateset->addUniform( new osg::Uniform("sceneTexture", rttSceneTextureUnit) );
+		stateset->addUniform( new osg::Uniform("intensityMapTexture", intensityMapTextureUnit) );
+		stateset->setAttributeAndModes(distortProgram, osg::StateAttribute::ON);
+	}
+}
