Advertisement
Guest User

Untitled

a guest
Oct 27th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include "terrain.h"
  2.  
  3. Terrain::Terrain(int width, int height)
  4. {
  5.     _width  = width;
  6.     _length = height;
  7.  
  8.     heights = new float*[_length];
  9.  
  10.     for(int i = 0; i < _length; i++)
  11.         heights[i] = new float[_width];
  12.  
  13.     normals = new vec3D*[_length];
  14.  
  15.     for(int i = 0; i < _length; i++)
  16.         normals[i] = new vec3D[_width];
  17.  
  18.     computedNormals = false;
  19. }
  20.  
  21. Terrain::~Terrain()
  22. {
  23.     for(int i = 0; i < _length; i++)
  24.         delete[] heights[i];
  25.  
  26.     delete[] heights;
  27.  
  28.     for(int i = 0; i < _length; i++)
  29.         delete[] normals[i];
  30.  
  31.     delete[] normals;
  32. }
  33.  
  34. void Terrain::setHeight(int x, int z, float y)
  35. {
  36.     heights[z][x] = y;
  37.     computedNormals = true;
  38. }
  39.  
  40. void Terrain::computeNormals()
  41. {
  42.     if(computedNormals)
  43.         return;
  44.  
  45.     vec3D** normals2 = new vec3D*[_length];
  46.  
  47.     for(int i = 0; i < _length; i++)
  48.         normals2[i] = new vec3D[_width];
  49.  
  50.     for(int z = 0; z < _length; z++)
  51.     {
  52.         for(int x = 0; x < _width; x++)
  53.         {
  54.             vec3D sun(0.0f, 0.0f, 0.0f);
  55.             vec3D out;
  56.             vec3D in;
  57.             vec3D left;
  58.             vec3D right;
  59.  
  60.             if(z > 0)
  61.                 out = vec3D(0.0f, heights[z - 1][x] - heights[z][x], -1.0f);
  62.  
  63.             if(z < _length - 1)
  64.                 in = vec3D(0.0f, heights[z + 1][x] - heights[z][x], 1.0f);
  65.  
  66.             if(x > 0)
  67.                 left = vec3D(-1.0f, heights[z][x - 1] - heights[z][x], 0.0f);
  68.  
  69.             if(x > 0 && z > 0)
  70.                 sun += out.cross(left).normalize();
  71.  
  72.             if(x > 0 && z < _length - 1)
  73.                 sun += left.cross(in).normalize();
  74.  
  75.             if(x < _width - 1 && z < _length - 1)
  76.                 sun += in.cross(right).normalize();
  77.  
  78.             if(x < _width - 1 && z > 0)
  79.                 sun += right.cross(out).normalize();
  80.  
  81.              normals2[z][x] = sun;
  82.         }
  83.     }
  84.  
  85.     const float FALLOUT_RATIO = 0.5f;
  86.  
  87.     for(int z = 0; z < _length; z++)
  88.     {
  89.         for(int x = 0; x < _width; x++)
  90.         {
  91.             vec3D sun = normals2[z][x];
  92.  
  93.             if(x > 0)
  94.                 sun += normals2[z][x - 1] * FALLOUT_RATIO;
  95.  
  96.             if(x < _width - 1)
  97.                 sun += normals2[z][x + 1] * FALLOUT_RATIO;
  98.  
  99.             if(z > 0)
  100.                 sun += normals2[z - 1][x] * FALLOUT_RATIO;
  101.  
  102.             if(z < 0)
  103.                 sun += normals2[z + 1][x] * FALLOUT_RATIO;
  104.  
  105.             if(sun.length() == 0)
  106.                 sun = vec3D(0.0f, 1.0f, 0.0f);
  107.  
  108.             normals[z][x] = sun;
  109.         }
  110.     }
  111.  
  112.     for(int i = 0; i < _length; i++)
  113.         delete[] normals2[_length];
  114.  
  115.     delete[] normals2;
  116.  
  117.     computedNormals = true;
  118. }
  119.  
  120. vec3D Terrain::getNormals(int x, int z)
  121. {
  122.     if(!computedNormals)
  123.         computeNormals();
  124.  
  125.     return normals[z][x];
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement