Index: /branches/terrain/src/lib/graphics/importer/terrain/terrain.cc
===================================================================
--- /branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8743)
+++ /branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8744)
@@ -32,8 +32,10 @@
 	pagesX = (heightfield.width/(pageSize-1) );
 	pagesZ = (heightfield.height/(pageSize-1) );
+	
 	//TODO: Determine layer visibility!	
-	for ( unsigned int i = 0; i < materials.size(); ++i ) {
-		
-	}
+	for ( unsigned int i = 0; i < layers.size(); ++i ) {
+		
+	}
+	
 	pages = new pTerrainPage[pagesX*pagesZ];	
 	for ( int x = 0; x < pagesX; ++x )
@@ -155,5 +157,4 @@
 void Terrain::draw( )
 {
-	static float s = 0.0f;
 	glGetError();
 	pTerrainPage page = NULL;
@@ -228,5 +229,6 @@
 		page = page->getNext();
 	}
-	//Finish the last buffer
+	//If there is some data in the buffer, we need to upload the data
+	//into the vram...
 	if ( buffers[current].numIndices != 0 ) {
 		BufferInfo bi = buffers[current];	
@@ -239,19 +241,36 @@
 	}
 	glPushAttrib( GL_COLOR_BUFFER_BIT );
-	for ( unsigned int i = 0; i < materials.size(); ++i ) {
+	glEnable( GL_BLEND );
+	glDepthFunc( GL_LEQUAL );
+	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
+	for ( unsigned int i = 0; i < layers.size(); ++i ) {
+		LayerInfo* layer= layers[i];
 		page = activePages;	
-		
-		//This is a hack! Remove this as soon as possible
-		materials[i]->unselect();
-		materials[i]->select();
-		
+				
 		glActiveTextureARB( GL_TEXTURE1_ARB );
 		glClientActiveTextureARB( GL_TEXTURE1_ARB );
-		glMatrixMode( GL_TEXTURE );
-		glLoadIdentity();
-		glScalef( pagesX, pagesZ, 1.0f );
+		if ( layer->detail ) {
+			//glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
+			glEnable( GL_TEXTURE_2D );
+			glBindTexture( GL_TEXTURE_2D, layer->detail->getTexture() );
+			glMatrixMode( GL_TEXTURE );
+			glLoadIdentity();
+			glScalef( layer->repeatX, layer->repeatZ, 1.0f );
+		}	
+		else {
+			glDisable( GL_TEXTURE_2D );
+		}
 
 		glClientActiveTextureARB( GL_TEXTURE0_ARB );
 		glActiveTextureARB( GL_TEXTURE0_ARB );
+		glEnable( GL_TEXTURE_2D );
+		if ( layer->alpha ) {
+			//glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );			
+			glBindTexture( GL_TEXTURE_2D, layer->alpha->getTexture() );
+		}
+		else {
+		
+			glBindTexture( GL_TEXTURE_2D, lightmap->getTexture() );			
+		}
 		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );				
 		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
@@ -280,16 +299,16 @@
 	}
 	
-	//Get rid of the buffers
-	
 	for ( unsigned int i = 0; i < buffers.size(); ++i )
 		broker->release( buffers[i].vbIdentifier, buffers[i].ibIdentifier );
+		
 	buffers.clear();
 		
 	glClientActiveTextureARB( GL_TEXTURE1_ARB );	
+	glActiveTextureARB( GL_TEXTURE1_ARB );
+	glDisable( GL_TEXTURE_2D );
 	glDisableClientState( GL_VERTEX_ARRAY );
 	glDisableClientState( GL_TEXTURE_COORD_ARRAY );	
 	glDisableClientState( GL_VERTEX_ARRAY );
 	glDisableClientState( GL_TEXTURE_COORD_ARRAY );	
-	materials[0]->unselect();
 	glActiveTextureARB( GL_TEXTURE0_ARB );
 	glClientActiveTextureARB( GL_TEXTURE0_ARB );	
