Index: /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h	(revision 1062)
+++ /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreIlluminationManager.h	(revision 1103)
@@ -67,4 +67,5 @@
 	*/
 	float maxRad;
+	unsigned int focusingMapSize;
 	std::map<RenderingRunType,float> maxRads;
 	/**
@@ -149,4 +150,5 @@
 	}
 	void setMaxJoinRadius(RenderingRunType type, float rad){maxRads[type] = rad;}
+	void setFocusingMapSize(unsigned int size){focusingMapSize = size;}
 	/**
 		@brief Returns the one and only OgreIlluminationManager instance.
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 1103)
@@ -0,0 +1,65 @@
+#pragma once
+
+//disable inheritance warning caused by multiple inheritance
+#if _WIN32
+#if _MSC_VER
+#pragma warning(disable: 4250)
+#endif
+#endif
+
+#include "OgreRenderingRun.h"
+#include "OgreSharedRuns.h"
+
+
+/**
+	@brief ColorCubeMapRenderingRun used in an OGRE environment.
+*/
+class OgreFocusingMapRenderingRun : public OgreRenderingRun									
+{	
+public:
+
+	/**
+		@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);
+	/**
+		@brief returns the name of the camera depth texture
+	*/
+	String getFocusingTextureName(){return name;}
+	void getMinMax(Vector3& min, Vector3& max);
+	void setLightMatrix(Matrix4 &m){this->lightMatrix = m;}
+	
+protected:
+	/**
+		@brief pointer to the player's viewport	
+	*/
+	Viewport* playerView;
+	Matrix4 lightMatrix;
+	/**
+		@brief pointer to the player's camera	
+	*/
+	Camera* playerCamera;
+	
+	/**
+		@brief the name of the depth texture that was created by this run
+	*/
+	String name;
+	/**
+		@brief a pointer to the scene depth texture that was created by this run
+	*/
+	Texture* focusingTexture;
+	unsigned int focusingMapSize;
+
+	//inherited
+	void updateFrame(unsigned long frameNum);	
+	
+	inline void createFocusingMap();
+
+};
Index: /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreDepthShadowMapRenderingRun.h
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreDepthShadowMapRenderingRun.h	(revision 1055)
+++ /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/RenderingRuns/OgreDepthShadowMapRenderingRun.h	(revision 1103)
@@ -11,4 +11,5 @@
 #include "DepthShadowMapRenderingRun.h"
 #include "OgreSharedRuns.h"
+#include "OgreFocusingMapRenderingRun.h"
 
 
@@ -45,5 +46,5 @@
 		@brief Refreshes light camera matrices, called in each update.
 	*/
