Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeSceneManager.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeSceneManager.h	(revision 1182)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeSceneManager.h	(revision 1185)
@@ -20,253 +20,257 @@
 namespace Ogre
 {
-	class KdTreeSceneNode;
-	class WireBoundingBox;
-	//typedef std::list < WireBoundingBox * > WBBList;
-
-	class KdTreeSceneManager : public SceneManager
+
+class KdTreeSceneNode;
+//class WireBoundingBox;
+
+class KdTreeSceneManager : public SceneManager
+{
+public:
+	KdTreeSceneManager(const String& name, GtpVisibility::VisibilityManager *vm);
+	~KdTreeSceneManager(void);
+
+	virtual const String& getTypeName(void) const;
+
+	//typedef std::list<KdTreeSceneNode *> NodeList;
+	//typedef std::list<WireBoundingBox *> BoxList;
+
+	/************************************************************************/
+	/* Functions overridden form SceneManager for KdTree                    */
+	/************************************************************************/
+
+	/** Override from SceneManager to create SceneNodes as instance of KdTreeSceneNode
+	*/
+	virtual SceneNode* createSceneNode(void);
+	/** Override from SceneManager to create SceneNodes as instance of KdTreeSceneNode
+	*/
+	virtual SceneNode* createSceneNode(const String& name);
+
+	/** Override from Scenemanager, employ kd-tree based culling or CHC
+	*/
+	virtual void _findVisibleObjects(Camera *cam, bool onlyShadowCasters);
+
+	virtual bool setOption(const String& strKey, const void* pValue);
+	virtual bool getOption(const String& strKey, void* pDestValue);
+
+	bool getOptionValues(const String & key, StringVector &refValueList);
+	bool getOptionKeys(StringVector &refKeys);
+
+	/** Overide from scene manager to destroy kdtree properly (before the scene graph is destroyed)
+	*/
+	virtual void clearScene()
 	{
-	public:
-		KdTreeSceneManager(const String& name, GtpVisibility::VisibilityManager *vm);
-		~KdTreeSceneManager(void);
-
-		virtual const String& getTypeName(void) const;
-
-
-		/************************************************************************/
-		/* Functions overridden form SceneManager for KdTree                    */
-		/************************************************************************/
-
-		/** Override from SceneManager to create SceneNodes as instance of KdTreeSceneNode
-		*/
-		virtual SceneNode* createSceneNode(void);
-		/** Override from SceneManager to create SceneNodes as instance of KdTreeSceneNode
-		*/
-		virtual SceneNode* createSceneNode(const String& name);
-
-		/** Override from Scenemanager, employ kd-tree based culling or CHC
-		*/
-		virtual void _findVisibleObjects(Camera *cam, bool onlyShadowCasters);
-
-		virtual bool setOption(const String& strKey, const void* pValue);
-		virtual bool getOption(const String& strKey, void* pDestValue);
-
-		bool getOptionValues(const String & key, StringVector &refValueList);
-		bool getOptionKeys(StringVector &refKeys);
-
-		/** Overide from scene manager to destroy kdtree properly (before the scene graph is destroyed)
-		*/
-		virtual void clearScene()
-		{
-			// DEBUG
-			//if (mKdTree)
-			//	mKdTree->dump();
-
-			// must happen before actual scene is cleared
-			OGRE_DELETE(mKdTree);
-
-			SceneManager::clearScene();
-		}
-
-		/************************************************************************/
-		/* Functions overridden from SceneManager for CHC                       */
-		/************************************************************************/
-
-		/** Override from SceneManager so that sub entities can be assigned an id for item buffer.
-		*/
-		Entity* createEntity(const String& entityName, const String& meshName);
-
-		/** Override from scene manager to set up culling manager
-		*/
-		virtual void _updateSceneGraph(Camera* cam);
-
-		/** Override from SceneManager, employ normal rendering or CHC
-		*/
-		virtual void _renderVisibleObjects();
-
-		/** Override pass so we can do the z-fail pass.
-		*/
-		const Pass* _setPass(Pass* pass);
-
-		/** Render a queue group. 
-		Override so we can handle delayed rendering of transparent objects
-		*/
-		void renderBasicQueueGroupObjects(RenderQueueGroup* pGroup, 
-			QueuedRenderableCollection::OrganisationMode om);
-
-		/** Override from scene manager
-		*/
-		void _renderQueueGroupObjects(RenderQueueGroup* pGroup,
-			QueuedRenderableCollection::OrganisationMode om);
-
-		/** Override from SceneManager so we can skip all but first pass for depth pass. 
-		*/
-		bool validatePassForRendering(Pass* pass);
-
-		/** Override from SceneManager because idontknow
-		*/
-		void renderAdditiveStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup,
-			QueuedRenderableCollection::OrganisationMode om);
-		void renderModulativeStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup,
-			QueuedRenderableCollection::OrganisationMode om);
-
-
-		/************************************************************************/
-		/* Functions which are specific to the KdTree                           */
-		/************************************************************************/
-		
-		/** Update the KdTree with the node (more or less disabled now)
-		*/
-		virtual void _updateNode(KdTreeSceneNode *node); // TODO: 
-
-		/** Show or hide the bounding boxes of KdTree nodes - obsolete, use options
-		*/
-		virtual void setShowBoxes(bool showboxes);
-		/** Tell if show boxes is enabled - obsolete, use options
-		*/
-		virtual bool getShowBoxes(void) const;
-
-		/************************************************************************/
-		/* Functions for CHC                                                    */
-		/************************************************************************/
-
-		/** Render a list of scenenodes
-		*/
-		virtual void _renderNodes(const KdRenderableList& nodelist, Camera * cam,
-			bool onlyShadowCasters, int leavePassesInQueue);
-
-		/** Sets the visibility manager.
-		@param visManager the visibility manager
-		*/
-		void setVisibilityManager(GtpVisibility::VisibilityManager *visManager);
-		/** See set.
-		*/
-		GtpVisibility::VisibilityManager *getVisibilityManager();
-
-		/** Returns pointer to visibility manager.
-		*/
-		GtpVisibility::VisibilityManager *GetVisibilityManager();
-
-		/** Returns hierarchy interface.
-		*/
-		KdTreeHierarchyInterface *GetHierarchyInterface();
-
-		/** Creates material for depth pass, e.g., a pass that only fills the depth buffer. 
-		*/
-		void InitDepthPass();
-		/** Creates material for item buffer. 
-		*/
-		void InitItemBufferPass();
-		/** Fills render queue so that a visualization can be rendered. 
-		*/
-		void PrepareVisualization(Camera *cam);
-		/** Initialises necessary parameters for hierarchical visibility culling.
-		*/
-		void InitVisibilityCulling(Camera *cam);
-
-		/** Writes out stats into the Ogre log file.
-		*/
-		void WriteLog();
-
-
-
-	protected:
-		/************************************************************************/
-		/* CHC-specific options & members                                       */
-		/************************************************************************/
-		// pointer to the visibility manager
-		GtpVisibility::VisibilityManager *mVisibilityManager;
-
-		// the hierarchy interface for CHC
-		KdTreeHierarchyInterface *mHierarchyInterface;
-
-		// if hierarchical culling is currently in use
-		bool mIsHierarchicalCulling;
-
-		// if a visualization of the hierarchical culling is shown
-		bool mShowVisualization;
-		
-		// if the culled nodes are indicated in the visualization
-		bool mVisualizeCulledNodes;
-
-		// consecutive number for sub-entities
-		int mCurrentEntityId;
-
-		// flag for passes which should not be deleted from queue during first traversal
-		int mLeavePassesInQueue;
-
-		// if symbols for the nodes are shown in the visualization
-		bool mRenderNodesForViz;
-		// if content of the nodes is shown in the visualization
-		bool mRenderNodesContentForViz;
-		/// render transparents after the hierarchical traversal
-		bool mDelayRenderTransparents;
-		
-		// if transparent object are considered for item buffer visibility
-		bool mRenderTransparentsForItemBuffer;
-		// Always execute the vertex program of a pass, e.g., for the depth pass or item buffer
-		bool mExecuteVertexProgramForAllPasses;
-
-		// the depth pass (no lighting, just filling the depth buffer)
-		Pass *mDepthPass;
-		// use a depth pass (i.e., fill only the depth buffer in the first pass)
-		bool mUseDepthPass;
-		// flag indicating if we currently render the depth pass
-		bool mIsDepthPassPhase;
-		// if depth write should be enabled
-		bool mEnableDepthWrite;
-		// if transparents are skipped during rendering
-		bool mSkipTransparents;
-
-		// the item buffer pass (render items color-coded)
-		Pass *mItemBufferPass;
-		// if we use an item buffer for rendering (i.e., object ids as color codes
-		bool mUseItemBuffer;
-		// if we currently render the item buffer 
-		bool mIsItemBufferPhase;
-
-		/************************************************************************/
-		/* Kd-Tree specific options & members                                   */
-		/************************************************************************/
-		// maximum depth of the kdtree
-		int mMaxDepth;
-
-		// the kdtree which holds the scene
-		KdTree *mKdTree;
-
-		// if bounding boxes of kdtree nodes are shown
-		bool mShowBoxes;
-
-		// the method/algorithm used when rendering the scene
-		KdTree::RenderMethod mRenderMethod;
-
-		// the method of building the tree
-		KdTree::BuildMethod mBuildMethod;
+		// DEBUG
+		//if (mKdTree)
+		//	mKdTree->dump();
+
+		// must happen before actual scene is cleared
+		OGRE_DELETE(mKdTree);
+
+		SceneManager::clearScene();
+	}
+
+	/************************************************************************/
+	/* Functions overridden from SceneManager for CHC                       */
+	/************************************************************************/
+
+	/** Override from SceneManager so that sub entities can be assigned an id for item buffer.
+	*/
+	Entity* createEntity(const String& entityName, const String& meshName);
+
+	/** Override from scene manager to set up culling manager
+	*/
+	virtual void _updateSceneGraph(Camera* cam);
+
+	/** Override from SceneManager, employ normal rendering or CHC
+	*/
+	virtual void _renderVisibleObjects();
+
+	/** Override pass so we can do the z-fail pass.
+	*/
+	const Pass* _setPass(Pass* pass);
+
+	/** Render a queue group. 
+	Override so we can handle delayed rendering of transparent objects
+	*/
+	void renderBasicQueueGroupObjects(RenderQueueGroup* pGroup, 
+		QueuedRenderableCollection::OrganisationMode om);
+
+	/** Override from scene manager
+	*/
+	void _renderQueueGroupObjects(RenderQueueGroup* pGroup,
+		QueuedRenderableCollection::OrganisationMode om);
+
+	/** Override from SceneManager so we can skip all but first pass for depth pass. 
+	*/
+	bool validatePassForRendering(Pass* pass);
+
+	/** Override from SceneManager because idontknow
+	*/
+	void renderAdditiveStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup,
+		QueuedRenderableCollection::OrganisationMode om);
+	void renderModulativeStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup,
+		QueuedRenderableCollection::OrganisationMode om);
+
+
+	/************************************************************************/
+	/* Functions which are specific to the KdTree                           */
+	/************************************************************************/
+	
+	/** Update the KdTree with the node (more or less disabled now)
+	*/
+	virtual void _updateNode(KdTreeSceneNode *node); // TODO: 
+
+	/** Show or hide the bounding boxes of KdTree nodes - obsolete, use options
+	*/
+	virtual void setShowBoxes(bool showboxes);
+	/** Tell if show boxes is enabled - obsolete, use options
+	*/
+	virtual bool getShowBoxes(void) const;
+
+	/************************************************************************/
+	/* Functions for CHC                                                    */
+	/************************************************************************/
+
+	/** Render a list of scenenodes
+	*/
+	virtual void _renderNodes(const KdRenderableList& nodelist, Camera * cam,
+		bool onlyShadowCasters, int leavePassesInQueue);
+
+	/** Sets the visibility manager.
+	@param visManager the visibility manager
+	*/
+	void setVisibilityManager(GtpVisibility::VisibilityManager *visManager);
+	/** See set.
+	*/
+	GtpVisibility::VisibilityManager *getVisibilityManager();
+
+	/** Returns pointer to visibility manager.
+	*/
+	GtpVisibility::VisibilityManager *GetVisibilityManager();
+
+	/** Returns hierarchy interface.
+	*/
+	KdTreeHierarchyInterface *GetHierarchyInterface();
+
+protected:
+	/** Creates material for depth pass, e.g., a pass that only fills the depth buffer. 
+	*/
+	void InitDepthPass();
+	/** Creates material for item buffer. 
+	*/
+	void InitItemBufferPass();
+	/** Fills render queue so that a visualization can be rendered. 
+	*/
+	void PrepareVisualization(Camera *cam);
+	/** Initialises necessary parameters for hierarchical visibility culling.
+	*/
+	void InitVisibilityCulling(Camera *cam);
+
+	/** Writes out stats into the Ogre log file.
+	*/
+	void WriteLog();
+
+
+	/************************************************************************/
+	/* CHC-specific options & members                                       */
+	/************************************************************************/
+	// pointer to the visibility manager
+	GtpVisibility::VisibilityManager *mVisibilityManager;
+
+	// the hierarchy interface for CHC
+	KdTreeHierarchyInterface *mHierarchyInterface;
+
+	// if hierarchical culling is currently in use
+	bool mIsHierarchicalCulling;
+
+	// if a visualization of the hierarchical culling is shown
+	bool mShowVisualization;
+	
+	// if the culled nodes are indicated in the visualization
+	bool mVisualizeCulledNodes;
+
+	// consecutive number for sub-entities
+	int mCurrentEntityId;
+
+	// flag for passes which should not be deleted from queue during first traversal
+	int mLeavePassesInQueue;
+
+	// if symbols for the nodes are shown in the visualization
+	bool mRenderNodesForViz;
+	// if content of the nodes is shown in the visualization
+	bool mRenderNodesContentForViz;
+	/// render transparents after the hierarchical traversal
+	bool mDelayRenderTransparents;
+	
+	// if transparent object are considered for item buffer visibility
+	bool mRenderTransparentsForItemBuffer;
+	// Always execute the vertex program of a pass, e.g., for the depth pass or item buffer
+	bool mExecuteVertexProgramForAllPasses;
+
+	// the depth pass (no lighting, just filling the depth buffer)
+	Pass *mDepthPass;
+	// use a depth pass (i.e., fill only the depth buffer in the first pass)
+	bool mUseDepthPass;
+	// flag indicating if we currently render the depth pass
+	bool mIsDepthPassPhase;
+	// if depth write should be enabled
+	bool mEnableDepthWrite;
+	// if transparents are skipped during rendering
+	bool mSkipTransparents;
+
+	// the item buffer pass (render items color-coded)
+	Pass *mItemBufferPass;
+	// if we use an item buffer for rendering (i.e., object ids as color codes
+	bool mUseItemBuffer;
+	// if we currently render the item buffer 
+	bool mIsItemBufferPhase;
+
+	// remember visited scene nodes for viz
+	KdRenderableList mVisibleNodes;
+
+	/************************************************************************/
+	/* Kd-Tree specific options & members                                   */
+	/************************************************************************/
+	// maximum depth of the kdtree
+	int mMaxDepth;
+
+	// the kdtree which holds the scene
+	KdTree *mKdTree;
+
+	// if bounding boxes of kdtree nodes are shown
+	bool mShowBoxes;
+
+	// the method/algorithm used when rendering the scene
+	KdTree::RenderMethod mRenderMethod;
+
+	// the method of building the tree
+	KdTree::BuildMethod mBuildMethod;
 #ifdef KDTREE_DEBUG
-		// bounding boxes of kd nodes will be highlighted on this level of the kd tree
-		int mHighlighLevel;
-		// if all bounding boxes shall be displayed, not only the highlighted level
-		bool mShowAllBoxes;
+	// bounding boxes of kd nodes will be highlighted on this level of the kd tree
+	int mHighlighLevel;
+	// if all bounding boxes shall be displayed, not only the highlighted level
+	bool mShowAllBoxes;
 #endif
+};
+
+
+
+/// Factory for KdTreeSceneManager
+class KdTreeSceneManagerFactory : public SceneManagerFactory
+{
+protected:
+	void initMetaData(void) const;
+	GtpVisibility::VisibilityManager *visManager;
+public:
+	KdTreeSceneManagerFactory(GtpVisibility::VisibilityManager * vm)
+	{
+		visManager = vm;
 	};
-
-
-
-	/// Factory for KdTreeSceneManager
-	class KdTreeSceneManagerFactory : public SceneManagerFactory
-	{
-	protected:
-		void initMetaData(void) const;
-		GtpVisibility::VisibilityManager *visManager;
-	public:
-		KdTreeSceneManagerFactory(GtpVisibility::VisibilityManager * vm)
-		{
-			visManager = vm;
-		};
-		~KdTreeSceneManagerFactory(void) {};
-		/// Factory type name
-		static const String FACTORY_TYPE_NAME;
-		SceneManager* createInstance(const String& instanceName);
-		void destroyInstance(SceneManager* instance);
-	};
+	~KdTreeSceneManagerFactory(void) {};
+	/// Factory type name
+	static const String FACTORY_TYPE_NAME;
+	SceneManager* createInstance(const String& instanceName);
+	void destroyInstance(SceneManager* instance);
+};
 
 } // namespace Ogre
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp	(revision 1183)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp	(revision 1185)
@@ -41,5 +41,5 @@
 mLeavePassesInQueue(0),
 mDelayRenderTransparents(true),
