title: Smooth lightning as seen in Minecraft
body: <p>I am about to create a light system for my game and I actually have it setup, but with this "blocky" lightning as seen in the image. I would like to smooth the faces so that two different tiles aren't notable.</p>
  
  <p>I have looked around on the subject and the only real answer I found is to average all 4 surrounding neighbors light and create a gradient from it. The answer was <a href="https://stackoverflow.com/questions/4535133/how-does-minecraft-perform-lighting">found here</a>, but with no special rules to follow because that is what I need to find out.</p>
  
  <p>The light intensity of each tile is a fixed number between 100-255, where 100 is the darkest and 255 is the brightest(opacity) and each vertex gets this value.</p>
  
  <p>Do I have to find out which neighbors use each tiles vertex point and go on from there? I really have no clue </p>
  
  <p><s>The current version of my light look like this: </s> EDIT: Now it looks like this. It has some flaws.
  <img src="https://i.stack.imgur.com/avpiE.png" alt="enter image description here"></p>
  
  <p>I tried to collect all the neighbours vertices and take the average of them(see image below). By following this method, I will receive different intensity values on each vertex.</p>
  
  <p><img src="https://i.stack.imgur.com/e8e9d.png" alt="enter image description here"></p>
  
  <p>This is a beginning of the algorithm. This function is meant to run for every tile in the light. From my current state of the algorithm I need to find the vertices on the same point. <S>I am stuck here! :'-(</S> I have updated the algorithm, but it's still got some visual flaws. Note how the top left and the bottom right sides are "blocky" when the others aren't. Also if you look very closely at the center, there is a hexagon shape, ~3 wide and high.</p>
  
  <pre><code>void Torch::smoothLight(int pX, int pY)
  {
      if (pX == 0 || pX == (mWidth - 1) || pY == 0 || pY == (mHeight - 1)) return;
  
      // Tile we are smoothing
      Tile *c = mTilesInRange[pY * mWidth + pX];
      if (c == nullptr) return;
  
      int x_min_1 = pX - 1;
      int x_plus_1 = pX + 1;
      int y_min_1 = pY - 1;
      int y_plus_1 = pY + 1;
  
      // Get all nearby tiles in all 8 weather directions
      Tile *NW = mTilesInRange[y_min_1 * mWidth + x_min_1];
      Tile *N = mTilesInRange[y_min_1 * mWidth + pX];
      Tile *NE = mTilesInRange[y_min_1 * mWidth + x_plus_1];
      Tile *E = mTilesInRange[pY * mWidth + x_plus_1];
      Tile *SW = mTilesInRange[y_plus_1 * mWidth + x_min_1];
      Tile *S = mTilesInRange[y_plus_1 * mWidth + pX];
      Tile *SE = mTilesInRange[y_plus_1 * mWidth + x_plus_1];
      Tile *W = mTilesInRange[pY * mWidth + x_min_1];
  
      /*
          - Loop trough all 4 vertices on the tile in the following order
  
          0 1
          2 3
  
          - For each vertex, find the 3 other vertices that lies on the same point, example vertex 1:
  
          0 1  &lt;--
          2 3
  
                              0 1 0 1
                              2 3 2 3
          Original quad   -&gt;  0 1 0 1
                          -&gt;  2 3 2 3
  
          Result: 3 2
                  1 0
      */
      for (int i = 0; i &lt; 4; i++)
      {
          int intensitySum = 0;
  
          // This is really ugly but I can't come up with a better algorithm
          if (i == 0) // upper left
          {
              intensitySum += c-&gt;getQuad()[0].color.a; // me
              intensitySum += N-&gt;getQuad()[3].color.a;
              intensitySum += NW-&gt;getQuad()[2].color.a;
              intensitySum += W-&gt;getQuad()[1].color.a;
  
              int alpha = intensitySum / 4;
              c-&gt;getQuad()[0].color.a = alpha;
          } 
          else if (i == 1) // upper right
          {
              intensitySum += c-&gt;getQuad()[1].color.a; // me
              intensitySum += N-&gt;getQuad()[2].color.a;
              intensitySum += NE-&gt;getQuad()[3].color.a;
              intensitySum += E-&gt;getQuad()[0].color.a;
  
              int alpha = intensitySum / 4;
              c-&gt;getQuad()[1].color.a = alpha;
          }
          else if (i == 2) // lower left
          {
              intensitySum += c-&gt;getQuad()[2].color.a; // me
              intensitySum += E-&gt;getQuad()[3].color.a;
              intensitySum += SE-&gt;getQuad()[0].color.a;
              intensitySum += S-&gt;getQuad()[1].color.a;
  
              int alpha = intensitySum / 4;
              c-&gt;getQuad()[2].color.a = alpha;
          }
          else if (i == 3) // lower right
          {
              intensitySum += c-&gt;getQuad()[3].color.a; // me
              intensitySum += S-&gt;getQuad()[0].color.a;
              intensitySum += SW-&gt;getQuad()[1].color.a;
              intensitySum += W-&gt;getQuad()[2].color.a;
  
              int alpha = intensitySum / 4;
              c-&gt;getQuad()[3].color.a = alpha;
          }
      }
  }
  </code></pre>