Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreFocusingMapRenderingRun.h
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreFocusingMapRenderingRun.h	(revision 1103)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreFocusingMapRenderingRun.h	(revision 1125)
@@ -22,10 +22,7 @@
 		@brief Constructor.
 
-		@param sharedRuns		a pointer to the OgreSharedRuns this run belongs to
-		@param name				the name of the scene depth texture to be created
-		@param playerView		pointer to the player's viewport	
+		
 	*/	
 	OgreFocusingMapRenderingRun(String name,
-								Viewport* playerView,
 								Matrix4 lightMatrix,
 								unsigned int focusingMapSize);
@@ -36,4 +33,5 @@
 	void getMinMax(Vector3& min, Vector3& max);
 	void setLightMatrix(Matrix4 &m){this->lightMatrix = m;}
+	void setCameraMatrices(const Matrix4 &view, const Matrix4 &projection);
 	
 protected:
@@ -41,10 +39,9 @@
 		@brief pointer to the player's viewport	
 	*/
-	Viewport* playerView;
 	Matrix4 lightMatrix;
 	/**
 		@brief pointer to the player's camera	
 	*/
-	Camera* playerCamera;
+	Camera* camera;
 	
 	/**
@@ -63,3 +60,8 @@
 	inline void createFocusingMap();
 
+	bool needUpdate(unsigned long frameNum )
+	{
+		return true;
+	}
+
 };
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.vcproj
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.vcproj	(revision 1103)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.vcproj	(revision 1125)
@@ -342,4 +342,7 @@
 				</File>
 				<File
+					RelativePath="..\..\..\..\..\App\Demos\Illum\Ogre\Media\materials\programs\GameTools_Focusing.hlsl">
+				</File>
+				<File
 					RelativePath="..\..\..\..\..\App\Demos\Illum\Ogre\Media\materials\programs\GameTools_Localized_EnvMap.hlsl">
 				</File>
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1120)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1125)
@@ -73,5 +73,5 @@
 	rt->update();
 	restoreMaterials();
-	//rt->writeContentsToFile("shadowmap1.dds");
+	rt->writeContentsToFile("shadowmap1.dds");
 }
 
@@ -88,48 +88,112 @@
 		if(light->getType() == Light::LT_DIRECTIONAL)
 		{
-			Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();
-			
-			OgreIlluminationManager::getSingleton().updatePerLightRun(light->getName(),
-																		ILLUMRUN_FOCUSING_MAP,
-																		frameNum);
-
+			Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();			
+			
 			OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*)
-				OgreIlluminationManager::getSingleton().getPerLightRun(light->getName(), ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun();
-			
-			Vector3 min;
-			Vector3 max;
-			frun->getMinMax(min, max);
+				OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun();
+			frun->setCameraMatrices(viewcam->getViewMatrix(), viewcam->getProjectionMatrix());
 
 			Vector3 lightpos = light->getPosition();
 			Vector3 lightdir = light->getDirection();
-			lightdir.normalise();
-
-			depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);	
-			/*depthMapCameras[0]->setAspectRatio(1.0);
-			depthMapCameras[0]->setFOVy(Radian(Degree(20)));
-			depthMapCameras[0]->setNearClipDistance(0.1);
-			depthMapCameras[0]->setFarClipDistance(200);*/
-
-			
+			lightdir.normalise();		
+			
+			Vector3 min;
+			Vector3 max;			
+
+			bool uselispsm = true;
+			if(uselispsm)
+			{
+				Camera* maincam = OgreIlluminationManager::getSingleton().getMainCamera();
+				Vector3 viewdir = maincam->getDirection();
+				viewdir.normalise();
+				Vector3 campos = maincam->getPosition();
+				float nearclip = maincam->getNearClipDistance();
+
+				float dotprod = viewdir.dotProduct(lightdir);
+				float sinGamma = sqrt(1.0 - dotprod * dotprod);
+				
+				Vector3 left = lightdir.crossProduct(viewdir);
+				Vector3 up = left.crossProduct(lightdir);
+				up.normalise();
+				//left = lightdir.crossProduct(up);
+				left.normalise();				
+
+				Matrix4 viewMatrix( left.x,      left.y,      left.z,		 0,
+									up.x,		 up.y,		  up.z,			 0,
+									lightdir.x,  lightdir.y,  lightdir.z,    0,
+									0,           0,           0,             1);				
+				
+				frun->setLightMatrix(viewMatrix);				
+				OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP,
+																			frameNum);			
+				frun->getMinMax(min, max);
+
+				float n;
+				float f;
+				float d = abs(max.y - min.y);
+					
+				float z_n = nearclip / sinGamma;
+				float one_min_singamma = (1 - sinGamma);
+				float fact = d * pow(one_min_singamma, 2);
+				float z_f = z_n + d * sinGamma * fact;
+				n = (z_n + sqrt(z_f * z_n)) / sinGamma;
+				///n = 100000;
+				f = n + d;
+
+				Vector3 pos = campos - up * (n - nearclip);
+				
+				Matrix4 newViewMatrix(  left.x,      left.y,      left.z,		 -left.dotProduct(pos),
+										up.x,		 up.y,		  up.z,			 -up.dotProduct(pos),
+										lightdir.x,  lightdir.y,  lightdir.z,    -lightdir.dotProduct(pos),
+										0,           0,           0,             1);
+
+
+				Matrix4 newProjection = Matrix4::IDENTITY;
+				
+				newProjection[1][1] = (f + n) / (f - n);
+				newProjection[1][3] = - 2* f * n / (f - n);
+				newProjection[3][1] = 1;
+				newProjection[3][3] = 0;
+				//newProjection = newProjection.transpose();
+				Matrix4 standardProjMatrix = Matrix4::IDENTITY;
+				standardProjMatrix.setScale(Vector3(1,1,-1));
+				newProjection = newProjection * standardProjMatrix;
+				depthMapCamera->setCustomProjectionMatrix(true, newProjection);
+				depthMapCamera->setCustomViewMatrix(true, viewMatrix);
+			}
+			else
+			{
+				Matrix4 standardProjMatrix = Matrix4::IDENTITY;
+				standardProjMatrix.setScale(Vector3(0,0,-1));
+				depthMapCamera->setCustomProjectionMatrix(true, standardProjMatrix);
+				depthMapCamera->setCustomViewMatrix(false);
+				depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);			
+				depthMapCamera->setPosition(lightpos);
+				depthMapCamera->setDirection(lightdir);
+			}
+
+
+			Matrix4 lightMatrix = depthMapCamera->getProjectionMatrix() * depthMapCamera->getViewMatrix();
+			frun->setLightMatrix(lightMatrix);
+			
+			OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP,
+																		frameNum);
+
+			frun->getMinMax(min, max);			
+
+						
 			Vector3 scale = (max - min);
-			scale *= 1.5;
-			//scale = Vector3(150,150,150);
-			Matrix4 projection = depthMapCamera->getProjectionMatrix();
-			projection = Matrix4::IDENTITY;
+			scale *= 1.5; // just for safety
+			scale.z = 200; //TODO: get scene bounding box to set z scale	
+			Matrix4 projection = Matrix4::IDENTITY;
 			projection.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, -2.0 / scale.z));			
 			projection.setTrans(Vector3(-(max.x + min.x) / scale.x,
 										-(max.y + min.y) / scale.y,
 										-(max.z + min.z) / scale.z));
+			projection = projection * depthMapCamera->getProjectionMatrix();
 			depthMapCamera->setCustomProjectionMatrix(true, projection);
 			
-			depthMapCamera->setPosition(lightpos);
-			depthMapCamera->setDirection(lightdir);
-			
-	/*
-			projection = Matrix4::IDENTITY;
-			projection.setScale(Vector3(1.0 / 100.0, 1.0 / 100.0, -1.0 / 100.0));
-			depthMapCameras[0]->setCustomProjectionMatrix(true, projection);
-			depthMapCameras[0]->setPosition( Vector3(50,50,50) );*/
-
+			
+			
 		}
 		else if(light->getType() == Light::LT_SPOTLIGHT)
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreFocusingMapRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreFocusingMapRenderingRun.cpp	(revision 1103)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreFocusingMapRenderingRun.cpp	(revision 1125)
@@ -58,5 +58,4 @@
 
 OgreFocusingMapRenderingRun::OgreFocusingMapRenderingRun(	String name,
-															Viewport* playerView,
 															Matrix4 lightMatrix,
 															unsigned int focusingMapSize
@@ -66,14 +65,15 @@
 {
 	this->name = name;
-	this->playerView = playerView;
 	this->lightMatrix = lightMatrix;
-	this->focusingMapSize = focusingMapSize;
+	this->focusingMapSize = focusingMapSize;	
 	
-	createFocusingMap();
-	
-	
+	createFocusingMap();	
 }
 
-
+void OgreFocusingMapRenderingRun::setCameraMatrices(const Matrix4 &view, const Matrix4 &projection)
+{
+	camera->setCustomViewMatrix(true, view);
+	camera->setCustomProjectionMatrix(true, projection);
+}
 
 void OgreFocusingMapRenderingRun::createFocusingMap()
@@ -91,9 +91,9 @@
 																		TU_RENDERTARGET);
 	focusingTexture = texPtr.getPointer();
