dimanche 23 octobre 2016

Java: My height map generator only writes binary

So today I started with a new project. I want to make a simple heightmap generator in java, so I tried the following:

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;

    public class Heightmap {


    public static int width = 200;
    public static int height = 200;

    public static void main(String[] args) {

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY );
        for(int x = 0; x < width; x++){
            for(int y = 0; y < height; y++){
                bufferedImage.setRGB(x, y, (byte )(Math.random() * 256 + 128) ); // + 128 because byte goes from -128 to 127
            }
        }

        File outputFile = new File("heightmap.png");
        try { 
            ImageIO.write(bufferedImage, "png", outputFile);
        }catch (IOException ioex){
            ioex.printStackTrace();
        }
    }
}

The code is very simple, I plan to try perlin noise as the next step. But first I need to resolve this problem: Generated Heightmap

The pixels in heightmap.png are either completely white, or completely black. There's no grays in the image, which of course is necessary in a heightmap. Does anyone know what I did wrong?

is it the BufferedImage.TYPE_BYTE_GRAY part? If so, what should I use instead?




Aucun commentaire:

Enregistrer un commentaire