Index: orxonox/branches/nico/src/importer/Makefile.am
===================================================================
--- orxonox/branches/nico/src/importer/Makefile.am	(revision 3384)
+++ orxonox/branches/nico/src/importer/Makefile.am	(revision 3390)
@@ -12,7 +12,10 @@
 	  	  material.cc \
 	  	  vector.cc
+
 heightmap_SOURCES= main.cc \
 		   heightMapTerrain.cc \
-		   heightMapViewer.cc
+		   heightMapViewer.cc \
+		   vector.cc \
+		   windowHandler.cc
 
 noinst_HEADERS= framework.h \
Index: orxonox/branches/nico/src/importer/Makefile.in
===================================================================
--- orxonox/branches/nico/src/importer/Makefile.in	(revision 3384)
+++ orxonox/branches/nico/src/importer/Makefile.in	(revision 3390)
@@ -54,5 +54,6 @@
 PROGRAMS = $(bin_PROGRAMS)
 am_heightmap_OBJECTS = main.$(OBJEXT) heightMapTerrain.$(OBJEXT) \
-	heightMapViewer.$(OBJEXT)
+	heightMapViewer.$(OBJEXT) vector.$(OBJEXT) \
+	windowHandler.$(OBJEXT)
 heightmap_OBJECTS = $(am_heightmap_OBJECTS)
 heightmap_LDADD = $(LDADD)
@@ -134,4 +135,6 @@
 SHELL = @SHELL@
 STRIP = @STRIP@
+SUB_PROJECTS_FALSE = @SUB_PROJECTS_FALSE@
+SUB_PROJECTS_TRUE = @SUB_PROJECTS_TRUE@
 VERSION = @VERSION@
 ac_ct_CC = @ac_ct_CC@
@@ -189,5 +192,7 @@
 heightmap_SOURCES = main.cc \
 		   heightMapTerrain.cc \
-		   heightMapViewer.cc
+		   heightMapViewer.cc \
+		   vector.cc \
+		   windowHandler.cc
 
 noinst_HEADERS = framework.h \
@@ -210,7 +215,7 @@
 	  esac; \
 	done; \
-	echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign  src/importer/Makefile'; \
+	echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu  src/importer/Makefile'; \
 	cd $(top_srcdir) && \
-	  $(AUTOMAKE) --foreign  src/importer/Makefile
+	  $(AUTOMAKE) --gnu  src/importer/Makefile
 .PRECIOUS: Makefile
 Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
Index: orxonox/branches/nico/src/importer/heightMapTerrain.cc
===================================================================
--- orxonox/branches/nico/src/importer/heightMapTerrain.cc	(revision 3384)
+++ orxonox/branches/nico/src/importer/heightMapTerrain.cc	(revision 3390)
@@ -27,6 +27,9 @@
 	if (bitmap == NULL) return false;
 	
-	cout << "bitmap-dimensions: " << bitmap->w << "x" << bitmap->h << endl;
-	
+	cout << "bitmap-dimensions:             " << bitmap->w << " x " << bitmap->h << endl;
+	cout << "bitmap-pitch:                  " << bitmap->pitch << endl;
+	cout << "bitmap->format->BytesPerPixel: " << (int)bitmap->format->BytesPerPixel << endl;
+	cout << "bitmap->format->BitsPerPixel:  " << (int)bitmap->format->BitsPerPixel << endl;
+	 
 	if (bitmap->w % width != 0) return false;
 	if (bitmap->h % height != 0) return false;
@@ -48,4 +51,7 @@
 		return false;
 	}
+	
+	cout << displayListCount << " display lists generated. starting at: " << displayListStart << endl;
+
 
 	int i,j;
