Index: trunk/src/lib/graphics/importer/material.cc
===================================================================
--- trunk/src/lib/graphics/importer/material.cc	(revision 8035)
+++ trunk/src/lib/graphics/importer/material.cc	(revision 8037)
@@ -108,4 +108,16 @@
       return true;
 
+/// !!  HACK !!! FIX THIS IN MODEL ///
+  else if (likely(Material::selectedMaterial != NULL))
+  {
+  Material::unselect();
+//     for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i)
+//     {
+//         glActiveTexture(Material::glTextureArbs[i]);
+//         glBindTexture(GL_TEXTURE_2D, 0);
+//         glDisable(GL_TEXTURE_2D);
+//     }
+  }
+
   if (likely(Material::selectedMaterial != NULL))
   {
@@ -146,5 +158,4 @@
     glShadeModel(GL_SMOOTH);
 
-
   for(unsigned int i = 0; i < this->textures.size(); ++i)
   {
@@ -160,4 +171,15 @@
 }
 
+void Material::unselect()
+{
+  Material::selectedMaterial = NULL;
+    for(unsigned int i = 0; i < 8; ++i)
+    {
+        glActiveTexture(Material::glTextureArbs[i]);
+        glBindTexture(GL_TEXTURE_2D, 0);
+        glDisable(GL_TEXTURE_2D);
+    }
+}
+
 /**
  *  Sets the Material Illumination Model.
@@ -366,9 +388,23 @@
 }
 
+/**
+ * @brief renders viewport buffer (?? or another buffer ;-)) to a texture
+ * @param textureNumber select the texture unit that will be overwritten
+*/
+void Material::renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height)
+{
+  assert(textureNumber < Material::getMaxTextureUnits());
+  assert(textureNumber < this->textures.size());
+
+  glActiveTexture(0);
+   glEnable(GL_TEXTURE_2D);
+   glBindTexture(GL_TEXTURE_2D, this->textures[textureNumber].getTexture());
+  glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);
+
+}
 
 /**
  * @brief Sets the Materials Ambient Map
- * @param aMap the Name of the Image to Use
-   @todo implement this
+ * @todo implement this
 */
 void Material::setAmbientMap(const std::string& aMap, GLenum target)
Index: trunk/src/lib/graphics/importer/material.h
===================================================================
--- trunk/src/lib/graphics/importer/material.h	(revision 8035)
+++ trunk/src/lib/graphics/importer/material.h	(revision 8037)
@@ -30,4 +30,6 @@
 
     bool select () const;
+    bool activateTextureUnit(unsigned int textureNumber);
+    static void unselect();
 
     void setIllum (int illum);
@@ -55,4 +57,5 @@
     void setDiffuseMap(const std::string& dMap, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0);
     void setSDLDiffuseMap(SDL_Surface *surface, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0);
+    void renderToTexture(unsigned int textureNumber, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height);
 
     void setAmbientMap(const std::string& aMap, GLenum target = GL_TEXTURE_2D);
Index: trunk/src/lib/graphics/shader.cc
===================================================================
--- trunk/src/lib/graphics/shader.cc	(revision 8035)
+++ trunk/src/lib/graphics/shader.cc	(revision 8037)
@@ -20,5 +20,7 @@
 #include "stdlibincl.h"
 #include "compiler.h"
-#include <stdio.h>
+//#include <stdio.h>
+#include <fstream>
+
 #include "debug.h"
 
@@ -26,6 +28,6 @@
 
 
-#ifndef PARSELINELENGHT
-#define PARSELINELENGHT     512       //!< how many chars to read at once
+#ifndef PARSELINELENGTH
+#define PARSELINELENGTH     512       //!< how many chars to read at once
 #endif
 
@@ -40,6 +42,4 @@
    this->setClassID(CL_SHADER, "Shader");
 
-   this->fragmentShaderFile = "";
-   this->vertexShaderFile = "";
    this->shaderProgram = 0;
    this->vertexShader = 0;
@@ -48,17 +48,13 @@
    if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)
      {
-       GLint status = 0;
-
        this->shaderProgram = glCreateProgramObjectARB();
 
        if (!vertexShaderFile.empty())
-         this->loadShaderProgramm(SHADER_VERTEX, vertexShaderFile);
+         this->loadShaderProgramm(Shader::Vertex, vertexShaderFile);
        if (!fragmentShaderFile.empty())
-         this->loadShaderProgramm(SHADER_FRAGMENT, fragmentShaderFile);
-       glLinkProgramARB(this->shaderProgram);
-       // link error checking
-       glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
-       if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
-         this->printError(this->shaderProgram);
+         this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile);
+
+       this->linkShaderProgram();
+
     }
    else
