Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8668 in orxonox.OLD


Ignore:
Timestamp:
Jun 21, 2006, 1:56:41 PM (18 years ago)
Author:
hdavid
Message:

branches/atmospheric_engine: implemented a sky plane with the cloud shader on it

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

    r8663 r8668  
    5353{
    5454  this->deactivate();
     55 
     56  delete shader;
    5557}
    5658
     
    6264  this->offsetZ = 0;
    6365  this->animationSpeed = 2;
    64 
    65   noise3DTexSize = 64;
     66  this->lightPos = Vector(0,0,0);
     67  this->scale = 0.01f;
     68  this->atmosphericRadius = 120;
     69  this->planetRadius = 90;
     70  this->divs = 100;
     71
     72  noise3DTexSize = 128;
    6673  noise3DTexName = 0;
    6774
     
    8996  Shader::Uniform(shader, "SkyColor").set(0.0f, 0.0f, 0.8f);
    9097  Shader::Uniform(shader, "CloudColor").set(0.8f, 0.8f, 0.8f);
    91   Shader::Uniform(shader, "LightPos").set(5.0f, 0.0f, 5.0f);
    92   Shader::Uniform(shader, "Scale").set(0.1f);
    9398 
    9499  offset = new Shader::Uniform(shader, "Offset");
     
    103108
    104109  LoadParam(root, "speed", this, CloudEffect, setAnimationSpeed);
     110  LoadParam(root, "lightPos", this, CloudEffect, setLightPosition);
     111  LoadParam(root, "scale", this, CloudEffect, setCloudScale);
     112 
     113  LoadParam(root, "planetRadius", this, CloudEffect, setPlanetRadius);
     114  LoadParam(root, "atmosphericRadius", this, CloudEffect, setAtmosphericRadius);
     115  LoadParam(root, "divisions", this, CloudEffect, setDivisions);
    105116 
    106117  LOAD_PARAM_START_CYCLE(root, element);
     
    115126{
    116127  PRINTF(0)( "Activating\n");
     128 
     129  // Can only be set after the loadParams call
     130  this->shader->activateShader();
     131  Shader::Uniform(shader, "Scale").set(this->scale);
     132  Shader::Uniform(shader, "LightPos").set(lightPos.x, lightPos.y, lightPos.z);
     133  this->shader->deactivateShader();
     134 
     135  this->generateSkyPlane(this->divs, this->planetRadius, this->atmosphericRadius, 1, 1);
    117136
    118137  this->cloudActivate = true;
     
    139158  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
    140159
    141   glPushMatrix();
     160  //glPushMatrix();
    142161
    143162  this->shader->activateShader();
     
    145164
    146165
    147   glColor3f(1.0, 1.0, 1.0);
     166  /*glColor3f(1.0, 1.0, 1.0);
    148167  glBegin(GL_QUADS);
    149168
     
    153172  glTexCoord2f(1.0f, 0.0f); glVertex3f(10.0f, 0.0f,  0.0f);
    154173
     174  glEnd();*/
     175
     176  glPushMatrix();
     177  glTranslatef(0.0f,pRadius,0.0f);
     178  //glRotatef(timeGetTime()/2000.0f,0.0f, 1.0f, 0.0f);
     179  //glTranslatef(0.0f,0.0f,0.0f);
     180
     181  glBegin(GL_TRIANGLES);
     182
     183  for (int i=0; i < numIndices; i++)
     184  {
     185    glColor3f(1.0f, 1.0f, 1.0f);
     186
     187    glTexCoord2f(planeVertices[indices[i]].u, planeVertices[indices[i]].v);
     188    glVertex3f(planeVertices[indices[i]].x, planeVertices[indices[i]].y, planeVertices[indices[i]].z);
     189  }
     190
    155191  glEnd();
    156192
     193  glPopMatrix();
     194
    157195  this->shader->deactivateShader();
    158196
    159   glPopMatrix();
     197  //glPopMatrix();
    160198  glPopAttrib();
    161199
     
    167205    return;
    168206  this->offsetZ += 0.05 * dt * this->animationSpeed;
     207}
     208
     209void CloudEffect::generateSkyPlane(int divisions, float planetRadius, float atmosphereRadius, float hTile, float vTile)
     210{
     211  // Make sure our vertex array is clear
     212  if (planeVertices)
     213  {
     214    delete planeVertices;
     215    planeVertices = NULL;
     216  }
     217
     218  // Make sure our index array is clear
     219  if (indices)
     220  {
     221    delete indices;
     222    indices = NULL;
     223  }
     224
     225  // Set the number of divisions into a valid range
     226  int divs = divisions;
     227  if (divisions < 1)
     228    divs = 1;
     229
     230  if (divisions > 256)
     231    divs = 256;
     232
     233  pRadius = planetRadius;
     234
     235  // Initialize the Vertex and indices arrays
     236  numPlaneVertices = (divs + 1) * (divs + 1);   // 1 division would give 4 verts
     237  numIndices  = divs * divs * 2 * 3;       // 1 division would give 6 indices for 2 tris
     238
     239  planeVertices = new VERTEX[numPlaneVertices];
     240  memset(planeVertices, 0, sizeof(VERTEX));
     241
     242  indices = new int[numIndices];
     243  memset(indices, 0, sizeof(int)*numIndices);
     244
     245  // Calculate some values we will need
     246  float plane_size = 2.0f * (float)sqrt((SQR(atmosphereRadius)-SQR(planetRadius)));
     247  float delta = plane_size/(float)divs;
     248  float tex_delta = 2.0f/(float)divs;
     249 
     250  // Variables we'll use during the dome's generation
     251  float x_dist   = 0.0f;
     252  float z_dist   = 0.0f;
     253  float x_height = 0.0f;
     254  float z_height = 0.0f;
     255  float height = 0.0f;
     256
     257  VERTEX SV; // temporary vertex
     258
     259  for (int i=0;i <= divs;i++)
     260  {
     261    for (int j=0; j <= divs; j++)
     262    {
     263      x_dist = (-0.5f * plane_size) + ((float)j*delta);
     264      z_dist = (-0.5f * plane_size) + ((float)i*delta);
     265
     266      x_height = (x_dist*x_dist) / atmosphereRadius;
     267      z_height = (z_dist*z_dist) / atmosphereRadius;
     268      height = x_height + z_height;
     269
     270      SV.x = x_dist;
     271      SV.y = 0.0f - height;
     272      SV.z = z_dist;
     273
     274      // Calculate the texture coordinates
     275      SV.u = hTile*((float)j * tex_delta*0.5f);
     276      SV.v = vTile*(1.0f - (float)i * tex_delta*0.5f);
     277
     278      planeVertices[i*(divs+1)+j] = SV;
     279    }
     280  }
     281
     282  // Calculate the indices
     283  int index = 0;
     284  for (int i=0; i < divs;i++)
     285  {
     286    for (int j=0; j < divs; j++)
     287    {
     288      int startvert = (i*(divs+1) + j);
     289
     290        // tri 1
     291      indices[index++] = startvert;
     292      indices[index++] = startvert+1;
     293      indices[index++] = startvert+divs+1;
     294
     295      // tri 2
     296      indices[index++] = startvert+1;
     297      indices[index++] = startvert+divs+2;
     298      indices[index++] = startvert+divs+1;
     299    }
     300  } 
    169301}
    170302
  • branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.h

    r8663 r8668  
    3131#define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
    3232
     33
     34#define DTOR (PI/180.0f)
     35#define SQR(x) (x*x)
     36
     37typedef struct {
     38  float x,y,z;
     39  unsigned int color;
     40  float u, v;
     41} VERTEX;
     42
     43
    3344class CloudEffect : public WeatherEffect
    3445{
     
    4556
    4657  inline void activateCloud()
    47   {
    48     this->activate();
    49   }
     58  { this->activate(); }
     59 
    5060  inline void deactivateCloud()
    51   {
    52     this->deactivate();
    53   }
     61  { this->deactivate(); }
     62 
    5463  inline void setCloudOption(const std::string& option)
    5564  {
     
    5766      this->cloudActivate = true;
    5867  }
     68 
    5969  inline void setAnimationSpeed(float speed)
    60   {
    61     this->animationSpeed = speed;
    62   }
     70  { this->animationSpeed = speed; }
     71 
     72  inline void setLightPosition(float x, float y, float z)
     73  { this->lightPos = Vector(x,y,z); }
    6374
     75  inline void setCloudScale(float scale)
     76  { this->scale = scale; }
     77 
     78  inline void setPlanetRadius(float planetRadius)
     79  { this->planetRadius = planetRadius; }
     80 
     81  inline void setAtmosphericRadius(float atmosphericRadius)
     82  { this->atmosphericRadius = atmosphericRadius; }
     83 
     84  inline void setDivisions(int divs)
     85  { this->divs = divs; }
     86 
    6487  virtual void draw() const;
    6588  virtual void tick(float dt);
     
    7295  void normalize3(double v[3]);
    7396
     97  void generateSkyPlane(int divisions, float planetRadius, float atmosphereRadius,
     98                        float hTile, float vTile);
    7499private:
    75100
    76   bool        cloudActivate;
    77   float       animationSpeed;
     101  bool             cloudActivate;
     102  float            animationSpeed;
    78103
    79104  // Material                 cloudMaterial;
    80   Shader*                 shader;
    81   Shader::Uniform*        offset;
     105 
     106  // SHADER STUFF
     107  Shader*          shader;
     108  Shader::Uniform* offset;
     109  float            offsetZ;
     110  Vector           lightPos;
     111  float            scale;
     112  float            planetRadius;
     113  float            atmosphericRadius;
     114  int              divs;
    82115
    83   float offsetZ;
     116  // NOISE STUFF
     117  int              noise3DTexSize;
     118  GLuint           noise3DTexName;
     119  GLubyte          *noise3DTexPtr;
    84120
    85   int noise3DTexSize;
    86   GLuint noise3DTexName;
    87   GLubyte *noise3DTexPtr;
     121  int              p[MAXB + MAXB + 2];
     122  double           g3[MAXB + MAXB + 2][3];
     123  double           g2[MAXB + MAXB + 2][2];
     124  double           g1[MAXB + MAXB + 2];
    88125
    89   int p[MAXB + MAXB + 2];
    90   double g3[MAXB + MAXB + 2][3];
    91   double g2[MAXB + MAXB + 2][2];
    92   double g1[MAXB + MAXB + 2];
     126  int              start;
     127  int              B;
     128  int              BM;
     129 
     130  // SKYPLANE STUFF
     131  VERTEX *planeVertices;
     132  int numPlaneVertices;
    93133
    94   int start;
    95   int B;
    96   int BM;
     134  int *indices;
     135  int numIndices;
     136
     137  float pRadius;
     138 
     139  VERTEX *vertices;
     140  int numVertices;
    97141};
    98142
Note: See TracChangeset for help on using the changeset viewer.