Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreRenderingRun.h
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreRenderingRun.h	(revision 2366)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/include/OgreRenderingRun.h	(revision 2375)
@@ -45,5 +45,6 @@
 		@see restoreMaterials
 	*/
-	std::map<Renderable*, String> visibleObjects;
+	std::map<Renderable*, String> materialRestoreMap;
+	std::set<Renderable*> visibleObjects;
 	/**
 		@brief fulls screen quad plane used in full screen quad rendering
@@ -126,4 +127,6 @@
 	*/
 	void setMaterialForVisibles(String& materialName, Camera* cam, bool shadowcastersonly = false, bool solidsonly = false, String triggerName = "");
+	
+	void setMaterialForRenderables(String& materialName, bool shadowcastersonly = false, String triggerName = "");
 	/**
 		@brief Restores previously stored materials.
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.7.10.vcproj
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.7.10.vcproj	(revision 2366)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/scripts/OgreIllumModule.7.10.vcproj	(revision 2375)
@@ -637,4 +637,7 @@
 			</Filter>
 		</Filter>
+		<File
+			RelativePath=".\VTune\IllumModule_Ogre.vpj">
+		</File>
 	</Files>
 	<Globals>
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreRenderingRun.cpp	(revision 2366)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/OgreRenderingRun.cpp	(revision 2375)
@@ -13,10 +13,15 @@
 	String materialName;
 	String triggerName;
-	std::map<Renderable*, String>* visibleObjects;
+	std::map<Renderable*, String>* materialRestoreMap;
+	std::set<Renderable*>* visibleObjects;
 public:
     
-	MaterialSetVisitor(String& materialName, std::map<Renderable*, String>* visibleObjects, String trigger = "")
+	MaterialSetVisitor(String& materialName,
+						std::map<Renderable*, String>* materialRestoreMap,
+						std::set<Renderable*>* visibleObjects,
+						String trigger = "")
 	{
 		this->materialName = materialName;
+		this->materialRestoreMap = materialRestoreMap;
 		this->visibleObjects = visibleObjects;
 		triggerName = trigger;
@@ -29,40 +34,101 @@
 		if( visibleObjects->count(rend) == 0)
 		{
-			(*visibleObjects)[rend] = rend->getMaterial().getPointer()->getName();
-			String materialToSet = materialName;
-			if(triggerName != "")//we have a trigger here
+			visibleObjects->insert(rend);
+
+			if( materialRestoreMap->count(rend) == 0)
 			{
-				OgreTechniqueGroup* tg = (OgreTechniqueGroup*)rend->getRenderTechniqueGroup();
-				if(tg)
+				(*materialRestoreMap)[rend] = rend->getMaterial().getPointer()->getName();
+
+				String materialToSet = materialName;
+				if(triggerName != "")//we have a trigger here
 				{
-					String triggeredMaterial = tg->getMaterialNameForTrigger(triggerName);
+					OgreTechniqueGroup* tg = (OgreTechniqueGroup*)rend->getRenderTechniqueGroup();
+					if(tg)
+					{
+						String triggeredMaterial = tg->getMaterialNameForTrigger(triggerName);
+						if(triggeredMaterial != "")//a trigger is associated
+							materialToSet = triggeredMaterial;
+					}
+				}				
+				rend->setMaterialName(materialToSet);
+			}
+		}
+	}
+	bool visit(const Pass* p){return true;}
+	void visit(const RenderablePass* rp)
+	{
+		Renderable* rend = rp->renderable; 
+		if( visibleObjects->count(rend) == 0)
+		{
+			visibleObjects->insert(rend);
+
+			if( materialRestoreMap->count(rend) == 0)
+			{
+				(*materialRestoreMap)[rend] = rend->getMaterial().getPointer()->getName();
+				String materialToSet = materialName;
+				if(triggerName != "")//we have a trigger here
+				{
+					String triggeredMaterial = 
+						((OgreTechniqueGroup*)rend->getRenderTechniqueGroup())
+							->getMaterialNameForTrigger(triggerName);
 					if(triggeredMaterial != "")//a trigger is associated
 						materialToSet = triggeredMaterial;
+				}				
+				rend->setMaterialName(materialToSet);
+			}
+		}
+	}
+
+};
+
+class RenderQueueFill
+{
+public:	
+
+	inline void Fill(	RenderQueue* rq, //the queue to add the renderables to
+						bool shadowcastersonly,
+						std::list<Renderable*>* renderables = 0, //if not null add only these materials
+						String materialName = "", //material to set, if "" leave original
+						std::map<Renderable*, String>* visibleObjects = 0, //map to restore materials later
+						String trigger = "") //trigger
+	{
+		if(renderables == 0)
+		{
+			SceneManager *sm = Root::getSingleton()._getCurrentSceneManager();
+			SceneManager::MovableObjectIterator it =  sm->getMovableObjectIterator("Entity");
+			while(it.hasMoreElements())
+			{
+				MovableObject* o = it.getNext();
+				Entity* e = (Entity*) o;
+				if( (!shadowcastersonly || e->getCastShadows()) && e->isVisible())
+				{
+					for (int i = 0; i < e->getNumSubEntities(); i++)
+					{
+						SubEntity* sube = e->getSubEntity(i);
+
+						if(materialName != "")
+						{
+							String materialToSet = materialName;
+
+							if(trigger != "")
+							{
+								String triggeredMaterial = 
+								((OgreTechniqueGroup*)sube->getRenderTechniqueGroup())
+									->getMaterialNameForTrigger(trigger);
+								if(triggeredMaterial != "")//a trigger is associated
+								materialToSet = triggeredMaterial;
+							}
+
+							if(visibleObjects->count(sube) == 0)
+								(*visibleObjects)[sube] = 
+									sube->getMaterial().getPointer()->getName();
+							sube->setMaterialName(materialToSet);							
+						}
+						rq->addRenderable(sube);						
+					}
 				}
-			}				
-			rend->setMaterialName(materialToSet);
+			}
 		}
 	}
-	bool visit(const Pass* p){return true;}
-	void visit(const RenderablePass* rp)
-	{
-		Renderable* rend = rp->renderable; 
-		
-		if( visibleObjects->count(rend) == 0)
-		{
-			(*visibleObjects)[rend] = rend->getMaterial().getPointer()->getName();
-			String materialToSet = materialName;
-			if(triggerName != "")//we have a trigger here
-			{
-				String triggeredMaterial = 
-					((OgreTechniqueGroup*)rend->getRenderTechniqueGroup())
-						->getMaterialNameForTrigger(triggerName);
-				if(triggeredMaterial != "")//a trigger is associated
-					materialToSet = triggeredMaterial;
-			}				
-			rend->setMaterialName(materialToSet);
-		}
-	}
-
 };
 
@@ -154,5 +220,8 @@
 	RenderQueue::QueueGroupIterator queueIt = rq->_getQueueGroupIterator();
    
-    while (queueIt.hasMoreElements())
+	visibleObjects.clear();
+	MaterialSetVisitor visitor(materialName, &materialRestoreMap, &visibleObjects, triggerName);	
+	
+	while (queueIt.hasMoreElements())
     {
         RenderQueueGroup* pGroup = queueIt.getNext();
@@ -165,5 +234,4 @@
 			const QueuedRenderableCollection& solids = pPriorityGrp->getSolidsBasic();
 			const QueuedRenderableCollection& transparents = pPriorityGrp->getTransparents();
-			MaterialSetVisitor visitor(materialName, &visibleObjects, triggerName);	
 			solids.acceptVisitor(&visitor, QueuedRenderableCollection::OM_PASS_GROUP);
 			if(!solidsonly)
@@ -175,10 +243,10 @@
 	rq->clear();
 
-	std::map<Renderable*, String>::iterator it = visibleObjects.begin();
-	std::map<Renderable*, String>::iterator itend = visibleObjects.end();
+	std::set<Renderable*>::iterator it = visibleObjects.begin();
+	std::set<Renderable*>::iterator itend = visibleObjects.end();
 
 	while(it != itend)
 	{
-		rq->addRenderable((*it).first);
+		rq->addRenderable((*it));
 		it++;
 	}
@@ -201,5 +269,4 @@
 	}
 
-	//sm->_findVisibleObjectsOC(cam, cam->getViewport(), false, false, shadowcastersonly);
 	sm->_findVisibleObjects(cam, shadowcastersonly);
 	
@@ -207,10 +274,22 @@
 }
 
+void OgreRenderingRun::setMaterialForRenderables(String& materialName, bool shadowcastersonly, String triggerName)
+{
+	SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
+	sm->setFindVisibleObjects(false);
+
+	RenderQueueFill RQF;
+	RenderQueue* rq = Root::getSingleton()._getCurrentSceneManager()->getRenderQueue();
+	rq->clear();
+	visibleObjects.clear();
+	RQF.Fill(rq, shadowcastersonly, 0, materialName, &materialRestoreMap, triggerName);
+}
+
 void OgreRenderingRun::restoreMaterials()
 {
 	SceneManager* sm = Ogre::Root::getSingleton()._getCurrentSceneManager();
 	
-	std::map<Renderable*, String>::iterator it = visibleObjects.begin();
-	std::map<Renderable*, String>::iterator itend = visibleObjects.end();
+	std::map<Renderable*, String>::iterator it = materialRestoreMap.begin();
+	std::map<Renderable*, String>::iterator itend = materialRestoreMap.end();
 
 	while(it != itend)
@@ -220,5 +299,5 @@
 	}
 	
-	visibleObjects.clear();
+	materialRestoreMap.clear();
 
 	sm->setFindVisibleObjects(true);
@@ -246,7 +325,8 @@
 	sm->getRenderQueue()->clear();
 	sm->getRenderQueue()->addRenderable(fullScreenQuadEntity->getSubEntity(0));
+	bool lastFindVisible = sm->getFindVisibleObjects();
 	sm->setFindVisibleObjects(false);
 	target->update();
-	sm->setFindVisibleObjects(true);
+	sm->setFindVisibleObjects(lastFindVisible);
 }
 
@@ -329,7 +409,8 @@
 	sm->getRenderQueue()->clear();
 	sm->getRenderQueue()->addRenderable(fullscreenGrid->getSubEntity(0));
+	bool lastFindVisible = sm->getFindVisibleObjects();
 	sm->setFindVisibleObjects(false);
 	rt->update();
-	sm->setFindVisibleObjects(true);
+	sm->setFindVisibleObjects(lastFindVisible);
 
 }
@@ -373,8 +454,9 @@
 	sm->getRenderQueue()->clear();
 	sm->getRenderQueue()->addRenderable(pixelSprites);
+	bool lastFindVisible = sm->getFindVisibleObjects();
 	sm->setFindVisibleObjects(false);
 	rt->update();
-	sm->setFindVisibleObjects(true);
-		
-}
-
+	sm->setFindVisibleObjects(lastFindVisible);
+		
+}
+
Index: GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 2366)
+++ GTP/trunk/Lib/Illum/IllumModule/OgreIllumModule/src/RenderingRuns/OgreDepthShadowMapRenderingRun.cpp	(revision 2375)
@@ -84,7 +84,24 @@
 		//updateDepthCubeFace(currentFace);
 		//currentFace = (currentFace+1)%6;
+		
+
+		//Timer* t = Root::getSingleton().getTimer();
+		//unsigned long  start =  t->getMicroseconds();		
+		//setMaterialForRenderables(materialName, true);				
+		//unsigned long  end =  t->getMicroseconds();
+		//double elapsed = (double) (end - start) / 1000.0;
+		//LogManager::getSingleton().logMessage(String("MaterialSetTime: ") + name + " : " + StringConverter::toString(Real(elapsed)) + " ms");
+		//visibleObjects.clear();
 
 		for(int i = 0; i < 6 ; i++)
 			updateDepthCubeFace(i);
+
+		//start =  t->getMicroseconds();
+		
+		restoreMaterials();
+
+		//end =  t->getMicroseconds();
+		//elapsed = (double) (end - start) / 1000000.0;
+		//LogManager::getSingleton().logMessage(String("MaterialRestoreTime: ") + name + " : " +  StringConverter::toString(Real(elapsed)) + " ms");	
 	}
 	else
@@ -95,18 +112,20 @@
 
 void OgreDepthShadowMapRenderingRun::updateDepthCubeFace(int facenum)
-{
+{	
+	Vector3 lightpos = light->getDerivedPosition();			
 	String triggerName = "ILLUM_TRIGGER_SM_POINT";	
-
-	Vector3 lightpos = light->getDerivedPosition();			
-	
+		
 	RenderTarget* rt = depthMapTexture->getBuffer(facenum, 0).getPointer()->getRenderTarget();	
 	Camera*  cam = 	rt->getViewport(0)->getCamera();
-	cam->setPosition(lightpos);	
 	cam->setFarClipDistance(lightFarPlane);
-
+	cam->setPosition(lightpos);
+	
+	
 	setMaterialForVisibles(materialName, cam, true, false, triggerName);
+	
 	rt->update();
-	restoreMaterials();
-
+
+//	restoreMaterials();
+	
 	//rt->writeContentsToFile("shadowmap_" + StringConverter::toString(facenum) + ".dds");
 
@@ -120,6 +139,5 @@
 		
 		//rt->writeContentsToFile("shadowmap_blurred_" + StringConverter::toString(facenum) + ".dds");
-	}
-	
+	}	
 }
 