@@ -78,6 +74,6 @@
 
   // delete what has to be deleted here
-  this->deleteProgram(SHADER_VERTEX);
-  this->deleteProgram(SHADER_FRAGMENT);
+  this->deleteProgram(Shader::Vertex);
+  this->deleteProgram(Shader::Fragment);
 
   if (this->fragmentShader != 0)
@@ -116,16 +112,18 @@
 
 
-bool Shader::loadShaderProgramm(SHADER_TYPE type, const std::string& fileName)
+bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName)
 {
   GLhandleARB shader = 0;
 
-  if (type != SHADER_VERTEX && type != SHADER_FRAGMENT)
+  if (type != Shader::Vertex && type != Shader::Fragment)
     return false;
   this->deleteProgram(type);
 
 
-  std::vector<char*>* program = fileReadArray(fileName);
-
-  if (type == SHADER_VERTEX && GLEW_ARB_vertex_shader)
+  std::string program;
+  if (!readShader(fileName, program))
+    return false;
+
+  if (type == Shader::Vertex && GLEW_ARB_vertex_shader)
   {
     this->vertexShaderFile = fileName;
@@ -134,5 +132,5 @@
   }
 
-  if (type == SHADER_FRAGMENT && GLEW_ARB_fragment_shader)
+  if (type == Shader::Fragment && GLEW_ARB_fragment_shader)
   {
     this->fragmentShaderFile = fileName;
@@ -144,6 +142,7 @@
   {
     GLint status = 0;
-    /// FIXME do it back
-    //    glShaderSourceARB(shader, program->size(), (const std::string&)&(*program)[0], NULL);
+    const char* prog = program.c_str();
+
+    glShaderSourceARB(shader, 1, &prog, NULL);
     glCompileShaderARB(shader);
     // checking on error.
@@ -154,57 +153,39 @@
       glAttachObjectARB(this->shaderProgram, shader);
   }
-  for (unsigned int i=0; i< program->size(); i++)
-    delete[] (*program)[i];
-  delete program;
-}
-
-char* Shader::fileRead(const std::string& fileName)
-{
-  FILE* fileHandle;
-  char* content = NULL;
-
-  int count = 0;
-
-  if (fileName.empty())
-    return NULL;
-
-  fileHandle = fopen(fileName.c_str(), "rt");
-
-  if (fileHandle == NULL)
-    return NULL;
-  fseek(fileHandle, 0, SEEK_END);
-  count = ftell(fileHandle);
-  rewind(fileHandle);
-  if (count > 0) {
-     content = new char[count+1];
-     count = fread(content, sizeof(char), count, fileHandle);
-     content[count] = '\0';
-   }
-   fclose(fileHandle);
- return content;
-}
-
-
-std::vector<char*>* Shader::fileReadArray(const std::string& fileName)
-{
-  FILE*    stream;           //< The stream we use to read the file.
-
-  if( (stream = fopen (fileName.c_str(), "rt")) == NULL)
-  {
-    PRINTF(1)("Shader could not open %s\n", fileName.c_str());
-    return NULL;
-  }
-  std::vector<char*>* file = new std::vector<char*>;
-
-  char lineBuffer[PARSELINELENGHT];
-  char* addString;
-  while(fgets (lineBuffer, PARSELINELENGHT, stream) != NULL)
-  {
-    addString = new char[strlen(lineBuffer)+1];
-    strcpy(addString, lineBuffer);
-    file->push_back(addString);
-  }
-  fclose(stream);
-  return file;
+}
+
+
+void Shader::linkShaderProgram()
+{
+  GLint status = 0;
+
+  glLinkProgramARB(this->shaderProgram);
+       // link error checking
+  glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status);
+  if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)
+    this->printError(this->shaderProgram);
+}
+
+
+bool Shader::readShader(const std::string& fileName, std::string& output)
+{
+  char lineBuffer[PARSELINELENGTH];
+
+  std::ifstream shader;
+  shader.open(fileName.c_str());
+  if (!shader.is_open())
+    return false;
+
+
+  while (!shader.eof())
+  {
+    shader.getline(lineBuffer, PARSELINELENGTH);
+    output += lineBuffer;
+    output += "\n";
+  }
+
+
+  shader.close();
+  return true;
 }
 
