Index: branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8548)
+++ branches/terrain/src/lib/graphics/importer/terrain/terrain_page.cc	(revision 8569)
@@ -18,5 +18,5 @@
 #include <stdio.h>
 #include <math.h>
-
+#define USE_VBO
 
 #define CHECK_GL_ERROR( _d ) do { \
@@ -33,5 +33,5 @@
 	numVertices = numIndices = 0;
 	errors = new LODError[TerrainPage::MAX_LODS];
-	vertices = NULL; indices = NULL; coords = NULL;
+	vertices = NULL; indices = NULL;
 	position = Triple( scale.x*_xOffset, 0.0f, scale.z*_zOffset );
 	isVisible = false;
@@ -45,9 +45,7 @@
 void TerrainPage::tesselateRow( int _z, int _xStride, int _zStride, bool _adaptLeft, bool _adaptRight )
 {
-	int xStart = 0, xEnd = owner->getPageSize();
-	
+	
+	int xStart = 0, xEnd = owner->getPageSize();	
 	int halfStride = _zStride >> 1;
-
-
 	if ( _z ) {
 		addAgain( );
@@ -454,4 +452,31 @@
 	determineBorderAdaption( adapt );
 	assert( isVisible );
+#ifdef USE_VBO
+	
+	glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbIdentifier );
+	
+	// The call to glBufferDataARB with a null argument for data is to tell the GPU, that the
+	// old data is invalid. Then calling call glMapBuffer() tells the driver that the previous 
+	// data aren’t valid. As a consequence, if the GPU is still working on them, there won’t 
+	// be a conflict because we invalidated these data. The function glMapBuffer() returns a 
+	// new pointer that we can use while the GPU is working on the previous set of data.. 
+	
+	glBufferDataARB( GL_ARRAY_BUFFER_ARB, count*sizeof( Vertex ), NULL, GL_DYNAMIC_DRAW_ARB );
+	
+	vertices = (Vertex*)glMapBufferARB( GL_ARRAY_BUFFER_ARB, 
+		GL_WRITE_ONLY_ARB );
+	
+	glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );
+	
+	glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 
+		count*3*sizeof( short ), NULL, GL_DYNAMIC_DRAW_ARB );	
+		
+	indices = (unsigned short*)glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 
+		GL_WRITE_ONLY_ARB );
+	
+	assert( indices ); 
+	assert( vertices );
+	
+#endif	
 	if ( _lod == TerrainPage::MAX_LODS-1 ) {
 		tesselateLevelFourPatch( adapt );
@@ -473,6 +498,5 @@
 	
 
-	if ( !adapt[TP_BOTTOM] )
-		return;
+	if ( adapt[TP_BOTTOM] ) {
 		
 	addAgain( );
@@ -480,4 +504,10 @@
 	
 	tesselateRow( owner->getPageSize()-1-stride, stride / 2, stride, adapt[TP_LEFT], adapt[TP_RIGHT] );
+	}
+	
+#ifdef USE_VBO
+	glUnmapBufferARB( GL_ARRAY_BUFFER_ARB );
+	glUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB );
+#endif
 
 }
@@ -488,21 +518,30 @@
 	int count = owner->getPageSize()*owner->getPageSize();
 	
-	vertices = new Triple[count];
+#ifdef USE_VBO	
+	owner->getBufferBroker()->acquire( vbIdentifier, ibIdentifier );
+	vertices = NULL;
+	indices = NULL;
+#else
+	vertices = new Vertex[count];
 	// Not the economical way, but we just want to have space for all indices.
 	indices = new unsigned short[count*3];
+#endif	
+
 	indexHash = new unsigned short[count];	
-	coords = new TexCoord[count];
 	forceTesselation = true;
 	activate();
 	active = true;
+	
 }
 
 void TerrainPage::hide( )
 {
+#ifdef USE_VBO
+	owner->getBufferBroker()->release( vbIdentifier, ibIdentifier );
+#else	
 	if ( vertices ) {
 		delete[] vertices;
 		vertices = NULL;
 	}
-	delete[] coords;	
 	if ( indices ) {
 		delete[] indices;
@@ -510,4 +549,5 @@
 		numIndices = 0;
 	}
+#endif
 	deactivate();
 }
@@ -552,13 +592,29 @@
 	CHECK_GL_ERROR( "1" );	
 	
-	glVertexPointer( 3, GL_FLOAT, 0, vertices );
-	glTexCoordPointer( 2, GL_FLOAT, 0, coords );	
+#ifdef USE_VBO
+	
+	glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbIdentifier );
+	glClientActiveTextureARB( GL_TEXTURE0_ARB );
+	glInterleavedArrays( GL_T2F_V3F, 0, NULL );
 	
 	glClientActiveTextureARB( GL_TEXTURE1_ARB );
-	glVertexPointer( 3, GL_FLOAT, 0, vertices );
-	glTexCoordPointer( 2, GL_FLOAT, 0, coords );
+	glInterleavedArrays( GL_T2F_V3F, 0, NULL );
+	
+	glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibIdentifier );	
 	
 	glDrawElements( GL_TRIANGLE_STRIP, numIndices, 
-					GL_UNSIGNED_SHORT, indices );
+					GL_UNSIGNED_SHORT, NULL );	
+#else	
+	glClientActiveTextureARB( GL_TEXTURE0_ARB );
+	glInterleavedArrays( GL_T2F_V3F, 0, vertices );
+	
+	glClientActiveTextureARB( GL_TEXTURE1_ARB );
+	glInterleavedArrays( GL_T2F_V3F, 0, vertices );
+	
+	glDrawElements( GL_TRIANGLE_STRIP, numIndices, 
+					GL_UNSIGNED_SHORT, indices );	
+#endif
+	
+
 	
 	if ( owner->debug() )
@@ -574,6 +630,6 @@
 		//The vertex didn't exists before, lets create it.
 		indexHash[index] = numVertices;
-		getVertex( _x, _z, vertices[numVertices] );
-		getCoord( _x, _z, coords[numVertices] );
+		getVertex( _x, _z, vertices[numVertices].p );
+		getCoord( _x, _z, vertices[numVertices].t );
 		numVertices++;
 	}
