Thread: Noise problems

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    Noise problems

    I need to generate clouds for my 2d game made with c++ and sdl, and I found this site: http://www.animeimaging.com/asp/PerlinNoise.aspx

    I took there code and just converted it over, but when I call PerlinNoise(), all that I get is "Pure" Noise, nothing cloudlike. I can't figure out what the problem is, since I just copied pretty much. Thanks, bye.

    Code:
    void PerlinNoise()
    {
    	Slock(screen);
    	for (x = 0; x < 256; x++)
    	{
    		for (y = 0; y < 256; y++)
    		{
    			float temp = FinalNoise(x, y);
    			temp = temp*0.5f + 0.5f;
    			if(temp<0) temp = 0.0f;
    			if(temp>1) temp = 1.0f;
    			Uint8 fin = (int)(temp*255.0f);
    			DrawPixel(screen, x, y, fin, fin , fin);
    		}
    	}
    	Sulock(screen);
    }
    float Noise(int X, int Y)
    {
    	int n = x + y * 57;
    	n = (n<<13) ^ n;
    	return (float)( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff ) / 1073741824.0);
    }
    float Interpolate(float x, float y, float a)
    {
         float fac1 = 3*pow(1-a, 2) - 2*pow(1-a,3);
         float fac2 = 3*pow(a, 2) - 2*pow(a, 3);
         return x*fac1 + y*fac2;
    }
    float GetValue(float x, float y)
    {
    	int Xint = (int)x;
    	int Yint = (int)y;
    	float Xfrac = x - Xint;
    	float Yfrac = y - Yint;
    	float x0y0 = Noise(Xint, Yint);
    	float x1y0 = Noise(Xint+1, Yint);
    	float x0y1 = Noise(Xint, Yint+1);
    	float x1y1 = Noise(Xint+1, Yint+1);
    	float v1 = Interpolate(x0y0, x1y0, Xfrac);
    	float v2 = Interpolate(x0y1, x1y1, Xfrac);
    	return Interpolate(v1, v2, Yfrac);
    }
    float Smooth_Noise(int x, int y)
    {
    	float corners = ( Noise(x-1, y-1) + Noise(x+1, y-1) + Noise(x-1, y+1) + Noise(x+1, y+1) ) / 16.0f;
    	float sides = ( Noise(x-1, y) +Noise(x+1, y) + Noise(x, y-1) + Noise(x, y+1) ) / 8.0f;
    	float center = Noise(x, y) / 4.0f;
    	return corners + sides + center;
    }
    float FinalNoise(float x, float y)
    {
    	float P = 0.5f;
    	int octaves = 4;
    	float total = 0.0f;
    	int freq = 1;
    	float pers = 1;
    	for(int i=0; i<octaves; i++)
    	{
    		total += GetValue(x*freq,y*freq) * pers;
    		freq *= 2;
    		pers *= P;
    	}
    	return total;
    }

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You must smooth it with a filter. Your smoothing function is most certainly where the issue is.

    True Perlin noise takes forever to create. There is a method that uses estimated Perlin noise and then it is ran through a smoothing filter. You can do a bilinear interpolation or average the pixels or use a matrix filter.

    Create a texture, smooth it. Reduce the octave, create a texture, etc., etc. Additive blend all the textures together to create the final texture.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Procedural Noise and OpenGL Help!
    By Freestyler in forum C Programming
    Replies: 8
    Last Post: 04-18-2008, 08:06 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Computer Problems
    By fac7 in forum Tech Board
    Replies: 13
    Last Post: 03-23-2004, 01:22 AM