@@ -228,8 +209,8 @@
 
 
-void Shader::deleteProgram(SHADER_TYPE type)
+void Shader::deleteProgram(Shader::Type type)
 {
   GLint status = 0;
-  if (type == SHADER_VERTEX && this->vertexShader != 0)
+  if (type == Shader::Vertex && this->vertexShader != 0)
   {
     this->vertexShaderFile = "";
@@ -241,5 +222,5 @@
     this->vertexShader = 0;
   }
-  else if (type == SHADER_FRAGMENT && this->fragmentShader != 0)
+  else if (type == Shader::Fragment && this->fragmentShader != 0)
   {
     this->fragmentShaderFile = "";
Index: trunk/src/lib/graphics/shader.h
===================================================================
--- trunk/src/lib/graphics/shader.h	(revision 8035)
+++ trunk/src/lib/graphics/shader.h	(revision 8037)
@@ -12,19 +12,80 @@
 
 
-typedef enum
-{
-  SHADER_NONE       = 0,
-  SHADER_FRAGMENT   = 1,
-  SHADER_VERTEX     = 0,
-
-} SHADER_TYPE;
-
 // FORWARD DECLARATION
 
 
+
 //! A class for ...
-class Shader : public BaseObject {
+class Shader : public BaseObject
+{
+public:
+  class Uniform
+  {
+  public:
+    Uniform(const Shader* shader, const std::string& location) { this->uniform = glGetUniformLocationARB(shader->getProgram(), location.c_str()) ; }
+    Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; };
+    Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; };
 
-  public:
+    void set(float v0) const { glUniform1f(this->uniform, v0); }
+    void set(float v0, float v1) const { glUniform2f(this->uniform, v0, v1); }
+    void set(float v0, float v1, float v2) const { glUniform3f(this->uniform, v0, v1, v2); }
+    void set(float v0, float v1, float v2, float v3) const { glUniform4f(this->uniform, v0, v1, v2, v3); }
+
+    void set(int v0) const { glUniform1i(this->uniform, v0); }
+    void set(int v0, int v1) const { glUniform2i(this->uniform, v0, v1); }
+    void set(int v0, int v1, int v2) const { glUniform3i(this->uniform, v0, v1, v2); }
+    void set(int v0, int v1, int v2, int v3) const { glUniform4i(this->uniform, v0, v1, v2, v3); }
+
+    void setV(unsigned int count, float* vv) const
+    {
+      switch (count)
+      {
+        case 1:
+          glUniform1fv(this->uniform, 1, vv);
+          break;
+        case 2:
+          glUniform2fv(this->uniform, 2, vv);
+          break;
+        case 3:
+          glUniform3fv(this->uniform, 3, vv);
+          break;
+        case 4:
+          glUniform4fv(this->uniform, 4, vv);
+          break;
+      }
+    }
+    void setV(unsigned int count, int* vv) const
+    {
+      switch (count)
+      {
+        case 1:
+          glUniform1iv(this->uniform, 1, vv);
+          break;
+        case 2:
+          glUniform2iv(this->uniform, 2, vv);
+          break;
+        case 3:
+          glUniform3iv(this->uniform, 3, vv);
+          break;
+        case 4:
+          glUniform4iv(this->uniform, 4, vv);
+          break;
+      }
+    }
+    private:
+    GLint uniform;
+  };
+
+  typedef enum
+  {
+    None       = 0,
+    Fragment   = 1,
+    Vertex     = 2,
+
+    Program    = 4,
+  }
+  Type;
+
+public:
   Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = "");
   virtual ~Shader();
@@ -32,36 +93,48 @@
   static bool unload(Shader* shader);
 
-  bool loadShaderProgramm(SHADER_TYPE type, const std::string& fileName);
+
+  static bool checkShaderAbility();
   void activateShader();
   static void deactivateShader();
-  void deleteProgram(SHADER_TYPE type);
 
-  char* fileRead(const std::string& fileName);
-  std::vector<char*>* fileReadArray(const std::string& fileName);
+  Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); }
 
-  static bool checkShaderAbility();
+  bool loadShaderProgramm(Shader::Type type, const std::string& fileName);
+  void deleteProgram(Shader::Type type);
 
-  inline static bool shaderActive() { return (Shader::storedShader != NULL)? true : false; };
+  void linkShaderProgram();
+
+
+  bool readShader(const std::string& fileName, std::string& output);
+
+
+inline static bool shaderActive() { return (Shader::storedShader != NULL)? true : false; };
   inline static Shader* getActiveShader() { return Shader::storedShader; };
   inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} };
   inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; };
 
