Index: /branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.cc
===================================================================
--- /branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.cc	(revision 8635)
+++ /branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.cc	(revision 8636)
@@ -1,16 +1,16 @@
 /*
-	orxonox - the future of 3D-vertical-scrollers
-
-	Copyright (C) 2004 orx
-
-	This program is free software; you can redistribute it and/or modify
-	it under the terms of the GNU General Public License as published by
-	the Free Software Foundation; either version 2, or (at your option)
-	any later version.
-
+  orxonox - the future of 3D-vertical-scrollers
+ 
+  Copyright (C) 2004 orx
+ 
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2, or (at your option)
+  any later version.
+ 
 ### File Specific:
-	main-programmer: hdavid, amaechler
-
-	INSPIRED BY http://www.codesampler.com/usersrc/usersrc_6.htm#oglu_sky_dome_shader
+  main-programmer: hdavid, amaechler
+ 
+  INSPIRED BY http://www.codesampler.com/usersrc/usersrc_6.htm#oglu_sky_dome_shader
 */
 
@@ -40,19 +40,19 @@
 CloudEffect::CloudEffect(const TiXmlElement* root)
 {
-	this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
-//  this->toList(OM_ENVIRON);
-
-	this->init();
-
-	if (root != NULL)
-		this->loadParams(root);
-
-	//if(cloudActivate)
-		this->activate();
+  this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
+  //  this->toList(OM_ENVIRON);
+
+  this->init();
+
+  if (root != NULL)
+    this->loadParams(root);
+
+  //if(cloudActivate)
+  this->activate();
 }
 
 CloudEffect::~CloudEffect()
 {
-	this->deactivate();
+  this->deactivate();
 }
 
@@ -60,17 +60,32 @@
 void CloudEffect::init()
 {
-	PRINTF(0)("Initializing CloudEffect\n");
+  PRINTF(0)("Initializing CloudEffect\n");
+
+  noise3DTexSize = 64;
+  noise3DTexName = 0;
+
+  this->make3DNoiseTexture();
+
+  glGenTextures(1, &noise3DTexName);
+  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
+
+  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
+  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+  glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
+               noise3DTexSize, noise3DTexSize, noise3DTexSize,
+               0, GL_RGBA, GL_UNSIGNED_BYTE, noise3DTexPtr);
+
+
+  //cloudMaterial.setDiffuseMap("pictures/sky/cloud1.jpg", GL_TEXTURE_2D, 0);
+
+  coor = 0;
   
-    /// 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;*/
-  
-  cloudMaterial.setDiffuseMap("pictures/sky/cloud1.jpg", GL_TEXTURE_2D, 0);
-
   shader = new Shader(ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.vert",
                       ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.frag");
-  
+
   this->shader->activateShader();
 
@@ -78,11 +93,9 @@
   Shader::Uniform(shader, "SkyColor").set(0.0f, 0.0f, 0.8f);
   Shader::Uniform(shader, "CloudColor").set(0.8f, 0.8f, 0.8f);
+  Shader::Uniform(shader, "LightPos").set(5.0f, 0.0f, 5.0f);
+  Shader::Uniform(shader, "Scale").set(0.1f);
   
-  // Change: Have to be updated each frame!like camerpos in mapped_water.cc
-  Shader::Uniform(shader, "Offset").set(0.0f, 0.0f, 0.0f);
-  
-  Shader::Uniform(shader, "LightPos").set(100.0f, 100.0f, 100.0f);
-  Shader::Uniform(shader, "Scale").set(1.0f);
-  
+  offset = new Shader::Uniform(shader, "Offset");
+
   this->shader->deactivateShader();
 }
@@ -91,13 +104,13 @@
 void CloudEffect::loadParams(const TiXmlElement* root)
 {
-	WeatherEffect::loadParams(root);
-
-	// LoadParam(root, "speed", this, CloudEffect, setCloudAnimation);
-	// LoadParam(root, "texture", this, CloudEffect, setCloudTexture);
+  WeatherEffect::loadParams(root);
+
+  // LoadParam(root, "speed", this, CloudEffect, setCloudAnimation);
+  // LoadParam(root, "texture", this, CloudEffect, setCloudTexture);
   /*
-	LOAD_PARAM_START_CYCLE(root, element);
-	{
-		LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
-	}
+  LOAD_PARAM_START_CYCLE(root, element);
+  {
+  LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
+  }
   LOAD_PARAM_END_CYCLE(element);*/
 }