-mUseDepthPass(false),
+mUseDepthPass(true),
 mIsDepthPassPhase(false),
 mUseItemBuffer(false),
@@ -589,4 +589,6 @@
 		}
 	}
+
+	mVisibleNodes.clear();
 }
 
@@ -695,11 +697,11 @@
 			if (mUseDepthPass)
 			{
-				//NodeList::const_iterator it, it_end = mVisible.end();
-
-				////getRenderQueue()->clear();
-				//for (it = mVisible.begin(); it != it_end; ++ it)
-				//{
-				//	(*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
-				//}
+				KdRenderableList::const_iterator it, it_end = mVisibleNodes.end();
+
+				//getRenderQueue()->clear();
+				for (it = mVisibleNodes.begin(); it != it_end; ++ it)
+				{
+					(*it)->queueObjects(mCameraInProgress, getRenderQueue(), false);
+				}
 			}
 #endif	
@@ -722,10 +724,11 @@
 }
 
-// TODO: looks too easy, verify if it works 
 void KdTreeSceneManager::_renderNodes(const KdRenderableList& nodelist, Camera * cam, 
 	bool onlyShadowCasters, int leavePassesInQueue)
 {
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	_deleteRenderedQueueGroups(leavePassesInQueue); //try avoiding modified ogre code
+	RenderQueueGroup *currentGroup = 
+		getRenderQueue()->getQueueGroup(getRenderQueue()->getDefaultQueueGroup());
+	currentGroup->clear(leavePassesInQueue);
 #else
 	getRenderQueue()->clear();
@@ -737,4 +740,5 @@
 		if (!(*it)->isQueued(mHierarchyInterface->GetFrameId(), cam))
 		{
+			mVisibleNodes.push_back(*it);
 			(*it)->queueObjects(cam, getRenderQueue(), onlyShadowCasters);
 		}
@@ -742,6 +746,5 @@
 	}
 
