Index: /trunk/src/lib/graphics/importer/height_map.cc
===================================================================
--- /trunk/src/lib/graphics/importer/height_map.cc	(revision 6955)
+++ /trunk/src/lib/graphics/importer/height_map.cc	(revision 6956)
@@ -10,5 +10,5 @@
 
    ### File Specific:
-   main-programmer: 
+   main-programmer: bottac@ee.ethz.ch
 */
 
@@ -17,5 +17,8 @@
 #include "texture.h"
 #include "vector.h"
-
+#include "material.h"
+#include "p_node.h"
+#include "state.h"
+#include "resource_manager.h"
 #include "debug.h"
 
@@ -27,18 +30,316 @@
 #endif
 
-HeightMap::HeightMap() : StaticModel()
-{
-	
-}
-
-HeightMap::HeightMap(const char* height_map_name = NULL) : StaticModel()
+Tile::Tile(int i1, int j1, int i2, int j2, HeightMap* hm ) 
+{
+
+PRINTF(0)("Tile Constructor\n");
+highResModel = new VertexArrayModel();
+lowResModel  = new VertexArrayModel();
+
+this->load(i1,j1,i2,j2,hm,highResModel,4);
+this->load(i1,j1,i2,j2,hm,lowResModel, 8);
+}
+
+Tile::Tile()
+{
+PRINTF(0)("ooops *********************************************************************************************************************************************************************************************************** \n");
+}
+
+Tile::~Tile()
+{
+ delete highResModel;
+ delete lowResModel;
+}
+
+void Tile::draw()
+{
+	 // This Draws the LOD's
+	 							//FIXME
+    float cameraDistance = (State::getCamera()->getAbsCoor() - Vector(this->x, hmref->offsetY , this->z) ).len();
+	if(cameraDistance < 0) cameraDistance = -cameraDistance;
+    if(cameraDistance > 5000 )
+    {
+     return;
+    }
+    else if (cameraDistance >= 1000 )
+    {
+      this->drawLowRes();
+    }
+    else 
+    {
+      this->drawHighRes();
+    }
+}
+
+void Tile::drawHighRes()
+{
+ highResModel->draw();
+}
+
+void Tile::drawLowRes()
+{
+ lowResModel->draw();
+}
+
+/*
+*/
+void Tile::load(int i1, int j1, int i2, int j2, HeightMap* hm, VertexArrayModel* model, int Res)
+{
+	
+	#define heightMap hm->heightMap
+	#define colours   hm->colours
+	#define scaleX hm->scaleX
+	#define scaleY hm->scaleY
+	#define scaleZ hm->scaleZ
+	#define shiftX hm->shiftX
+	#define shiftY hm->shiftY
+	#define shiftZ hm->shiftZ
+	#define normalVectorField hm->normalVectorField
+	
+	hmref = hm; // FIXME
+	
+	this->x = hm->offsetX + (heightMap->h - ((i1+i2)/2))*scaleX;
+	this->z = hm->offsetZ + ((j1 + j2 )/2 )	* scaleZ;
+	
+	int sampleRate = Res;
+	
+	float height = 0;
+ 	int offset = 0;
+ 	
+	float r = 0.0;
+	float g = 0.0;
+	float b = 0.0;
+
+ 
+
+ 
+ if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
+	
+	SDL_LockSurface(heightMap);
+	SDL_LockSurface(hm->colourMap);
+	
+	for(int i = i1 ; i <= i2  ; i +=sampleRate)
+	{
+		int w = 0;
+
+		
+		
+		 if(hm->hasColourMap)
+		 {
+		 r = colours[(3*w+2 + 3*i*(heightMap->w )) ];
+		 g = colours[(3*w+1 + 3*i*(heightMap->w)) ];
+		 b = colours[(3*w+0 + 3*i*(heightMap->w))];
+		 }
+		
+		w = j1;
+	    	model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
+		model->addNormal(normalVectorField[i % heightMap->h][w % heightMap->w].y,normalVectorField[i % heightMap->h][w % heightMap->w].z,normalVectorField[i % heightMap->h][w % heightMap->w].x);
+		model->addTexCoor((float)(j1-sampleRate) /(texRate), (float)(i %heightMap->h)/(texRate));
+		
+ 		model->addColor(r/255.0,g/255.0,b/255.0);
+		
+	    for(int j = j1  ; j <= j2    ;  j += sampleRate)
+	    {
+	    
+	    
+	    	 // To be fixed
+		 if(hm->hasColourMap)
+		 {
+		 r = colours[(3*j+2 + 3*i*(heightMap->w )) ];
+		 g =  colours[(3*j+1 + 3*i*(heightMap->w)) ];
+		 b =  colours[(3*j+0 + 3*i*(heightMap->w))];
+		 }
+		height = (float)(unsigned char) hm->heights[(j +sampleRate+ i*(heightMap->w )) ];
+		height += (float)(unsigned char) hm->heights[(j+ 1 + sampleRate + (i+1)*(heightMap->w )) ];
+		height +=  (float) (unsigned char) hm->heights[(j -1+ sampleRate   + (i+1)*(heightMap->w ))];
+		height +=  (float)(unsigned char)hm->heights[(j +sampleRate+ (i+2)*(heightMap->w )) ];
+		height +=  (float)(unsigned char)hm->heights[(j+sampleRate + (i)*(heightMap->w )) ];
+		height=height/5.0;
+		
+		model->addVertex(scaleX*(heightMap->h -i) + shiftX ,((double)(height)*scaleY) + shiftY ,scaleZ*(j) + shiftZ); // Top Right	
+		model->addNormal(normalVectorField[i % heightMap->h][j % heightMap->w].y,normalVectorField[i % heightMap->h][j % heightMap->w].z,normalVectorField[i % heightMap->h][j % heightMap->w].x);
+		model->addTexCoor((float)(j) /(texRate), (float)(i %heightMap->h)/(texRate));
+		
+		//PRINTF(0)("TexCoord:  %f %f \n",(float)j / 100.0, (float)(i %heightMap->h)/100.0);
+		
+ 		model->addColor(r/255.0,g/255.0,b/255.0);
+		w = j;
+	    }
+		
+		
+		model->addVertex(scaleX*(heightMap->h -i)+ shiftX,shiftY,scaleZ*(w)+ shiftZ); // Top Right
+		model->addNormal(normalVectorField[i % heightMap->h][w % heightMap->w].y,normalVectorField[i % heightMap->h][w % heightMap->w].z,normalVectorField[i% heightMap->h][w % heightMap->w].x);
+		model->addTexCoor((float)(j2+sampleRate) /(texRate), (float)(i %heightMap->h)/(texRate));
+ 		model->addColor(r/255.0,g/255.0,b/255.0);
+		
+	}
+	
+	
+	
+
+
+	SDL_UnlockSurface(heightMap);
+	int cnt = 0;
+	for(int i = i1   ; i < i2  ; i +=sampleRate)
+	{
+		
+	    for(int j = j1-sampleRate  ; j < j2  + 2*sampleRate  ;  j += sampleRate)
+	    {
+		
+	    	model->addIndice(cnt);
+		model->addIndice(cnt  + (j2 -j1 + 3* sampleRate  )/ sampleRate );
+		cnt++;
+		
+	    }
+	    	
+	    	
+	    	
+		 model->newStripe();	
+		
+		
+	}
+	 cnt += (j2 -j1 + 3* sampleRate)/ sampleRate;
+	
+		for(int j = j1 ; j <= j2    ;  j += sampleRate)
+	    {
+	    	int i = i1;
+	    
+	    	 // To be fixed
+		 if(hm->hasColourMap)
+		 {
+		 r = (float)colours[(3*j+2 + 3*i*(heightMap->w )) ];
+		 g = (float)colours[(3*j+1 + 3*i*(heightMap->w)) ];
+		 b = (float)colours[(3*j+0 + 3*i*(heightMap->w))];
+		 }
+		
+		model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right	
+		model->addNormal(normalVectorField[i % heightMap->h][j % heightMap->w].y,normalVectorField[i % heightMap->h][j % heightMap->w].z,normalVectorField[i % heightMap->h][j % heightMap->w].x);
+		model->addTexCoor((float)j /(texRate), (float)((i - sampleRate) %heightMap->h)/(texRate));
+ 		model->addColor(r/255.0,g/255.0,b/255.0);		
+	
+	    }
+
+	        for(int j = j1  ; j <= j2    ;  j += sampleRate)
+	    {
+	    	int i = i1;
+		height = (float)(unsigned char) hm->heights[(j +sampleRate+ i*(heightMap->w )) ];
+		height += (float)(unsigned char) hm->heights[(j+ 1 + sampleRate + (i+1)*(heightMap->w )) ];
+		height +=  (float) (unsigned char) hm->heights[(j -1+ sampleRate   + (i+1)*(heightMap->w ))];
+		height +=  (float)(unsigned char)hm->heights[(j +sampleRate+ (i+2)*(heightMap->w )) ];
+		height +=  (float)(unsigned char)hm->heights[(j+sampleRate + (i)*(heightMap->w )) ];
+		height=height/5.0;
+		
+		model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right	
+		model->addNormal(normalVectorField[i % heightMap->h][j % heightMap->w].y,normalVectorField[i % heightMap->h][j% heightMap->w].z,normalVectorField[i%heightMap->h][j%heightMap->w].x);
+		model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
+ 		model->addColor(r/255.0,g/255.0,b/255.0);
+	    
+	    }
+	    
+	        
+	    
+	    for(int j = j1  ; j <= j2    ;  j += sampleRate)
+	    {
+	    	int i = i2;
+	    
+	    	 // To be fixed
+		 if(hm->hasColourMap)
+		 {
+		 r = (float)colours[3*j+2 + 3*i*(heightMap->w )];
+		 g = (float)colours[3*j+1 + 3*i*(heightMap->w)];
+		 b = (float)colours[3*j+0 + 3*i*(heightMap->w)];
+		 }
+		
+		model->addVertex(scaleX*(heightMap->h -i) + shiftX , shiftY ,scaleZ*(j) + shiftZ); // Top Right	
+		model->addNormal(normalVectorField[i%heightMap->h][j%heightMap->w].y,normalVectorField[i%heightMap->h][j%heightMap->w].z,normalVectorField[i%heightMap->h][j%heightMap->w].x);
+		model->addTexCoor((float)j /(texRate), (float)((i+ sampleRate) %heightMap->h)/(texRate));
+ 		model->addColor(r/255.0,g/255.0,b/255.0);		
+	
+	    }
+	    	    
+	    
+	    for(int j = j1 ; j <= j2    ;  j += sampleRate)
+	    {
+	    	int i = i2;
+		height = (float)(unsigned char) hm->heights[(j +sampleRate+ i*(heightMap->w )) ];
+		height += (float)(unsigned char) hm->heights[(j+ 1 + sampleRate + (i+1)*(heightMap->w )) ];
+		height +=  (float) (unsigned char) hm->heights[(j -1+ sampleRate   + (i+1)*(heightMap->w ))];
+		height +=  (float)(unsigned char)hm->heights[(j +sampleRate+ (i+2)*(heightMap->w ))];
+		height +=  (float)(unsigned char)hm->heights[(j+sampleRate + (i)*(heightMap->w )) ];
+		height=height/5.0;
+		model->addVertex(scaleX*(heightMap->h -i) + shiftX , ((double)(height)*scaleY) +shiftY ,scaleZ*(j) + shiftZ); // Top Right	
+		model->addNormal(normalVectorField[i%heightMap->h][j%heightMap->w].y,normalVectorField[i%heightMap->h][j%heightMap->w].z,normalVectorField[i%heightMap->h][j%heightMap->w].x);
+		model->addTexCoor((float)j /(texRate), (float)(i %heightMap->h)/(texRate));
+ 		model->addColor(r/255.0,g/255.0,b/255.0);
+	    
+	    }
+	   
+
+	
+	
+	 // link Boarder Stripe
+	    for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
+	    {
+		
+	    	model->addIndice(cnt);
+		model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
+		cnt++;
+		
+	    }
+	    	
+	    	cnt++;
+	    	
+		 model->newStripe();	
+		
+		
+	
+	
+	
+	 cnt += (j2-j1)/ sampleRate;
+	
+	// link 2nd BoarderStripe
+	    for(int j = j1-sampleRate  ; j < j2    ;  j += sampleRate)
+	    {
+		
+	    	model->addIndice(cnt);
+		model->addIndice(cnt  + (j2 -j1 +  sampleRate  )/ sampleRate );
+		cnt++;
+		
+	    }
+	 
+	    
+	    SDL_UnlockSurface(hm->colourMap);
+	
+	 model->finalize();
+	 
+	 
+	 
+	 
+	#undef heightMap 
+	#undef colours   
+	#undef scaleX 
+	#undef scaleY 
+	#undef scaleZ 
+	#undef shiftX 
+	#undef shiftY 
+	#undef shiftZ 
+	#undef normalVectorField 
+	 
+}
+
+HeightMap::HeightMap()
+{
+
+}
+
+HeightMap::HeightMap(const char* height_map_name = NULL) : VertexArrayModel()
 {
    this->setClassID(CL_HEIGHT_MAP, "HeightMap");
    heightMap =  IMG_Load(height_map_name);
    if(heightMap!=NULL) {
-	
+
 		 PRINTF(0)("loading Image %s\n", height_map_name);
 		 PRINTF(0)("width : %i\n", heightMap->w);
-		 PRINTF(0)("hight : %i\n", heightMap->h);
+		 PRINTF(0)("height : %i\n", heightMap->h);
 		 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
 		 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
@@ -46,8 +347,75 @@
 		 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
 		 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
-		 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);		
+		 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
 		}
