Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 874)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 1055)
@@ -1,3 +1,4 @@
 #include "OgreDepthShadowMapRenderingRun.h"
+#include "OgreIlluminationManager.h"
 
 
@@ -15,5 +16,6 @@
 	this->light = light;
 	this->sharedRuns = sharedRuns;
-	this->name = name;	
+	names[0] = name + "1";	
+	names[1] = name + "2";	
 	this->materialName = materialName;
 
@@ -23,21 +25,24 @@
 void OgreDepthShadowMapRenderingRun::createDepthMap()
 {
-	TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(name, 
-																		"default",
-																		TEX_TYPE_2D,
-																		resolutionX,
-																		resolutionY,
-																		0,
-																		0,
-																		PF_FLOAT16_RGBA,
-																		TU_RENDERTARGET);
-	 depthMapTexture = texPtr.getPointer();
-	 depthMapCamera = Root::getSingleton()._getCurrentSceneManager()->createCamera(name + "_CAMERA");
-	 //add viewport to rendertarget
-	 HardwarePixelBuffer* hpb = (depthMapTexture->getBuffer()).getPointer();
-	 RenderTarget* rt = hpb->getRenderTarget();
-	 Viewport* v = rt->addViewport(depthMapCamera);
-	 v->setOverlaysEnabled(false);
-	 rt->setAutoUpdated(false);
+	for(int i = 0; i < 2; i++)
+	{
+		TexturePtr texPtr = Ogre::TextureManager::getSingleton().createManual(names[i], 
+																			"default",
+																			TEX_TYPE_2D,
+																			resolutionX,
+																			resolutionY,
+																			0,
+																			0,
+																			PF_FLOAT16_RGBA,
+																			TU_RENDERTARGET);
+		depthMapTextures[i] = texPtr.getPointer();
+		depthMapCameras[i] = Root::getSingleton()._getCurrentSceneManager()->createCamera(names[i] + "_CAMERA");
+		//add viewport to rendertarget
+		HardwarePixelBuffer* hpb = (depthMapTextures[i]->getBuffer()).getPointer();
+		RenderTarget* rt = hpb->getRenderTarget();
+		Viewport* v = rt->addViewport(depthMapCameras[i]);
+		v->setOverlaysEnabled(false);
+		rt->setAutoUpdated(false);
+	}
 }
 
@@ -64,15 +69,22 @@
 void OgreDepthShadowMapRenderingRun::updateDepthMap()
 {
-	setMaterialForVisibles(materialName, depthMapCamera, true);
-	
-	RenderTarget* rt = depthMapTexture->getBuffer().getPointer()->getRenderTarget();
-	
-	SceneManager* sm = Root::getSingleton()._getCurrentSceneManager();
-	
-	rt->update();	
-
-	//rt->writeContentsToFile("shadowmap.dds");
-
+	setMaterialForVisibles(materialName, depthMapCameras[0], true);	
+	RenderTarget* rt = depthMapTextures[0]->getBuffer().getPointer()->getRenderTarget();	
+	rt->update();
+	//restoreMaterials();
+	//rt->writeContentsToFile("shadowmap1.dds");
+	
+	//setMaterialForVisibles(materialName, depthMapCameras[1], true);	
+	RenderTarget* rt2 = depthMapTextures[1]->getBuffer().getPointer()->getRenderTarget();
+	rt2->update();
 	restoreMaterials();
+	//rt2->writeContentsToFile("shadowmap2.dds");
+
+	
+}
+
+void setnear()
+{
+
 }
 
@@ -84,22 +96,95 @@
 		if(light->getType() == Light::LT_DIRECTIONAL)
 		{
-			Vector3 dir = light->getDirection();
-			depthMapCamera->setDirection( dir );
+			//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();
+			
 			//TODO: where to put light
-			depthMapCamera->setPosition( - dir * 100000 );
-			depthMapCamera->setProjectionType(PT_ORTHOGRAPHIC);		
+			size = 30;
+			pos = Vector3(0,0,0);
+			
+			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);
+
+
+			//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;
+
+			float dotProd = viewdir.dotProduct(lightdir);
+			float sinGamma;
+			sinGamma = sqrt(1.0-dotProd*dotProd);
+			g_fsinGamma = sinGamma;
+
+			Vector3 newVdir = viewdir;
+			Matrix4 proj;*/
+
 		}
 		else if(light->getType() == Light::LT_SPOTLIGHT)
 		{
-			Vector3 dir = light->getDirection();
+	/*		Vector3 dir = light->getDirection();
 			depthMapCamera->setDirection( dir );
 			Vector3 pos = light->getParentNode()->getWorldPosition();
 			depthMapCamera->setPosition(pos);
 			depthMapCamera->setProjectionType(PT_PERSPECTIVE);		
-			depthMapCamera->setFOVy(light->getSpotlightOuterAngle());
+			depthMapCamera->setFOVy(light->getSpotlightOuterAngle());*/
 		}
 		else//point light
 		{
-			Vector3 pos = light->getParentNode()->getWorldPosition();
+	/*		Vector3 pos = light->getParentNode()->getWorldPosition();
 			Vector3 dir = -pos;
 			depthMapCamera->setDirection( dir );
@@ -111,5 +196,5 @@
 	
 			//OGRE_EXCEPT(0, "NOT implemented for Pointlight", "OgreDepthShadowMapRenderingRun::refreshLight");
-			
+		*/	
 			/*Vector3 pos = light->getParentNode()->getWorldPosition();
 			Vector3 dir = -pos;
@@ -123,12 +208,12 @@
 }
 
-Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix()
-{
-	return depthMapCamera->getViewMatrix();
-}
-
-Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix()
-{
-	return depthMapCamera->getProjectionMatrix() * depthMapCamera->getViewMatrix();
-}
-
+Matrix4 OgreDepthShadowMapRenderingRun::getLightViewMatrix(int i)
+{
+	return depthMapCameras[i]->getViewMatrix();
+}
+
+Matrix4 OgreDepthShadowMapRenderingRun::getLightViewProjMatrix(int i)
+{
+	return depthMapCameras[i]->getProjectionMatrix() * depthMapCameras[i]->getViewMatrix();
+}
+
