(C++) Perlin Noise Demo

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Xeus32
Posts: 20
Joined: Tue Mar 22, 2005 9:11 am
Location: Italy , Padua

(C++) Perlin Noise Demo

Post by Xeus32 »

I work around dynamic clouds! Bat before I write a Noise Generetor!
So i made a simple demo!

Image

It is very very simple!

The binary demo is http://xeus32.altervista.org/index.php? ... ar&mode=go

the Noise Class is :

Code: Select all


// Class automatically generated by Dev-C++ New Class wizard

#ifndef CPERLINNOISE_H
#define CPERLINNOISE_H


#include <irrlicht.h>

using namespace irr;
/**
 * Perlin Noise
 */
class cPerlinNoise
{
	public:
		// class constructor
		cPerlinNoise(core::dimension2d<s32> tDimension,float Frequency,float Persitence, float Octave,video::SColor MaskColor);
		// class destructor
		~cPerlinNoise();
		
		/// Dimension
		core::dimension2d<s32>* Dimension;
		char* ImageBuffer;
		float* FloatBuffer;
		// interpola tra 2 punti
		static float Interpolate1d(float x , float y , float a);
		/**
		 * Genera Noise 2d
		 */
		 static float Noise2d(int x, int y);
         /**
		  * get value
         **/
		static float GetValue (float x , float y);
		// Add noise
		void Add(cPerlinNoise *in);
		// interpolate between 
		void Interpolate(cPerlinNoise *in1 , cPerlinNoise *in2 ,float a);
};

#endif // CPERLINNOISE_H

Code: Select all


// Class automatically generated by Dev-C++ New Class wizard

#include "cperlinnoise.h" // class's header file

#include <stdlib.h>


// class constructor
cPerlinNoise::cPerlinNoise(core::dimension2d<s32> tDimension,float Frequency,float Persitence, float Octave,video::SColor MaskColor=video::SColor(255,255,255,255))
{
     Dimension = new core::dimension2d<s32>(tDimension) ;


     ImageBuffer = new char [Dimension->Height*Dimension->Width*4];
     float freq = Frequency;
     float pers = Persitence;

     int random = rand();
     
     for (int i=0; i<Dimension->Height; i++)
          for (int j=0; j<Dimension->Width; j++) 
          {
              float Total =0;
              float Amplitude =1;
              float freq_factor = freq;    
              
              for (int k =0 ; k < Octave; k++)
              {
                  Total += GetValue((j+random)*freq_factor,(i+random)*freq_factor)*Amplitude;
                  Amplitude *= pers;
                  freq_factor *=2;
              }
              
              Total = Total *0.5f + 0.5f;
              
              if (Total <0) Total =0;
              if (Total >1) Total =1.0f;
              

              int Offset = (i*Dimension->Width+j)*4;
              // format ECF_A8R8G8B8 
              // ImageBuffer[Offset+3] alpha
              // ImageBuffer[Offset+2] red
              // ImageBuffer[Offset+1] green
              // ImageBuffer[Offset+0] blue 


              ImageBuffer[Offset+0]=   (int) (MaskColor.getBlue()  * Total);  //blue
              ImageBuffer[Offset+1]= (int) (MaskColor.getGreen()  * Total);     //green
              ImageBuffer[Offset+2]= (int) (MaskColor.getRed() * Total);    //red
              ImageBuffer[Offset+3]= (int) (MaskColor.getAlpha()  * Total);      //alpha         




          }



}

// class destructor
cPerlinNoise::~cPerlinNoise()
{
     delete ImageBuffer;
}

/*
 * Genera Noise 2d
 */
float cPerlinNoise::Noise2d(int x, int y)
{
	int n = x +y *57;
	n= (n<<13)*n;
	float res = (float) (1.0f - ((n*(n*n*15731+789221)+ 1376312589)&0x7FFFFFFF) / 1073741824.0);
    return res;
}

/**
 * interpola tra 2 punti
**/
float cPerlinNoise::Interpolate1d(float x , float y , float a)
{
   float invA = 1-a;

   float  fac1 = 3* (invA)*(invA)-2*(invA)*(invA)*(invA);
   float  fac2 = 3* (a)*(a)-2*(a)*(a)*(a);

   return fac1* x + fac2* y;

}

// get value
float cPerlinNoise::GetValue (float x , float y)
{
    int Xint = (int)x;
    int Yint = (int)y;
    
    float Xfrac = x - Xint;
    float Yfrac = y - Yint;
    
    float x0y0 = Noise2d(Xint,Yint);
    float x1y0 = Noise2d(Xint+1,Yint);
    float x0y1 = Noise2d(Xint,Yint+1);
    float x1y1 = Noise2d(Xint+1,Yint+1);

    float v1 = Interpolate1d(x0y0,x1y0,Xfrac);            
    float v2 = Interpolate1d(x0y1,x1y1,Xfrac);
    float fin = Interpolate1d(v1,v2,Yfrac);

    return fin;

}


// Add noise
void cPerlinNoise::Add(cPerlinNoise *in)
{
     if ( in->Dimension != Dimension)
        return;
        
          for (int i=0; i<Dimension->Height; i++)
          for (int j=0; j<Dimension->Width; j++) 
          {
              int Offset = (i*Dimension->Width+j)*4;
              int color = in->ImageBuffer[Offset] + ImageBuffer[Offset];
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset] = color;

              color = in->ImageBuffer[Offset+1] + ImageBuffer[Offset+1];
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset+1] = color;
               
              color = in->ImageBuffer[Offset+2] + ImageBuffer[Offset+2];
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset+2] = color;
  
              color = in->ImageBuffer[Offset+3] + ImageBuffer[Offset+3];
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset+3] = color;
              
           }
}

// interpolate between 
void cPerlinNoise::Interpolate(cPerlinNoise *in1 , cPerlinNoise *in2 ,float a)
{
       
       if (a>1) a=1;
       if (a<0) a=0;

       float InvA= 1-a;
        
          for (int i=0; i<Dimension->Height; i++)
          for (int j=0; j<Dimension->Width; j++) 
          {
              int Offset = (i*Dimension->Width+j)*4;
              int color = in1->ImageBuffer[Offset]*a + in2->ImageBuffer[Offset]*InvA;
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset] = color;

              color = in1->ImageBuffer[Offset+1]*a + in2->ImageBuffer[Offset+1]*InvA;
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset+1] = color;
               
              color = in1->ImageBuffer[Offset+2]*a + in2->ImageBuffer[Offset+2]*InvA;
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset+2] = color;
  
              color = in1->ImageBuffer[Offset+3]*a + in2->ImageBuffer[Offset+3]*InvA;
              if ( color < 0) color = 0;
              if ( color > 255) color = 255;
              ImageBuffer[Offset+3] = color;
              
           }
}

TNX

Xeus32
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

The download link doesn't work for me, just took me to the altervista homepage i think.

But the screenshot looks pretty cool! Could be useful for funky special effects!
Image Image Image
andrei25ni
Posts: 326
Joined: Wed Dec 14, 2005 10:08 pm

Post by andrei25ni »

Copy / paste the link in the browser and it will work!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

IrrSpintz also has perlin noise texture generator.
trivtn
Posts: 132
Joined: Tue Jan 17, 2006 12:30 pm
Location: Viet Nam
Contact:

Post by trivtn »

I 've try it, very nice and easy to use. Thanks to Xeus32 share the class
There's something is fantastic, there's nothing is absolute.
Post Reply