Index: trunk/VUT/OcclusionCullingSceneManager/TestCulling/TestCullingApplication.cpp
===================================================================
--- trunk/VUT/OcclusionCullingSceneManager/TestCulling/TestCullingApplication.cpp	(revision 42)
+++ trunk/VUT/OcclusionCullingSceneManager/TestCulling/TestCullingApplication.cpp	(revision 44)
@@ -20,13 +20,13 @@
 #include "windows.h"
 
-RaySceneQuery* raySceneQuery = 0;
+//RaySceneQuery* mRaySceneQuery = 0;
 
 /***********************************************/
 /* TestCullingApplication implementation       */
 /***********************************************/
-TestCullingApplication::~TestCullingApplication()
-{
-	delete raySceneQuery;
-}
+/*TestCullingApplication::~TestCullingApplication()
+{
+	delete mRaySceneQuery;
+}*/
 //-----------------------------------------------------------------------
 void TestCullingApplication::createCamera( void )
@@ -57,5 +57,5 @@
 	l->setPosition(20,80,50);
 
-	// Fog
+	// --Fog
 	// NB it's VERY important to set this before calling setWorldGeometry 
 	// because the vertex program picked will be different
@@ -66,7 +66,7 @@
 	mWindow->getViewport(0)->setBackgroundColour(fadeColour);
 
-	 // Create a skybox
-     //mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 500, false);
-	 //mSceneMgr->setSkyDome( true, "Examples/CloudySky", 5, 8, 500, false );
+	// Create a skybox
+    //mSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox", 500, false);
+	//mSceneMgr->setSkyDome( true, "Examples/CloudySky", 5, 8, 500, false );
 
 	std::string terrain_cfg("terrain.cfg");
@@ -94,8 +94,8 @@
 	mCamera->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
 
-	raySceneQuery = mSceneMgr->createRayQuery(
-	Ray(mCamera->getPosition(), Vector3::NEGATIVE_UNIT_Y));
-
-	Entity *robotEnt = mSceneMgr->createEntity( "Robot", "robot.mesh" );
+	//mRaySceneQuery = mSceneMgr->createRayQuery(
+	//	Ray(mCamera->getPosition(), Vector3::NEGATIVE_UNIT_Y));
+
+	/*Entity *robotEnt = mSceneMgr->createEntity( "Robot", "robot.mesh" );
 	SceneNode *robotNode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "RobotNode", Vector3( 750, 25, 600 ));
 	robotNode->attachObject( robotEnt );
@@ -113,5 +113,5 @@
     ogreNode2->attachObject( ogreEnt2 );
 	ogreNode2->scale(.05,.05,.05);
-	ogreNode->yaw( Degree( 40 ) );
+	ogreNode->yaw( Degree( 40 ) );*/
 
 	// CEGUI setup
@@ -131,10 +131,4 @@
 
 	 CEGUI::MouseCursor::getSingleton().show( );
-     
-	/* CEGUI::Window* sheet = 
-            CEGUI::WindowManager::getSingleton().loadWindowLayout(
-                (CEGUI::utf8*)"ogregui.layout"); 
-
-     mGUISystem->setGUISheet(sheet);*/
 }
 //-----------------------------------------------------------------------
@@ -155,5 +149,4 @@
 /* MouseQueryListener implementation           */
 /***********************************************/
-
 //-----------------------------------------------------------------------
 MouseQueryListener::MouseQueryListener(RenderWindow* win, Camera* cam, SceneManager *sceneManager, CEGUI::Renderer *renderer)
@@ -163,5 +156,5 @@
 
 	// Setup default variables
-//	mCurrentObject = NULL;
+	mCurrentObject = NULL;
 	mLMouseDown = false;
 	mRMouseDown = false;
@@ -180,4 +173,6 @@
 	mEventProcessor->addKeyListener(this);
 
+	mRaySceneQuery = mSceneMgr->createRayQuery( Ray() );
+
 	// show overlay
 	Overlay* pOver = OverlayManager::getSingleton().getByName("Example/OcclusionCullingDemoOverlay"); 
@@ -200,5 +195,10 @@
 
     pOver->show();