@@ -106,20 +119,30 @@
 void CloudEffect::activate()
 {
-	PRINTF(0)( "Activating\n");
-
-//	this->cloudActivate = true;
+  PRINTF(0)( "Activating\n");
+
+  //  this->cloudActivate = true;
 }
 
 void CloudEffect::deactivate()
 {
-	PRINTF(0)("Deactivating CloudEffect\n");
-
-//	this->cloudActivate = false;
+  PRINTF(0)("Deactivating CloudEffect\n");
+
+  //  this->cloudActivate = false;
 }
 
 void CloudEffect::draw() const
 {
-	//if (!this->cloudActivate)
-	//	return;
+  //if (!this->cloudActivate)
+  //  return;
+  glPushAttrib(GL_ENABLE_BIT);
+
+  glDisable(GL_LIGHTING);
+  glDisable(GL_BLEND);
+
+  glEnable(GL_TEXTURE_3D);
+  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
+
+
+  //this->shader->activateShader();
 
   //glMatrixMode(GL_MODELVIEW);
@@ -128,31 +151,192 @@
   //glTranslatef(0,this->waterHeight,0);
 
-  cloudMaterial.unselect();
-  cloudMaterial.select();
+  //cloudMaterial.unselect();
+  //cloudMaterial.select();
 
   this->shader->activateShader();
-
-
+  //PRINTF(0)("coor: %f\n", coor);
+  offset->set(coor, coor, coor);
+
+  //glutSolidTeapot(1);
+
+
+  glColor3f(1.0, 1.0, 1.0);
   glBegin(GL_QUADS);
-  
+
   glTexCoord2f(1.0f, 1.0f); glVertex3f(0.0f, 0.0f,  0.0f);
-  glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 0.0f,  100.0f);
-  glTexCoord2f(0.0f, 0.0f); glVertex3f( 100.0f, 0.0f,  100.0f);
-  glTexCoord2f(1.0f, 0.0f); glVertex3f(100.0f, 0.0f,  0.0f);
-  
+  glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 10.0f,  0.0f);
+  glTexCoord2f(0.0f, 0.0f); glVertex3f( 10.0f, 10.0f,  0.0f);
+  glTexCoord2f(1.0f, 0.0f); glVertex3f(10.0f, 0.0f,  0.0f);
+
   glEnd();
 
   this->shader->deactivateShader();
 
-  cloudMaterial.unselect();
+  //cloudMaterial.unselect();
 
   glPopMatrix();
 
+  glPopAttrib();
+
 }
 
 void CloudEffect::tick (float dt)
 {
-	//if (!this->cloudActivate)
-	//	return;
-}
-
+  //if (!this->cloudActivate)
+  //  return;
+  coor += 10;
+}
+
+void CloudEffect::make3DNoiseTexture()
+{
+  int f, i, j, k, inc;
+  int startFrequency = 4;
+  int numOctaves = 4;
+  double ni[3];
+  double inci, incj, inck;
+  int frequency = startFrequency;
+  GLubyte *ptr;
+  double amp = 0.5;
+
+  if ((noise3DTexPtr = (GLubyte *) malloc(noise3DTexSize *
+                                          noise3DTexSize *
+                                          noise3DTexSize * 4)) == NULL)
+    PRINTF(0)("ERROR: Could not allocate 3D noise texture\n");
+
+  for (f=0, inc=0; f < numOctaves;
+       ++f, frequency *= 2, ++inc, amp *= 0.5)
+  {
+    SetNoiseFrequency(frequency);
+    ptr = noise3DTexPtr;
+    ni[0] = ni[1] = ni[2] = 0;
+
+    inci = 1.0 / (noise3DTexSize / frequency);
+    for (i=0; i<noise3DTexSize; ++i, ni[0] += inci)
+    {
+      incj = 1.0 / (noise3DTexSize / frequency);
+      for (j=0; j<noise3DTexSize; ++j, ni[1] += incj)
+      {
+        inck = 1.0 / (noise3DTexSize / frequency);
+        for (k=0; k<noise3DTexSize; ++k, ni[2] += inck, ptr+= 4)
+        {
+          *(ptr+inc) = (GLubyte) (((noise3(ni)+1.0) * amp)*128.0);
+        }
+      }
+    }
+  }
+}
+
+void CloudEffect::initNoise()
+{
+  int i, j, k;
+
+  srand(30757);
+  for (i = 0 ; i < B ; i++)
+  {
+    p[i] = i;
+    g1[i] = (double)((rand() % (B + B)) - B) / B;
+
+    for (j = 0 ; j < 2 ; j++)
+      g2[i][j] = (double)((rand() % (B + B)) - B) / B;
+    normalize2(g2[i]);
+
+    for (j = 0 ; j < 3 ; j++)
+      g3[i][j] = (double)((rand() % (B + B)) - B) / B;
+    normalize3(g3[i]);
+  }
+
+  while (--i)
+  {
+    k = p[i];
+    p[i] = p[j = rand() % B];
+    p[j] = k;
+  }
+
+  for (i = 0 ; i < B + 2 ; i++)
+  {
+    p[B + i] = p[i];
+    g1[B + i] = g1[i];
+    for (j = 0 ; j < 2 ; j++)
+      g2[B + i][j] = g2[i][j];
+    for (j = 0 ; j < 3 ; j++)
+      g3[B + i][j] = g3[i][j];
+  }
+}
+
+void CloudEffect::SetNoiseFrequency( int frequency)
+{
+  start = 1;
+  B = frequency;
+  BM = B-1;
+}
+
+double CloudEffect::noise3( double vec[3])
+{
+  int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
+  double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
+  int i, j;
+
+  if (start)
+  {
+    start = 0;
+    initNoise();
+  }
+
+  setup(0, bx0,bx1, rx0,rx1);
+  setup(1, by0,by1, ry0,ry1);
+  setup(2, bz0,bz1, rz0,rz1);
+
+  i = p[ bx0 ];
+  j = p[ bx1 ];
+
+  b00 = p[ i + by0 ];
+  b10 = p[ j + by0 ];
+  b01 = p[ i + by1 ];
+  b11 = p[ j + by1 ];
+
+  t  = s_curve(rx0);
+  sy = s_curve(ry0);
+  sz = s_curve(rz0);
+
+  q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
+  q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
+  a = lerp(t, u, v);
+
+  q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
+  q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
+  b = lerp(t, u, v);
+
+  c = lerp(sy, a, b);
+
+  q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
+  q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
+  a = lerp(t, u, v);
+
+  q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
+  q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
+  b = lerp(t, u, v);
+
+  d = lerp(sy, a, b);
+
+  return lerp(sz, c, d);
+}
+
+void CloudEffect::normalize2( double v[2])
+{
+  double s;
+
+  s = sqrt(v[0] * v[0] + v[1] * v[1]);
+  v[0] = v[0] / s;
+  v[1] = v[1] / s;
+}
+
+void CloudEffect::normalize3( double v[3])
+{
+  double s;
+
+  s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
+  v[0] = v[0] / s;
+  v[1] = v[1] / s;
+  v[2] = v[2] / s;
+}
+
Index: /branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.h
===================================================================
--- /branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.h	(revision 8635)
+++ /branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.h	(revision 8636)
@@ -15,39 +15,81 @@
 #include "shader.h"
 