+
+     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
+
+	
+  generateNormalVectorField();
+
+  shiftX = 0;
+  shiftY = 0;
+  shiftZ = 0;
+
+}
+
+HeightMap::HeightMap(const char* height_map_name = NULL, const char* colour_map_name = NULL) : VertexArrayModel()
+{
+this->setClassID(CL_HEIGHT_MAP, "HeightMap");
+
+   heightMap =  IMG_Load(height_map_name);
+   if(heightMap!=NULL) {
+
+		 PRINTF(0)("loading Image %s\n", height_map_name);
+		 PRINTF(0)("width : %i\n", heightMap->w);
+		 PRINTF(0)("height : %i\n", heightMap->h);
+		 PRINTF(0)("%i Byte(s) per Pixel \n", heightMap->format->BytesPerPixel);
+		 PRINTF(0)("Rshift : %i\n", heightMap->format->Rshift);
+		 PRINTF(0)("Bshift: %i\n", heightMap->format->Bshift);
+		 PRINTF(0)("Gshift: %i\n", heightMap->format->Gshift);
+		 PRINTF(0)("Rmask: %i\n", heightMap->format->Rmask);
+		 PRINTF(0)("Gmask: %i\n", heightMap->format->Gmask);
+		}
+
+     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
+
+
+    generateNormalVectorField();
+
+  colourMap=NULL;
+  if(colour_map_name != NULL)
+  {
+  colourMap = IMG_Load(colour_map_name);
+  }
+  
+  if(colourMap != NULL)
+  		{
+  		 PRINTF(0)("loading Image %s\n", colour_map_name);
+		 PRINTF(0)("width : %i\n", colourMap->w);
+		 PRINTF(0)("height : %i\n", colourMap->h);
+		 PRINTF(0)("%i Byte(s) per Pixel \n", colourMap->format->BytesPerPixel);
+		 PRINTF(0)("Rshift : %i\n", colourMap->format->Rshift);
+		 PRINTF(0)("Bshift: %i\n", colourMap->format->Bshift);
+		 PRINTF(0)("Gshift: %i\n", colourMap->format->Gshift);
+		 PRINTF(0)("Rmask: %i\n", colourMap->format->Rmask);
+		 PRINTF(0)("Gmask: %i\n", colourMap->format->Gmask);
+		 }
+		 else       PRINTF(0)("oops! couldn't load colorMap for some reason.\n");
+
 		 
-     else       PRINTF(4)("oops! couldn't load %s for some reason.\n", height_map_name);
+		   
+  if(colourMap != NULL)
+  {
+  colours = (unsigned char *) colourMap->pixels;
+  hasColourMap = true;
+  }
+  else hasColourMap = false;
+  
+	
+  heights  = (unsigned char*) heightMap->pixels;
+  shiftX = 0;
+  shiftY = 0;
+  shiftZ = 0;
 }
 