-} // MouseQueryListener
+} 
+//-----------------------------------------------------------------------
+MouseQueryListener::~MouseQueryListener( )
+{
+	delete mRaySceneQuery;
+}
 //-----------------------------------------------------------------------
 void MouseQueryListener::mouseMoved (MouseEvent *e)
@@ -214,6 +214,27 @@
      {
 		 CEGUI::MouseCursor::getSingleton().hide( );
-		 mLMouseDown = true;
-     } // if
+
+		 // Setup the ray scene query
+         Ray mouseRay = mCamera->getCameraToViewportRay( e->getX(), e->getY() );
+         mRaySceneQuery->setRay( mouseRay );
+
+         // Execute query
+         RaySceneQueryResult &result = mRaySceneQuery->execute();
+         RaySceneQueryResult::iterator itr = result.begin( );
+
+         // Get results, create a node/entity on the position
+         if ( itr != result.end() && itr->worldFragment )
+         {
+			 char name[16];
+             sprintf( name, "Robot%d", mCount++ );
+
+             Entity *ent = mSceneMgr->createEntity( name, "robot.mesh" );
+             mCurrentObject = mSceneMgr->getRootSceneNode( )->createChildSceneNode( String(name) + "Node", itr->worldFragment->singleIntersection );
+             mCurrentObject->attachObject( ent );
+             mCurrentObject->setScale( 0.1f, 0.1f, 0.1f );
+         }
+
+         mLMouseDown = true; 
+     }
      // Right mouse button down
      else if (e->getButtonID() & InputEvent::BUTTON1_MASK)
@@ -243,12 +264,18 @@
 void MouseQueryListener::mouseDragged (MouseEvent *e)
  {
-	 /*
-	 // If we are dragging the left mouse button.	 
+	 // If we are dragging the left mouse button.	 	 
 	 if ( mLMouseDown )
-	 {
-		mShipNode->translate(-e->getRelX() * 200, -e->getRelY() * 200, 0.0);
+     {
+		 Ray mouseRay = mCamera->getCameraToViewportRay( e->getX(), e->getY() );
+		 mRaySceneQuery->setRay( mouseRay );
+
+		 RaySceneQueryResult &result = mRaySceneQuery->execute();
+		 RaySceneQueryResult::iterator itr = result.begin( );
+
+		 if ( itr != result.end() && itr->worldFragment )
+		 {
+			 mCurrentObject->setPosition( itr->worldFragment->singleIntersection );
+		 } 
      }
-	 */
-	 
 	 // If we are dragging the right mouse button.
 	 if ( mRMouseDown )
@@ -262,16 +289,17 @@
 {
 	// clamp to terrain
-	static Ray updateRay;
-	updateRay.setOrigin(mCamera->getPosition());
-	updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
-	raySceneQuery->setRay(updateRay);
-	RaySceneQueryResult& qryResult = raySceneQuery->execute();
-	RaySceneQueryResult::iterator i = qryResult.begin();
-	 
-	if (i != qryResult.end() && i->worldFragment)
+	Vector3 camPos = mCamera->getPosition( );
+    Ray cameraRay( Vector3(camPos.x, 5000.0f, camPos.z), Vector3::NEGATIVE_UNIT_Y );
+    mRaySceneQuery->setRay( cameraRay );
+
+    // Perform the scene query
+    RaySceneQueryResult &queryResult = mRaySceneQuery->execute();
+    RaySceneQueryResult::iterator it = queryResult.begin( );
+
+	if (it != queryResult.end() && it->worldFragment)
 	{
-		SceneQuery::WorldFragment* wf = i->worldFragment;
-		mCamera->setPosition(mCamera->getPosition().x, 
-			i->worldFragment->singleIntersection.y + 10, 
+		SceneQuery::WorldFragment* wf = it->worldFragment;
+
+		mCamera->setPosition(mCamera->getPosition().x, wf->singleIntersection.y + 10, 
 			mCamera->getPosition().z);
 	}
@@ -290,5 +318,5 @@
     KEY_PRESSED(KC_SPACE, 0.3, changeAlgorithm());
 
-	KEY_PRESSED(KC_SUBTRACT, 0.3, changeThreshold(-10));
+	KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
 	KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
 	//KEY_PRESSED(KC_T, 1, change);