-	 playerCamera = playerView->getCamera();
 	 //add viewport to rendertarget
 	 HardwarePixelBuffer* hpb = (focusingTexture->getBuffer()).getPointer();
 	 RenderTarget* rt = hpb->getRenderTarget();
-	 Viewport* v = rt->addViewport(playerCamera);
+	 this->camera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA"); 
+	 Viewport* v = rt->addViewport(camera);
 	 v->setOverlaysEnabled(false);
 	 rt->setAutoUpdated(false);
@@ -110,5 +110,5 @@
 	Vparams->setNamedConstant("lightTransform", lightMatrix);
 		
-	setMaterialForVisibles(String("GameTools/FocusingShader"), playerCamera);
+	setMaterialForVisibles(String("GameTools/FocusingShader"), camera);
 
 	RenderTarget* rt = focusingTexture->getBuffer().getPointer()->getRenderTarget();
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp	(revision 1103)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp	(revision 1125)
@@ -512,4 +512,15 @@
 			globalSharedRuns.addRun(ILLUMRUN_SCENE_CAMERA_DEPTH, run);
 		}
+		case ILLUMRUN_FOCUSING_MAP:
+		if(globalSharedRuns.getRun(ILLUMRUN_FOCUSING_MAP) == 0)
+		{
+			OgreFocusingMapRenderingRun* run  = new OgreFocusingMapRenderingRun(
+				"LIGHT_FOCUSING_MAP",				
+				Matrix4::IDENTITY,
+				focusingMapSize
+				);
+			globalSharedRuns.addRun(ILLUMRUN_FOCUSING_MAP, run);
+			
+		}
 		break;
 	}
@@ -520,4 +531,6 @@
 RenderingRun* OgreIlluminationManager::getGlobalRun(RenderingRunType runType)
 {
+	if(globalSharedRuns.getRun(runType) == 0)
+		createGlobalRun(runType);
 	return globalSharedRuns.getRun(runType);
 }
@@ -565,45 +578,11 @@
 				lightName + "DEPTH_SHADOW_MAP",
 				sm->getLight(lightName),
-				512, //TODO
-				512, //TODO
+				128, //TODO
+				128, //TODO
 				"GameTools/ShadowMapDepth" //TODO
 				);
 			runs->addRun(ILLUMRUN_DEPTH_SHADOWMAP, run);
 			
-		}
-		case ILLUMRUN_FOCUSING_MAP:
-		if(runs->getRun(ILLUMRUN_FOCUSING_MAP) == 0)
-		{
-			SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
-			Light* light = sm->getLight(lightName);
-			Vector3 position = light->getPosition();
-			Vector3 direction = light->getDirection();
-			Matrix4 transMatrix;
-			Camera cam("tempCamera", sm);
-			cam.setPosition(position);
-			cam.setDirection(direction);
-			transMatrix = cam.getViewMatrix();
-
-			if(light->getType() == Light::LT_SPOTLIGHT)
-			{
-				Radian fov = light->getSpotlightOuterAngle();
-				float far = light->getAttenuationRange();
-				cam.setNearClipDistance(0.1);
-				cam.setFarClipDistance(far);
-				cam.setFOVy(fov);
-				Matrix4 proj = cam.getProjectionMatrix();
-				transMatrix = transMatrix * proj;
-			}			
-			sm->destroyCamera("tempCamera");
-
-			OgreFocusingMapRenderingRun* run  = new OgreFocusingMapRenderingRun(
-				lightName + "FOCUSING_MAP",
-				mainViewport,
-				transMatrix,
-				focusingMapSize
-				);
-			runs->addRun(ILLUMRUN_FOCUSING_MAP, run);
-			
-		}
+		}		
 		break;
 	}	
Index: GTP/trunk/Lib/Illum/IllumModule/IllumModule/include/RenderingRun.h
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/IllumModule/include/RenderingRun.h	(revision 949)
+++ GTP/trunk/Lib/Illum/IllumModule/IllumModule/include/RenderingRun.h	(revision 1125)
@@ -52,5 +52,5 @@
 			@param frameNum current frame number			
 		*/
-		bool needUpdate(unsigned long frameNum )
+		virtual bool needUpdate(unsigned long frameNum )
 		{
 			if(frameNum == lastupdated )