@@ -56,81 +424,180 @@
 {
 	delete heightMap;
+	delete colourMap;
+	
+
+	for(int i=0;i <    heightMap->h/tileSize ; i++)
+	{
+	for(int j = 0; j < heightMap->w/ tileSize; j++)
+	{
+	 delete tiles [i][j];
+	}
+	}
+	for(int i=0;i <    heightMap->h/tileSize ; i++)
+	delete[] tiles[i];
+	delete[] tiles;
+	
+	
+	
+
+
+for(int i=0;i<heightMap->h;i++)
+delete[] normalVectorField [i];
+delete[] normalVectorField;
+
+
+
+
+	
+	
+	
 }
 
 void HeightMap::load()
 {
- unsigned char height = 0; 
- int offset = 0;
-
- char *  bmp = (char*) heightMap->pixels;
-
-  if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
+
+//Create a Dynamicly sized 2D-Array for Tiles
+tiles =  new Tile** [heightMap->h/tileSize];
+for(int i=0;i <    heightMap->h/tileSize ; i++)
+tiles [i]= new (Tile* [heightMap->w /tileSize ]);
+
+//SetUp Arrays
+for(int i = 0; i < (heightMap->h  )/ tileSize; i ++)
+{
+	for(int j = 0; j < (heightMap->w )/ tileSize; j ++)
+	{
+	 
+	 tiles[i][j] =    new Tile( i*tileSize ,  j*tileSize , (i+1)*tileSize, (j+1)*tileSize , this ) ;
+	}
+}
+
+}
+
+
+void HeightMap::draw() 
+{
+const PNode* camera = State::getCamera();
+Vector v = camera->getAbsCoor();
+
+int i_min = 0;
+int i_max = (heightMap->h )/ tileSize;
+int j_min = 0;
+int j_max= (heightMap->w  ) / tileSize;
+
+
+
+for(int i = 0; i <  i_max	 ; i ++)
+{
+	for(int j = 0; j < j_max ; j++)
+	{
+	 tiles[i][j]->draw();
+	}	
+} 
+
+}
+void HeightMap::generateNormalVectorField()
+{
+int delta = 1;
+heights  = (unsigned char*) heightMap->pixels;
+
+//Create a Dynamicly sized 2D-Array to store our normals
+normalVectorField =  new Vector* [heightMap->h];
+for(int i=0;i<heightMap->h;i++)
+normalVectorField [i]= new (Vector [heightMap->w]);
+
+
+
+
+// Initialize 
+for(int i=0; i< heightMap->h; i++)
+{
+	for(int j = 0; j> heightMap->w; j++)
+	{
+	Vector v = Vector(0.0, 1.0, 0.0);
+	 normalVectorField[i][j] = v;
+	}
+}
+
+// !!! Does not yet calculate the normals of some border points!!!!!
+
+if(heightMap != NULL && heightMap->format->BitsPerPixel == 8 )
 	{
 	SDL_LockSurface(heightMap);
-	for(int i = 0 ; i < heightMap->h  ; i +=2)
-	{
-	    for(int j = 0; j < heightMap->w   ;  j += 2)
-	    {
-	    	// get local hight from heightMap
-		// This seems to work only on 8-Bit Grayscale-Bitmaps
-		height = bmp[j + i*(heightMap->w )];
-		
-			
-		/*height = heightMap->format->palette->colors[offset].r +
-			   heightMap->format->palette->colors[offset].g +
-			   heightMap->format->palette->colors[offset].b ; */
-					
-				
-		this->addVertex( 20*(heightMap->h - i) , (( (double)height)/0.3)-1200  ,20*j);
-	     }	
+	for(int i = 0 ; i < heightMap->h - 1  ; i ++)
+	{
+	    for(int j = 0; j < heightMap->w  - 1  ;  j ++)
+	    {
+
+
+		delta = (int)heights[j + (i+1)*(heightMap->w )] -  (int) heights[j + i*(heightMap->w )];
+		Vector a =  Vector(-scaleX,(float)delta*scaleY  ,0.0f);
+
+		delta = (int)heights[j+1 + i*(heightMap->w )] - (int)heights[j + i*(heightMap->w )];
+		Vector b =  Vector(0.0f,(float) delta*scaleY ,scaleZ);
+
+
+		 normalVectorField[i][j] = b.cross(a);
+		 normalVectorField[i][j].normalize();
+		  
+	     }
 	}
 	SDL_UnlockSurface(heightMap);
 
-
-
-	int c = (heightMap->w)/2 ; // One line
-	for(int i = 0; i < (heightMap->w)/2  -2  ; i ++)
-	{
-	    for(int j = 0; j < (heightMap->h)/2 - 2; j++)
-	    {
-		 
-		/* Two Triangles or ...*/ 
-		
-		//this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j+1+i*c , j + i*c );
-		//this->addFace (3, VERTEX_ONLY,j + (i+1)*c,j + (i+1)*c +1 ,j+i*c +1 );				
-				 
-		/* ... one square*/
-
-		this->addFace (4 ,VERTEX_ONLY,j+i*c,j+(i+1)*c  ,j + (i+1)*c +1, j +i*c+1 );
-	    }
-				
-			
-				
-	}
-		
-
-	}//if
-  else
-   	{ 
-
-  //make a cube
-  this->setName("HardCore");
-  this->addVertex (-0.5, -0.5, 0.5);
-  this->addVertex (0.5, -0.5, 0.5);
-  this->addVertex (-0.5, 0.5, 0.5);
-  this->addVertex (0.5, 0.5, 0.5);
-  this->addVertex (-0.5, 0.5, -0.5);
-  this->addVertex (0.5, 0.5, -0.5);
-  this->addVertex (-0.5, -0.5, -0.5);
-  this->addVertex (0.5, -0.5, -0.5);
-	
-
-
-
-  this->addFace (3, VERTEX_ONLY, 4, 3, 2);
-
-	}
-      this->finalize();
-
-
-}
+}
+
+ 	
+
+
+}
+
+
+void HeightMap::scale(Vector v)
+{
+ scaleX = v.x;
+ scaleY = v.y;
+ scaleZ = v.z;
+ generateNormalVectorField();
+}
+
+void HeightMap::setAbsCoor(Vector v)
+{
+ offsetX = v.x;
+ offsetY = v.y;
+ offsetZ  = v.z;
+}
+
+
+float HeightMap::getHeight(float x, float y) 
+{
+
+ x -= offsetX;
+ y -= offsetZ; 
+ 
+ 
+ int xInt = (int)x / scaleX;  x -= (float)((int)x); xInt = heightMap->h - xInt;
+ int yInt = (int)y / scaleZ;    y -= (float) ((int) y); /*yInt = heightMap->w - yInt;*/
+ 
+ //PRINTF(0)("xInt: %i, yInt: %i, x: %f, y: %f\n", xInt, yInt, x, y);
+ 
+ if(xInt <= 0 || xInt >= heightMap->h || yInt <= 0 || yInt >= heightMap->w  ) return 0;
+ if( y >= 0.5*x)
+ {
+	// Check for ... 
+ }
+
+ float height = heights[yInt + (xInt)*heightMap->w]*scaleY;
+ 
+ 
+ float a = normalVectorField[(xInt)][yInt].x;
+ float b = normalVectorField [(xInt)][yInt].z;
+ float c = normalVectorField [(xInt)][yInt].y;
+ 
+ PRINTF(0)("a: %f \n" ,a);
+  PRINTF(0)("b: %f \n" ,b);
+   PRINTF(0)("c: %f \n" ,c);
+ 
+ height -= ( (a/c)*(x) + (b/c)*(y));
+  
+ PRINTF(0)("height: %f \n" ,height );
+ return (height + offsetZ);
+}
Index: /trunk/src/lib/graphics/importer/height_map.h
===================================================================
--- /trunk/src/lib/graphics/importer/height_map.h	(revision 6955)
+++ /trunk/src/lib/graphics/importer/height_map.h	(revision 6956)
@@ -10,24 +10,107 @@
 
    ### File Specific:
-   main-programmer:
+   main-programmer: bottac@ee.ethz.ch
 */
 
 
-#include "static_model.h"
+#include "vertex_array_model.h"
+
+
+#define texRate      32
+#define texRatef     4.0f
+#define tileSize     64
 
 class SDL_Surface;
 class Vector;
+class Material;
+class PNode;
+class Texture;
+class HeightMap;
 
-class HeightMap : public StaticModel
+class Tile 
 {
 public:
+void draw();
+void drawHighRes();
+void drawLowRes();
+int getRes();
+int setHighRes(bool b);
+Tile(int i1, int j1, int i2, int j2, HeightMap* hm ) ;
+Tile();
+float x;
+float z;
+~Tile();
+
+
+
+private:
+HeightMap* hmref;
+VertexArrayModel* highResModel;
+VertexArrayModel* lowResModel; 
+int highRes;
+int lowRes;
+void load(int i1, int j1, int i2, int i2, HeightMap* hm, VertexArrayModel* model, int Res);
+};
+
+class HeightMap : public VertexArrayModel
+{
+public:
+void draw();
 void load();
-void load(const char*, int);
+void load(int Mode);
+void load(const char*, int Mode);
+void scale( Vector V); 
+void setAbsCoor(Vector V);
+float getHeight(float x, float y); 
+float getNormal(float x, float y);
 HeightMap();
 HeightMap(const char*);
+HeightMap(const char*, const char*);
 ~HeightMap();
+
+friend class Tile;
 
 private:
 SDL_Surface* heightMap;
+SDL_Surface* colourMap;
+unsigned char* heights; 
+unsigned char* colours;
 
+void generateNormalVectorField();
+void drawRect(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight );
+void fixBoarder(int xBottomLeft, int yBottomLeft, int xTopRight, int yTopRight);
+Vector  **  normalVectorField ; 
+Tile    ***  tiles;
+Vector    camCoords;
+Vector offsetCoords;  
+Material * tmp_mat;
+Material* red_mat;
+Texture* texture;
+const PNode* camera;
+float scaleX ;
+float scaleY ;
+float scaleZ ;
+float shiftX ; // to be removed
+float shiftY ; // to be removed
+float shiftZ ; // to be removed
+float offsetX;
+float offsetY;
+float offsetZ;
+int   cmScaleX;
+int   cmScaleY;
+bool  hasColourMap;
+
+inline int abs(int val)
+{
+ if(val < 0) val = -val;
+ return val;
+}
+inline int max(int x, int y)
+{
+ return (x>y)? x:y;
+}
+inline int min(int x, int y)
+{
+return (x<y)? x: y;
+}
 };
