Export heightmap from j3o file

I want to export a heightmap from a j3o file to an image file.
I have the following code so far:

    private Node terrain;
    private TerrainQuad terrainQuad;
    private String track;
    
    private Image heightmap;
    private ImageRaster imageRaster;

    terrain = (Node) getAssetManager().loadModel("Tracks/" + track + "/Scenes/terrain_1.j3o");
    terrainQuad = (TerrainQuad)terrain.getChild("terrain-terrain_1_node");

    Format format = Luminance8;
    int width = terrainQuad.getTerrainSize();
    int height = terrainQuad.getTerrainSize();
    ByteBuffer data = BufferUtils.createByteBuffer( (int)Math.ceil(format.getBitsPerPixel() / 8.0) * width * height);
    heightmap = new Image(format, width, height, data,null, heightmap.getColorSpace());
        
    imageRaster = ImageRaster.create(heightmap);

    float terrainheight;
    for(int y=0; y<terrainQuad.getTerrainSize(); y++) {
        for(int x=0; x<terrainQuad.getTerrainSize(); x++) {
            terrainheight = terrainQuad.getHeight(new Vector2f(x, y));
            imageRaster.setPixel(x, y, new ColorRGBA(terrainheight, 0, 0, 0));
        }
    }

I have the following questions:

  • Is this the correct format for a heightmap image file (Luminance8)?
  • Can I use the jME API for saving to an image file?

Thanks in advance.

1 Like