-	void refreshLight();
+	void refreshLight(unsigned long frameNum);
 	/**
 		@brief retuns the view matrix of the camera from which the depth shadow map was created
Index: /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.vcproj
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.vcproj	(revision 1055)
+++ /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.vcproj	(revision 1103)
@@ -187,4 +187,7 @@
 				</File>
 				<File
+					RelativePath="..\src\RenderingRuns\OgreFocusingMapRenderingRun.cpp">
+				</File>
+				<File
 					RelativePath="..\src\RenderingRuns\OgrePhotonMapRenderingRun.cpp">
 				</File>
@@ -272,4 +275,7 @@
 				</File>
 				<File
+					RelativePath="..\include\RenderingRuns\OgreFocusingMapRenderingRun.h">
+				</File>
+				<File
 					RelativePath="..\include\RenderingRuns\OgrePhotonMapRenderingRun.h">
 				</File>
Index: /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1055)
+++ /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1103)
@@ -25,5 +25,5 @@
 void OgreDepthShadowMapRenderingRun::createDepthMap()
 {
-	for(int i = 0; i < 2; i++)
+	for(int i = 0; i < 1; i++)
 	{
 		TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(names[i], 
@@ -49,5 +49,5 @@
 void OgreDepthShadowMapRenderingRun::updateFrame(unsigned long frameNum)
 {
-	refreshLight();
+	refreshLight(frameNum);
 
 	if(light->getType() == Light::LT_POINT)
@@ -69,17 +69,9 @@
 void OgreDepthShadowMapRenderingRun::updateDepthMap()
 {
-	setMaterialForVisibles(materialName, depthMapCameras[0], true);	
+	setMaterialForVisibles(materialName, depthMapCameras[0], false);	
 	RenderTarget* rt = depthMapTextures[0]->getBuffer().getPointer()->getRenderTarget();	
 	rt->update();
-	//restoreMaterials();
+	restoreMaterials();
 	//rt->writeContentsToFile("shadowmap1.dds");
-	
-	//setMaterialForVisibles(materialName, depthMapCameras[1], true);	
-	RenderTarget* rt2 = depthMapTextures[1]->getBuffer().getPointer()->getRenderTarget();
-	rt2->update();
-	restoreMaterials();
-	//rt2->writeContentsToFile("shadowmap2.dds");
-
-	
 }
 
@@ -89,5 +81,5 @@
 }
 
-void OgreDepthShadowMapRenderingRun::refreshLight()
+void OgreDepthShadowMapRenderingRun::refreshLight(unsigned long frameNum)
 {
 	
@@ -96,81 +88,42 @@
 		if(light->getType() == Light::LT_DIRECTIONAL)
 		{
-			//Vector3 dir = light->getDirection();
-			Vector3 dir = Vector3(0,-1,-1);
-			Vector3 pos;
-			float size;
-			
 			Camera* viewcam = OgreIlluminationManager::getSingleton().getMainCamera();
 			
-			//Matrix4 camView = viewcam->getViewMatrix();
-			//Matrix4 camViewI = camView.inverse();
-			//const AxisAlignedBox bt = viewcam->getWorldBoundingBox(true);
-			//AxisAlignedBox b = bt;
-			//b.transform(camView);
-			//SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
-			//SceneNode* node = sm->getRootSceneNode();
-			//const AxisAlignedBox bs(-35,-5 ,-75, 35, 15, 75);// = node->_getWorldAABB();
-			//AxisAlignedBox intersection = b.intersection(bs);
-			//pos = intersection.getCenter();			
-			//size = (intersection.getMaximum() - pos).length();
+			OgreIlluminationManager::getSingleton().updatePerLightRun(light->getName(),
+																		ILLUMRUN_FOCUSING_MAP,
+																		frameNum);
+
+			OgreFocusingMapRenderingRun* frun = (OgreFocusingMapRenderingRun*)
+				OgreIlluminationManager::getSingleton().getPerLightRun(light->getName(), ILLUMRUN_FOCUSING_MAP)->asOgreRenderingRun();
 			
-			//TODO: where to put light
-			size = 30;
-			pos = Vector3(0,0,0);
+			Vector3 min;
+			Vector3 max;
+			frun->getMinMax(min, max);
+
+			Vector3 lightpos = light->getPosition();
+			Vector3 lightdir = light->getDirection();
 			
-			depthMapCameras[0]->setDirection( dir );
-			pos = pos - size * dir;			
-			depthMapCameras[0]->setPosition( pos );
-			depthMapCameras[0]->setProjectionType(PT_ORTHOGRAPHIC);		
-			Matrix4 fproj;
-			fproj = depthMapCameras[0]->getProjectionMatrix();
-			fproj = Matrix4::IDENTITY;
-			fproj.setScale(Vector3(1.0/20, 1.0/25, -1.0/100));
-			//fproj.setTrans(Vector3(0,0,-50));
-			depthMapCameras[0]->setCustomProjectionMatrix(true,fproj);
+			Vector3 center = (min + max) / 2.0;
+			Vector3 scale = max - min;
+			scale.x = 2.0 / scale.x;
+			scale.y = 2.0 / scale.y;
+			scale.z = 2.0 / scale.z;
+			center.x *= scale.x;
+			center.y *= scale.y;
+			center.z *= scale.z;
 
 
-			//camera2
-			dir.normalise();
-			size = abs(dir.crossProduct(Vector3(0,0,1)).length() * 2);
-			Vector3 viewDir = viewcam->getDirection();
-			float y = viewDir.y;
-			viewDir.y = std::min(0.0f, y);
-			viewDir.normalise();
-			Vector3 down(0,-1,0);
-			float cosA = viewDir.dotProduct(down);
-			Vector3 newDir = viewDir + down * 0.4; 
-			newDir.normalise();
-			pos = viewcam->getPosition();
-			size *= pos.y * 0.5 + 1;
-			pos += newDir * viewcam->getPosition().y 
-											/ newDir.dotProduct(down);
-			//pos.y = 0;
-			
-			
-			
-			depthMapCameras[1]->setDirection( dir );
-			pos = pos - 30 * dir;			
-			depthMapCameras[1]->setPosition( pos );
-			depthMapCameras[1]->setProjectionType(PT_ORTHOGRAPHIC);		
-			Matrix4 fproj2;
-			fproj2 = depthMapCameras[1]->getProjectionMatrix();
-			fproj2 = Matrix4::IDENTITY;
-			fproj2.setScale(Vector3(1.0/size, 1.0/size, -1.0/100));
-			//fproj.setTrans(Vector3(0,0,-50));
-			depthMapCameras[1]->setCustomProjectionMatrix(true,fproj2);
-			
-			
-/*LISPSM
-			Vector3 viewdir = viewcam->getDirection();
-			Vector3 lightdir = dir;
+			depthMapCameras[0]->setDirection( lightdir );
+			depthMapCameras[0]->setPosition( lightpos );
 
-			float dotProd = viewdir.dotProduct(lightdir);
-			float sinGamma;
-			sinGamma = sqrt(1.0-dotProd*dotProd);
-			g_fsinGamma = sinGamma;
-
-			Vector3 newVdir = viewdir;
-			Matrix4 proj;*/
+			Matrix4 projection = Matrix4::IDENTITY;
+			projection.setTrans(center);
+			projection.setScale(scale);
+			depthMapCameras[0]->setCustomProjectionMatrix(true, projection);	
+/*
+			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) );*/
 
 		}
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 1103)
@@ -0,0 +1,126 @@
+#include "OgreFocusingMapRenderingRun.h"
+
+
+void OgreFocusingMapRenderingRun::getMinMax(Vector3& min, Vector3& max)
+{	
+	unsigned int buffersize = focusingMapSize * focusingMapSize * 4;
+	float* floatbuffer = new float[buffersize];
+	PixelBox lockBox(focusingMapSize, focusingMapSize, 1, PF_FLOAT32_RGBA, floatbuffer);
+	focusingTexture->getBuffer()->blitToMemory(lockBox);
+	
+    float minX = 0;
+	float minY = 0;
+	float minZ = 0;
+	float maxX = 0;
+	float maxY = 0;
+	float maxZ = 0;
+
+	bool first = true;
+	
+	for(int i = 0; i < buffersize; i+= 4)
+	{
+		float x = floatbuffer[i];
+		float y = floatbuffer[i + 1];
+		float z = floatbuffer[i + 2];
+		float valid = floatbuffer[i + 3];
+
+		if(valid)
+		{
+			if(first)
+			{
+				minX = x;
+				minY = y;
+				minZ = z;
+				maxX = x;
+				maxY = y;
+				maxZ = z;
+
+				first = false;
+			}
+			else
+			{
+				if(x < minX)minX = x;
+				if(y < minY)minY = y;
+				if(z < minZ)minZ = z;
+
+				if(x > maxX)maxX = x;
+				if(y > maxY)maxY = y;
+				if(z > maxZ)maxZ = z;
+			}
+		}
+	}
+
+	min.x = minX; min.y = minY; min.z = minZ;
+	max.x = maxX; max.y = maxY; max.z = maxZ;
+
+	delete[] floatbuffer;
+}
+
+OgreFocusingMapRenderingRun::OgreFocusingMapRenderingRun(	String name,
+															Viewport* playerView,
+															Matrix4 lightMatrix,
+															unsigned int focusingMapSize
+														   )
+														   : OgreRenderingRun(1, 1)
+														   , RenderingRun(1, 1)
+{
+	this->name = name;
+	this->playerView = playerView;
+	this->lightMatrix = lightMatrix;
+	this->focusingMapSize = focusingMapSize;
+	
+	createFocusingMap();
+	
+	
+}
+
+
+
+void OgreFocusingMapRenderingRun::createFocusingMap()
+{
+	int width = focusingMapSize;
+	int height = focusingMapSize;
+	TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name, 
+																		"default",
+																		TEX_TYPE_2D,
+																		width,
+																		height,
+																		0,
+																		0,
+																		PF_FLOAT32_RGBA,
+																		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);
+	 v->setOverlaysEnabled(false);
+	 rt->setAutoUpdated(false);
+}
+
+
+
+void OgreFocusingMapRenderingRun::updateFrame(unsigned long frameNum)
+{
+	
+	
+	MaterialPtr mat = MaterialManager::getSingleton().getByName("GameTools/FocusingShader");
+	GpuProgramParameters* Vparams = mat->getTechnique(0)->getPass(0)->getVertexProgramParameters().getPointer();
+	Vparams->setNamedConstant("lightTransform", lightMatrix);
+		
+	setMaterialForVisibles(String("GameTools/FocusingShader"), playerCamera);
+
+	RenderTarget* rt = focusingTexture->getBuffer().getPointer()->getRenderTarget();
+
+	ColourValue backColor(0,0,0,0);	
+	rt->getViewport(0)->setBackgroundColour(backColor);
+		
+	rt->update();	
+
+	//rt->writeContentsToFile("focusingmap.dds");
+	
+	restoreMaterials();	
+}
+
+
Index: /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgreDepthShadowRecieverRenderTechnique.cpp
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgreDepthShadowRecieverRenderTechnique.cpp	(revision 1055)
+++ /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderTechniques/OgreDepthShadowRecieverRenderTechnique.cpp	(revision 1103)
@@ -103,6 +103,6 @@
 			fpParams->setNamedConstant("lightView", depthRun->getLightViewMatrix(0)); 
 			fpParams->setNamedConstant("lightViewProj", depthRun->getLightViewProjMatrix(0)); 
