Index: trunk/VUT/work/ogre_changes/OgreMain/include/OgreHardwareOcclusionQuery.h
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/include/OgreHardwareOcclusionQuery.h	(revision 61)
+++ trunk/VUT/work/ogre_changes/OgreMain/include/OgreHardwareOcclusionQuery.h	(revision 88)
@@ -103,5 +103,5 @@
 	  *        0 will generate all the levels till 1x1. [default: 0]
       */
-	virtual bool pullOcclusionQuery( unsigned int* NumOfFragments ) = 0;
+	virtual bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult ) = 0;
 
 	/**
@@ -112,5 +112,6 @@
 	  * In OpenGL mode it makes no difference if you specify OCCLUSIONQUERY_FLUSH or OCCLUSIONQUERY_NOFLUSH.
       */
-	virtual bool pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  ) = 0;
+	virtual bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, 
+									 const HW_OCCLUSIONQUERY flag  ) = 0;
 
 	/**
@@ -133,14 +134,4 @@
 	virtual void	setSkipRate( int skip ) = 0;
 	virtual int		getSkipRate() = 0;
-
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	/**
-	  *   
-	  * @Remarks This function returns true if the result of the 
-	  * query is already availbable, false otherwise.
-	  */
-	virtual bool resultAvailable() = 0;
-#endif // GTP_VISIBILITY_MODIFIED_OGRE
-
 //----------------------------------------------------------------------
 // Private members
Index: trunk/VUT/work/ogre_changes/RenderSystem/GL/include/OgreGLHardwareOcclusionQuery.h
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystem/GL/include/OgreGLHardwareOcclusionQuery.h	(revision 61)
+++ trunk/VUT/work/ogre_changes/RenderSystem/GL/include/OgreGLHardwareOcclusionQuery.h	(revision 88)
@@ -88,9 +88,8 @@
 	void beginOcclusionQuery();
 	void endOcclusionQuery();
-	bool pullOcclusionQuery( unsigned int* NumOfFragments);
-	bool pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  ); 
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	bool resultAvailable();
-#endif
+	bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult );
+	bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult,
+							 const HW_OCCLUSIONQUERY flag  ); 
+
 	unsigned int getLastQuerysPixelcount() { return m_uintPixelCount; }
 
Index: trunk/VUT/work/ogre_changes/RenderSystem/GL/src/OgreGLHardwareOcclusionQuery.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystem/GL/src/OgreGLHardwareOcclusionQuery.cpp	(revision 61)
+++ trunk/VUT/work/ogre_changes/RenderSystem/GL/src/OgreGLHardwareOcclusionQuery.cpp	(revision 88)
@@ -95,9 +95,5 @@
 	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
 	{
-		//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
-		//{
-			glBeginOcclusionQueryNV_ptr( m_uintQuery[0] );
-		//}
+		glBeginOcclusionQueryNV_ptr( m_uintQuery[0] );
 	}
 }
@@ -107,62 +103,45 @@
 	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
 	{
-		//if( m_SkipCounter == 0 && m_uintPixelCount != 0 ) // New or none visable objects must allways be tested but visable objects can be skiped
-		//{
-			glEndOcclusionQueryNV_ptr();
-		//}
-		//m_SkipCounter++;
+		glEndOcclusionQueryNV_ptr();
 	}
 }
 
-bool GLHardwareOcclusionQuery::pullOcclusionQuery( unsigned int * NumOfFragments) 
+bool GLHardwareOcclusionQuery::pullOcclusionQuery( unsigned int * NumOfFragments, const bool waitForResult) 
 {
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
+	unsigned int isAvailable = GL_TRUE;
+
+	if( m_bHWOcclusionSupport )	
 	{
-		glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, NumOfFragments );
+		if(!waitForResult)
+		{
+			glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_AVAILABLE_NV, &isAvailable );
+		}
+
+		if(isAvailable == GL_TRUE)
+		{
+			glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, NumOfFragments );
+		}
 	} 
 	else
 	{
-		*NumOfFragments = 100000;		// Fails quitlly -> every object tested is visable.
+		// In case hardware occlusion isn't supported, every object is set visible.
+		*NumOfFragments = 100000;		
 	}
 
 	m_uintPixelCount = *NumOfFragments; 
 
-	return true;
+	return isAvailable == GL_TRUE;	
 }
 
 //------------------------------------------------------------------
-// OpenGL dosn't use the flag, but to fulfil the abstract interface we need to include this function.
+// OpenGL doesn't use the flag, but to implement the abstract interface we need to include this function.
 // Using this function in OpenGL mode simple works the same way as calling the other function without the flag parameter,
 // but in DX9 it works differentlly, see notes in the DX9 implementation.
 //--