Index: /trunk/src/world_entities/terrain.cc
===================================================================
--- /trunk/src/world_entities/terrain.cc	(revision 6955)
+++ /trunk/src/world_entities/terrain.cc	(revision 6956)
@@ -26,6 +26,10 @@
 #include "network_game_manager.h"
 
+#include "height_map.h"
+#include "material.h"
 
 #include "glincl.h"
+
+#include "state.h"
 
 using namespace std;
@@ -41,4 +45,13 @@
   if( root != NULL)
     this->loadParams(root);
+
+   this->heightMapMaterial = new Material();
+   heightMapMaterial->setTransparency(1.0);
+
+   heightMapMaterial->setDiffuse(0.4,0.4,0.4);
+   heightMapMaterial->setAmbient(0.4,0.4,0.4 );
+   heightMapMaterial->setSpecular(0.4,0.4,0.4);
+   heightMapMaterial->setShininess(.5);
+   heightMapMaterial->setTransparency(1.0);
 
 //  if (this->model != NULL)
@@ -90,4 +103,7 @@
     ResourceManager::getInstance()->unload(this->vegetation);
   }
+
+  if(this->heightMap)
+        delete heightMap;
 }
 
@@ -101,4 +117,7 @@
   this->ssp = NULL;
   this->vegetation = NULL;
