Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 20, 2006, 3:42:03 PM (18 years ago)
Author:
hdavid
Message:

branches/atmospheric_engine: creating clouds with the shader works, animation of the clouds with the shader still not working

Location:
branches/atmospheric_engine/src/lib/graphics/effects
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.cc

    r8495 r8636  
    11/*
    2         orxonox - the future of 3D-vertical-scrollers
    3 
    4         Copyright (C) 2004 orx
    5 
    6         This program is free software; you can redistribute it and/or modify
    7         it under the terms of the GNU General Public License as published by
    8         the Free Software Foundation; either version 2, or (at your option)
    9         any later version.
    10 
     2  orxonox - the future of 3D-vertical-scrollers
     3 
     4  Copyright (C) 2004 orx
     5 
     6  This program is free software; you can redistribute it and/or modify
     7  it under the terms of the GNU General Public License as published by
     8  the Free Software Foundation; either version 2, or (at your option)
     9  any later version.
     10 
    1111### File Specific:
    12         main-programmer: hdavid, amaechler
    13 
    14         INSPIRED BY http://www.codesampler.com/usersrc/usersrc_6.htm#oglu_sky_dome_shader
     12  main-programmer: hdavid, amaechler
     13 
     14  INSPIRED BY http://www.codesampler.com/usersrc/usersrc_6.htm#oglu_sky_dome_shader
    1515*/
    1616
     
    4040CloudEffect::CloudEffect(const TiXmlElement* root)
    4141{
    42         this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
    43 //  this->toList(OM_ENVIRON);
    44 
    45         this->init();
    46 
    47         if (root != NULL)
    48                 this->loadParams(root);
    49 
    50         //if(cloudActivate)
    51                 this->activate();
     42  this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
     43  //  this->toList(OM_ENVIRON);
     44
     45  this->init();
     46
     47  if (root != NULL)
     48    this->loadParams(root);
     49
     50  //if(cloudActivate)
     51  this->activate();
    5252}
    5353
    5454CloudEffect::~CloudEffect()
    5555{
    56         this->deactivate();
     56  this->deactivate();
    5757}
    5858
     
    6060void CloudEffect::init()
    6161{
    62         PRINTF(0)("Initializing CloudEffect\n");
     62  PRINTF(0)("Initializing CloudEffect\n");
     63
     64  noise3DTexSize = 64;
     65  noise3DTexName = 0;
     66
     67  this->make3DNoiseTexture();
     68
     69  glGenTextures(1, &noise3DTexName);
     70  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
     71
     72  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     73  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     74  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
     75  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     76  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     77
     78  glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
     79               noise3DTexSize, noise3DTexSize, noise3DTexSize,
     80               0, GL_RGBA, GL_UNSIGNED_BYTE, noise3DTexPtr);
     81
     82
     83  //cloudMaterial.setDiffuseMap("pictures/sky/cloud1.jpg", GL_TEXTURE_2D, 0);
     84
     85  coor = 0;
    6386 
    64     /// initialization of the texture coords, speeds etc...
    65 /*  this->move = 0.0f;
    66   this->g_WaterUV = 35.0f;
    67   this->kNormalMapScale = 0.25f;
    68   this->g_WaterFlow = 0.0015f;*/
    69  
    70   cloudMaterial.setDiffuseMap("pictures/sky/cloud1.jpg", GL_TEXTURE_2D, 0);
    71 
    7287  shader = new Shader(ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.vert",
    7388                      ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.frag");
    74  
     89
    7590  this->shader->activateShader();
    7691
     
    7893  Shader::Uniform(shader, "SkyColor").set(0.0f, 0.0f, 0.8f);
    7994  Shader::Uniform(shader, "CloudColor").set(0.8f, 0.8f, 0.8f);
     95  Shader::Uniform(shader, "LightPos").set(5.0f, 0.0f, 5.0f);
     96  Shader::Uniform(shader, "Scale").set(0.1f);
    8097 
    81   // Change: Have to be updated each frame!like camerpos in mapped_water.cc
    82   Shader::Uniform(shader, "Offset").set(0.0f, 0.0f, 0.0f);
    83  
    84   Shader::Uniform(shader, "LightPos").set(100.0f, 100.0f, 100.0f);
    85   Shader::Uniform(shader, "Scale").set(1.0f);
    86  
     98  offset = new Shader::Uniform(shader, "Offset");
     99
    87100  this->shader->deactivateShader();
    88101}
     
    91104void CloudEffect::loadParams(const TiXmlElement* root)
    92105{
    93         WeatherEffect::loadParams(root);
    94 
    95         // LoadParam(root, "speed", this, CloudEffect, setCloudAnimation);
    96         // LoadParam(root, "texture", this, CloudEffect, setCloudTexture);
     106  WeatherEffect::loadParams(root);
     107
     108  // LoadParam(root, "speed", this, CloudEffect, setCloudAnimation);
     109  // LoadParam(root, "texture", this, CloudEffect, setCloudTexture);
    97110  /*
    98         LOAD_PARAM_START_CYCLE(root, element);
    99         {
    100                 LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
    101         }
     111  LOAD_PARAM_START_CYCLE(root, element);
     112  {
     113  LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
     114  }
    102115  LOAD_PARAM_END_CYCLE(element);*/
    103116}
     
    106119void CloudEffect::activate()
    107120{
    108         PRINTF(0)( "Activating\n");
    109 
    110 //      this->cloudActivate = true;
     121  PRINTF(0)( "Activating\n");
     122
     123  //  this->cloudActivate = true;
    111124}
    112125
    113126void CloudEffect::deactivate()
    114127{
    115         PRINTF(0)("Deactivating CloudEffect\n");
    116 
    117 //      this->cloudActivate = false;
     128  PRINTF(0)("Deactivating CloudEffect\n");
     129
     130  //  this->cloudActivate = false;
    118131}
    119132
    120133void CloudEffect::draw() const
    121134{
    122         //if (!this->cloudActivate)
    123         //      return;
     135  //if (!this->cloudActivate)
     136  //  return;
     137  glPushAttrib(GL_ENABLE_BIT);
     138
     139  glDisable(GL_LIGHTING);
     140  glDisable(GL_BLEND);
     141
     142  glEnable(GL_TEXTURE_3D);
     143  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
     144
     145
     146  //this->shader->activateShader();
    124147
    125148  //glMatrixMode(GL_MODELVIEW);
     
    128151  //glTranslatef(0,this->waterHeight,0);
    129152
    130   cloudMaterial.unselect();
    131   cloudMaterial.select();
     153  //cloudMaterial.unselect();
     154  //cloudMaterial.select();
    132155
    133156  this->shader->activateShader();
    134 
    135 
     157  //PRINTF(0)("coor: %f\n", coor);
     158  offset->set(coor, coor, coor);
     159
     160  //glutSolidTeapot(1);
     161
     162
     163  glColor3f(1.0, 1.0, 1.0);
    136164  glBegin(GL_QUADS);
    137  
     165
    138166  glTexCoord2f(1.0f, 1.0f); glVertex3f(0.0f, 0.0f,  0.0f);
    139   glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 0.0f,  100.0f);
    140   glTexCoord2f(0.0f, 0.0f); glVertex3f( 100.0f, 0.0f,  100.0f);
    141   glTexCoord2f(1.0f, 0.0f); glVertex3f(100.0f, 0.0f,  0.0f);
    142  
     167  glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 10.0f,  0.0f);
     168  glTexCoord2f(0.0f, 0.0f); glVertex3f( 10.0f, 10.0f,  0.0f);
     169  glTexCoord2f(1.0f, 0.0f); glVertex3f(10.0f, 0.0f,  0.0f);
     170
    143171  glEnd();
    144172
    145173  this->shader->deactivateShader();
    146174
    147   cloudMaterial.unselect();
     175  //cloudMaterial.unselect();
    148176
    149177  glPopMatrix();
    150178
     179  glPopAttrib();
     180
    151181}
    152182
    153183void CloudEffect::tick (float dt)
    154184{
    155         //if (!this->cloudActivate)
    156         //      return;
    157 }
    158 
     185  //if (!this->cloudActivate)
     186  //  return;
     187  coor += 10;
     188}
     189
     190void CloudEffect::make3DNoiseTexture()
     191{
     192  int f, i, j, k, inc;
     193  int startFrequency = 4;
     194  int numOctaves = 4;
     195  double ni[3];
     196  double inci, incj, inck;
     197  int frequency = startFrequency;
     198  GLubyte *ptr;
     199  double amp = 0.5;
     200
     201  if ((noise3DTexPtr = (GLubyte *) malloc(noise3DTexSize *
     202                                          noise3DTexSize *
     203                                          noise3DTexSize * 4)) == NULL)
     204    PRINTF(0)("ERROR: Could not allocate 3D noise texture\n");
     205
     206  for (f=0, inc=0; f < numOctaves;
     207       ++f, frequency *= 2, ++inc, amp *= 0.5)
     208  {
     209    SetNoiseFrequency(frequency);
     210    ptr = noise3DTexPtr;
     211    ni[0] = ni[1] = ni[2] = 0;
     212
     213    inci = 1.0 / (noise3DTexSize / frequency);
     214    for (i=0; i<noise3DTexSize; ++i, ni[0] += inci)
     215    {
     216      incj = 1.0 / (noise3DTexSize / frequency);
     217      for (j=0; j<noise3DTexSize; ++j, ni[1] += incj)
     218      {
     219        inck = 1.0 / (noise3DTexSize / frequency);
     220        for (k=0; k<noise3DTexSize; ++k, ni[2] += inck, ptr+= 4)
     221        {
     222          *(ptr+inc) = (GLubyte) (((noise3(ni)+1.0) * amp)*128.0);
     223        }
     224      }
     225    }
     226  }
     227}
     228
     229void CloudEffect::initNoise()
     230{
     231  int i, j, k;
     232
     233  srand(30757);
     234  for (i = 0 ; i < B ; i++)
     235  {
     236    p[i] = i;
     237    g1[i] = (double)((rand() % (B + B)) - B) / B;
     238
     239    for (j = 0 ; j < 2 ; j++)
     240      g2[i][j] = (double)((rand() % (B + B)) - B) / B;
     241    normalize2(g2[i]);
     242
     243    for (j = 0 ; j < 3 ; j++)
     244      g3[i][j] = (double)((rand() % (B + B)) - B) / B;
     245    normalize3(g3[i]);
     246  }
     247
     248  while (--i)
     249  {
     250    k = p[i];
     251    p[i] = p[j = rand() % B];
     252    p[j] = k;
     253  }
     254
     255  for (i = 0 ; i < B + 2 ; i++)
     256  {
     257    p[B + i] = p[i];
     258    g1[B + i] = g1[i];
     259    for (j = 0 ; j < 2 ; j++)
     260      g2[B + i][j] = g2[i][j];
     261    for (j = 0 ; j < 3 ; j++)
     262      g3[B + i][j] = g3[i][j];
     263  }
     264}
     265
     266void CloudEffect::SetNoiseFrequency( int frequency)
     267{
     268  start = 1;
     269  B = frequency;
     270  BM = B-1;
     271}
     272
     273double CloudEffect::noise3( double vec[3])
     274{
     275  int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
     276  double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
     277  int i, j;
     278
     279  if (start)
     280  {
     281    start = 0;
     282    initNoise();
     283  }
     284
     285  setup(0, bx0,bx1, rx0,rx1);
     286  setup(1, by0,by1, ry0,ry1);
     287  setup(2, bz0,bz1, rz0,rz1);
     288
     289  i = p[ bx0 ];
     290  j = p[ bx1 ];
     291
     292  b00 = p[ i + by0 ];
     293  b10 = p[ j + by0 ];
     294  b01 = p[ i + by1 ];
     295  b11 = p[ j + by1 ];
     296
     297  t  = s_curve(rx0);
     298  sy = s_curve(ry0);
     299  sz = s_curve(rz0);
     300
     301  q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
     302  q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
     303  a = lerp(t, u, v);
     304
     305  q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
     306  q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
     307  b = lerp(t, u, v);
     308
     309  c = lerp(sy, a, b);
     310
     311  q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
     312  q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
     313  a = lerp(t, u, v);
     314
     315  q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
     316  q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
     317  b = lerp(t, u, v);
     318
     319  d = lerp(sy, a, b);
     320
     321  return lerp(sz, c, d);
     322}
     323
     324void CloudEffect::normalize2( double v[2])
     325{
     326  double s;
     327
     328  s = sqrt(v[0] * v[0] + v[1] * v[1]);
     329  v[0] = v[0] / s;
     330  v[1] = v[1] / s;
     331}
     332
     333void CloudEffect::normalize3( double v[3])
     334{
     335  double s;
     336
     337  s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
     338  v[0] = v[0] / s;
     339  v[1] = v[1] / s;
     340  v[2] = v[2] / s;
     341}
     342
  • branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.h

    r8495 r8636  
    1515#include "shader.h"
    1616
    17 class CloudEffect : public WeatherEffect {
     17#define MAXB 0x100
     18#define N 0x1000
     19#define NP 12   /* 2^N */
     20#define NM 0xfff
     21
     22#define s_curve(t) ( t * t * (3. - 2. * t) )
     23#define lerp(t, a, b) ( a + t * (b - a) )
     24#define setup(i,b0,b1,r0,r1)\
     25        t = vec[i] + N;\
     26        b0 = ((int)t) & BM;\
     27        b1 = (b0+1) & BM;\
     28        r0 = t - (int)t;\
     29        r1 = r0 - 1.;
     30#define at2(rx,ry) ( rx * q[0] + ry * q[1] )
     31#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
     32
     33class CloudEffect : public WeatherEffect
     34{
    1835public:
    19     CloudEffect(const TiXmlElement* root = NULL);
    20     virtual ~CloudEffect();
     36  CloudEffect(const TiXmlElement* root = NULL);
     37  virtual ~CloudEffect();
    2138
    22     virtual void loadParams(const TiXmlElement* root);
     39  virtual void loadParams(const TiXmlElement* root);
    2340
    24     virtual void init();
     41  virtual void init();
    2542
    26     virtual void activate();
    27     virtual void deactivate();
     43  virtual void activate();
     44  virtual void deactivate();
    2845
    29     inline void activateCloud() {
    30         this->activate();
    31     }
    32     inline void deactivateCloud() {
    33         this->deactivate();
    34     }
     46  inline void activateCloud()
     47  {
     48    this->activate();
     49  }
     50  inline void deactivateCloud()
     51  {
     52    this->deactivate();
     53  }
    3554
    36     virtual void draw() const;
    37     virtual void tick(float dt);
     55  virtual void draw() const;
     56  virtual void tick(float dt);
    3857
    39     /*inline void setCloudOption(const std::string& option) {
    40         if (option == "activate")
    41     this->cloudActivate = true;
    42 }*/
     58  void make3DNoiseTexture();
     59  void initNoise();
     60  void SetNoiseFrequency(int frequency);
     61  double noise3(double vec[3]);
     62  void normalize2(double v[2]);
     63  void normalize3(double v[3]);
     64
     65  /*inline void setCloudOption(const std::string& option) {
     66      if (option == "activate")
     67  this->cloudActivate = true;
     68  }*/
    4369
    4470
    4571private:
    46     //void initialize(char* fileName);
     72  //void initialize(char* fileName);
    4773
    48     //bool        cloudActivate;
     74  //bool        cloudActivate;
    4975
    50     Material                 cloudMaterial;
    51     Shader*                 shader;
     76  // Material                 cloudMaterial;
     77  Shader*                 shader;
     78  Shader::Uniform*        offset;
     79 
     80  float coor;
     81
     82  int noise3DTexSize;
     83  GLuint noise3DTexName;
     84  GLubyte *noise3DTexPtr;
     85
     86  int p[MAXB + MAXB + 2];
     87  double g3[MAXB + MAXB + 2][3];
     88  double g2[MAXB + MAXB + 2][2];
     89  double g1[MAXB + MAXB + 2];
     90
     91  int start;
     92  int B;
     93  int BM;
    5294};
    5395
Note: See TracChangeset for help on using the changeset viewer.