-  GLhandleARB getProgram() { return this->shaderProgram; }
-  GLhandleARB getVertexS() { return this->vertexShader; }
-  GLhandleARB getFragmentS() { return this->fragmentShader; }
 
-  void printError(GLhandleARB program);
+
+
+  GLhandleARB getProgram() const { return this->shaderProgram; }
+  GLhandleARB getVertexS() const { return this->vertexShader; }
+  GLhandleARB getFragmentS() const { return this->fragmentShader; }
+
   void debug() const;
 
-  private:
+  static void printError(GLhandleARB program);
 
- private:
-   std::string            fragmentShaderFile;
-   std::string            vertexShaderFile;
-   GLhandleARB            shaderProgram;
-   GLhandleARB            vertexShader;
-   GLhandleARB            fragmentShader;
 
-   static Shader*         storedShader;
+private:
+  std::string            fragmentShaderFile;
+  std::string            vertexShaderFile;
+
+  GLhandleARB            shaderProgram;
+
+  GLhandleARB            vertexShader;
+  GLhandleARB            fragmentShader;
+
+
+  static Shader*         storedShader;
 };
 
Index: trunk/src/lib/graphics/text_engine/text.cc
===================================================================
--- trunk/src/lib/graphics/text_engine/text.cc	(revision 8035)
+++ trunk/src/lib/graphics/text_engine/text.cc	(revision 8037)
@@ -225,4 +225,8 @@
   // setting the Blending effects
   glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
+
+
+  glActiveTexture(GL_TEXTURE0);
+
   glEnable(GL_BLEND);
   glEnable(GL_TEXTURE_2D);
Index: trunk/src/story_entities/game_world.cc
===================================================================
--- trunk/src/story_entities/game_world.cc	(revision 8035)
+++ trunk/src/story_entities/game_world.cc	(revision 8037)
@@ -496,6 +496,7 @@
       LightManager::getInstance()->draw();
       // draw everything to be included in the reflection
-      for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
-        this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
+      this->drawEntityList(State::getObjectManager()->getReflectionList());
+//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
+//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
 
       // clean up from reflection rendering
@@ -511,5 +512,37 @@
  */
 void GameWorld::renderPassRefraction()
-{}
+{
+
+    // clear buffer
+  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+  glLoadIdentity();
+
+  const std::list<BaseObject*>* reflectedWaters;
+  MappedWater* mw;
+
+  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
+  {
+    std::list<BaseObject*>::const_iterator it;
+    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
+    {
+      mw =  dynamic_cast<MappedWater*>(*it);
+
+      // prepare for reflection rendering
+      mw->activateRefraction();
+
+      //camera and light
+      this->dataTank->localCamera->apply ();
+      this->dataTank->localCamera->project ();
+      LightManager::getInstance()->draw();
+      // draw everything to be included in the reflection
+      this->drawEntityList(State::getObjectManager()->getReflectionList());
+//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
+//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
+
+      // clean up from reflection rendering
+      mw->deactivateRefraction();
+    }
+  }
+}
 
 
Index: trunk/src/util/object_manager.cc
===================================================================
--- trunk/src/util/object_manager.cc	(revision 8035)
+++ trunk/src/util/object_manager.cc	(revision 8037)
@@ -65,7 +65,6 @@
       delete this->objectLists[i].front();
 
-  // delete reflectin list
-  while( !this->reflectionList.empty())
-    delete this->reflectionList.front();
+  // delete reflection list
+  this->reflectionList.clear();
 }
 
Index: trunk/src/world_entities/environments/mapped_water.cc
===================================================================
--- trunk/src/world_entities/environments/mapped_water.cc	(revision 8035)
+++ trunk/src/world_entities/environments/mapped_water.cc	(revision 8037)
@@ -18,4 +18,5 @@
 #include "util/loading/factory.h"
 #include "util/loading/resource_manager.h"
+#include "state.h"
 
 
@@ -26,5 +27,4 @@
 
 MappedWater::MappedWater(const TiXmlElement* root)
-    : texture(GL_TEXTURE_2D)
 {
   this->setClassID(CL_MAPPED_WATER, "MappedWater");
@@ -34,15 +34,13 @@
     this->loadParams(root);
 
-  //! HACK FIXME HACK FIXME HACK FIXME HACK FIXME
-  //! todo: rename texture to reflection texture
+  PRINTF(0)("MaxTextureUnits: %i\n", Material::getMaxTextureUnits());
+
+  // TODO rename texture to reflection texture
+  /// loads the textures
   // set up refleciton texture
-  //mat.setDiffuseMap(&this->texture, 0);
+  //mat.setDiffuseMap(this->texture, 0);
   mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0);