+
+  this->heightMap = NULL;
+  this->heightMapMaterial = NULL;
 }
 
@@ -110,5 +129,59 @@
   LoadParam(root, "vegetation", this, Terrain, loadVegetation)
       .describe("the fileName of the vegetation, that should be loaded onto this terrain. (must be relative to the data-dir)") ;
-}
+
+
+  LoadParam(root, "height-map", this, Terrain, loadHeightMap)
+      .describe("The HeightMap, splitted into two strings seperated by ','. 1: HeighMap, 2: ColorMap");
+
+
+  LoadParam(root, "heigt-texture", this, Terrain, loadTexture)
+      .describe("The name of the Texture for this heightMap");
+}
+
+void Terrain::loadHeightMap(const char* heightMapFile, const char* colorMap)
+{
+  if (this->heightMap != NULL)
+    delete this->heightMap;
+  this->heightMap = NULL;
+
+  char* hmName = ResourceManager::getFullName(heightMapFile);
+  char* hmColorName = ResourceManager::getFullName(colorMap);
+
+
+  this->heightMap = new HeightMap(hmName, hmColorName);
+  heightMap->scale(Vector(43.0f,4.7f,43.0f));
+  heightMap->setAbsCoor(this->getAbsCoor());
+  heightMap->load();
+  delete[] hmName;
+  delete[] hmColorName;
+
+}
+
+
+void Terrain::loadTexture(const char* textureName)
+{
+  if (this->heightMapMaterial != NULL)
+      delete this->heightMapMaterial;
+
+delete this->heightMapMaterial;
+
+   this->heightMapMaterial = new Material();
+   heightMapMaterial->setTransparency(1.0);
+
+   heightMapMaterial->setDiffuse(1.0,1.0,1.0);
+   heightMapMaterial->setAmbient(1.0,1.0,1.0 );
+   heightMapMaterial->setSpecular(1.0,1.0,1.0);
+   heightMapMaterial->setShininess(.5);
+   heightMapMaterial->setTransparency(1.0);
+
+   heightMapMaterial->setDiffuseMap(textureName);
+   heightMapMaterial->setAmbientMap(textureName);
+   heightMapMaterial->setSpecularMap(textureName);
+
+
+
+}
+
+
 
 void Terrain::loadVegetation(const char* vegetationFile)
