title: OpenGL heightmap terrain and normals with low resolution
body: <p>I'm did a basic terrain generation with a heightmap images in openGL, work fine, but the terrain vertices and normals are too low resolution, as in the follow images:</p>
  
  <p>Image of the normals<br/>
  <a href="https://i.stack.imgur.com/4p9sH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4p9sH.png" alt="Image of the normals"></a></p>
  
  <p>Image of the vertices in terrain<br/>
  <a href="https://i.stack.imgur.com/8bkUz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8bkUz.png" alt="Image of the vertices in terrain"></a></p>
  
  <p>The terrain mesh generation doesn't metter here, it's just a basic triangles generation, so let's go to the normals and heightmap generation part:</p>
  
  <pre><code>//Calculate the height of the vertex in the terrain
  int Terrain::GetPixelHeight(unsigned char* data, int imageWidth, int x, int y)
  {
      if(x &lt; 0 || x &gt;= 256 || y &lt; 0 || y &gt;= 256) //If coordinates passes the image limit, just return 0
          return 0;
      else
      {
          unsigned char* pixelOffset = data + (x + imageWidth * y) * 1; //Get the image
          unsigned char r = pixelOffset[0]; //Get R value
          unsigned char g = pixelOffset[1]; //Get G value
          unsigned char b = pixelOffset[2]; //Get B value
          float height = (float)r + (float)g + (float)b; //Put values together to calculate the height
          return height / 40; //Return the height with smooth in 40
      }
  }
  </code></pre>
  
  <p>Now the normals calculation:</p>
  
  <pre><code>//Calculate the normals based in the terrain height
  float heightL = GetPixelHeight(data, y, x-1, z);
  float heightR = GetPixelHeight(data, y, x+1, z);
  float heightD = GetPixelHeight(data, y, x, z-1);
  float heightU = GetPixelHeight(data, y, x, z+1);
  glm::vec3 normalVector = glm::normalize(glm::vec3(heightL - heightR, 2.0f, heightD - heightU));
  normals.push_back(normalVector.x); normals.push_back(normalVector.y); normals.push_back(normalVector.z);
  </code></pre>
  
  <p>Shaders are basic too, I can guarantee there are no problems with them, because is the same shaders of the others objects in game, and this objects doesn't have problems with light calculation, so i'm think the problem is the normals calculation with the heightmap.</p>
  
  <p>PS: Terrain size is the same of the image, 256x256, the image just have black and white channels.</p>