Index: GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodStripsLibrary.h
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodStripsLibrary.h	(revision 1058)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodStripsLibrary.h	(revision 1069)
@@ -127,5 +127,5 @@
 			void	LoadStripMesh(char *name, Mesh *geomesh);
 
-			Geometry::CREATEMULTIINDEXDATAFUNC create_multi_index_data_func;
+			Geometry::CREATEINDEXDATAFUNC create_index_data_func;
 
 			//	Strips vector.
@@ -136,19 +136,13 @@
 		public:
 
-			MultiIndexData *dataRetrievalInterface;
+			IndexData *dataRetrievalInterface;
 			
 			/**	Constructor, receives as a parameter the name of the file
 			 * 	including the multiresolution object.
 			*/
-			LodStripsLibrary(std::string, Mesh *geomesh, CREATEMULTIINDEXDATAFUNC midfun=NULL);
+			LodStripsLibrary(std::string, Mesh *geomesh, CREATEINDEXDATAFUNC idfun=NULL);
 
 			/// Destructor.
 			~LodStripsLibrary(void);
-
-			/// Returns the highest LOD.
-			uint32	MaxLod();
-
-			/// Returns the lowest LOD.
-			uint32	MinLod();
 
 			/// Returns de current LOD and changes to the specified LOD.
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodManager.h
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodManager.h	(revision 1069)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodManager.h	(revision 1069)
@@ -0,0 +1,55 @@
+/*==========================================================================
+ *	(C) 2006 Universidad Jaime I de Castellón
+ *==========================================================================
+ *	PROYECT:	GAME TOOLS
+ *==========================================================================*/
+/*	CONTENT:
+ *
+ *
+ *	@file	GeoLodManager.h
+ *==========================================================================*/
+
+#ifndef _GEOLODMANAGER
+#define _GEOLODMANAGER
+
+#include "GeoVector3.h"
+#include "GeoLodObject.h"
+#include <map>
+
+namespace Geometry
+{
+	class lodobj_dist_node_t
+	{
+	public:
+		lodobj_dist_node_t(LodObject *o):dist2cam(0.0f),lodobj(o),must_recalculate_distance(true){}
+		lodobj_dist_node_t(const lodobj_dist_node_t &c):dist2cam(c.dist2cam),lodobj(c.lodobj),must_recalculate_distance(c.must_recalculate_distance){}
+		lodobj_dist_node_t(void):dist2cam(0.0f),lodobj(NULL),must_recalculate_distance(true){}
+		float dist2cam;
+		LodObject *lodobj;
+		bool operator < (const lodobj_dist_node_t &o) const { return (dist2cam < o.dist2cam); }
+		bool must_recalculate_distance;
+	};
+
+	class lodobj_idpos_node_t;
+
+	class LodManager
+	{	
+	public:
+		LodManager(float near, float far, const Geometry::Vector3 &campos);
+		void AddLodObj(LodObject*, const Geometry::Vector3 &);
+		void UpdateLOD(void);
+		void UpdateCamera(const Geometry::Vector3 &);
+		void UpdateLODObjectPos(LodObject*,const Geometry::Vector3 &);
+	private:
+		std::map<float,Geometry::lodobj_dist_node_t> lodobj_dists; // key: dist2cam for efficient distance finding
+		std::map<Geometry::lodobj_idpos_node_t,Geometry::Vector3> lodobj_pos; // map key: LodObject UniqueID for efficient ID-based finding
+		unsigned int targetFPS;
+		Vector3 camera_pos;
+		bool camera_changed;
+		float near_range, far_range;
+		float CalculateDistToCamera(const Geometry::Vector3 &) const;
+		float SelectRandomDistance(void) const;
+	};
+};
+
+#endif
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/include/VertexData.h
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/include/VertexData.h	(revision 1019)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/include/VertexData.h	(revision 1069)
@@ -45,5 +45,5 @@
 	};
 
-	class MultiIndexData
+/*	class MultiIndexData
 	{
 	private:
@@ -76,5 +76,5 @@
 		void SetNumValidIndices(unsigned int iprim, unsigned int valid){ num_valid_inds[iprim]=valid; }
 		unsigned int GetNumPrims(void) const { return num_prims; }
-	};
+	};*/
 
 	typedef VertexData* CreateVertexDataFunc(unsigned int numv);
@@ -82,6 +82,6 @@
 	typedef IndexData* CreateIndexDataFunc(unsigned int numi);
 	typedef CreateIndexDataFunc* CREATEINDEXDATAFUNC;
-	typedef MultiIndexData* CreateMultiIndexDataFunc(unsigned int numprims, unsigned int *numi);
-	typedef CreateMultiIndexDataFunc* CREATEMULTIINDEXDATAFUNC;
+/*	typedef MultiIndexData* CreateMultiIndexDataFunc(unsigned int numprims, unsigned int *numi);
+	typedef CreateMultiIndexDataFunc* CREATEMULTIINDEXDATAFUNC;*/
 
 	class DefaultVertexData : public VertexData
@@ -138,5 +138,5 @@
 	IndexData *DefaultIndexDataCreator(unsigned int numi);
 
-	class DefaultMultiIndexData : public MultiIndexData
+/*	class DefaultMultiIndexData : public MultiIndexData
 	{
 	private:
@@ -158,5 +158,5 @@
 	};
 	MultiIndexData *DefaultMultiIndexDataCreator(unsigned int numprims, unsigned int *numi);
-
+*/
 }
 
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodTreeLibrary.h
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodTreeLibrary.h	(revision 1058)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/include/GeoLodTreeLibrary.h	(revision 1069)
@@ -51,9 +51,9 @@
 //		uint32 Get_Trunk_VertexCount(void) const;
 //		uint32 Get_Trunk_MaxIndexCount(uint32 istrip) const;
-		uint32 CurrentLOD_Trunk_StripCount(void) const;
+//		uint32 CurrentLOD_Trunk_StripCount(void) const;
 //		uint32 CurrentLOD_Trunk_IndexCount(void) const;
-		uint32 CurrentLOD_Trunk_IndexCountByStrip(uint32 istrip) const;
+//		uint32 CurrentLOD_Trunk_IndexCountByStrip(uint32 istrip) const;
 /*		const VertexData* Get_Trunk_VertexData(void) const;*/
-		const MultiIndexData* CurrentLOD_Trunk_Indices(void) const;
+		const IndexData* CurrentLOD_Trunk_Indices(void) const;
 
 //		uint32 Get_Foliage_VertexCount(void) const;
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodStripsLibrary.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodStripsLibrary.cpp	(revision 1058)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodStripsLibrary.cpp	(revision 1069)
@@ -17,6 +17,6 @@
 //	Constructors.
 //-----------------------------------------------------------------------------
