Index: trunk/VUT/work/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp	(revision 97)
+++ trunk/VUT/work/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp	(revision 115)
@@ -29,6 +29,4 @@
 namespace Ogre {
 
-int D3D9HardwareOcclusionQuery::m_Skip = 0;
-
 /**
   * This is a class that is the DirectX9 implementation of 
@@ -36,30 +34,31 @@
   *
   * @author Lee Sandberg
+	*
+	* Updated on 12/7/2004 by Chris McGuirk
   */
 
 /**
   * Default object constructor
-  * 
   */
 D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery( IDirect3DDevice9* pD3DDevice ) 
 { 
-	m_pD3DDevice = pD3DDevice;
-	m_uintPixelCount = 0; 
-	m_SkipCounter = 0;
-	m_bHWOcclusionSupport = false;
+		mpDevice = pD3DDevice;
+		mPixelCount = 0; 
+		mSkipCounter = 0;
+		mSkipInterval = 0;
+		mHasOcclusionSupport = false;
 
-	HRESULT hr = m_pD3DDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &m_pD3DQuery);
+		// create the occlusion query
+		HRESULT hr = mpDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &mpQuery);
+
 	if ( hr != D3D_OK ) 
 	{
-		//OGRE_EXCEPT(hr, "D3D9HardwareOcclusionQuery couldn't create hardware occlusion query object.", 
-        //        "D3D9HardwareOcclusionQuery::D3D9HardwareOcclusionQuery");
-		m_bHWOcclusionSupport = false;
+			mHasOcclusionSupport = false;
 	}
 	else 
 	{
-		m_bHWOcclusionSupport = true;
+			mHasOcclusionSupport = true;
 	}
 }
-
 
 /**
@@ -68,8 +67,7 @@
 D3D9HardwareOcclusionQuery::~D3D9HardwareOcclusionQuery() 
 { 
-	SAFE_RELEASE( m_pD3DQuery ); 
+		SAFE_RELEASE(mpQuery); 
 }
 
-#ifndef GTP_VISIBILITY_MODIFIED_OGRE
 //------------------------------------------------------------------
 // Occlusion query functions (see base class documentation for this)
@@ -77,61 +75,45 @@
 void D3D9HardwareOcclusionQuery::beginOcclusionQuery() 
 { 
-	if( m_bHWOcclusionSupport )		// Make it fail silently if hardware occlusion isn't supported
+		// Make it fail silently if hardware occlusion isn't supported
+		if(mHasOcclusionSupport)
 	{
-		if  ( m_SkipCounter ==  m_Skip ) { m_SkipCounter = 0; };		// Counter starts at 0 again at m_Skip 
-		
-		if ( m_SkipCounter == 0 && m_uintPixelCount != 0 )	// New or none visable objects must allways be tested but visable objects can be skiped
+#ifndef GTP_VISIBILITY_MODIFIED_OGRE
+			// Counter starts at 0 again at mSkipInterval 
+			if(mSkipCounter == mSkipInterval) 
+#else
+			if (mSkipCounter >= mSkipInterval)  // otherwise no query ever issued if mSkipInterval = 0 !
+#endif // GTP_VISIBILITY_MODIFIED_OGRE
 		{
-			m_pD3DQuery->Issue(D3DISSUE_BEGIN); 
+				mSkipCounter = 0; 
+}
+			//std::stringstream d; d << "count: " << mSkipCounter;
+			//LogManager::getSingleton().logMessage(d.str());
+			if (mSkipCounter == 0)
+		{
+				mpQuery->Issue(D3DISSUE_BEGIN); 
 		}
 	}
+	
 }
-	
-void D3D9HardwareOcclusionQuery::endOcclusionQuery() 
-{ 
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
+
+	void D3D9HardwareOcclusionQuery::endOcclusionQuery() 
 	{
-		if (m_SkipCounter == 0 && m_uintPixelCount != 0 ) // New or none visable objects must allways be tested but visable objects can be skiped
-		{
-			m_pD3DQuery->Issue(D3DISSUE_END); 
-		}
-		m_SkipCounter++;								 // The skip counter is increased 
+		// Make it fail silently if hardware occlusion isn't supported
+		if(mHasOcclusionSupport)
+			{
+			if(mSkipCounter == 0)
+			{ 
+				mpQuery->Issue(D3DISSUE_END); 
+	}
+
+			// The skip counter is increased
+			mSkipCounter++; 
 	}
 }
 
 //------------------------------------------------------------------
-// This version of pullOcclusionQuery cases the DX9 API/Driver to flush all commands to the 3D card
-// to allow a fast result from the query, but at the cost of poorer batching of API calls to the card.
-// Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
-//--
-bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments) 
-{
-	HRESULT hr;
-	
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{
-		hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), D3DGETDATA_FLUSH );
-
-		if ( hr != S_OK ) 
-			{
-				return false; 
-			}
-			else 
-			{ 
-				m_uintPixelCount = *NumOfFragments; 
-				return true; 
-			}
-	}
-	else
-	{
-		m_uintPixelCount = 100000; // Fails quitlly if hardware occlusion is not supported - every object is visable
-		return true;
-	}
-}
-
-//------------------------------------------------------------------
-// This version of pullOcclusionQuery cases the DX9 API/Driver to not flush all commands to the 3D card
+	// This version of pullOcclusionQuery causes the DX9 API/Driver to not flush all commands to the 3D card
 // to allow a fast result from the query, but the batching of API calls to the card will be normal. 
-// But the query wont be processed until the card recives the query in the nexr batch.
+	// But the query wont be processed until the card recives the query in the next batch.
 // Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
 //--
@@ -140,97 +122,52 @@
 	HRESULT hr;
 
-	// TO DO: USE lockOpts= D3D9Mappings::get(options); instead of RS_OCCLUSIONQUERY enum
+		// Make it fail silently if hardware occlusion isn't supported
+		if(mHasOcclusionSupport)
+	{
+			DWORD d3dFlags = (flag == HWOCCLUSIONQUERY_FLUSH) ? D3DGETDATA_FLUSH : 0;
+	
+			// run until success (http://www.gamedev.net/reference/programming/features/occlusionculling/page2.asp)
+			while(hr = mpQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), d3dFlags) == S_FALSE);
 
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{
-	
-		switch( flag )
-		{
-			case HWOCCLUSIONQUERY_FLUSH : 	
-				hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), D3DGETDATA_FLUSH );
-			break;
-
-			case HWOCCLUSIONQUERY_NOFLUSH :	
-				hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), 0 );
-			break;
-		};
-
-
-
-		if ( hr != S_OK ) 
-			{
-				return false; 
+			mPixelCount = *NumOfFragments;
 			}
 			else 
 			{ 
-				m_uintPixelCount = *NumOfFragments; 
-				return true; 
+			// fail silently if not supported, assume visible i suppose
+			mPixelCount = 100000;
 			}
-	}
-	else 
-	{
-		m_uintPixelCount = 100000; // Fails quitlly if hardware occlusion is not supported - every object is visable
+
 		return true;
 	}
-}
-#else // GTP_VISIBILITY_MODIFIED_OGRE
+
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
 //------------------------------------------------------------------
-void D3D9HardwareOcclusionQuery::beginOcclusionQuery() 
-{ 
-	if( m_bHWOcclusionSupport )		// Make it fail silently if hardware occlusion isn't supported
+	bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, 
+													     const HW_OCCLUSIONQUERY flag ) 
 	{
-		m_pD3DQuery->Issue(D3DISSUE_BEGIN); 
+		HRESULT hr = S_OK;
+		DWORD queryFlag = (flag == HWOCCLUSIONQUERY_FLUSH) ? D3DGETDATA_FLUSH : 0;
+
+		if (mHasOcclusionSupport)	// In case hardware occlusion isn't supported
+		{	
+			do
+			{
+				hr = mpQuery->GetData(NumOfFragments, sizeof(NumOfFragments), queryFlag);
+	    	}
+			while(waitForResult && (hr == S_FALSE));
+		
+			mPixelCount = *NumOfFragments;
+		}
+		else
+		{
+			mPixelCount = 100000; // every object is visible
+		}
+	
+		return hr == S_OK;
 	}
-}
-//------------------------------------------------------------------
-void D3D9HardwareOcclusionQuery::endOcclusionQuery() 
-{ 
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{
-		m_pD3DQuery->Issue(D3DISSUE_END); 
-	}
-}
-//------------------------------------------------------------------
-bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult ) 
-{
-	return pullOcclusionQuery(NumOfFragments, waitForResult, HWOCCLUSIONQUERY_FLUSH);
-}
-
-//------------------------------------------------------------------
-bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, 
-												     const HW_OCCLUSIONQUERY flag  ) 
-{
-	DWORD queryFlag = flag == HWOCCLUSIONQUERY_FLUSH ? D3DGETDATA_FLUSH : 0;
-	// TO DO: USE lockOpts= D3D9Mappings::get(options); instead of RS_OCCLUSIONQUERY enum
-
-	return pullOcclusionQuery(NumOfFragments, waitForResult, queryFlag);
-}
-
-//------------------------------------------------------------------
-bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, 
-												     const DWORD queryFlag ) 
-{
-	HRESULT hr = S_OK;
-
-	if( m_bHWOcclusionSupport )	// In case hardware occlusion isn't supported
-	{	
-		do
-		{
-			hr = m_pD3DQuery->GetData( NumOfFragments, sizeof( NumOfFragments ), queryFlag );
-	    	}
-		while(waitForResult && (hr == S_FALSE));
-		
-		m_uintPixelCount = *NumOfFragments; 
-	}
-	else
-	{
-		m_uintPixelCount = 100000; // every object is visible
-	}
-	
-	return hr == S_OK;
-}
 
 
 #endif // GTP_VISIBILITY_MODIFIED_OGRE
 
-} // namespace OGre
+
+}
