Index: branches/terrain/src/lib/graphics/importer/terrain/frustum.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/frustum.h	(revision 8321)
+++ branches/terrain/src/lib/graphics/importer/terrain/frustum.h	(revision 8328)
@@ -17,7 +17,14 @@
 #include "types.h"
 #include "glincl.h"
-
+#include <stdio.h>
 #define m( _row,_col )  _m[_col*4+_row-5]
 
+#define CHECK_GL_ERROR( _d ) do { \
+	GLenum __err = glGetError(); \
+	if ( __err != GL_NO_ERROR ) \
+		printf( "check%s: %s\n", _d, (char*)gluErrorString( __err ) );\
+	}\
+	while ( 0 )
+	
 /** 
  * Code borrowed from Lighthouse 3D 
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8321)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8328)
@@ -3,4 +3,6 @@
 #include "terrain_page.h"
 #include "glincl.h"
+#include "util/loading/resource_manager.h"
+#include "debug.h"
 
 #ifdef HAVE_SDL_SDL_IMAGE_H
@@ -12,7 +14,9 @@
 void Terrain::build()
 {
-	SDL_Surface *tmpData = IMG_Load( heightmapSource.c_str() );
+	ResourceManager *MANAGER = ResourceManager::getInstance();
+	std::string full = MANAGER->getFullName( heightmapSource );
+	SDL_Surface *tmpData = IMG_Load( full.c_str() );
  	if ( !tmpData ) {
-		printf( "sorry, can't load!!!\n" );
+		PRINTF(0)( "I' sorry, I can't load %s\n", full.c_str() );
 		return;
 	}		
@@ -25,9 +29,10 @@
 	memcpy( heightfield.data, tmpData->pixels, sizeof(UByte)*dataSize );
 	SDL_FreeSurface( tmpData );
-	//TODO: Check for modulo!
+	//TODO: Perform some checks...
 	pagesX = (heightfield.width/(pageSize-1) );
 	pagesZ = (heightfield.height/(pageSize-1) );
-	tex = new Texture( lightmapSource );
-	
+	tex = (Texture*)MANAGER->load( lightmapSource );
+	
+	//TODO: Determine layer visibility!
 	printf( "creating terrain pages ( %d, %d )...", pagesX, pagesZ );
 	pages = new pTerrainPage[pagesX*pagesZ];	
@@ -72,6 +77,4 @@
 	newPage->setPosition( Triple( scale.x*_xOffset, 0.0f, scale.z*_zOffset ) );
 	newPage->calculateErrors();
-	
-	//newPage->calculateMinMax();
 	return newPage;
 }
@@ -81,4 +84,5 @@
 //		through the activePages list is sufficient to remove all invisible 
 //		pages.
+//		DONE
 void Terrain::determineVisiblePages( pTerrainQuad _node )
 {
@@ -111,22 +115,18 @@
 void Terrain::draw( )
 {
+	glGetError();
 	pTerrainPage page = NULL;
-	
 	frustum->extractPlanes();
-	
 	glEnableClientState( GL_VERTEX_ARRAY );
+
 	glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 
 	glEnable( GL_TEXTURE_2D );
 	glEnable( GL_DEPTH_TEST );
-	glEnable( GL_CULL_FACE );
-
-	
+	glDisable( GL_CULL_FACE );
 	glBindTexture( GL_TEXTURE_2D, tex->getTexture() );
 	glDisable( GL_LIGHTING );
-
 	glEnable( GL_COLOR_MATERIAL );
 	glColor3f( 1.0f, 1.0f, 1.0f );
 	cullCount = 0;
-	
 	// Lets see which pages are visible.
 	determineVisiblePages( root );
@@ -134,10 +134,11 @@
 	int count = 0;
 	page = activePages;	
-	
 	while ( page ) {
 		if ( !page->isActive() ) {
 			pTerrainPage tmp = page;
 			page = tmp->getNext();
+			tmp->setVisibility( false );
 			tmp->deactivate();
+			deactivatedCount++;
 			continue;
 		}	
@@ -149,8 +150,7 @@
 		page = page->getNext();
 	}
-	
 	//printf( "%d pages activated, %d pages deactivated\n", activatedCount, deactivatedCount );
 	activatedCount = 0; deactivatedCount = 0;
-	//float percentage = (float)cullCount/(float)(pagesX*pagesZ)*100.0f;
+	float percentage = (float)cullCount/(float)(pagesX*pagesZ)*100.0f;
 	//printf( "culled %f%% terrain pages away\n",  percentage );
 }
@@ -205,6 +205,7 @@
 		for ( int z = 0; z < _height; ++z ) {
 			pTerrainPage page = getPage( _x0+x, _z0+z );
-			page->chooseLOD( );
-			page->show( );
+			page->setVisibility( true );			
+			page->chooseLOD();
+
 		}
 	}
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8321)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8328)
@@ -67,4 +67,5 @@
 	
 		void getAltitude( Triple& _alt, Triple& _normal );
+		
 		void showPages( int _x0, int _z0, int _width, int _height );
 					
@@ -106,9 +107,9 @@
 		inline float getAltitude( int _x, int _z ) const;
 		
-		inline void setLightmap( std::string &_lightmap )
+		inline void setLightmap( const std::string &_lightmap )
 		{
 			lightmapSource = _lightmap;
 		}
-		inline void setHeightmap( std::string &_heightmap ) 
+		inline void setHeightmap( const std::string &_heightmap ) 
 		{ 
 			heightmapSource = _heightmap; 
@@ -180,6 +181,4 @@
 inline float Terrain::getAltitude( int _x, int _z ) const
 {
-	if ( _x > heightfield.width || _z > heightfield.height )
-		return 0.0f;
 	return heightfield.data[heightfield.pitch*_z+_x]/255.0f;	
 }
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8321)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8328)
@@ -10,5 +10,5 @@
  
 	### File Specific:
-	main programmer: Marco Biasini
+	main-programmer: Marco Biasini
  
  */
