Index: trunk/VUT/work/TestCullingTerrain/SceneContentGenerator.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/SceneContentGenerator.cpp	(revision 80)
+++ trunk/VUT/work/TestCullingTerrain/SceneContentGenerator.cpp	(revision 80)
@@ -0,0 +1,106 @@
+/**
+    \file 
+        SceneContentGenerator.cpp
+    \brief
+        Creates content for a scene.
+*/
+#include "SceneContentGenerator.h"
+#include <windows.h>
+
+namespace Ogre {
+
+/*******************************************************/
+/*     SceneContentGenerator implementation            */
+/*******************************************************/
+
+//-----------------------------------------------------------------------
+SceneContentGenerator::SceneContentGenerator(SceneManager *sm):
+mSceneMgr(sm), mCount(0),
+mMinPos(Vector3(-70.0f, -70.0f, 0.0f)),
+mMaxPos(Vector3(70.0f, 70.0f, 600.0f)), 
+mMinAngle(Vector3(0.0f, 0.0f, 0.0f)),
+mMaxAngle(Vector3(360, 360, 360))
+{
+}
+//-----------------------------------------------------------------------
+void SceneContentGenerator::GenerateScene(int numObjects, const String &objName)
+{
+	// initialise random generator
+	srand (time(0));
+	
+	Vector3 rotationRatio;
+	Vector3 translationRatio;
+
+	int currentCount = mCount;
+	int limit = 0;
+
+	//-- create random values between zero and one
+	while ((mCount - currentCount < numObjects) && (limit < 50000))
+	{
+		rotationRatio.x = rand() / (float) RAND_MAX;
+		rotationRatio.y = rand() / (float) RAND_MAX;
+		rotationRatio.z = rand() / (float) RAND_MAX;
+
+		translationRatio.x = rand() / (float) RAND_MAX;
+		translationRatio.y = rand() / (float) RAND_MAX;
+		translationRatio.z = rand() / (float) RAND_MAX;
+
+		// Setup the ray scene query
+		Vector3 rotation = mMinAngle + rotationRatio * (mMaxAngle - mMinAngle);
+		Vector3 position = mMinPos + translationRatio * (mMaxPos - mMinPos);
+
+		(void)GenerateSceneObject(position, rotation, objName);
+		++limit; // just in case: to avoid endless loop
+	}
+}
+//-----------------------------------------------------------------------
+SceneNode *SceneContentGenerator::GenerateSceneObject(const Vector3 &position, 
+					 const Vector3 &rotation, const String& objName)
+{
+	char name[25];
+	sprintf(name, "Entity%d", mCount++);
+		
+	Entity *ent = mSceneMgr->createEntity(name, objName);
+		
+	SceneNode *currentObject = mSceneMgr->getRootSceneNode()->
+		createChildSceneNode(String(name) + "Node", position);
+
+	currentObject->attachObject(ent);
+	currentObject->setScale(0.1f, 0.1f, 0.1f);
+		
+	currentObject->yaw(Degree(rotation.x));
+	currentObject->pitch(Degree(rotation.y));
+	currentObject->roll(Degree(rotation.z));
+
+	return currentObject;
+	
+	/*
+	char msg[100];
+	sprintf(msg, "pos: %3.3f %3.3f %3.3f, newpos: %3.3f %3.3f %3.3f\n", 
+		position.x, position.y, position.z, queryResult.x, queryResult.y, queryResult.z);
+
+	OutputDebugString(msg);
+	*/
+}
+//-----------------------------------------------------------------------
+void SceneContentGenerator::SetMinAngle(Vector3 minAngle)
+{
+	mMinAngle = minAngle;
+}
+//-----------------------------------------------------------------------
+void SceneContentGenerator::SetMaxAngle(Vector3 maxAngle)
+{
+	mMaxAngle = maxAngle;
+}
+//-----------------------------------------------------------------------
+void SceneContentGenerator::SetMinPos(Vector3 minPos)
+{
+	mMinPos = minPos;
+}
+//-----------------------------------------------------------------------
+void SceneContentGenerator::SetMaxPos(Vector3 maxPos)
+{
+	mMaxPos = maxPos;
+}
+
+} // namespace Ogre
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrain.vcproj
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrain.vcproj	(revision 74)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrain.vcproj	(revision 80)
@@ -122,4 +122,7 @@
 			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}">
 			<File
+				RelativePath=".\SceneContentGenerator.cpp">
+			</File>
+			<File
 				RelativePath=".\TerrainContentGenerator.cpp">
 			</File>
@@ -133,4 +136,7 @@
 			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
 			<File
+				RelativePath=".\SceneContentGenerator.h">
+			</File>
+			<File
 				RelativePath=".\TerrainContentGenerator.h">
 			</File>
Index: trunk/VUT/work/TestCullingTerrain/SceneContentGenerator.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/SceneContentGenerator.h	(revision 80)
+++ trunk/VUT/work/TestCullingTerrain/SceneContentGenerator.h	(revision 80)
@@ -0,0 +1,50 @@
+#ifndef _SCENECONTENTGENERATOR_H__
+#define _SCENECONTENTGENERATOR_H__
+
+#include <OgrePrerequisites.h>
+#include <OgreMath.h>
+#include <Ogre.h>
+
+namespace Ogre {
+
+/** Class which fills a scene with content in a random fashion.
+*/
+class SceneContentGenerator
+{
+public:
+	SceneContentGenerator(SceneManager *sm);
+	
+	/** Generates scene object in a random fashion.
+		@param number of objects to generate
+		@param objName the name of the mesh to be generated
+	*/
+	void GenerateScene(int numObjects, const String &objName);
+	/** Generates a new object in the scene using ray scene queries 
+		and inserts it into scene hierarchy.
+		@param position position where the object is created
+		@param rotation rotation angle applied locally to object
+		@objName the name of the object entity
+		@returns scene object if it was successfully created, NULL otherwise
+	*/
+	SceneNode *GenerateSceneObject(const Vector3 &position, 
+		const Vector3 &rotation, const String &objName);
+
+	void SetMinAngle(Vector3 minAngle);
+	void SetMaxAngle(Vector3 maxAngle);
+	void SetMinPos(Vector3 minPos);
+	void SetMaxPos(Vector3 maxPos);
+	
+protected:
+
+	Vector3 mMinAngle;
+	Vector3 mMaxAngle;
+	Vector3 mMaxPos;
+	Vector3 mMinPos;
+	
+	SceneManager *mSceneMgr;
+	int mCount;		// The number of objects on the screen
+};
+
+} // namespace Ogre
+
+#endif // SCENECONTENTGENERATOR_H_
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 76)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 80)
@@ -20,10 +20,10 @@
 
 
-/************************************************/
-/* TestCullingTerrainApplication implementation */
-/************************************************/
+/*******************************************************/
+/*     TestCullingTerrainApplication implementation    */
+/*******************************************************/
 TestCullingTerrainApplication::~TestCullingTerrainApplication()
 {
-	delete mTerrainContentGenerator;
+	delete mSceneContentGenerator;
 }
 //-----------------------------------------------------------------------
@@ -91,10 +91,15 @@
 	// CEGUI setup
 	setupGui();
-
-	mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
-	mTerrainContentGenerator->generateScene(300, "robot.mesh");
-}
-//-----------------------------------------------------------------------
-void TestCullingTerrainApplication::setupGui( void )
+	// HACK: necessary to call once before the content creation for
+	// terrain initialisation
+	mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
+
+	mSceneContentGenerator = new TerrainContentGenerator(mSceneMgr);
+	mSceneContentGenerator->GenerateScene(300, "robot.mesh");
+	// no limitations now: the user can set objects also on peaks
+	mSceneContentGenerator->SetMaxHeight(5000); 
+}
+//-----------------------------------------------------------------------
+void TestCullingTerrainApplication::setupGui()
 {
 	 mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, 
@@ -111,23 +116,24 @@
 }
 //-----------------------------------------------------------------------
-void TestCullingTerrainApplication::createFrameListener(void)
-{
-	mFrameListener= new MouseQueryListener(mWindow, mCamera, mSceneMgr, mGUIRenderer);
+void TestCullingTerrainApplication::createFrameListener()
+{
+	mFrameListener= new MouseQueryListener(mWindow, mCamera, mSceneMgr, 
+		mGUIRenderer, mSceneContentGenerator);
 	mFrameListener->showDebugOverlay(true);
 	mRoot->addFrameListener(mFrameListener);
 }
 //-----------------------------------------------------------------------
-void TestCullingTerrainApplication::chooseSceneManager(void)
+void TestCullingTerrainApplication::chooseSceneManager()
 {
 	mSceneMgr = mRoot->getSceneManager(ST_EXTERIOR_CLOSE);
 }
-
 /***********************************************/
-/* MouseQueryListener implementation           */
+/*      MouseQueryListener implementation      */
 /***********************************************/
 //-----------------------------------------------------------------------
 MouseQueryListener::MouseQueryListener(RenderWindow* win, Camera* cam, 
 									   SceneManager *sceneManager, 
-									   CEGUI::Renderer *renderer): 
+									   CEGUI::Renderer *renderer,
+									   TerrainContentGenerator *sceneGenerator): 
 ExampleFrameListener(win, cam, false, true), mGUIRenderer(renderer), 
 mShutdownRequested(false)