-/*  mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1);
-  mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 2);
-  mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 3);
-  mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4);*/
   // load refraction texture
-  //mat.setDiffuseMap(&this->refractionTexture, 1);
+  mat.setDiffuseMap("pictures/error_texture.png", GL_TEXTURE_2D, 1);
   // load normal map
   //mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
@@ -50,24 +48,30 @@
   //mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
   // set up depth texture
-  //mat.setDiffuseMap(&this->depthTexture, 4);
-
+  //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4);
+
+
+
+  /// MAKE THE MAPPING TEXTURE.
+  // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE
   // set the size of the refraction and reflection textures
-
-
-
-  /// MAKE THE MAPPING TEXTURE.
-  // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE.
   this->textureSize = 512;
   unsigned int channels = 32;
   GLenum type = GL_RGBA;
-  unsigned int* pTexture = new unsigned int [this->textureSize * this->textureSize * channels];
-  memset(pTexture, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
+  unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels];
+  memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
+  unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels];
+  memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
   // Register the texture with OpenGL and bind it to the texture ID
   mat.select();
   glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
-  printf("========= CREATE %d\n", this->texture.getTexture());
 
   // Create the texture and store it on the video card
-  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTexture);
+  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection);
+
+  //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type,  GL_UNSIGNED_INT, pTexture);
+
+  //the same for the refraction
+  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
+  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction);
 
   // Set the texture quality
@@ -78,7 +82,37 @@
 
   // Since we stored the texture space with OpenGL, we can delete the image data
-  delete [] pTexture;
-
-  //  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
+  delete [] pTextureReflection;
+  delete [] pTextureRefraction;
+
+
+  /// initialization of the texture coords, speeds etc...
+  this->move = 0.0f;
+  this->g_WaterUV = 35.0f;
+  this->kNormalMapScale = 0.25f;
+  this->g_WaterFlow = 0.0015f;
+
+  /// initialization of the shaders
+  // load shader files
+/*  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
+
+  this->shader->activateShader();
+  // Set the variable "reflection" to correspond to the first texture unit
+  Shader::Uniform(shader, "reflection").set(0);
+  // Set the variable "refraction" to correspond to the second texture unit
+  Shader::Uniform(shader, "refraction").set(1);
+  // Set the variable "normalMap" to correspond to the third texture unit
+  Shader::Uniform(shader, "normalMap").set(2);
+  // Set the variable "dudvMap" to correspond to the fourth texture unit
+  Shader::Uniform(shader, "dudvMap").set(3);
+  // Set the variable "depthMap" to correspond to the fifth texture unit
+  Shader::Uniform(shader, "depthMap").set(4);
+  // Give the variable "waterColor" a blue color
+  Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f);
+  // Give the variable "lightPos" our hard coded light position
+  Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f);
+  // uniform for the camera position
+  cam_uni = new Shader::Uniform(shader, "cameraPos");
+
+  this->shader->deactivateShader();*/
 }
 
@@ -86,4 +120,5 @@
 {
   //delete shader;
+  //delete cam_uni;
 }
 
@@ -93,4 +128,5 @@
 
   LoadParam(root, "waterHeight", this, MappedWater, setHeight);
+  LoadParam(root, "lightPos", this, MappedWater, setLightPosition);
 }
 
@@ -101,97 +137,50 @@
   glTranslatef(0,this->waterHeight,0);
 
-  //glEnable(GL_LIGHTING);
-
-
-  //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1);
-  //mat.setTransparency(1.0);
-  //mat.setDiffuse(1.0, 0, .1);
-
-
-  // HACK
-
-  //glDisable(GL_BLEND);
-  //glActiveTexture(GL_TEXTURE0);
-  //glBindTexture(GL_TEXTURE_2D, this->texture);
-
+  mat.unselect();
   mat.select();
