private void generateHeightmap_Click(object sender, EventArgs e) { Heightmap heightmap = Heightmap.Instance; heightmap.Perturb((float)this.frequency.Value, (float)this.distance.Value); // show height map, set up new values this.heightmapPicture.Height = heightmap.Height; this.heightmapPicture.Width = heightmap.Width; this.heightmapPicture.Image = heightmap.Texture; }
private void generateHeightmap_Click(object sender, EventArgs e) { Heightmap heightmap = Heightmap.Instance; heightmap.SetDimensions((int)mapWidth.Value, (int)mapHeight.Value); heightmap.PerlinNoise((int)octaves.Value, (double)persistence.Value, (double)frequency.Value, (double)amplitude.Value, true); // show height map, set up new values this.heightmapPicture.Height = heightmap.Height; this.heightmapPicture.Width = heightmap.Width; this.heightmapPicture.Image = heightmap.Texture; }
private void generateHeightmap_Click(object sender, EventArgs e) { Heightmap heightmap = Heightmap.Instance; heightmap.SetDimensions((int)mapWidth.Value, (int)mapHeight.Value); int seed; if (this.enableSeed.Checked) { seed = (int)seedVal.Value; } else { seed = (new Random()).Next(); seedVal.Value = (Decimal)seed; } heightmap.ImprovedPerlinNoise((float)this.frequency.Value, seed); // show height map, set up new values this.heightmapPicture.Height = heightmap.Height; this.heightmapPicture.Width = heightmap.Width; this.heightmapPicture.Image = heightmap.Texture; }
private void generateHeightmap_Click(object sender, EventArgs e) { Heightmap heightmap = Heightmap.Instance; heightmap.SetDimensions((int)mapWidth.Value, (int)mapHeight.Value); int seed; if (this.enableSeed.Checked) { seed = (int)seedVal.Value; } else { seed = (new Random()).Next(); seedVal.Value = (Decimal)seed; } heightmap.DiffusionLimitedAgregation(seed, (float)(this.occupation.Value / this.occupation.Maximum), (int)this.frequency.Value); // apply gaussian pass, multiple kernel sizes sum and normalize if (gaussianEnable.Checked) { List <Bitmap> dlaCopies = new List <Bitmap>(); Bitmap dilatedBitmap = (Bitmap)heightmap.Texture.Clone(); AForge.Imaging.Filters.GaussianBlur gaussian = new AForge.Imaging.Filters.GaussianBlur(4.0, 21); AForge.Imaging.Filters.Dilatation dilatationFilter = new AForge.Imaging.Filters.Dilatation(); GaussianBlur gaus = new GaussianBlur(2); for (int i = 0; i < (int)copyCount.Value; i++) { // apply filter, first increasinly dilate the picture dilatedBitmap = dilatationFilter.Apply(dilatedBitmap); // then apply gaussian blur with an increasing radius size Bitmap blurredBitmap = (Bitmap)dilatedBitmap.Clone(); if (radialBlur.Checked) { for (int j = 0; j < (i * 3) + 1; j++) { blurredBitmap = gaussian.Apply(blurredBitmap); } } else { gaus.Radius = (int)Math.Pow(2, i + 1); blurredBitmap = gaus.ProcessImage(blurredBitmap); } dlaCopies.Add(blurredBitmap); } // add noise to final texture collections if (noiseEnabled.Checked) { dlaCopies.Add(NoiseTexture((int)perlinOctaves.Value, (double)perlinPersistence.Value, (double)perlinFreq.Value, (double)perlinAmplitude.Value)); } // normalize all blurred heightmaps Bitmap bmp = (Bitmap)heightmap.Texture.Clone(); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); byte[] finalData = new byte[bmpData.Height * bmpData.Stride]; // sum average between all blurred pictures for (int i = 0; i < dlaCopies.Count; i++) { // AForge.Imaging.Filters addFilter = new AForge.Imaging.Filters.Add(blurredBitmap); Bitmap current = dlaCopies[i]; BitmapData currentData = current.LockBits(new Rectangle(0, 0, current.Width, current.Height), ImageLockMode.ReadOnly, current.PixelFormat); byte[] currentRawData = new byte[currentData.Height * currentData.Stride]; Marshal.Copy(currentData.Scan0, currentRawData, 0, currentData.Height * currentData.Stride); // lamba operation sums averages between blurred pictures Parallel.For(0, currentRawData.Length, index => { finalData[index] = (byte)(currentRawData[index] * 1.0f / (float)dlaCopies.Count + finalData[index]); }); } // save final raw pixel data Marshal.Copy(finalData, 0, bmpData.Scan0, finalData.Length); bmp.UnlockBits(bmpData); // store final values on heightmap heightmap.MapValues = finalData; heightmap.Texture = bmp; } // show height map, set up new values this.heightmapPicture.Height = heightmap.Height; this.heightmapPicture.Width = heightmap.Width; this.heightmapPicture.Image = heightmap.Texture; }