-bool GLHardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  ) 
+bool GLHardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, const HW_OCCLUSIONQUERY flag  ) 
 {
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{
-		glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, NumOfFragments );
-	}
-	else
-	{
-		*NumOfFragments = 100000;		// Fails quitlly -> every object tested is visable.
-	}
-
-	m_uintPixelCount = *NumOfFragments; 
-	
-	return true;
+	return pullOcclusionQuery(NumOfFragments, waitForResult);
 }
 
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-bool GLHardwareOcclusionQuery::resultAvailable()
-{
-	unsigned int result = GL_TRUE;
-
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{
-		glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, &result );
-	}
-
-	return result == GL_TRUE;
-}
-#endif
 }
 
Index: trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/include/OgreD3D9HardwareOcclusionQuery.h
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/include/OgreD3D9HardwareOcclusionQuery.h	(revision 61)
+++ trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/include/OgreD3D9HardwareOcclusionQuery.h	(revision 88)
@@ -71,11 +71,11 @@
 	void beginOcclusionQuery();	
 	void endOcclusionQuery();
-	bool pullOcclusionQuery( unsigned int* NumOfFragments);
-	bool pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag  );
+	bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult );
+	bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult,
+							 const HW_OCCLUSIONQUERY flag  ); 
+
 	unsigned int getLastQuerysPixelcount() { return m_uintPixelCount; }
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	bool resultAvailable();
-#endif
-	// This functions are optional, it's a simple filter that simply skipps some hardware occlusion tests on visable objects only
+	
+	// This functions are optional, it's a simple filter that simply skips some hardware occlusion tests on visable objects only
 	// It's easy to use if you don't have to keep track on which objects are visable (can be skipped) and what objects arn't visable..
 	// (None visable objects and object you introduce for the first time have allways to be tested allthough the cheepest possible 
@@ -93,5 +93,5 @@
 
 
-	void setSkipRate( int skip ) { m_Skip = skip; }			// Using 2 only 50 % of the tests are actully made and 3 results in only 33% of the tests. So on.
+	void setSkipRate( int skip ) { m_Skip = skip; }			// Using 2 only 50 % of the tests are actually made and 3 results in only 33% of the tests. So on.
 	int	 getSkipRate() { return m_Skip; } 
 
@@ -100,4 +100,7 @@
 //--
 protected:
+
+	bool pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, 
+						     const DWORD queryFlag );
 
 	unsigned int		m_uintPixelCount;		
Index: trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp	(revision 61)
+++ trunk/VUT/work/ogre_changes/RenderSystem/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp	(revision 88)
@@ -78,22 +78,13 @@
 	if( m_bHWOcclusionSupport )		// Make it fail silently if hardware occlusion isn't supported
 	{
-		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
-		{
-			m_pD3DQuery->Issue(D3DISSUE_BEGIN); 
-		}
+		m_pD3DQuery->Issue(D3DISSUE_BEGIN); 
 	}
 }
-	
+//------------------------------------------------------------------
 void D3D9HardwareOcclusionQuery::endOcclusionQuery() 
 { 
 	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
 	{
-		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 
+		m_pD3DQuery->Issue(D3DISSUE_END); 
 	}
 }
@@ -104,27 +95,7 @@
 // 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) 
+bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult ) 
 {
-	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;
-	}
+	return pullOcclusionQuery(NumOfFragments, waitForResult, HWOCCLUSIONQUERY_FLUSH);
 }
 
@@ -135,59 +106,34 @@
 // 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, const HW_OCCLUSIONQUERY flag  ) 
+bool D3D9HardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments, const bool waitForResult, 
+												     const HW_OCCLUSIONQUERY flag  ) 
 {
-	HRESULT hr;
-
+	DWORD queryFlag = flag == HWOCCLUSIONQUERY_FLUSH ? D3DGETDATA_FLUSH : 0;
 	// TO DO: USE lockOpts= D3D9Mappings::get(options); instead of RS_OCCLUSIONQUERY enum
 
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
+	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
+	}
 	
-		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; 
-			}
-			else 
-			{ 
-				m_uintPixelCount = *NumOfFragments; 
-				return true; 
-			}
-	}
-	else 
-	{
-		m_uintPixelCount = 100000; // Fails quitlly if hardware occlusion is not supported - every object is visable
-		return true;
-	}
+	return hr == S_OK;
 }
-
-
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-
-bool D3D9HardwareOcclusionQuery::resultAvailable()
-{
-	bool result = true;
-
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{	//TODO: implement
-		//glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, &result );
-	}
-
-	return result;
-}
-
-#endif // GTP_VISIBILITY_MODIFIED_OGRE
-
-}
+} // namespace OGre
