Heightmap Loader
===

I checked for a LibGDX heightmap loader [and found one][1], but I am not quite sure how the buildIndices() code works. I am also not sure why it only generates a heightmap chunk that is 128*128*x, anything bigger than 128 (eg. 1024) makes the loader mess up like this: 

***1024 chunk (textures wouldn't bind)***
![1024 chunk (textures wouldn't bind)][2]

As you can see, the mountains have holes and the chunk shape renders as a 128*1024 mesh

***128 chunk (textures wouldn't bind)***
![128 chunk (textures wouldn't bind)][3]

Perfectly ok with the 128*128 chunk


Here is my code:


    package com.amzoft.game.utils;
    
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.files.FileHandle;
    import com.badlogic.gdx.graphics.Color;
    import com.badlogic.gdx.graphics.Pixmap;
    
    public class HeightmapConverter {
    
    	private float[] heightMap;
    	private short width, height;
    	private float[] vertices;
    	private short[] indices;
    	private int vertexSize, strength;
    	private String heightmapFile;
    	
    	public HeightmapConverter(int width, int height, int vertexSize, int strength, String heightmapFile)
    	{
    		this.heightMap = new float[(width+1) * (height+1)];
    		this.width = (short)width;
    		this.height = (short)height;
    		this.vertices = new float[heightMap.length * vertexSize];
            this.indices = new short[width * height * 6];
            this.vertexSize = vertexSize;
            this.strength = strength;
            this.heightmapFile = heightmapFile;
            loadHeightmap();
            createIndices();
            createVertices();
    	}
    	
    	public void loadHeightmap()
    	{
    		try{
    			FileHandle handle = Gdx.files.internal(heightmapFile);
    			Pixmap heightmapImage = new Pixmap(handle);
    			Color color = new Color();
    			int indexToIterate = 0;
    			for(int x = 0;  x < width + 1; x++)
    			{
    				for(int y = 0; y < height + 1; y++)
    				{
    					Color.rgba8888ToColor(color, heightmapImage.getPixel(x, y));
    					heightMap[indexToIterate++] = color.r;
    				}
    			}
    			handle = null;
    			heightmapImage.dispose();
    			heightmapImage = null;
    			color = null;
    			indexToIterate = 0;
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    	
    	public void createVertices() 
    	{
            int heightPitch = height + 1;
            int widthPitch = width + 1;
    
            int idx = 0;
            int hIdx = 0;
            int inc = vertexSize - 3;          
    
            for(int z = 0; z < heightPitch; z++)
            {
            	for(int x = 0; x < widthPitch; x++) 
                {
            		vertices[idx++] = x;
            		vertices[idx++] = heightMap[hIdx++] * strength;
            		vertices[idx++] = z;
                    idx += inc;
                }
            }
    	}
    
    	private void createIndices() 
    	{
            int idx = 0;
            short pitch = (short)(width + 1);
            short i1 = 0;
            short i2 = 1;
            short i3 = (short)(1 + pitch);
            short i4 = pitch;
    
            short row = 0;
    
            for(int z = 0; z < height; z++) 
            {
            	for(int x = 0; x < width; x++) 
                {
                	indices[idx++] = i1;
                    indices[idx++] = i2;
                    indices[idx++] = i3;
    
                    indices[idx++] = i3;
                    indices[idx++] = i4;
                    indices[idx++] = i1;
    
                    i1++;
                    i2++;
                    i3++;
                    i4++;
                }
    
    	        row += pitch;
    	        i1 = row;
    	        i2 = (short)(row + 1);
    	        i3 = (short)(i2 + pitch);
    	        i4 = (short)(row + pitch);
            }
    	}
    	
    	public float[] getVertices()
    	{
    		return vertices;
    	}
    	
    	public short[] getIndices()
    	{
    		return indices;
    	}
    	
    	public String getHeightmapFile()
    	{
    		return heightmapFile;
    	}
    	
    }

If anyone could clarify why smaller chunks don't work and how the createIndices() works (called buildIndices() in the LibGDX code), that would be great :)


  [1]: http://code.google.com/p/libgdx/source/browse/trunk/tests/gdx-tests/src/com/badlogic/gdx/tests/TerrainTest.java
  [2]: http://i.stack.imgur.com/g4ag0.png
  [3]: http://i.stack.imgur.com/9S2e9.png