Index: trunk/src/world_entities/environments/mapped_water.cc
===================================================================
--- trunk/src/world_entities/environments/mapped_water.cc	(revision 8310)
+++ trunk/src/world_entities/environments/mapped_water.cc	(revision 8312)
@@ -40,11 +40,11 @@
   // set up refleciton texture
   //mat.setDiffuseMap(this->texture, 0);
-  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0);
+  mat.setDiffuseMap("pictures/refl.bmp", GL_TEXTURE_2D, 0);
   // load refraction texture
-  mat.setDiffuseMap("pictures/error_texture.png", GL_TEXTURE_2D, 1);
+  mat.setDiffuseMap("pictures/refr.bmp", GL_TEXTURE_2D, 1);
   // load normal map
-  //mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
+  mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
   // load dudv map
-  //mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
+  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
   // set up depth texture
   //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4);
@@ -56,32 +56,51 @@
   // set the size of the refraction and reflection textures
   this->textureSize = 512;
+  //unsigned int channels = 32;
+  //GLenum type = GL_RGBA;
+  //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));
+
+  // 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, 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);
+
   unsigned int channels = 32;
   GLenum type = GL_RGBA;
   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();
+  //mat.select();
   glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
-
-  // 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, 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
   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection);
+
+
+  unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels];
+  memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
+  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
+  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction);
+
+  // Set the texture quality
+
 
   // Since we stored the texture space with OpenGL, we can delete the image data
-  delete [] pTextureReflection;
-  delete [] pTextureRefraction;
+  //delete [] pTextureReflection;
+  //delete [] pTextureRefraction;
 
 
@@ -94,5 +113,5 @@
   /// initialization of the shaders
   // load shader files
-/*  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
+  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
 
   this->shader->activateShader();
@@ -106,5 +125,5 @@
   Shader::Uniform(shader, "dudvMap").set(3);
   // Set the variable "depthMap" to correspond to the fifth texture unit
-  Shader::Uniform(shader, "depthMap").set(4);
+  Shader::Uniform(shader, "depthMap").set(2);
   // Give the variable "waterColor" a blue color
   Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f);
@@ -114,5 +133,5 @@
   cam_uni = new Shader::Uniform(shader, "cameraPos");
 
-  this->shader->deactivateShader();*/
+  this->shader->deactivateShader();
 }
 
@@ -134,4 +153,6 @@
 void MappedWater::draw() const
 {
+  glMatrixMode(GL_MODELVIEW);
+
   glPushMatrix();
   glTranslatef(0,this->waterHeight,0);
@@ -140,45 +161,55 @@
   mat.select();
 
-  ///this->shader->activateShader();
+  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);
+  cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
+
+  //glDisable(GL_BLEND);
 
   glBegin(GL_QUADS);
   // 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_TEXTURE0, 0.0f, 0);//g_WaterUV);            // Reflection texture
+ // glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0);//refrUV - move);        // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV);            // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 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
+  //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_TEXTURE0, 0.0f, 1);//0.0f);                  // Reflection texture
+ // glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1);//0.0f - move);           // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);                  // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 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
+  //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_TEXTURE0, 1,1); //g_WaterUV, 0.0f);             // Reflection texture
+ // glMultiTexCoord2f(GL_TEXTURE1, 1,1);//refrUV, 0.0f - move);         // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f);             // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 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
+  //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_TEXTURE0, 1,0);//g_WaterUV, g_WaterUV);        // Reflection texture
+ // glMultiTexCoord2f(GL_TEXTURE1, 1,0);//refrUV, refrUV - move);       // Refraction texture
+  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV);        // Reflection texture
+  glMultiTexCoord2f(GL_TEXTURE1, 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
+  //glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
   glVertex3f(1000.0f, waterHeight, 0.0f);
   glEnd();
 
-  ///this->shader->deactivateShader();
+  this->shader->deactivateShader();
 
   mat.unselect();
@@ -207,4 +238,5 @@
   glViewport(0,0, textureSize, textureSize);
 
+  glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
 
@@ -214,4 +246,6 @@
   glEnable(GL_CLIP_PLANE0);
   Vector pos = State::getCameraNode()->getAbsCoor();
+  //pos.debug();
+  //PRINTF(0)("waterheight: %f\n", waterHeight);
   if(pos.y > waterHeight)
   {
@@ -242,7 +276,14 @@
   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);
+  //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);
+  ///
+
+  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
+
+  // Create the texture and store it on the video card
+  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
+
 
   glPopMatrix();
@@ -251,5 +292,41 @@
 
 void MappedWater::activateRefraction()
-{
+{/*
+  // save viewport matrix and change the viewport size
+  glPushAttrib(GL_VIEWPORT_BIT);
+  glViewport(0,0, textureSize, textureSize);
+
+  glMatrixMode(GL_MODELVIEW);
+  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.
+  glEnable(GL_CLIP_PLANE0);
+  Vector pos = State::getCameraNode()->getAbsCoor();
+  //pos.debug();
+  //PRINTF(0)("waterheight: %f\n", waterHeight);
+  if(pos.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};
+    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);
+  }
+*/
+
   // 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
@@ -261,4 +338,5 @@
   glViewport(0,0, textureSize, textureSize);
 
+  glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
 
@@ -275,4 +353,5 @@
     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
@@ -290,8 +369,12 @@
   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);
+
+  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
+
+  // Create the texture and store it on the video card
+  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
+
+  glPopMatrix();
+  glPopAttrib();
 
   // Bind the current scene to our refraction texture
@@ -303,5 +386,3 @@
 //  glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, textureSize, textureSize, 0);
 
-  glPopMatrix();
-  glPopAttrib();
-}
+}