-  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
-
-  // Shader init
-  //this->shader->activateShader();
-  GLint uniform;
-
-  // Set the variable "reflection" to correspond to the first texture unit
-  uniform = glGetUniformLocationARB(shader->getProgram(), "reflection");
-  glUniform1iARB(uniform, 0); //second paramter is the texture unit
-
-  // Set the variable "refraction" to correspond to the second texture unit
-  //uniform = glGetUniformLocationARB(shader->getProgram(), "refraction");
-  //glUniform1iARB(uniform, 0);
-  // FIXME glUniform1iARB(uniform, 1);
-
-  // Set the variable "normalMap" to correspond to the third texture unit
-  //uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap");
-  //glUniform1iARB(uniform, 2);
-
-  // Set the variable "dudvMap" to correspond to the fourth texture unit
-  uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap");
-  glUniform1iARB(uniform, 3);
-
-  // Set the variable "depthMap" to correspond to the fifth texture unit
-  //uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap");
-  //glUniform1iARB(uniform, 4);
-  // FIXME we dont have a depthMap yet :-(
-
-  // Give the variable "waterColor" a blue color
-  uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor");
-  glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f);
-
-  // FIXME set camera and light information
-  Vector lightPos(100.0f, 150.0f, 100.0f);
-
-  // Store the camera position in a variable
-  //CVector3 vPosition = g_Camera.Position();
-  Vector vPosition(50.0f, 50.0f, 50.0f);
-
-  // Give the variable "lightPos" our hard coded light position
-  uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos");
-  glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f);
-
-  // Give the variable "cameraPos" our camera position
-  uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos");
-  glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f);
+
+  ///this->shader->activateShader();
+
+  // reset the camera uniform to the current cam position
+  Vector pos = State::getCameraNode()->getAbsCoor();
+  ///cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
 
   glBegin(GL_QUADS);
-  glNormal3f(0,1,0);
-  glMultiTexCoord2f(GL_TEXTURE0, 0,0);
-  glMultiTexCoord2f(GL_TEXTURE1, 0,0);
-  glMultiTexCoord2f(GL_TEXTURE2, 0,0);
-  glMultiTexCoord2f(GL_TEXTURE3, 0,0);
-  glMultiTexCoord2f(GL_TEXTURE4, 0,0);
-  glVertex3f(0,0,0);
-
-  glMultiTexCoord2f(GL_TEXTURE0, 1,0);
-  glMultiTexCoord2f(GL_TEXTURE1, 1,0);
-  glMultiTexCoord2f(GL_TEXTURE2, 1,0);
-  glMultiTexCoord2f(GL_TEXTURE3, 1,0);
-  glMultiTexCoord2f(GL_TEXTURE4, 1,0);
-  glVertex3f(100,0,0);
-
-  glMultiTexCoord2f(GL_TEXTURE0, 1,1);
-  glMultiTexCoord2f(GL_TEXTURE1, 1,1);
-  glMultiTexCoord2f(GL_TEXTURE2, 1,1);
-  glMultiTexCoord2f(GL_TEXTURE3, 1,1);
-  glMultiTexCoord2f(GL_TEXTURE4, 1,1);
-  glVertex3f(100,0,-100);
-
-  glMultiTexCoord2f(GL_TEXTURE0, 0,1);
-  glMultiTexCoord2f(GL_TEXTURE1, 0,1);
-  glMultiTexCoord2f(GL_TEXTURE2, 0,1);
-  glMultiTexCoord2f(GL_TEXTURE3, 0,1);
-  glMultiTexCoord2f(GL_TEXTURE4, 0,1);
-  glVertex3f(0,0,-100);
+  // The back left vertice for the water
+  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0);//g_WaterUV);            // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0);//refrUV - move);        // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2);     // Normal map texture
+  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                       // DUDV map texture
+  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                       // Depth texture
+  glVertex3f(0.0f, waterHeight, 0.0f);
+
+  // The front left vertice for the water
+  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1);//0.0f);                  // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1);//0.0f - move);           // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2);          // Normal map texture
+  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
+  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
+  glVertex3f(0.0f, waterHeight, 1000.0f);
+
+  // The front right vertice for the water
+  glMultiTexCoord2f(GL_TEXTURE0, 1,1); //g_WaterUV, 0.0f);             // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 1,1);//refrUV, 0.0f - move);         // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2);      // Normal map texture
+  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
+  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
+  glVertex3f(1000.0f, waterHeight, 1000.0f);
+
+  // The back right vertice for the water
+  glMultiTexCoord2f(GL_TEXTURE0, 1,0);//g_WaterUV, g_WaterUV);        // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 1,0);//refrUV, refrUV - move);       // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);  // Normal map texture
+  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
+  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
+  glVertex3f(1000.0f, waterHeight, 0.0f);
   glEnd();
-  //this->shader->deactivateShader();
+
+  ///this->shader->deactivateShader();
+
+  mat.unselect();
 
   glPopMatrix();
@@ -200,4 +189,9 @@
 void MappedWater::tick(float dt)
 {
+  // makes the water flow
+  this->move += this->g_WaterFlow;
+  this->move2 = this->move * this->kNormalMapScale;
+  this->refrUV = this->g_WaterUV;
+  this->normalUV = this->g_WaterUV * this->kNormalMapScale;
 }
 