-			fpParams->setNamedConstant("lightView2", depthRun->getLightViewMatrix(1)); 
-			fpParams->setNamedConstant("lightViewProj2", depthRun->getLightViewProjMatrix(1)); 
+		//	fpParams->setNamedConstant("lightView2", depthRun->getLightViewMatrix(1)); 
+		//	fpParams->setNamedConstant("lightViewProj2", depthRun->getLightViewProjMatrix(1)); 
 			
 			passes.at(i)->setActive(true);
Index: /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp	(revision 1062)
+++ /GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreIlluminationManager.cpp	(revision 1103)
@@ -79,4 +79,5 @@
 	visitor = new VisibleFinderVisitor(&visibleObjects);
 	maxRad = 400;
+	focusingMapSize = 32;
 	
 	for(int i = 0; i < RUN_TYPE_COUNT; i++)
@@ -571,4 +572,38 @@
 			
 		}
+		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/SharedRuns.h
===================================================================
--- /GTP/trunk/Lib/Illum/IllumModule/IllumModule/include/SharedRuns.h	(revision 1055)
+++ /GTP/trunk/Lib/Illum/IllumModule/IllumModule/include/SharedRuns.h	(revision 1103)
@@ -21,5 +21,6 @@
 	ILLUMRUN_DEPTH_SHADOWMAP,
 	ILLUMRUN_SCENE_CAMERA_DEPTH,
-	ILLUMRUN_HPP_IMPOSTOR
+	ILLUMRUN_HPP_IMPOSTOR,
+	ILLUMRUN_FOCUSING_MAP
 };
 