-LodStripsLibrary::LodStripsLibrary(	std::string	name, Mesh *geomesh, CREATEMULTIINDEXDATAFUNC midfun)
-:create_multi_index_data_func(midfun?midfun:DefaultMultiIndexDataCreator)
+LodStripsLibrary::LodStripsLibrary(std::string	name, Mesh *geomesh, CREATEINDEXDATAFUNC idfun)
+:create_index_data_func(idfun?idfun:DefaultIndexDataCreator)
 {
 	const	char	*name_of_file;
@@ -241,20 +241,4 @@
 
 //-----------------------------------------------------------------------------
-//	Gets the maximum level of detail.
-//-----------------------------------------------------------------------------
-uint32 LodStripsLibrary::MaxLod()
-{
-	return	mMaxLod;
-}
-
-//-----------------------------------------------------------------------------
-//	Gets the minimum level of detail.
-//-----------------------------------------------------------------------------
-uint32 LodStripsLibrary::MinLod()
-{
-	return	mMinLod;
-}
-
-//-----------------------------------------------------------------------------
 //	Returns the number of vertices of the highest LOD.
 //-----------------------------------------------------------------------------
@@ -386,8 +370,9 @@
 	mTotalFaces	=	0;
 
-	unsigned int * strip_sizes = new unsigned int[mFileStrips.size()];
+//	unsigned int * strip_sizes = new unsigned int[mFileStrips.size()];
+	unsigned int total_strip_size = 0;
 	for (i = 0; i < mFileStrips.size(); i++)
 	{
-		strip_sizes[i] = t = int(mFileStrips[i].size());
+		total_strip_size += t = int(mFileStrips[i].size());
 		if (t>max)
 			max	=	t;
@@ -406,12 +391,12 @@
 	
 	//	Copy strips.
-	dataRetrievalInterface=create_multi_index_data_func(mFileStrips.size(),strip_sizes);
-	dataRetrievalInterface->Begin();
-	for (i = 0; i < mFileStrips.size(); i++)
-	{
-		for (j = 0; j < mFileStrips[i].size(); j++)
+	dataRetrievalInterface=create_index_data_func(total_strip_size);
+	dataRetrievalInterface->Begin();	
+	for (int ii = 0, i = 0; i < mFileStrips.size(); i++)
+	{
+		for (j = 0; j < mFileStrips[i].size(); j++,ii++)
 		{
 			mStrips[i].push_back(mFileStrips[i][j]);
-			dataRetrievalInterface->SetIndex(i,j,mFileStrips[i][j]);
+			dataRetrievalInterface->SetIndex(ii,mFileStrips[i][j]);
 		}
 		
@@ -434,5 +419,5 @@
 	}
 
-	delete[] strip_sizes;
+//	delete[] strip_sizes;
 }
 
@@ -593,10 +578,9 @@
 {
 	dataRetrievalInterface->Begin();
-	for (int i=0; i<mTotalStrips; i++)
-	{
-		unsigned int j=0;
-		for (SmallIntVector::iterator it=mStrips[i].begin(); it!=mStrips[i].end(); it++, j++)
-			dataRetrievalInterface->SetIndex(i,j,*it);
-		dataRetrievalInterface->SetNumValidIndices(i,j);
+	for (int i=0, ii=0; i<mTotalStrips; i++)
+	{
+		for (SmallIntVector::iterator it=mStrips[i].begin(); it!=mStrips[i].end(); it++, ii++)
+			dataRetrievalInterface->SetIndex(ii,*it);
+		dataRetrievalInterface->SetNumValidIndices(ii);
 	}
 	dataRetrievalInterface->End();
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodManager.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodManager.cpp	(revision 1069)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodManager.cpp	(revision 1069)
@@ -0,0 +1,77 @@
+/*==========================================================================
+ *	(C) 2006 Universitat Jaume I
+ *==========================================================================
+ *	PROYECT:	GAME TOOLS
+ *==========================================================================
+ *	CONTENT:
+ *
+ *
+ *	@file	GeoLodManager.cpp
+ *===========================================================================*/
+
+#include "GeoLodManager.h"
+#include "GeoLodObject.h"
+
+namespace Geometry
+{
+	class lodobj_idpos_node_t
+	{
+	public:
+		lodobj_idpos_node_t(LodObject *o):lodobj(o){}
+		LodObject *lodobj;
+		bool operator < (const lodobj_idpos_node_t &o) const { return (lodobj->GetUniqueID()<o.lodobj->GetUniqueID()); }
+	};
+}
+
+using namespace Geometry;
+
+LodManager::LodManager(float near, float far, const Geometry::Vector3 &campos)
+:targetFPS(30),camera_changed(true),camera_pos(campos),
+ near_range(near),far_range(far)
+{
+}
+
+void Geometry::LodManager::AddLodObj(LodObject * o, const Geometry::Vector3 &p)
+{
+	float dist = CalculateDistToCamera(p);	
+	lodobj_dists[dist] = lodobj_dist_node_t(o);
+	UpdateLODObjectPos(o,p);
+}
+
+
+void Geometry::LodManager::UpdateLOD(void)
+{
+	// recalculate distances if have to
+	if (camera_changed)
+	{
+		// if the camera has changed, recalculate all lodobject distances to it
+		float random_dist = SelectRandomDistance();
+
+		camera_changed=false;
+	}
+}
+
+void Geometry::LodManager::UpdateCamera(const Geometry::Vector3 &p)
+{
+	camera_pos = p;
+	camera_changed = true;
+}
+
+void Geometry::LodManager::UpdateLODObjectPos(LodObject *o, const Geometry::Vector3 &p)
+{
+	lodobj_pos[lodobj_idpos_node_t(o)] = p;
+}
+
+
+float Geometry::LodManager::CalculateDistToCamera(const Geometry::Vector3 &p) const
+{
+	return camera_pos.dotProduct(p);
+}
+
+float Geometry::LodManager::SelectRandomDistance(void) const
+{
+	float linear_01 = (float)rand()/(float)RAND_MAX;
+	float exp_01 = linear_01*linear_01;
+	float real_dist = exp_01*near_range + (1.0f-exp_01)*far_range;
+	return real_dist;
+}
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/VertexData.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/VertexData.cpp	(revision 831)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/VertexData.cpp	(revision 1069)
@@ -35,5 +35,5 @@
 
 
-Geometry::DefaultMultiIndexData::DefaultMultiIndexData(unsigned int numprims, unsigned int *numi):Geometry::MultiIndexData(numprims,numi)
+/*Geometry::DefaultMultiIndexData::DefaultMultiIndexData(unsigned int numprims, unsigned int *numi):Geometry::MultiIndexData(numprims,numi)
 {
 	indices = new unsigned int*[numprims];	
@@ -52,2 +52,3 @@
 	return new DefaultMultiIndexData(numprims,numi); 
 }
+*/
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodTreeLibrary.cpp
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodTreeLibrary.cpp	(revision 1058)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/src/GeoLodTreeLibrary.cpp	(revision 1069)
@@ -48,14 +48,14 @@
 //	if (current_foliage_indices) delete[] current_foliage_indices;*/
 }
-
+/*
 uint32 LodTreeLibrary::CurrentLOD_Trunk_StripCount(void) const
 {
 	assert(trunk);
-//	return trunk->TOTALTIRAS;
-	return trunk->dataRetrievalInterface->GetNumPrims();
-}
-
-
-uint32 LodTreeLibrary::CurrentLOD_Trunk_IndexCountByStrip(uint32 istrip) const
+	return trunk->TOTAL;
+//	return trunk->dataRetrievalInterface->GetNumPrims();
+}*/
+
+
+/*uint32 LodTreeLibrary::CurrentLOD_Trunk_IndexCountByStrip(uint32 istrip) const
 {
 	assert(trunk);
@@ -64,5 +64,5 @@
 	return trunk->dataRetrievalInterface->GetNumValidIndices(istrip);
 }
-/*
+
 uint32 LodTreeLibrary::CurrentLOD_Trunk_IndexCount(void) const
 {
@@ -146,5 +146,5 @@
 	return trunk->vertices; 
 }*/
-const MultiIndexData* LodTreeLibrary::CurrentLOD_Trunk_Indices(void) const 
+const IndexData* LodTreeLibrary::CurrentLOD_Trunk_Indices(void) const 
 { 
 	return trunk->dataRetrievalInterface;
Index: GTP/trunk/Lib/Geom/shared/GTGeometry/GTGeometry.vcproj
===================================================================
--- GTP/trunk/Lib/Geom/shared/GTGeometry/GTGeometry.vcproj	(revision 1058)
+++ GTP/trunk/Lib/Geom/shared/GTGeometry/GTGeometry.vcproj	(revision 1069)
@@ -112,4 +112,7 @@
 			</File>
 			<File
+				RelativePath=".\src\GeoLodManager.cpp">
+			</File>
+			<File
 				RelativePath=".\src\GeoLodStripsConstructor.cpp">
 			</File>
@@ -349,4 +352,7 @@
 			<File
 				RelativePath=".\include\GeoBase.h">
+			</File>
+			<File
+				RelativePath=".\include\GeoLodManager.h">
 			</File>
 			<File
