Index: trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneManager.cpp
===================================================================
--- trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneManager.cpp	(revision 22)
+++ trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneManager.cpp	(revision 23)
@@ -5,4 +5,5 @@
 #include "OgreCamera.h"
 #include "OgreHardwareOcclusionQuery.h"
+#include "OgreWireBoundingBox.h"
 
 #include <windows.h>
@@ -12,5 +13,5 @@
 	//-----------------------------------------------------------------------
 	OcclusionCullingSceneManager::OcclusionCullingSceneManager(): 
-	mFrameID(1), mDistanceQueue(NULL), mVisibilityThreshold(0), mCurrentTestIdx(0),
+	mFrameId(1), mDistanceQueue(NULL), mVisibilityThreshold(0), mCurrentTestIdx(0),
 	mQueryMode(MODE_RENDER), mNumSceneNodes(0)
 	{		
@@ -25,5 +26,5 @@
 	{		
 		deleteQueries();
-		SceneManager::~SceneManager();
+		//SceneManager::~SceneManager();
 	}
 	//-----------------------------------------------------------------------
@@ -33,7 +34,10 @@
 		
 		mDistanceQueue->push(mSceneRoot);
+
+		mCurrentTestIdx = 0;
 		//renderZPass();
 		//renderCullFrustum();
-		renderCoherentWithQueue();
+		//renderCoherentWithQueue();
+		renderStopAndWait();
 		delete mDistanceQueue;
 
@@ -46,5 +50,5 @@
 		*/
 		
-		mFrameID ++;
+		mFrameId ++;
 	}
 	//-----------------------------------------------------------------------
@@ -88,5 +92,5 @@
 				{
 					// identify previously visible nodes
-					bool wasVisible = node->isNodeVisible() && (node->lastVisited() == mFrameID - 1);
+					bool wasVisible = node->isNodeVisible() && (node->lastVisited() == mFrameId - 1);
 					
 					// identify nodes that we cannot skip queries for
@@ -97,5 +101,5 @@
 
 					// update node's visited flag
-					node->setLastVisited(mFrameID);
+					node->setLastVisited(mFrameId);
 				
 					// skip testing previously visible interior nodes
@@ -144,5 +148,5 @@
 			// interesting for the visualization
 			//node->SetVisible(false);
-			node->setLastVisited(15);
+			node->setLastVisited(mFrameId);
 
 			if(mCameraInProgress->isVisible(node->_getWorldAABB()))
@@ -152,6 +156,9 @@
 				unsigned int visiblePixels;
 				// wait if result not available
-				mOcclusionQueries[mCurrentTestIdx++]->pullOcclusionQuery(&visiblePixels);
+				node->getOcclusionQuery()->pullOcclusionQuery(&visiblePixels);
 				
+				//char str[100]; sprintf(str, "number: %d, id: %d", (int)visiblePixels, mCurrentTestIdx);
+				//MessageBox( NULL, str, "this is my plugin", MB_OK | MB_ICONERROR | MB_TASKMODAL);
+
 				// node visible
 				if(visiblePixels > mVisibilityThreshold)
@@ -165,19 +172,51 @@
 	void OcclusionCullingSceneManager::issueOcclusionQuery(SceneNode *node, bool wasVisible)
 	{
+		// change state so the bounding box gets not actually rendered on the screen
+		setRenderingMode(MODE_RENDER);
+
 		// get next available test id
-		mOcclusionQueries[mCurrentTestIdx]->beginOcclusionQuery();
-	
-		// change state so the bounding box gets not actually rendered on the screen
-		setRenderingMode(MODE_QUERY);
+		HardwareOcclusionQuery *query = mOcclusionQueries[mCurrentTestIdx++];
+		node->setOcclusionQuery(query);
+
+		query->beginOcclusionQuery();
+		
+		WireBoundingBox box;
+		box.setupBoundingBox(node->_getWorldAABB());
+
 		//HACK: too slow
-        node->_addBoundingBoxToQueue(getRenderQueue());
-		SceneManager::_renderVisibleObjects();
-		getRenderQueue()->clear();
-		/* static RenderOperation ro;
-		   node->getRenderOperation(ro);
-		   ro.srcRenderable = node; 
-		   mDestRenderSystem->_render(ro); */
-		
-		mOcclusionQueries[mCurrentTestIdx++]->endOcclusionQuery();
+		
+		/*
+		static RenderOperation ro;
+
+		
+		static Matrix4 xform[256];
+		unsigned short numMatrices;
+
+        //node->_addBoundingBoxToQueue(getRenderQueue());
+		//SceneManager::_renderVisibleObjects();
+		//getRenderQueue()->clear();
+
+		// Set world transformation
+		/*box.getWorldTransforms(xform);
+		numMatrices = box.getNumWorldTransforms();
+		if (numMatrices > 1)
+		{
+			mDestRenderSystem->_setWorldMatrices(xform, numMatrices);
+		}
+		else
+		{
+	        mDestRenderSystem->_setWorldMatrix(*xform);
+	    }
+*/
+		// Issue view / projection changes if any
+		/*useRenderableViewProjMode(&box);
+
+		box.getRenderOperation(ro);
+		ro.srcRenderable = &box; 
+		mDestRenderSystem->_render(ro);*/
+		setPass(box.getTechnique()->getPass(0)); setRenderingMode(MODE_QUERY);
+		SceneManager::renderSingleObject(&box, box.getTechnique()->getPass(0), false);
+
+		query->endOcclusionQuery();
 	}
 
@@ -189,5 +228,7 @@
 		{
 			bool enabled = (mode == MODE_RENDER);
-
+			
+		//	char str[100]; sprintf(str, "number: %d", mode);
+		//	MessageBox( NULL, str, "this is my plugin", MB_OK | MB_ICONERROR | MB_TASKMODAL);
 			mDestRenderSystem->_setColourBufferWriteEnabled(enabled, 
 				enabled, enabled, enabled);
@@ -231,19 +272,18 @@
 	{
 		int numnodes = countSceneNodes(mSceneRoot);
-		//char str[100]; sprintf(str, "number: %d", mNumSceneNodes);
-		//MessageBox( NULL, str, "this is my plugin", MB_OK | MB_ICONERROR | MB_TASKMODAL);
 		
 		//-- initialise occlusion queries.
 		if(numnodes != mNumSceneNodes)
 		{
+			deleteQueries();
 			mNumSceneNodes = numnodes;
 
-			deleteQueries();
-
-			for(int i=0; i < mNumSceneNodes; i++)
+			for(unsigned int i=0; i < mNumSceneNodes; i++)
 			{
 				HardwareOcclusionQuery *hw = mDestRenderSystem->createHardwareOcclusionQuery();
 				mOcclusionQueries.push_back(hw);
 			}
+			//char str[100]; sprintf(str, "number: %d", mNumSceneNodes);
+			//MessageBox( NULL, str, "this is my plugin", MB_OK | MB_ICONERROR | MB_TASKMODAL);
 		}
 
@@ -286,5 +326,5 @@
 	void OcclusionCullingSceneManager::deleteQueries()
 	{
-		for(int i=0; i < mNumSceneNodes; i++)
+		for(unsigned int i=0; i < mNumSceneNodes; i++)
 			delete mOcclusionQueries[i];
 