@@ -138,4 +144,5 @@
 	mRMouseDown = false;
 	mSceneMgr = sceneManager;
+	mSceneContentGenerator = sceneGenerator;
 
     // Reduce move speed
@@ -195,21 +202,12 @@
 
 		 // Setup the ray scene query
-         Ray mouseRay = mCamera->getCameraToViewportRay( e->getX(), e->getY() );
+         Ray mouseRay = mCamera->getCameraToViewportRay(e->getX(), e->getY());
    
 		 Vector3 queryResult;
-         // Get results, create a node/entity on the position
-		 if(mRayQueryExecutor->executeRayQuery(&queryResult, mouseRay))
-         {
-			 char name[16];
-             sprintf(name, "Robot%d", mCount++);
-
-             Entity *ent = mSceneMgr->createEntity(name, "robot.mesh");
-             mCurrentObject = mSceneMgr->getRootSceneNode()->
-				 createChildSceneNode(String(name) + "Node", queryResult);
-             mCurrentObject->attachObject(ent);
-             mCurrentObject->setScale(0.1f, 0.1f, 0.1f);
-			 
-         }
-
+		 
+		 // Get results, create a node/entity on the position
+		 mCurrentObject = mSceneContentGenerator->GenerateSceneObject(
+			 mouseRay.getOrigin(), mouseRay.getDirection(), "robot.mesh");
+		
          mLMouseDown = true; 
      }
@@ -249,5 +247,6 @@
 		 if(mRayQueryExecutor->executeRayQuery(&queryResult, mouseRay))
 		 {
-			 mCurrentObject->setPosition(queryResult);
+			 if(mCurrentObject)
+				 mCurrentObject->setPosition(queryResult);
 		 }
      }
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 75)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 80)
@@ -30,8 +30,7 @@
 
     MouseQueryListener(RenderWindow* win, Camera* cam, SceneManager *sceneManager, 
-		CEGUI::Renderer *renderer);
+		CEGUI::Renderer *renderer, TerrainContentGenerator *contentGenerator);
 
-
-    ~MouseQueryListener( );
+    ~MouseQueryListener();
 
 	bool frameStarted(const FrameEvent& evt);
@@ -87,4 +86,5 @@
 
 	RayQueryExecutor *mRayQueryExecutor;
+	TerrainContentGenerator *mSceneContentGenerator;
 };
 
@@ -111,5 +111,5 @@
 	Vector3 mMaxAngle;
 
-	TerrainContentGenerator *mTerrainContentGenerator;
+	TerrainContentGenerator *mSceneContentGenerator;
 
 private:
