Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.