Index: /branches/terrain/src/lib/graphics/importer/terrain/terrain.h
===================================================================
--- /branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8743)
+++ /branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8744)
@@ -26,23 +26,29 @@
 #include <cstdlib>
 #include <string>
-#include <list>
-#include "material.h"
+#include <vector>
 #include "texture.h"
 #include "frustum.h"
 
-
-typedef struct {
+class Terrain;
+typedef Terrain *pTerrain;
+	
+struct Heightfield {
 	int 			width;
 	int 			height;
 	int				pitch;
 	UByte			*data;
-} Heightfield, *pHeightfield;
+};
+
+struct LayerInfo {
+
+	int				envmode;
+	float			repeatX,
+					repeatZ;
+	Texture			*alpha;
+	Texture			*detail;
+	
+};
 
 struct BufferInfo {
-	
-	unsigned int	vbIdentifier,
-					ibIdentifier,
-					numIndices,
-					numVertices;
 	
 	BufferInfo( unsigned int _vb, unsigned int _ib, 
@@ -58,12 +64,10 @@
 		vbIdentifier = ibIdentifier = numIndices = numVertices = 0;
 	}
-};
-
-
-class Terrain;
-
-typedef Terrain *pTerrain;
-
-class TerrainPage;
+	unsigned int	vbIdentifier,
+					ibIdentifier,
+					numIndices,
+					numVertices;	
+};
+
 
 class Terrain {
@@ -99,4 +103,8 @@
 		void getAltitude( Triple& _alt, Triple& _normal );
 		
+		inline void setLightmap( Texture *_lightmap )
+		{
+			lightmap = _lightmap;
+		}
 		//What a bad name: Need to rename that :(
 		void addLevelFourPage( int _numVertices, Vertex *_vertices, 
@@ -147,13 +155,7 @@
 		inline float getAltitude( int _x, int _z ) const;
 		
-		inline void addMaterial( Material *_material )
-		{
-			materials.push_back( _material );
-		}
-		
-		/*inline void setLightmap( const std::string &_lightmap )
-		{
-			lightmapSource = _lightmap;
-		}*/
+		inline void addMaterialLayer( LayerInfo *_layer )
+		{ layers.push_back( _layer ); }
+		
 		inline void setHeightmap( const std::string &_heightmap ) 
 		{ 
@@ -215,5 +217,5 @@
 		pTerrainPage				*pages;		// the references to all pages
 		std::string					heightmapSource;
-		std::string					lightmapSource;
+		Texture						*lightmap;
 		Heightfield					heightfield;
 		Triple						scale;
@@ -236,5 +238,5 @@
 		pFrustum					frustum;
 		pTerrainPage				activePages;
-		std::vector<Material*>		materials;
+		std::vector<LayerInfo*>		layers;
 };
 
Index: /branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc
===================================================================
--- /branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8743)
+++ /branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8744)
@@ -713,5 +713,4 @@
 		
 	assert( isVisible ); assert( numIndices > 0 );
-	assert( active );
 	active = false;
 	CHECK_GL_ERROR( "1" );	
