Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411505 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 25, 2024, 07:25:04 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Noise
Pages: [1]
Print
Author Topic: Noise  (Read 2472 times)
kafka
Guest
« on: June 05, 2008, 11:59:32 PM »

Does anyone know how to implement perlin noise in C++? I have looked the mathematical algorithms on the web, but I honestly don't understand them.
« Last Edit: June 06, 2008, 02:07:44 AM by kafka » Logged
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #1 on: June 06, 2008, 01:42:43 AM »

The way I did it was to get a grid of floats the size you want the texture to be. Then fill that with noise between 0 and 1. Then get another grid of noise half that size, fill that with noise. At this point you could resize this new noise on the graphics card to take advantage of the bilinear filtering, or if you're doing it in software you need to implement a lerp that will get a value in between four others. Then add this on to the original grid, and repeat by halving the size of the texture you're adding on each time. Then divide each value in the original grid by the number of iterations you've done.

Here's some C# code for the lerp

Code:
        

        // where mLayers is an array of 2d float arrays each half the size of the previous one

        float GetValue(int layer, int x, int y)
        {
            int plus = (layer + 1);
            plus = (int)(Math.Pow(2, plus));
            int w = (mWidth / plus);
            int h = (mHeight / plus);

            int xindex = (int)(x / plus);
            int yindex = (int)(y / plus);
            float dx = ((x % plus) + 1) / (float)plus;
            int right = (xindex + 1) % w;
            int down = (yindex + 1) % h;
            float valuex0 = mLayers[layer][xindex, yindex] + (mLayers[layer][right, yindex] - mLayers[layer][xindex, yindex]) * dx;
            float valuex1 = mLayers[layer][xindex, down] + (mLayers[layer][right, down] - mLayers[layer][xindex, down]) * dx;
            float dy = ((y % plus) + 1) / (float)plus;
            float value = valuex0 + (valuex1 - valuex0) * dy;
            return value;
        }

SDL.NET code for drawing it:
Code:
            float val = 0;
            for (int i = 0; i < mNumLayers; i++)
            {
                val += GetValue(i, mX, mY);
            }
            val /= mNumLayers;
            int col = (int)(val * 255);
            mSurface.Draw(new Point(mX, mY), Color.FromArgb(255, col, col, col));

As a note, it's really REALLY easy to make wrapping perlin noise by making sure the values you use for the edges of the grid are the same. So if you have a 16x16 grid of noise you want to use the pixel at (0,0) instead of the pixel at (15,0) etc.
« Last Edit: June 06, 2008, 01:46:51 AM by haowan » Logged

Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #2 on: June 06, 2008, 04:55:52 AM »

I just used this class  :D

http://www.flipcode.com/archives/Perlin_Noise_Class.shtml
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Gold Cray
Level 10
*****


Gold Cray


View Profile WWW
« Reply #3 on: June 06, 2008, 05:50:48 PM »

Here's another couple of perlin noise functions is the above wasn't enough.
Logged
BenH
Level 2
**


Dirty boy, wash your hands!


View Profile WWW
« Reply #4 on: June 07, 2008, 04:12:10 AM »

Here's a good article on the subject http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
Logged

kafka
Guest
« Reply #5 on: June 08, 2008, 11:30:04 PM »

Thanks for the help! Smiley Have to try these when I get home from work.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic