title: Merging meshes opengl and libgdx
body: <p>I am using the following code to merge several meshes: </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Arrays;
  
  import org.obsgolem.crystalia.gfx.Renderer;
  
  import com.badlogic.gdx.graphics.Mesh;
  import com.badlogic.gdx.graphics.VertexAttribute;
  import com.badlogic.gdx.graphics.VertexAttributes.Usage;
  import com.badlogic.gdx.math.Vector3;
  import com.badlogic.gdx.utils.Array;
  import java.util.*;
  
  public class MeshBatch
  {
  
      private final static VertexAttribute[] attributeConfig = new VertexAttribute[]{
          new VertexAttribute(Usage.Position, 3, "a_position"),
          new VertexAttribute(Usage.ColorPacked, 4, "a_color"),
          new VertexAttribute(Usage.Normal, 3, "a_normal")};
      private final static int VERTEX_SIZE = 3 + 1 + 3;
  
      private Mesh m;
      private List&lt;Float&gt; vertices = new ArrayList&lt;Float&gt;();
      private List&lt;Short&gt; indices = new ArrayList&lt;Short&gt;();
  
      public void addMesh(float[] vert, short[] ind)
      {
          int offset = (vertices.size() / VERTEX_SIZE);
  
          //You have to throw an exception when you get over the limit of short indices
          if (offset + vert.length / VERTEX_SIZE &gt; Short.MAX_VALUE) {
              throw new RuntimeException("blablabla");
          }
  
          for (short i : addOffset(ind, offset)) {
              indices.add(i);
          }
  
          for (float v : vert) {
              vertices.add(v);
          }
      }
  
      public short[] addOffset(short[] ind, int offset)
      {
          short[] indarr = new short[ind.length];
          for (int i = 0; i &lt; ind.length; ++i) {
              //Do you really need this check? You are the only one using this code
              //so make sure that you never provide a null value. If you really want to have a chekc throw an exception instead
              short value = ind[i];//ind[i] == null ? 0 : ind[i];
              indarr[i] = (short) (value + offset);
          }
          return indarr;
      }
  
  
      public void end()
      {
          m = new Mesh(false, vertices.size(), indices.size(), attributeConfig);
          m.setVertices(Renderer.makeFloatArray(vertices));
          m.setIndices(Renderer.makeShortArray(indices));
      }
  
      public void render()
      {
          Renderer.getInstance().render(m);
      }
  }
  </code></pre>
  
  <p>However when I draw using this class I get strange effects with lighting. With this mesh the light is brighter for all other objects while the one rendered with has lighting which looks flat. With the normal way(Seperate meshes for each object) I get nice smooth lighing. Here are two screenshots I took:  </p>
  
  <p>With merged mesh:<br>
  <img src="https://i.stack.imgur.com/BUUNl.jpg" alt="Merged mesh lighting"></p>
  
  <p>Without merged mesh:<br>
  <img src="https://i.stack.imgur.com/2zWxU.jpg" alt="Smooth per pixel lighting without merged mesh"></p>
  
  <p>What is causing this problem, and how can it effect the lighting of another mesh? My vertices are sent in with the proper format(3 floats for vertices, 1 for color, and 3 for normals). My indices are also working. The problem occurs only when the mesh is actually rendered. Without it lighting works perfectly. I think that the problem has something to do with the normals, but I can not figure out what that problem might be. </p>
  
  <p><strong>Edit:</strong> I think I have fixed the problem. When I switched the tiles from using seperate meshes to meshbatch the lighting fixed itself. How could this happen?</p>