-	SceneManager::_renderVisibleObjects();
-
+	_renderQueueGroupObjects(currentGroup, QueuedRenderableCollection::OM_PASS_GROUP);
 }
 //-----------------------------------------------------------------------
@@ -946,35 +949,36 @@
 		//}
 
-		if (mRenderNodesForViz || mRenderNodesContentForViz)
-		{
-			// HACK: change node material so it is better suited for visualization
-			MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
-			nodeMat->setAmbient(1, 1, 0);
-			nodeMat->setLightingEnabled(true);
-			nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
-
-			//for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
-			//{
-			//	if (mRenderNodesForViz)
-			//	{
-			//		// render the leaf nodes
-			//		if ((*it)->numAttachedObjects() && 
-			//			!(*it)->numChildren() && 
-			//			((*it)->getAttachedObject(0)->getMovableType() == "Entity") &&
-			//			(*it)->getAttachedObject(0)->isVisible())
-			//		{
-			//			getRenderQueue()->addRenderable((*it));
-			//		}
-
-			//		// addbounding boxes instead of node itself
-			//		//(*it)->_addBoundingBoxToQueue(getRenderQueue());
-			//	}
-			//	// add renderables itself
-			//	if (mRenderNodesContentForViz) 
-			//	{
-			//		(*it)->_addToRenderQueue(cam, getRenderQueue(), false);
-			//	}
-			//}
-		}	
+	if (mRenderNodesForViz || mRenderNodesContentForViz)
+	{
+		// HACK: change node material so it is better suited for visualization
+		MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
+		nodeMat->setAmbient(1, 1, 0);
+		nodeMat->setLightingEnabled(true);
+		nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
+
+		for (KdRenderableList::iterator it = mVisibleNodes.begin(); it != mVisibleNodes.end(); ++it)
+		{
+			if (mRenderNodesForViz)
+			{
+				KdTreeSceneNode * node = static_cast<KdTreeSceneNode *>(*it);
+				// render the leaf nodes
+				if (node->numAttachedObjects() && 
+					!node->numChildren() && 
+					(node->getAttachedObject(0)->getMovableType() == "Entity") &&
+					node->getAttachedObject(0)->isVisible())
+				{
+					getRenderQueue()->addRenderable(node);
+				}
+
+				// addbounding boxes instead of node itself
+				//(*it)->_addBoundingBoxToQueue(getRenderQueue());
+			}
+			// add renderables itself
+			if (mRenderNodesContentForViz) 
+			{
+				(*it)->queueObjects(cam, getRenderQueue(), false);
+			}
+		}
+	}	
 }
 //-----------------------------------------------------------------------
