Example #1
0
        private void btnNoise_Click(object sender, EventArgs e)
        {
            waterWeatherBtn.Enabled = true;

            btnNoise.Enabled = false;

            // take in values from form and package them for the hight map generation
            NoiseMapParams mapParams = new NoiseMapParams();

            mapParams.amplitude   = (float)amplitudeUpDown.Value;
            mapParams.frequancy   = (float)frequencyUpDown.Value;
            mapParams.lacunarity  = (float)lacunarityUpDown.Value;
            mapParams.octaves     = (int)octaveUpDown.Value;
            mapParams.persistance = (float)persistanceUpDown.Value;

            bool doColor;
            int? newSeed = null;

            // some older that needs to be replaced but......
            if (radioButtonColorMap.Checked)
            {
                doColor = true;
            }
            else
            {
                doColor = false;
            }

            // is the seed changed update it, i dont think this actually works
            if (heightMap.MapSeed != seedUpDown.Value)
            {
                newSeed = (int)seedUpDown.Value;
            }

            // see color height map class
            heightMap.GenerateNoise(mapParams, doColor, newSeed);

            // draws map based on form radio buttons
            DrawMap();

            // generation takes a bit so dissabling then enabling the button was needed
            btnNoise.Enabled = true;
        }
        public Bitmap GenerateNoise(NoiseMapParams mapParams, bool doColor, int?newSeed)
        {
            if (newSeed != null)
            {
                prng = new System.Random((int)newSeed);
            }

            // mapParams holds all the info needed to make a varied hightmap
            //octaves, frequancy, amplitude, persistance, lacunarity;
            Vector2[] octaveOffsets = new Vector2[mapParams.octaves];

            // generate octave offsets
            for (int i = 0; i < mapParams.octaves; i++)
            {
                float offsetX = prng.Next(0, 1500000);
                float offsetY = prng.Next(0, 1500000);
                octaveOffsets[i] = new Vector2(offsetX, offsetY);
            }

            // used for lerping later
            minNoiseHeight = double.MaxValue;
            maxNoiseHeight = double.MinValue;

            // fill temp data
            int    octaves     = mapParams.octaves;
            double frequancy   = mapParams.frequancy;
            double amplitude   = mapParams.amplitude;
            double persistance = mapParams.persistance;
            double lacunarity  = mapParams.lacunarity;

            double heightValue;
            double perlinValue;

            // Define our map area in latitude/longitude
            double southLatBound = -180;
            double northLatBound = 180;
            double westLonBound  = -90;
            double eastLonBound  = 90;

            double lonExtent = eastLonBound - westLonBound;
            double latExtent = northLatBound - southLatBound;

            double xDelta = lonExtent / (double)mapWidth;
            double yDelta = latExtent / (double)mapHeight;

            double curLon = westLonBound;
            double curLat = southLatBound;

            // Loop through each tile using its lat/long coordinates
            for (int x = 0; x < mapWidth; x++)
            {
                curLon = westLonBound;

                for (int y = 0; y < mapHeight; y++)
                {
                    frequancy   = mapParams.frequancy;
                    amplitude   = mapParams.amplitude;
                    heightValue = 0;

                    //Noise range
                    //double x1 = 0, x2 = 1;
                    //double y1 = 0, y2 = 1;
                    //double dx = x2 - x1;
                    //double dy = y2 - y1;

                    double x1 = 0, y1 = 0, z1 = 0;

                    for (int i = 1; i < octaves + 1; i++)
                    {
                        //Sample noise at smaller intervals
                        //double s = (double)x / bitmap.Width;
                        //double t = (double)y / bitmap.Height;

                        // Calculate our 4D coordinates, this is used to make the output tileable
                        //double nx = x1 + Math.Cos(s * 2 * Math.PI) * dx / (2 * Math.PI) * frequancy + octaveOffsets[i - 1].X;
                        //double ny = y1 + Math.Cos(t * 2 * Math.PI) * dy / (2 * Math.PI) * frequancy + octaveOffsets[i - 1].Y;
                        //double nz = x1 + Math.Sin(s * 2 * Math.PI) * dx / (2 * Math.PI) * frequancy + octaveOffsets[i - 1].X;
                        //double nw = y1 + Math.Sin(t * 2 * Math.PI) * dy / (2 * Math.PI) * frequancy + octaveOffsets[i - 1].Y;

                        // mapping the perlin noise sphereically
                        LatLonToXYZ(curLat, curLon, ref x1, ref y1, ref z1, frequancy, octaveOffsets[i - 1]);
                        //LatLonToXYZ(curLat, curLon, ref x1, ref y1, ref z1);

                        // pull height value
                        perlinValue  = OpenSimplexNoise.noise(x1, y1, z1);
                        heightValue += perlinValue * amplitude * frequancy + octaveOffsets[i - i].Y;

                        amplitude *= persistance;
                        frequancy *= lacunarity;
                    }



                    // keep track of the max and min values found
                    if (heightValue > maxNoiseHeight)
                    {
                        maxNoiseHeight = heightValue;
                    }
                    else if (heightValue < minNoiseHeight)
                    {
                        minNoiseHeight = heightValue;
                    }

                    // save it
                    noiseMap[x, y] = heightValue;

                    curLon += xDelta;
                }
                curLat += yDelta;
            }


            if (doColor)
            {
                MapBitmapToColor(true);
            }
            else
            {
                MapBitmapToGrayScale(true);
            }

            return(bitmap);
        }