@@ -69,58 +75,154 @@
 bool HeightMapTerrain::readIntoList(int left, int top, int width, int height, int levelOfDetail, GLuint displayListNr)
 {
-	// y is the height value in OpenGL convention
-	GLuint x,y,z;
-	GLfloat col;
-	
 	cout << "creating list Nr " << displayListNr << endl;
 	cout << "left, top, width, height: " << left << ", " << top << ", " << width << ", " << height << endl; 
 	
-	glNewList(displayListNr,GL_COMPILE);
-	{
-		for (z=top; z<top+height; z++)
+	// y is the height value in OpenGL convention
+	int x,y,z;
+	GLfloat col;
+
+	// used for normal calculations
+	Vector O; // origin: the vertex where i'm standing
+	Vector N,E,S,W,NE,SE,SW,NW; // vectors pointing to north, east, ...
+
+	bool eightTriangles = false;
+	
+	
+	// contains a normal vector for every vertex
+	normals = new Vector[width*height];
+
+	// precalculate all vertex normals for later use in display list generation
+	// many superflous calculations i know...
+	for (z=top; z<top+height; z++)
+	{
+		for (x=left; x<left+width; x++)
 		{
-			glBegin(GL_QUAD_STRIP);
+			O = Vector(x,getHeightValue(x,z),z);
+
+			N = Vector(x,getHeightValue(x,z-1),z-1) - O;
+			E = Vector(x+1,getHeightValue(x+1,z),z) - O;
+			S = Vector(x,getHeightValue(x,z+1),z+1) - O;
+			W = Vector(x-1,getHeightValue(x-1,z),z) - O;
+
+			if (eightTriangles == false)
+				// calculate the average normal vector by adding up all 4 adjacent triangle normal vectors
+				normals[(z-top) * height + (x-left)] = N.cross(W) + W.cross(S) + S.cross(E);
+
+			else
 			{
-				for (x=left; x<left+width; x++)
+				NE = Vector(x+1,getHeightValue(x+1,z-1),z-1) - O;
+				SE = Vector(x+1,getHeightValue(x+1,z+1),z+1) - O;
+				SW = Vector(x-1,getHeightValue(x-1,z+1),z+1) - O;
+				NW = Vector(x-1,getHeightValue(x-1,z-1),z-1) - O;
+				// calculate the average normal vector by adding up all 8 adjacent triangle normal vectors
+				normals[(z-top) * height + (x-left)] = N.cross(NW) + NW.cross(W) + W.cross(SW) + SW.cross(S) + S.cross(SE) + SE.cross(E) + E.cross(NE) + NE.cross(N);
+			}
+
+			// every second vertex is the edge of eightTriangles (the others have only four triangles)
+			eightTriangles = !eightTriangles;
+		}
+	}
+	
+	cout << "normals precalculated for list " << displayListNr << endl;
+	
+	glNewList(displayListStart + displayListNr,GL_COMPILE);
+	{
+		glBegin(GL_TRIANGLES);
+		{
+			// don't go through last col and row, because they are already done by second last col&row
+			for (z = top; z < (top + height) - 1; z++)
+			{
+				for (x = left; x < (left + width) - 1; x++)
 				{
-					y = heightValue(x,z+1);
-					col = y / 256.0;
-					glColor3f(1.0,0.0,0.0);
-					glVertex3i(x,y,z+1);
-					
-					y = heightValue(x,z);
-					col = y / 256.0;
-					glColor3f(1.0,0.0,0.0);
-					glVertex3i(x,y,z);
+					// draw 2 triangles per (x,z) pair
+					
+					if (eightTriangles)
+					{
+						call_glNormal_and_glVertex(x,z,top,left,height);
+						call_glNormal_and_glVertex(x,z+1,top,left,height);
+						call_glNormal_and_glVertex(x+1,z,top,left,height);
+					
+						call_glNormal_and_glVertex(x+1,z,top,left,height);
+						call_glNormal_and_glVertex(x,z+1,top,left,height);
+						call_glNormal_and_glVertex(x+1,z+1,top,left,height);
+					}
+					
+					else
+					{
+						call_glNormal_and_glVertex(x,z,top,left,height);
+						call_glNormal_and_glVertex(x,z+1,top,left,height);
+						call_glNormal_and_glVertex(x+1,z+1,top,left,height);
+						
+						call_glNormal_and_glVertex(x+1,z,top,left,height);
+						call_glNormal_and_glVertex(x,z,top,left,height);
+						call_glNormal_and_glVertex(x+1,z+1,top,left,height);
+					}
+					
+					
+					// use the same trick to change the triangles everytime. once |/| and once |\|
+					eightTriangles = !eightTriangles;
+
 				}
 			}
-			glEnd();
 		}
-		
+		glEnd();
+
 	}
 	glEndList();
+
+	
+	delete[] normals;
 	
 	return true;
 }
 
-GLint HeightMapTerrain::heightValue(Uint8 x, Uint8 z)
-{
-	return 100;
+
+void HeightMapTerrain::call_glNormal_and_glVertex(int x,int z,int top,int left,int height)
+{
+/*
+	GLfloat color[4];           // color parameter for glMaterial
+	
+	int heightVal = getHeightValue(x,z);
+
+	color[0] = 1.0 / 256.0 * heightVal;
+	color[1] = 1.0 / 256.0 * heightVal;
+	color[2] = 1.0 / 256.0 * heightVal;
+	color[3] = 1;  // alpha channel (1 = solid)
+
+	glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
+*/
+	glNormal3f(normals[(z-top) * height + (x-left)].x,normals[(z-top) * height + (x-left)].y,normals[(z-top) * height + (x-left)].z);
+	glVertex3i(x,getHeightValue(x,z),z);
+}
+
+
+GLint HeightMapTerrain::getHeightValue(int x, int z)
+{
+	if (bitmap == NULL) return 0;
+
+	// catch out of image access
+	int X = x % bitmap->w;
+	int Z = z % bitmap->h;
 	
 	/*
-	if (bitmap == NULL) return 0;
+	if (X != x) cout << "x lies outside of image (" << x << "," << z << ")" << endl;
+	if (Z != z) cout << "z lies outside of image (" << x << "," << z << ")" << endl;
+	*/
+	
+	// if x or z exeed the image size, return 0 as height value
+	if (X != x || Z != z) return 0;
+	
 	
 	Uint8 index;
-	SDL_Color* color = NULL;
-	
+	SDL_Color color;
 	
 	SDL_LockSurface(bitmap);
-	
-	index = *((Uint8*)bitmap->pixels[z * bitmap->h + x]);
-	color = &bitmap->format->palette->colors[index];
-	
+
+	index = *((Uint8*)bitmap->pixels + Z * bitmap->pitch + X * bitmap->format->BytesPerPixel);
+	color = bitmap->format->palette->colors[index];
+
 	SDL_UnlockSurface(bitmap);
-
-	return color->r;
-	 */
-}
+	
+	// return the red component, because in a grayscale pic, r,g and b are all the same value
+	return (GLint)color.r;
+}
Index: orxonox/branches/nico/src/importer/heightMapTerrain.h
===================================================================
--- orxonox/branches/nico/src/importer/heightMapTerrain.h	(revision 3384)
+++ orxonox/branches/nico/src/importer/heightMapTerrain.h	(revision 3390)
@@ -11,8 +11,6 @@
 using namespace std;
 
+#include "vector.h"
 #include "../stdincl.h"
-
-#include "vector.h"
-
 
 class HeightMapTerrain
@@ -35,14 +33,19 @@
 	
 	// array of display lists
-	GLuint* displayLists;
+	//GLuint* displayLists;
 
 private:
 	// returns the height value between 0 and 256 for a point (x,z) in the picture
-	GLint heightValue(Uint8 x, Uint8 z);
-		
+	GLint getHeightValue(int x, int z);
+	
+	// makes a call to glNormal and glVertex for supplied x and z
+	void call_glNormal_and_glVertex(int x,int z,int top,int left,int height);
+
 	// holds the loaded pixels
 	SDL_Surface* bitmap;
 	
+	// holds normal vector for every vertex
+	Vector* normals;
+};
 