Index: /branches/terrain/src/world_entities/terrain_entity.cc
===================================================================
--- /branches/terrain/src/world_entities/terrain_entity.cc	(revision 8743)
+++ /branches/terrain/src/world_entities/terrain_entity.cc	(revision 8744)
@@ -115,15 +115,46 @@
 void TerrainEntity::loadParams(const TiXmlElement* root)
 {
-	WorldEntity::loadParams(root);
+	WorldEntity::loadParams( root );
 
 	LoadParam(root, "scale", this, TerrainEntity, setScale)
 		.describe("The scale in x,y,z direction");
 	
-	LoadParam( root, "lightmap", this, TerrainEntity, loadLightmap )
+	LoadParam( root, "light_map", this, TerrainEntity, loadLightmap )
 		.describe("The name of the lightmap.");
 		
-	LoadParam( root, "elevationmap", this, TerrainEntity, loadElevationmap )
+	LoadParam( root, "elevation_map", this, TerrainEntity, loadElevationmap )
 		.describe( "The name of the elevation map. Must be an 8bit image" );
-		
+	ResourceManager *manager = ResourceManager::getInstance();
+	TiXmlElement * layer = (TiXmlElement*)root->FirstChild( "material_layer" );		
+	while ( layer ) {
+		LayerInfo *info = new LayerInfo();
+		TiXmlElement *detail = (TiXmlElement*)layer->FirstChild( "detail_map" );
+		if ( detail ) {
+			string detailmap( detail->Attribute( "file" ) );
+			info->detail = (Texture*)manager->load( detailmap, 
+				IMAGE, RP_GAME, (int)GL_TEXTURE_2D );			
+			if ( !info->detail ) {
+				PRINTF(0)( "%s not found\n", detailmap.c_str() );
+			}	
+			TiXmlElement *repeat = (TiXmlElement*)detail->FirstChild( "repeat" );
+			if ( repeat ) {
+				repeat->QueryFloatAttribute( "x", &info->repeatX );
+				repeat->QueryFloatAttribute( "z", &info->repeatZ );				
+			}
+		}
+		else {
+			PRINTF(0)( "Please specify a detail-map" );
+		}
+		TiXmlElement *alpha = (TiXmlElement*)layer->FirstChild( "alpha" );
+		if ( alpha ) {
+				if ( !alpha->Attribute( "full" ) ) {
+					string alphamap( alpha->Attribute( "file" ) );
+					info->alpha = (Texture*)manager->load( alphamap, 
+						IMAGE, RP_GAME, (int)GL_TEXTURE_2D );
+				}
+		}	
+		terrain->addMaterialLayer( info );
+		layer = (TiXmlElement*)layer->NextSibling( "material_layer" );
+	}
 }
 
@@ -140,10 +171,11 @@
 void TerrainEntity::loadLightmap( const std::string& _lightFile )
 {
-	Material *mat = new Material( "TERR_LM" );
-	mat->setDiffuse( 1.0f, 1.0f, 1.0f );
-	mat->setAmbient( 1.0f, 1.0f, 1.0f );
-	mat->setDiffuseMap( _lightFile, GL_TEXTURE_2D, 0 );	
-	mat->setDiffuseMap( std::string( "pictures/debug.png" ), GL_TEXTURE_2D, 1 );	
-	terrain->addMaterial( mat );
+	ResourceManager *manager = ResourceManager::getInstance();
+	Texture *lightmap = (Texture*)manager->load( _lightFile, 
+		IMAGE, RP_GAME, (int)GL_TEXTURE_2D );	
+	if ( lightmap )	
+		terrain->setLightmap( lightmap );
+	else
+		PRINTF(0)("no lightmap %s\n", _lightFile.c_str() );
 }
 
@@ -173,5 +205,4 @@
   	glPushMatrix();
 
-  /* translate */
    glTranslatef( this->getAbsCoor().x,
                  this->getAbsCoor().y,
@@ -184,24 +215,6 @@
 		terrain->draw( );
   	}
-
   	glPopMatrix();
-  /*
-    glMatrixMode(GL_MODELVIEW);
-    glPushMatrix();
-    glLoadIdentity();
-    Vector camera =   State::getCameraNode()->getAbsCoor(); // Go on here ..........!!!
-
-    float height =    heightMap->getHeight(camera.x, camera.z);
-
-    glEnable (GL_COLOR_MATERIAL) ;
-    glBegin(GL_QUADS);            // Draw The Cube Using quads
-    glColor3f(0.0f,1.0f,0.0f);  // Color Blue
-    glVertex3f(camera.x + 63.0f,TerrainEntity->getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f);      // Top Right Of The Quad (Top)
-    glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z-10.0f)+13.0f,camera.z-10.0f);      // Top Left Of The Quad (Top)
-    glVertex3f(camera.x-63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f);      // Bottom Left Of The Quad (Top)
-    glVertex3f(camera.x+ 63.0f, getHeight(camera.x+63.0f, camera.z+10.0f)+13.0f, camera.z+10.0f);      // Bottom Right Of The Quad (Top)
-    glEnd();                      // End Drawing The Plan
-
-    glPopMatrix();*/
+
 
 }