Index: trunk/VUT/work/TestCullingTerrain/TerrainContentGenerator.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainContentGenerator.cpp	(revision 77)
+++ 	(revision )
@@ -1,120 +1,0 @@
-/**
-    \file 
-        TerrainContentGenerator.cpp
-    \brief
-        Creates content for the terrain.
-*/
-#include "TerrainContentGenerator.h"
-#include <windows.h>
-/********************************************/
-/*  TerrainContentGenerator implementation  */
-/********************************************/
-//-----------------------------------------------------------------------
-TerrainContentGenerator::TerrainContentGenerator(SceneManager *sm)
-{
-	mSceneMgr = sm;
-	mRayQueryExecutor = new RayQueryExecutor(sm);
-
-	mMinPos = Vector3(-500.0f, 5000.0f, -500.0f);
-	mMaxPos = Vector3(500.0f, 5000.0f, 500.0f);
-
-	mMinAngle = 0;
-	mMaxAngle = 360;
-
-	mCount = 0;
-}
-//-----------------------------------------------------------------------
-TerrainContentGenerator::~TerrainContentGenerator()
-{
-	delete mRayQueryExecutor;
-}
-//-----------------------------------------------------------------------
-void TerrainContentGenerator::generateScene(int numObjects, const String &meshName)
-{
-	srand (time(0));
-	
-	Vector3 rotationRatio;
-	Vector3 translationRatio;
-
-	for(int i=0; i < numObjects; ++i)
-	{
-		rotationRatio.x = rand() / (float) RAND_MAX;
-		rotationRatio.y = rand() / (float) RAND_MAX;
-		rotationRatio.z = rand() / (float) RAND_MAX;
-
-		translationRatio.x = rand() / (float) RAND_MAX;
-		translationRatio.y = rand() / (float) RAND_MAX;
-		translationRatio.z = rand() / (float) RAND_MAX;
-
-		generateSceneObject(translationRatio, rotationRatio, i, meshName);
-	}
-}
-//-----------------------------------------------------------------------
-void TerrainContentGenerator::generateSceneObject(const Vector3 &translationRatio, 
-					 const Vector3 &rotationRatio, const int idx, const String& entName)
-{
-	// Setup the ray scene query
-	Real rotation = mMinAngle + rotationRatio.x * (mMaxAngle - mMinAngle);
-	Vector3 position = mMinPos + translationRatio * (mMaxPos - mMinPos);
-
-	Vector3 queryResult;
-
-	char msg[100];
-	sprintf(msg, "pos: %3.3f %3.3f %3.3f\n", position.x, position.y, position.z);
-
-	if (mRayQueryExecutor->executeRayQuery(&queryResult, position, Vector3::NEGATIVE_UNIT_Y))
-	{
-		char name[16];
-		sprintf(name, "%s%d", entName.c_str(), mCount++);
-		OutputDebugString("Creating new object "); OutputDebugString(msg);
-
-		Entity *ent = mSceneMgr->createEntity(name, entName);
-		SceneNode *currentObject = mSceneMgr->getRootSceneNode()->
-			createChildSceneNode(String(name) + "Node", queryResult);
-
-		currentObject->attachObject(ent);
-		currentObject->setScale(0.1f, 0.1f, 0.1f);
-		currentObject->setPosition(position);
-	}
-	else
-	{
-		OutputDebugString(msg);
-	}
-}
-/****************************************/
-/*  RayQueryExecutor implementation     */
-/****************************************/
-//-----------------------------------------------------------------------
-RayQueryExecutor::RayQueryExecutor(SceneManager *sm)
-{
-	mRaySceneQuery = sm->createRayQuery(Ray());
-}
-//-----------------------------------------------------------------------
-RayQueryExecutor::~RayQueryExecutor()
-{
-	delete mRaySceneQuery;
-}
-//-----------------------------------------------------------------------
-bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Vector3 &pos, const Vector3 &dir)
-{
-	return executeRayQuery(result, Ray(pos, dir));
-}
-
-bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Ray &ray)
-{
-    mRaySceneQuery->setRay(ray);
-
-    // Perform the scene query
-    RaySceneQueryResult &queryResult = mRaySceneQuery->execute();
-    RaySceneQueryResult::iterator it = queryResult.begin();
-
-	if (it != queryResult.end() && it->worldFragment)
-	{
-		SceneQuery::WorldFragment* wf = it->worldFragment;
-
-		*result = wf->singleIntersection; 
-		return true;
-	}
-
-	return false;
-}
Index: trunk/VUT/work/TestCullingTerrain/TerrainContentGenerator.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainContentGenerator.h	(revision 75)
+++ 	(revision )
@@ -1,53 +1,0 @@
-/**
-    \file 
-        TerrainContentGenerator.h
-*/
-#include "ExampleApplication.h"
-
-/** Class which executes a simple ray query.
-*/
-class RayQueryExecutor
-{
-public:
-	RayQueryExecutor(SceneManager *sm);
-	~RayQueryExecutor();
-	
-	bool executeRayQuery(Vector3 *result, const Vector3 &pos, const Vector3 &dir);
-	bool RayQueryExecutor::executeRayQuery( Vector3 *result, const Ray &ray);
-
-protected:
-	RaySceneQuery *mRaySceneQuery;
-};
-/** Class which fills a terrain with content in a random way.
-*/
-class TerrainContentGenerator
-{
-public:
-	TerrainContentGenerator(SceneManager *sm);
-	~TerrainContentGenerator();
-	/** Generates a the scene hierarchy with random values .
-		@param number of objects to generate
-		@param the name of the mesh to be generated
-	*/
-	void generateScene(int numObjects, const String &meshName);
-	/** Generates a new scene object 
-		@param tranlationRatio ratio between minimal and maximal possible translation
-		@param rotationRatio ratio between minimal and maximal possible rotation
-		@idx the index of the new object
-		@entName the name of the object entity
-	*/
-	void generateSceneObject(const Vector3 &translationRatio, const Vector3 &rotationRatio, 
-							 const int idx, const String &entName);
-
-protected:
-
-	Real mMinAngle;
-	Real mMaxAngle;
-	Vector3 mMaxPos;
-	Vector3 mMinPos;
-
-	RayQueryExecutor *mRayQueryExecutor;
-	SceneManager *mSceneMgr;
-	int mCount;		// The number of objects on the screen
-};
-
