0
\$\begingroup\$

Right now I'm trying to load a heightmap into my LWJGL application. I load it like that into a float[][] array and I can read the height value at a given x and z position in the map:

public class HeightMap
{

float[][] heightMap;

public HeightMap (String fileName)
{
    int height, width;
    try
    {
        BufferedImage image = ImageIO.read(getClass().getResourceAsStream(fileName));
        heightMap = new float[image.getWidth()][image.getHeight()];
        for (int x = 0; x < image.getHeight(); ++x)
        {
            for (int z = 0; z < image.getWidth(); ++z)
            {
                heightMap[x][z] = new Color(image.getRGB(x, z)).getRed() / 255f;
            }
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

My problem is that I don't understand how to create the float[] vertices and byte[] indices arrays that I can pass to the vao and iao(and also texture coords (tco)).

Hope you can help me,

bebe

\$\endgroup\$
1
\$\begingroup\$

Creating the vertices is quite easy. You already have the x and z component, the y component is the height in your heightmap with the given position. You probably want to scale this somehow as heights are often expressed as value between 0 and 1. For the indices you just have to visualize for yourself how the triangles will be made up. You need two triangles per square, so six indices. This question on StackOverflow deals with the generation of these indices.

In your code you traverse the map, place your vertices and calculate the indices along with it. This site has some example code for exactly your case. If you look into it, you will see that the algorithm is not hard. This site even covers how you generate normals for your map.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for your fast answer. I'll look at the demos (for some reason I din't find them) and play a little bit around with the code :D \$\endgroup\$ – kessler bebe Aug 31 '16 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.