@@ -209,54 +203,35 @@
 void MappedWater::activateReflection()
 {
+  // save viewport matrix and change the viewport size
   glPushAttrib(GL_VIEWPORT_BIT);
-  //   glPushMatrix();
-
-  //glLoadIdentity();
   glViewport(0,0, textureSize, textureSize);
 
-
-  // Clear the color and depth bits, reset the matrix and position our camera.
-
-  //g_Camera.Look();
-
+  glPushMatrix();
 
   // If our camera is above the water we will render the scene flipped upside down.
   // In order to line up the reflection nicely with the world we have to translate
   // the world to the position of our reflected surface, multiplied by two.
-  //if(g_Camera.Position().y > waterHeight)
-  //{
-  // Translate the world, then flip it upside down
-  //   glTranslatef(0.0f, waterHeight*2.0f, 0.0f);
-  //  glScalef(1.0, -1.0, 1.0);
-
-  // Since the world is updside down we need to change the culling to FRONT
-  //glCullFace(GL_FRONT);
-
-  // Set our plane equation and turn clipping on
-  // double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
-  //   glEnable(GL_CLIP_PLANE0);
-  // glClipPlane(GL_CLIP_PLANE0, plane);
-
-  // Render the world upside down and clipped (only render the top flipped).
-  // If we don't turn OFF caustics for the reflection texture we get horrible
-  // artifacts in the water.  That is why we set bRenderCaustics to FALSE.
-  //RenderWorld(false);
-
-  // Turn clipping off
-  // glDisable(GL_CLIP_PLANE0);
-
-  // Restore back-face culling
-  //  glCullFace(GL_BACK);
-  //}
-  /*else
+  glEnable(GL_CLIP_PLANE0);
+  Vector pos = State::getCameraNode()->getAbsCoor();
+  if(pos.y > waterHeight)
   {
-      // If the camera is below the water we don't want to flip the world,
-      // but just render it clipped so only the top is drawn.
-      double plane[4] = {0.0, 1.0, 0.0, waterHeight};
-      glEnable(GL_CLIP_PLANE0);
-      glClipPlane(GL_CLIP_PLANE0, plane);
-      RenderWorld(true);
-      glDisable(GL_CLIP_PLANE0);
-  }*/
+    // Translate the world, then flip it upside down
+    glTranslatef(0.0f, waterHeight*2.0f, 0.0f);
+    glScalef(1.0, -1.0, 1.0);
+
+    // Since the world is updside down we need to change the culling to FRONT
+    glCullFace(GL_FRONT);
+
+    // Set our plane equation and turn clipping on
+    double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
+    glClipPlane(GL_CLIP_PLANE0, plane);
+  }
+  else
+  {
+    // If the camera is below the water we don't want to flip the world,
+    // but just render it clipped so only the top is drawn.
+    double plane[4] = {0.0, 1.0, 0.0, waterHeight};
+    glClipPlane(GL_CLIP_PLANE0, plane);
+  }
 }
 
@@ -264,21 +239,69 @@
 void MappedWater::deactivateReflection()
 {
-//  glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select();
-  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
-
-  //mat.setDiffuseMap(&texture, 0);
-  // mat.select();
-  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
-
-
+  glDisable(GL_CLIP_PLANE0);
+  glCullFace(GL_BACK);
+
+  mat.select();
+  //glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
+  mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
+
+  glPopMatrix();
   glPopAttrib();
-  //   glPopMatrix();
 }
 
 void MappedWater::activateRefraction()
 {
+  // To create the refraction and depth textures we do the same thing
+  // we did for the reflection texture, except we don't need to turn
+  // the world upside down.  We want to find the depth of the water,
+  // not the depth of the sky and above water terrain.
+
+  // save viewport matrix and change the viewport size
+  glPushAttrib(GL_VIEWPORT_BIT);
+  glViewport(0,0, textureSize, textureSize);
+
+  glPushMatrix();
+
+  // If our camera is above the water we will render only the parts that
+  // are under the water.  If the camera is below the water we render
+  // only the stuff above the water.  Like the reflection texture, we
+  // incorporate clipping planes to only render what we need.
+
+  // If the camera is above water, render the data below the water
+  glEnable(GL_CLIP_PLANE0);
+  Vector pos = State::getCameraNode()->getAbsCoor();
+  if(pos.y > waterHeight)
+  {
+    double plane[4] = {0.0, -1.0, 0.0, waterHeight}; 
+    glClipPlane(GL_CLIP_PLANE0, plane);
+  }
+  // If the camera is below the water, only render the data above the water
+  else
+  {
+    glCullFace(GL_FRONT);
+    double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; 
+    glClipPlane(GL_CLIP_PLANE0, plane);
+  }
 }
 
 void MappedWater::deactivateRefraction()
 {
-}
+  glDisable(GL_CLIP_PLANE0);
+  glCullFace(GL_BACK);
+
+  mat.select();
+  mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
+  //glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, textureSize, textureSize, 0);
+  //mat.renderToTexture(0, GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, textureSize, textureSize, 0);
+
+  // Bind the current scene to our refraction texture
+//  glBindTexture(GL_TEXTURE_2D, g_Texture[REFRACTION_ID]);
+//  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
+
+  // Bind the current scene to our depth texture
+//  glBindTexture(GL_TEXTURE_2D, g_Texture[DEPTH_ID]);
+//  glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, textureSize, textureSize, 0);
+
+  glPopMatrix();
+  glPopAttrib();
+}
Index: trunk/src/world_entities/environments/mapped_water.h
===================================================================
--- trunk/src/world_entities/environments/mapped_water.h	(revision 8035)
+++ trunk/src/world_entities/environments/mapped_water.h	(revision 8037)
@@ -10,5 +10,4 @@
 #include "world_entity.h"
 #include "material.h"