-	
-};
+
Index: orxonox/branches/nico/src/importer/heightMapViewer.cc
===================================================================
--- orxonox/branches/nico/src/importer/heightMapViewer.cc	(revision 3384)
+++ orxonox/branches/nico/src/importer/heightMapViewer.cc	(revision 3390)
@@ -21,7 +21,8 @@
 
 	upVector = Vector(0,1,0);
-
+	
 	wireframe = false;
 	mousedown = false;
+	smoothShading = false;
 	
 }
@@ -38,5 +39,9 @@
 	cout << "HeightMapViewer init()" << endl;
 	
-	if (windowhandler.createOpenGLWindow(WIDTH,HEIGHT,FULLSCREEN) == false)
+#ifdef FULLSCREEN
+	if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, true))
+#else
+	if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, false))
+#endif
 	{
 		cout << "could not create OpenGL-Window." << endl;
@@ -46,9 +51,9 @@
 	if (terrain.loadBitmap(fileName) == false)
 	{
-		cout << "could not create OpenGL-Window." << endl;
+		cout << "could not load bitmap." << endl;
 		return false;
 	}
 	
-	terrain.createDisplayLists(64, 64, 1);
+	terrain.createDisplayLists(128, 128, 1);
 	
 	return true;
@@ -73,14 +78,19 @@
 			{
 				case SDL_MOUSEMOTION:
-					if(mousedown)
+					if (mousedown==true)
 					{
-						angleX = PI / 180 * event.motion.xrel * 0.1;
-						angleY = PI / 180 * event.motion.yrel * 0.1;
+						angleX = PI / 180 * event.motion.xrel * 0.2;
+						angleY = PI / 180 * event.motion.yrel * 0.2;
 
 						// Quaternion(angle,axis)
-						rotator = Quaternion(angleX,Vector(0,1,0));
+						rotator = Quaternion(-angleX,Vector(0,1,0));
 						sightDirection = rotator.apply(sightDirection);
+
+					#ifdef FULLSCREEN
+						rotator = Quaternion(angleY,perpendicular(sightDirection));
+					#else
+						rotator = Quaternion(-angleY,perpendicular(sightDirection));
+					#endif
 						
-						rotator = Quaternion(-angleY,sightDirection.perpendicular());
 						sightDirection = rotator.apply(sightDirection);
 						
@@ -99,4 +109,32 @@
 					switch(event.key.keysym.sym)
 					{
+						case SDLK_UP:
+							// move in direction of sight
+							cameraPos = cameraPos + sightDirection * 0.7;							
+							updateView();
+							break;
+
+						case SDLK_DOWN:
+							// move in direction of sight
+							cameraPos = cameraPos - sightDirection * 0.7;
+							updateView();
+							break;
+							
+						case SDLK_LEFT:
+							cameraPos = cameraPos + perpendicular(sightDirection) * 0.7;
+							updateView();
+							break;
+							
+						case SDLK_RIGHT:
+							cameraPos = cameraPos - perpendicular(sightDirection) * 0.7;
+							updateView();
+							break;
+							
+						case SDLK_s:
+							smoothShading = !smoothShading;
+							if (smoothShading) 	glShadeModel(GL_SMOOTH);
+							else glShadeModel(GL_FLAT);
+							break;
+							
 						case SDLK_w:
 							wireframe = !wireframe;
@@ -105,30 +143,11 @@
 							break;
 							
-						case SDLK_UP:
-							// move in direction of sight
-							cameraPos = cameraPos + sightDirection;
-							updateView();
-							break;
-
-						case SDLK_DOWN:
-							// move in direction of sight
-							cameraPos = cameraPos - sightDirection;
-							updateView();
-							break;
-							
-						case SDLK_LEFT:
-							cameraPos = cameraPos - sightDirection.perpendicular();
-							updateView();
-							break;
-							
-						case SDLK_RIGHT:
-							cameraPos = cameraPos + sightDirection.perpendicular();
-							updateView();
-							break;
-							
 						case SDLK_r:
-							// restore original view vectors
-							cameraPos = Vector(0,0,40);
-							sightDirection = Vector(0,0,-1);
+							// reset view vectors
+							//cameraPos = Vector(0,0,40);
+							//sightDirection = Vector(0,0,-1);							
+							cameraPos = Vector(73.9871,172.496,286.137);
+							sightDirection = Vector(0.23429,-0.736527,-0.625574);
+
 							updateView();
 							break;
@@ -138,4 +157,6 @@
 							<< "display list count:    " << terrain.displayListCount << endl
 							<< "first display list at: " << terrain.displayListStart << endl
+							<< "camera position:       " << "(" << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << ")" << endl
+							<< "sightDirection:        " << "(" << sightDirection.x << "," << sightDirection.y << "," << sightDirection.z << ")" << endl
 							<< endl;
 							
@@ -165,5 +186,5 @@
 		SDL_GL_SwapBuffers();	
 
-		//SDL_Delay(100);
+		SDL_Delay(1);
 	}
 	
@@ -175,5 +196,5 @@
 	// be sure to use up to date lookAt-vector
 	lookAt = cameraPos + sightDirection;
-	upVector = sightDirection.cross(sightDirection.perpendicular()) * -1;
+	upVector = sightDirection.cross(perpendicular(sightDirection));
 	
 	glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
@@ -181,5 +202,5 @@
 	
 	// Calculate The Aspect Ratio Of The Window
-	gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.1f,100.0f);
+	gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.5f,300.0f);
 	gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z,
 			   lookAt.x,lookAt.y,lookAt.z,
@@ -191,14 +212,45 @@
 
 void HeightMapViewer::drawScene()
-{
+{	
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-	glutWireCube(1.0);
-	
-	int i;
-	
-	for (i=0; i<terrain.displayListCount; i++)
-	{
-		glCallList(terrain.displayListStart + i);
-	}
-}
+	glPushMatrix();
+	{
+		//glTranslatef(0.0,1.0,0.0);
+		glScalef(1,0.2,1);
+	
+		for (int i=0; i<terrain.displayListCount; i++)
+		{
+			glCallList(terrain.displayListStart + i);
+		}
+	}
+	glPopMatrix();
+	
+	glColor3f(0.0,1.0,0.0);
+	//	glutWireCube(1.0);
+	
+	glColor3f(0.0,0.0,1.0);
+	glBegin(GL_POINTS);
+	for (float z=0;z<10;z++)
+		for (float x=0;x<10;x++)
+			glVertex3f(x,0,z);
+	glEnd();
+	
+}
+
+
+
+/**
+   \brief returns a vector that is perpendicular and lies in the xz-plane
+ */
+Vector perpendicular (Vector perpendic)
+{
+	Vector r;
+	
+	r.x =  perpendic.z;
+	r.z = -perpendic.x;
+	
+	r.normalize();
+	
+	return r;	
+}
Index: orxonox/branches/nico/src/importer/heightMapViewer.h
===================================================================
--- orxonox/branches/nico/src/importer/heightMapViewer.h	(revision 3384)
+++ orxonox/branches/nico/src/importer/heightMapViewer.h	(revision 3390)
@@ -18,13 +18,12 @@
 #include "../stdincl.h"
 
+
 #define WIDTH        640
 #define HEIGHT       480
-#define FULLSCREEN   false
 
-/*
-#define WIDTH        1280
-#define HEIGHT       854
-#define FULLSCREEN   true
-*/
+//#define WIDTH        1280
+//#define HEIGHT       854
+//#define FULLSCREEN
+
 
 class HeightMapViewer
@@ -46,4 +45,5 @@
 	bool wireframe;
 	bool mousedown;
+	bool smoothShading;
 
 	Vector cameraPos;
@@ -56,2 +56,4 @@
 	Quaternion rotator;
 };
+
+Vector perpendicular (Vector perpendic);
