Index: branches/terrain/src/lib/graphics/importer/terrain/terrain.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8744)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain.cc	(revision 8745)
@@ -33,8 +33,4 @@
 	pagesZ = (heightfield.height/(pageSize-1) );
 	
-	//TODO: Determine layer visibility!	
-	for ( unsigned int i = 0; i < layers.size(); ++i ) {
-		
-	}
 	
 	pages = new pTerrainPage[pagesX*pagesZ];	
@@ -52,4 +48,8 @@
 	
 	root = createQuadTree( 0, 0, pagesX, pagesZ );
+	
+	for ( unsigned int i = 0; i < layers.size(); ++i ) {
+		determineLayerVisibility( i );
+	}	
 	activePages = NULL;
 }
@@ -321,5 +321,79 @@
 }
 
-
+inline Uint8 getAlpha( const SDL_Surface *_s, int _x, int _y )
+{
+	printf( "getalpha: %d, %d\n", _x, _y );
+    int bpp = _s->format->BytesPerPixel;
+    Uint8 *p = (Uint8 *)_s->pixels + _y*_s->pitch + _x * bpp;
+	Uint32 pixel = 0;
+    switch( bpp ) {
+    case 1:
+        pixel = *p;
+		break;
+    case 2:
+        pixel = *(Uint16 *)p;
+		break;
+    case 3:
+        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
+            pixel = p[0] << 16 | p[1] << 8 | p[2];
+        else
+            pixel = p[0] | p[1] << 8 | p[2] << 16;
+		break;
+    case 4:
+        pixel = *(Uint32 *)p;
+		break;
+    default:
+        return 0;       /* shouldn't happen, but avoids warnings */
+    }
+	Uint8 r,g,b,a;
+	SDL_GetRGBA( pixel, _s->format, &r, &g, &b, &a );
+	return a;
+}
+
+void Terrain::determineLayerVisibility( int _layer )
+{
+	LayerInfo * layer = layers[_layer];
+	if ( !layer->alpha ) {
+		int numPages = pagesX*pagesZ;
+		for ( int i = 0; i < numPages; ++i )
+			pages[i]->setLayerVisibility( _layer, LV_FULL );
+			
+		return;
+	}
+	SDL_Surface *alpha = const_cast<SDL_Surface*>( layer->alpha->getStoredImage() );	
+	SDL_LockSurface( alpha );
+	float du = ( (float)alpha->w)/pagesX;
+	float dv = ( (float)alpha->h)/pagesZ;
+	float u = 0.0f, v = 0.0f;
+	for ( int pageX = 0; pageX < pagesX; ++pageX ) {
+		v = 0.0f;
+		for ( int pageZ = 0; pageZ < pagesZ; ++pageZ ) {
+			bool full = true; bool has = false;
+			for ( int x = 0; x < (int)PAGE_SIZE; ++x ) {
+				for ( int z = 0; z < (int)PAGE_SIZE; ++z ) {
+					Uint8 a = getAlpha( alpha, (int)u, (int)v );
+					if ( a ) 
+						has = true;
+					if ( a < 250 )
+						full = false;
+				}
+			}
+			LayerVisibility lv;
+			if ( has ) {
+				if ( full )
+					lv = LV_FULL;
+				else
+					lv = LV_PARTIAL;
+			}
+			else {
+				lv = LV_NO;
+			}
+			getPage( pageX, pageZ )->setLayerVisibility( _layer, lv );
+			v+= dv;
+		}
+		u+= du;
+	}
+	SDL_UnlockSurface( alpha );
+}
 
 void Terrain::getAltitude( Triple& _alt, Triple& _normal )
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8744)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain.h	(revision 8745)
@@ -33,47 +33,9 @@
 typedef Terrain *pTerrain;
 	