-#include "texture.h"
 #include "shader.h"
 
@@ -20,4 +19,5 @@
 
     void loadParams(const TiXmlElement* root);
+
 
     void activateReflection();
@@ -31,13 +31,23 @@
 
   private:
+    void setLightPosition(float x, float y, float z) { this->lightPos = Vector(x,y,z); };
     void setHeight(float height);
 
   private:
-    float           waterHeight;       //!< y-coord of the Water
-    Material        mat;
+    float               waterHeight;                //!< y-coord of the Water
 
-    Texture         texture;
-    int             textureSize;       //!< size of the texture
-    Shader*         shader;
+    float               move;                       //!< textures coords, speeds, positions for the shaded textures....
+    float               move2;                      //!<
+    float               g_WaterUV;                  //!<
+    float               g_WaterFlow;                //!<
+    float               refrUV;                     //!<
+    float               normalUV;                   //!<
+    float               kNormalMapScale;            //!<
+
+    int                 textureSize;                //!< size of the texture
+    Vector              lightPos;
+    Material            mat;
+    Shader*             shader;
+    Shader::Uniform*    cam_uni;                        //!< uniform that is used for the camera position
 
 };
Index: trunk/src/world_entities/skybox.cc
===================================================================
--- trunk/src/world_entities/skybox.cc	(revision 8035)
+++ trunk/src/world_entities/skybox.cc	(revision 8037)
@@ -74,4 +74,5 @@
   this->setClassID(CL_SKYBOX, "SkyBox");
   this->toList(OM_BACKGROUND);
+  this->toReflectionList();
   //this->size = 100.0;
   this->textureSize = 1024.0f;
Index: trunk/src/world_entities/terrain.cc
===================================================================
--- trunk/src/world_entities/terrain.cc	(revision 8035)
+++ trunk/src/world_entities/terrain.cc	(revision 8037)
@@ -106,4 +106,5 @@
   this->setClassID(CL_TERRAIN, "Terrain");
   this->toList(OM_ENVIRON_NOTICK);
+  this->toReflectionList();
 
   this->objectList = 0;
Index: trunk/src/world_entities/world_entity.cc
===================================================================
--- trunk/src/world_entities/world_entity.cc	(revision 8035)
+++ trunk/src/world_entities/world_entity.cc	(revision 8037)
@@ -270,5 +270,14 @@
 }
 
-
+void WorldEntity::toReflectionList()
+{
+  State::getObjectManager()->toReflectionList( this );
+}
+
+void removeFromReflectionList()
+{
+/// TODO
+///  State::getObject
+}
 
 /**
Index: trunk/src/world_entities/world_entity.h
===================================================================
--- trunk/src/world_entities/world_entity.h	(revision 8035)
+++ trunk/src/world_entities/world_entity.h	(revision 8037)
@@ -80,4 +80,8 @@
   /* --- Object Manager Block --- */
   void toList(OM_LIST list);
+
+  void toReflectionList();
+  void removeFromReflectionList();
+
   /** @returns a Reference to the objectListNumber to set. */
   OM_LIST& getOMListNumber() { return this->objectListNumber; }
