Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h	(revision 1425)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h	(revision 1628)
@@ -70,4 +70,7 @@
 	unsigned int focusingMapSize;
 	unsigned int shadowMapSize;
+	bool useLISPSM;
+	bool useVSM;
+	bool blurSM;
 	unsigned int phaseTextureSize;
 	std::map<RenderingRunType,float> maxRads;
@@ -302,4 +305,11 @@
 		return FrameListener::frameStarted(evt);
 	}
+
+	bool getUseLISPSM(){return useLISPSM;}
+	bool getUseVSM(){return useVSM;}
+	bool getBlurShadowMap(){return blurSM;}
+	void setUseLISPSM(bool use){useLISPSM = use;}
+	void setUseVSM(bool use){useVSM = use;}
+	void setBlurShadowMap(bool use){blurSM = use;}
 };
 
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreDepthShadowMapRenderingRun.h
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreDepthShadowMapRenderingRun.h	(revision 1130)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreDepthShadowMapRenderingRun.h	(revision 1628)
@@ -42,5 +42,6 @@
 		@brief returns the depth shadow map texture created by this run
 	*/
-	String getDepthMapTextureName(){return blurredname;}
+	String getDepthMapTextureName();
+	
 	/**
 		@brief Refreshes light camera matrices, called in each update.
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgrePhotonMapRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgrePhotonMapRenderingRun.cpp	(revision 1055)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgrePhotonMapRenderingRun.cpp	(revision 1628)
@@ -108,6 +108,6 @@
 			Matrix4 proj;
 			proj = Matrix4::IDENTITY;
-			proj.setScale(Vector3(1.0/r, 1.0/r, -1.0/r));
-			proj.setTrans(Vector3(0,0,-1));
+			proj.setScale(Vector3(1.0/r, 1.0/r, -1.5/r));
+			//proj.setTrans(Vector3(0,0,-1));
 			photonMapCamera->setCustomProjectionMatrix(true,proj);
 		}
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1130)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1628)
@@ -42,22 +42,26 @@
 		RenderTarget* rt = hpb->getRenderTarget();
 		Viewport* v = rt->addViewport(depthMapCamera);
+		v->setBackgroundColour(ColourValue::White);
 		v->setOverlaysEnabled(false);
 		rt->setAutoUpdated(false);
-
-		texPtr = Ogre::TextureManager::getSingleton().createManual(blurredname, 
-																			"default",
-																			TEX_TYPE_2D,
-																			resolutionX,
-																			resolutionY,
-																			6,
-																			0,
-																			PF_FLOAT16_RGBA,
-																			TU_RENDERTARGET);
-		blurredDepthMapTexture = texPtr.getPointer();
-		hpb = (blurredDepthMapTexture->getBuffer()).getPointer();
-		rt = hpb->getRenderTarget();
-		v = rt->addViewport(depthMapCamera);
-		v->setOverlaysEnabled(false);
-		rt->setAutoUpdated(false);
+		
+		if(OgreIlluminationManager::getSingleton().getBlurShadowMap())
+		{
+			texPtr = Ogre::TextureManager::getSingleton().createManual(blurredname, 
+																				"default",
+																				TEX_TYPE_2D,
+																				resolutionX,
+																				resolutionY,
+																				6,
+																				0,
+																				PF_FLOAT16_RGBA,
+																				TU_RENDERTARGET);
+			blurredDepthMapTexture = texPtr.getPointer();
+			hpb = (blurredDepthMapTexture->getBuffer()).getPointer();
+			rt = hpb->getRenderTarget();
+			v = rt->addViewport(depthMapCamera);
+			v->setOverlaysEnabled(false);
+			rt->setAutoUpdated(false);
+		}
 }
 
@@ -88,10 +92,12 @@
 	rt->update();
 	restoreMaterials();
-
-	rt = blurredDepthMapTexture->getBuffer().getPointer()->getRenderTarget();
-	Material*  mat = (Material*) MaterialManager::getSingleton().getByName("GameTools/Blur").getPointer();
-	mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(depthMapTexture->getName());
-	renderFullscreenQuad("GameTools/Blur", rt);
-	
+	
+	if(OgreIlluminationManager::getSingleton().getBlurShadowMap())
+	{
+		rt = blurredDepthMapTexture->getBuffer().getPointer()->getRenderTarget();
+		Material*  mat = (Material*) MaterialManager::getSingleton().getByName("GameTools/Blur").getPointer();
+		mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(depthMapTexture->getName());
+		renderFullscreenQuad("GameTools/Blur", rt);
+	}	
 	
 	//rt->writeContentsToFile("shadowmap1.dds");
@@ -99,8 +105,10 @@
 	//depthMapTexture->();
 }
-
-void setnear()
-{
-
+String OgreDepthShadowMapRenderingRun::getDepthMapTextureName()
+{
+		if(OgreIlluminationManager::getSingleton().getBlurShadowMap())
+			return blurredname;
+
+		return name;
 }
 
@@ -110,4 +118,5 @@
 	if(light!= 0)
 	{
+		bool uselispsm = OgreIlluminationManager::getSingleton().getUseLISPSM();
 		if(light->getType() == Light::LT_DIRECTIONAL)
 		{
@@ -118,5 +127,5 @@
 			frun->setCameraMatrices(viewcam->getViewMatrix(), viewcam->getProjectionMatrix());
 
-			Vector3 lightpos = light->getPosition();
+			Vector3 lightpos = light->getParentSceneNode()->getWorldPosition();
 			Vector3 lightdir = light->getDirection();
 			lightdir.normalise();		
@@ -125,5 +134,5 @@
 			Vector3 max;			
 
-			bool uselispsm = true;
+			
 			if(uselispsm)
 			{
@@ -138,4 +147,5 @@
 				
 				Vector3 left = lightdir.crossProduct(viewdir);
+				left.normalise();
 				Vector3 up = left.crossProduct(lightdir);
 				up.normalise();
@@ -167,5 +177,5 @@
 				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),
+										-lightdir.x,  -lightdir.y,  -lightdir.z,    lightdir.dotProduct(pos),
 										0,           0,           0,             1);
 
@@ -200,20 +210,139 @@
 			Vector3 scale = (max - min);
 			scale *= 1.5; // just for safety
-			scale.z = 200; //TODO: get scene bounding box to set z scale	
+			if(uselispsm)
+				scale.z += 1;
+			else
+				scale.z += 1000; //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));
+			Matrix4 trans = Matrix4::IDENTITY;
+			trans.setTrans(Vector3(-(max.x + min.x) / 2.0,
+								   -(max.y + min.y) / 2.0,
+								   -(max.z + min.z) / 2.0));
+			Matrix4 scaleM = Matrix4::IDENTITY;
+			scaleM.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, -2.0 / scale.z));
+			projection = scaleM * trans;			
 			depthMapCamera->setCustomProjectionMatrix(true, projection);			
 		}
 		else if(light->getType() == Light::LT_SPOTLIGHT)
 		{
-	/*		Vector3 dir = light->getDirection();
-			depthMapCamera->setDirection( dir );
-			Vector3 pos = light->getParentNode()->getWorldPosition();
-			depthMapCamera->setPosition(pos);
-			depthMapCamera->setProjectionType(PT_PERSPECTIVE);		
-			depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/
+			Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();			
+			
+			OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*)
+				OgreIlluminationManager::getSingleton().getGlobalRun(ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun();
+			frun->setCameraMatrices(viewcam->getViewMatrix(), viewcam->getProjectionMatrix());
+
+			Vector3 lightpos = light->getParentSceneNode()->getWorldPosition ();
+			Vector3 lightdir = light->getDirection();
+			lightdir.normalise();		
+			Radian lightangle = light->getSpotlightOuterAngle();
+				
+			Vector3 min;
+			Vector3 max;
+			
+			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();
+				
+				depthMapCamera->setCustomProjectionMatrix(false);
+				depthMapCamera->setProjectionType(PT_PERSPECTIVE);
+				depthMapCamera->setFOVy(lightangle);
+				depthMapCamera->setAspectRatio(1);
+				depthMapCamera->setNearClipDistance(0.1);
+				depthMapCamera->setFarClipDistance(1.0);
+
+				Matrix4 projMatrix = depthMapCamera->getProjectionMatrixWithRSDepth();
+
+				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(projMatrix * 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 lispMat = Matrix4::IDENTITY;
+				
+				lispMat[1][1] = f / (f - n);
+				lispMat[1][3] = f * n / (f - n);
+				lispMat[3][1] = 1;
+				lispMat[3][3] = 0;
+				
+				depthMapCamera->setCustomProjectionMatrix(true, Matrix4::IDENTITY);
+				depthMapCamera->setCustomViewMatrix(true, lispMat * projMatrix * newViewMatrix);
+			}
+			else
+			{
+				depthMapCamera->setCustomProjectionMatrix(false);
+				depthMapCamera->setCustomViewMatrix(false);
+				depthMapCamera->setPosition(lightpos);
+				depthMapCamera->setDirection(lightdir);
+				depthMapCamera->setProjectionType(PT_PERSPECTIVE);
+				depthMapCamera->setFOVy(lightangle);
+				depthMapCamera->setAspectRatio(1);
+				depthMapCamera->setNearClipDistance(0.1);
+				depthMapCamera->setFarClipDistance(1.0);
+			}
+			
+
+			Matrix4 lightMatrix = depthMapCamera->getViewMatrix();
+			frun->setLightMatrix(lightMatrix);
+			
+			OgreIlluminationManager::getSingleton().updateGlobalRun( ILLUMRUN_FOCUSING_MAP,
+																		frameNum);
+
+			frun->getMinMax(min, max);			
+			
+			if(min.z < 0)
+			{
+				float farP= -1.5 * min.z;
+				if(farP < 0.2) farP = 0.2;
+				depthMapCamera->setFarClipDistance(farP);
+
+				Vector3 scale = (max - min);
+				scale *= 1.5; // just for safety
+				
+				Matrix4 projection = Matrix4::IDENTITY;
+				Matrix4 trans = Matrix4::IDENTITY;
+				trans.setTrans(Vector3(-(max.x + min.x) / 2.0,
+								   -(max.y + min.y) / 2.0,
+								     0));
+				Matrix4 scaleM = Matrix4::IDENTITY;
+				scaleM.setScale(Vector3(2.0 / scale.x, 2.0 / scale.y, 1));
+				projection = scaleM * trans;			
+				depthMapCamera->setCustomProjectionMatrix(true, projection * depthMapCamera->getProjectionMatrix());				               
+			}
 		}
 		else//point light
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgreDepthShadowRecieverRenderTechnique.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgreDepthShadowRecieverRenderTechnique.cpp	(revision 1130)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgreDepthShadowRecieverRenderTechnique.cpp	(revision 1628)
@@ -51,5 +51,7 @@
 		
 		TextureUnitState* st = newpass->createTextureUnitState();		
-		st->setTextureFiltering(TFO_ANISOTROPIC);						
+		st->setTextureFiltering(TFO_ANISOTROPIC);
+		st->setTextureAddressingMode(TextureUnitState::TAM_BORDER);
+		st->setTextureBorderColour(ColourValue::White);
 
 		newpass->setSceneBlending(SBT_MODULATE);
@@ -73,35 +75,41 @@
 							100000.0,
 							lights);
-	
+
 	//fill passes
+	unsigned int l = 0;
 	for(unsigned int i = 0; i < passes.size(); i++)
 	{
-		if(lights.size() > i)
+		while(lights.size() != l && !lights.at(l)->getCastShadows())
+		 l++;
+
+		if(lights.size() > l)
 		{
-			//create run if not exists
-			OgreIlluminationManager::getSingleton().createPerLightRun(lights.at(i)->getName(),
-																		ILLUMRUN_DEPTH_SHADOWMAP);
-			//update light depth map
-			OgreIlluminationManager::getSingleton().updatePerLightRun(lights.at(i)->getName(),
-																		ILLUMRUN_DEPTH_SHADOWMAP,
-																		frameNum);
 			
-			//set texture to pass
-			OgreDepthShadowMapRenderingRun* depthRun = 
-				(OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
-													.getPerLightRun(lights.at(i)->getName(), 
-													ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
-					
-			passes.at(i)->getTextureUnitState(0)->setTextureName(
-				depthRun->getDepthMapTextureName());
-		
-			//TODO: set matrices
-			GpuProgramParametersSharedPtr fpParams = passes.at(i)->getFragmentProgramParameters();
-			GpuProgramParametersSharedPtr vpParams = passes.at(i)->getVertexProgramParameters();
-		
-			//fpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix()); 
-			vpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix());
+				//create run if not exists
+				OgreIlluminationManager::getSingleton().createPerLightRun(lights.at(l)->getName(),
+																			ILLUMRUN_DEPTH_SHADOWMAP);
+				//update light depth map
+				OgreIlluminationManager::getSingleton().updatePerLightRun(lights.at(l)->getName(),
+																			ILLUMRUN_DEPTH_SHADOWMAP,
+																			frameNum);
+				
+				//set texture to pass
+				OgreDepthShadowMapRenderingRun* depthRun = 
+					(OgreDepthShadowMapRenderingRun*) OgreIlluminationManager::getSingleton()
+														.getPerLightRun(lights.at(l)->getName(), 
+														ILLUMRUN_DEPTH_SHADOWMAP)->asOgreRenderingRun();
+						
+				passes.at(i)->getTextureUnitState(0)->setTextureName(
+					depthRun->getDepthMapTextureName());
 			
-			passes.at(i)->setActive(true);
+				//TODO: set matrices
+				GpuProgramParametersSharedPtr fpParams = passes.at(i)->getFragmentProgramParameters();
+				GpuProgramParametersSharedPtr vpParams = passes.at(i)->getVertexProgramParameters();
+			
+				//fpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix()); 
+				vpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix());
+				
+				passes.at(i)->setActive(true);
+			
 		}	
 		else
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp	(revision 1425)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp	(revision 1628)
@@ -82,4 +82,7 @@
 	shadowMapSize = 512;
 	phaseTextureSize = 256;
+	useLISPSM = false;
+	useVSM = false;
+	blurSM = false;
 	
 	for(int i = 0; i < RUN_TYPE_COUNT; i++)