@@ -19,8 +19,8 @@
 #include <math.h>
 
-#define CHECK_GL_ERROR() do { \
+#define CHECK_GL_ERROR( _d ) do { \
 	GLenum __err = glGetError(); \
 	if ( __err != GL_NO_ERROR ) \
-		printf( (char*)gluErrorString( __err ) );\
+		printf( "check%s: %s\n", _d, (char*)gluErrorString( __err ) );\
 	}\
 	while ( 0 )
@@ -77,4 +77,5 @@
 void TerrainPage::activate()
 {
+	
 	pTerrainPage list = owner->getActiveList();
 	next = list;
@@ -202,6 +203,5 @@
 	int stride = 1 << _lod;
 	for ( int z = 0; z < owner->getPageSize()-stride; z+=stride ) {
-		if ( z > 0 ) {
-			
+		if ( z > 0 ) {			
 			//Connect the two rows together by inserting the last index of last row
 			//and the first index of the next row two times. This will produce two
@@ -278,9 +278,11 @@
 void TerrainPage::draw( )
 {
-	
+	//These give an error
 	assert( glIsEnabled( GL_VERTEX_ARRAY ) );
 	assert( !glIsEnabled( GL_NORMAL_ARRAY ) );
-	assert( isVisible ); assert( numIndices );
+	assert( isVisible ); assert( numIndices > 0 );
+	assert( active );
 	active = false;
+	CHECK_GL_ERROR( "1" );	
 	glVertexPointer( 3, GL_FLOAT, 0, vertices );
 	glTexCoordPointer( 2, GL_FLOAT, 0, coords );	
@@ -290,6 +292,6 @@
 	if ( owner->debug() )
 		drawBox( );
-	
-	CHECK_GL_ERROR();
+		
+	CHECK_GL_ERROR( "2" );	
 }
 
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h	(revision 8321)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h	(revision 8328)
@@ -247,4 +247,5 @@
 			show( );
 		}
+		active = true;
 	}
 	else {