-class CloudEffect : public WeatherEffect {
+#define MAXB 0x100
+#define N 0x1000
+#define NP 12   /* 2^N */
+#define NM 0xfff
+
+#define s_curve(t) ( t * t * (3. - 2. * t) )
+#define lerp(t, a, b) ( a + t * (b - a) )
+#define setup(i,b0,b1,r0,r1)\
+        t = vec[i] + N;\
+        b0 = ((int)t) & BM;\
+        b1 = (b0+1) & BM;\
+        r0 = t - (int)t;\
+        r1 = r0 - 1.;
+#define at2(rx,ry) ( rx * q[0] + ry * q[1] )
+#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
+
+class CloudEffect : public WeatherEffect
+{
 public:
-    CloudEffect(const TiXmlElement* root = NULL);
-    virtual ~CloudEffect();
+  CloudEffect(const TiXmlElement* root = NULL);
+  virtual ~CloudEffect();
 
-    virtual void loadParams(const TiXmlElement* root);
+  virtual void loadParams(const TiXmlElement* root);
 
-    virtual void init();
+  virtual void init();
 
-    virtual void activate();
-    virtual void deactivate();
+  virtual void activate();
+  virtual void deactivate();
 
-    inline void activateCloud() {
-        this->activate();
-    }
-    inline void deactivateCloud() {
-        this->deactivate();
-    }
+  inline void activateCloud()
+  {
+    this->activate();
+  }
+  inline void deactivateCloud()
+  {
+    this->deactivate();
+  }
 
-    virtual void draw() const;
-    virtual void tick(float dt);
+  virtual void draw() const;
+  virtual void tick(float dt);
 
-    /*inline void setCloudOption(const std::string& option) {
-        if (option == "activate")
-    this->cloudActivate = true;
-}*/
+  void make3DNoiseTexture();
+  void initNoise();
+  void SetNoiseFrequency(int frequency);
+  double noise3(double vec[3]);
+  void normalize2(double v[2]);
+  void normalize3(double v[3]);
+
+  /*inline void setCloudOption(const std::string& option) {
+      if (option == "activate")
+  this->cloudActivate = true;
+  }*/
 
 
 private:
-    //void initialize(char* fileName);
+  //void initialize(char* fileName);
 
-    //bool        cloudActivate;
+  //bool        cloudActivate;
 
-    Material                 cloudMaterial;
-    Shader*                 shader;
+  // Material                 cloudMaterial;
+  Shader*                 shader;
+  Shader::Uniform*        offset;
+  
+  float coor;
+
+  int noise3DTexSize;
+  GLuint noise3DTexName;
+  GLubyte *noise3DTexPtr;
+
+  int p[MAXB + MAXB + 2];
+  double g3[MAXB + MAXB + 2][3];
+  double g2[MAXB + MAXB + 2][2];
+  double g1[MAXB + MAXB + 2];
+
+  int start;
+  int B;
+  int BM;
 };
 