-struct Heightfield {
-	int 			width;
-	int 			height;
-	int				pitch;
-	UByte			*data;
-};
-
-struct LayerInfo {
-
-	int				envmode;
-	float			repeatX,
-					repeatZ;
-	Texture			*alpha;
-	Texture			*detail;
-	
-};
-
-struct BufferInfo {
-	
-	BufferInfo( unsigned int _vb, unsigned int _ib, 
-		unsigned int _numV, unsigned int _numI )
-	{
-		vbIdentifier = _vb; ibIdentifier = _ib; 
-		numIndices = _numI; numVertices =_numV;
-		
-	}
-	
-	BufferInfo()
-	{
-		vbIdentifier = ibIdentifier = numIndices = numVertices = 0;
-	}
-	unsigned int	vbIdentifier,
-					ibIdentifier,
-					numIndices,
-					numVertices;	
-};
-
-
 class Terrain {
 	public:
-		const static int PAGE_SIZE 			= 17;
-		const static int MAX_VERTICES 		= PAGE_SIZE*PAGE_SIZE;
-		const static int MAX_INDICES		= 2*( PAGE_SIZE*PAGE_SIZE + PAGE_SIZE -2 );
+		const static unsigned int PAGE_SIZE 		= 17;
+		const static unsigned int MAX_VERTICES 		= PAGE_SIZE*PAGE_SIZE;
+		const static unsigned int MAX_INDICES		= 2*( PAGE_SIZE*PAGE_SIZE + PAGE_SIZE -2 );
 		/**
 		 * The amount of geometry rendered is largely depending on this constant. So chose a
@@ -209,5 +171,6 @@
 		 * frustum of the camera.
 		 */
-		void determineVisiblePages( pTerrainQuad _node );						
+		void determineVisiblePages( pTerrainQuad _node );			
+		void determineLayerVisibility( int _layer );			
 		
 #ifdef USE_VBO
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8744)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8745)
@@ -40,4 +40,5 @@
 	previous = NULL;
 	currentLOD = -1;
+	hasfull = false;
 	forceTesselation = true;
 	vbIdentifier = 0; ibIdentifier = 0;
@@ -773,5 +774,21 @@
 	_vertex.z = position.z+scale.z*_z/( owner->getPageSize()-1 );
 }
-
+void TerrainPage::setLayerVisibility( int _layer, LayerVisibility _lv )
+{
+	if ( hasfull ) return;
+	if ( _lv == LV_FULL ) {
+		for ( int i = 0; i < 8; ++i )
+			layerVisibility[i] = LV_NO;
+		if ( _layer )
+			hasfull = true;
+	}	
+	printf( "setting layer %d vis to %d\n", _lv );	
+	layerVisibility[_layer] = _lv;	
+}
+
+bool TerrainPage::hasMaterial( int _layer )
+{ 
+	return ( layerVisibility[_layer] != LV_NO );
+}
 void TerrainPage::calculateErrors()
 {
Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h	(revision 8744)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.h	(revision 8745)
@@ -24,5 +24,4 @@
 class TerrainPage;
 class Terrain;
-
 typedef TerrainPage *pTerrainPage;
 
@@ -185,5 +184,6 @@
 		 */
 		inline pTerrainPage getNext() { return next; }
-		
+		void setLayerVisibility( int _layer, LayerVisibility _lv );
+		bool hasMaterial( int _layer );		
 		/**
 		 * Returns the previous active page
@@ -206,8 +206,5 @@
 		 */
 		void activate();		
-		inline bool hasMaterial( int _i )
-		{
-			return true;
-		}
+
 		inline void setWantedLOD( int _lod )
 		{
@@ -286,5 +283,6 @@
 		int							numIndices;
 		int							numVertices;
-		
+		LayerVisibility				layerVisibility[8];
+		bool						hasfull;
 #ifdef	USE_VBO
 		GLuint						ibIdentifier,
Index: branches/terrain/src/lib/graphics/importer/terrain/types.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/types.h	(revision 8744)
+++ branches/terrain/src/lib/graphics/importer/terrain/types.h	(revision 8745)
@@ -20,5 +20,5 @@
 #endif
 #include <math.h>
-
+#include "texture.h"
 
 struct Triple {
@@ -139,3 +139,40 @@
 }
 typedef unsigned char UByte;
+struct Heightfield {
+	int 			width;
+	int 			height;
+	int				pitch;
+	UByte			*data;
+};
+
+struct LayerInfo {
+
+	int				envmode;
+	float			repeatX,
+					repeatZ;
+	Texture			*alpha;
+	Texture			*detail;
+	
+};
+typedef enum { LV_FULL, LV_PARTIAL, LV_NO } LayerVisibility;
+
+struct BufferInfo {
+	
+	BufferInfo( unsigned int _vb, unsigned int _ib, 
+		unsigned int _numV, unsigned int _numI )
+	{
+		vbIdentifier = _vb; ibIdentifier = _ib; 
+		numIndices = _numI; numVertices =_numV;
+		
+	}
+	
+	BufferInfo()
+	{
+		vbIdentifier = ibIdentifier = numIndices = numVertices = 0;
+	}
+	unsigned int	vbIdentifier,
+					ibIdentifier,
+					numIndices,
+					numVertices;	
+};
 #endif
