使用perlin噪声绘制高度图

时间:2014-01-02 07:59:49

标签: java image awt perlin-noise

我正在尝试制作一个程序,它将在PNG文件上输出高度图。我想知道是否有人帮我制作黑白图像并使图像更自然地改变颜色。

我试图让程序制作一个像

这样的PNG文件

http://www.google.com/imgres?newwindow=1&client=firefox-a&hs=5az&sa=X&rls=org.mozilla:en-US:official&channel=fflb&biw=1408&bih=680&tbm=isch&tbnid=6sTzrisEjn29EM:&imgrefurl=http://www.gamedev.net/blog/33/entry-2227887-more-on-minecraft-type-world-gen/&docid=OYdwp9nNACCRzM&imgurl=http://members.gamedev.net/vertexnormal/basicfractal.jpg&w=256&h=256&ei=Lh7FUoboC4bakQfdx4CYDA&zoom=1&ved=1t:3588,r:7,s:0,i:102&iact=rc&page=1&tbnh=184&tbnw=173&start=0&ndsp=16&tx=91&ty=90

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

public class HeightmapGen {

private final static int WIDTH = 900;
private final static int HEIGHT = 600;
private final static float DX = .09f;
public static void main(String[] args) {
    BufferedImage img;
    img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB );

    int[] pixel = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

    for(int i = 0; i < (WIDTH*HEIGHT); i++)
    {
        pixel[i] = (int) perlin.PerlinNoise(i*DX, i*DX);
        System.out.println(pixel[i]);
    }

    try {
        ImageIO.write(img, "png", new File("test.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 }

这就是噪音等级。

public class perlin {

private static float frequency ;
private static float amplitude ;
private static double persistence = .5;
private static double Number_Of_Octaves =8;
static float seed = 2000;

public static void setFrequency(float x)
{
    frequency = x;
}

public static void setAmplitude(float x)
{
    amplitude = x;
}

public static void setPersistence(double x)
{
    persistence = x;
}

public static void setOctaves(double x)
{
    Number_Of_Octaves = x;
}

private static float Noise(int f, int g)
 {
    int n;

    n = f + g * 57;
    n = (n<<13) ^ n;
    n = (n * (n * n * 15731 + 789221) + 1376312589);
    return (float) ( 1.0 - ( n & 0x7fffffff) / 1073741824.0);    
 }

 private  static float SmoothNoise1(int x, int y)
  {
    float corners, sides, center; 
    corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16;
    sides   = ( Noise(x-1, y)  +Noise(x+1, y)  +Noise(x, y-1)  +Noise(x, y+1) ) /  8;
    center  =  Noise(x, y) / 4;
    return corners + sides + center;
    }

  private static float Interpolate(float a, float b, float x)
  {
      float ft, f;
      ft = x * 3.1415927f;
    f = (float) ((1 - Math.cos(ft)) * .5);
    return    (a*(1-f) + b*f);
  }

  static public float InterpolatedNoise_1(float x, float y)
  {
  int X    = (int)x;
  float fractional_X = x - X;

  int Y    = (int)y;
  float fractional_Y = y - Y;

  float v1, v2, v3, v4, i1, i2;
  v1 = SmoothNoise1(X,     Y);
  v2 = SmoothNoise1(X + 1, Y);
  v3 = SmoothNoise1(X,     Y + 1);
  v4 = SmoothNoise1(X + 1, Y + 1);

  i1 = Interpolate(v1 , v2 , fractional_X);
  i2 = Interpolate(v3 , v4 , fractional_X);

  return Interpolate(i1 , i2 , fractional_Y);
  }

  static public float PerlinNoise(float x, float y)
  {
      float total;
      double p, n;

  total = 0;
  p = persistence;
  n = Number_Of_Octaves - 1;

  for(double i =0; i <= n ; i++)
  {
      frequency = (float) Math.pow(2, i);
      amplitude = (float) Math.pow(p, i);

      total = total + InterpolatedNoise_1(x * frequency, y * frequency) * amplitude;

  }

  return seed * total;
  }
 }

0 个答案:

没有答案
相关问题