@@ -128,4 +201,6 @@
 
 
+
+
 void Terrain::draw () const
 {
@@ -137,6 +212,6 @@
                 this->getAbsCoor ().z);
   /* rotate */
-  Vector tmpRot = this->getAbsDir().getSpacialAxis();
-  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
+ // Vector tmpRot = this->getAbsDir().getSpacialAxis();
+  //glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
 
   if (this->objectList)
@@ -146,6 +221,41 @@
   if (this->vegetation)
     this->vegetation->draw();
+
+  if(this->heightMap)
+        {
+        this->heightMapMaterial->select();
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+        glEnable(GL_LIGHTING);
+        glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ;
+        glEnable (GL_COLOR_MATERIAL) ;
+        this->heightMap->draw();
+        }
   glPopMatrix();
 
+
+
+
+
+ glMatrixMode(GL_MODELVIEW);
+glPushMatrix();
+glLoadIdentity();
+ Vector camera =   State::getCamera()->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,Terrain->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();
   /* THIS IS ONLY FOR DEBUGGING INFORMATION */
   if (this->ssp != NULL)
@@ -360,2 +470,9 @@
 {
 }
+
+float Terrain::getHeight(float x, float y)
+{
+        if(this->heightMap != NULL)
+                return (this->heightMap->getHeight(x, y));
+        return 0;
+}
Index: /trunk/src/world_entities/terrain.h
===================================================================
--- /trunk/src/world_entities/terrain.h	(revision 6955)
+++ /trunk/src/world_entities/terrain.h	(revision 6956)
@@ -15,4 +15,6 @@
 // FORWARD DECLARATION
 class SpatialSeparation;
+class HeightMap;
+class Material;
 
 //! A simple method to call a desired debug world.
@@ -40,5 +42,10 @@
   void loadVegetation(const char* vegetationFile);
 
+  void loadHeightMap(const char* heightMapFile, const char* colorMap = NULL);
+  void loadTexture(const char* textureName);
+
   void buildDebugTerrain(DebugTerrain debugTerrain);
+  
+  float getHeight(float x, float y);
   virtual void draw() const;
 
@@ -50,4 +57,6 @@
    int                 objectList;
 
+   HeightMap*          heightMap;
+   Material*           heightMapMaterial;
 };
 
