Jump to content
  • Advertisement
Sign in to follow this  
manhaeve

Perlin Noise acting weird

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I fixed the problems in my previous post, but I now have another problem. The noise doesnt look like perlin noise at all, here is what i get: This is my code:
#ifndef PERLIN_H
#define PERLIN_H

#include <cstdlib>
#include <cstring>
#include <SDL/SDL.H>
#include <cmath>

#define SIZE 256


void findcoord_circle(int x, int y, double radius, int degrees, double &getx, double &gety)
{
 double degInRad=degrees*(3.14159/180);
 getx=(cos(degInRad)*radius);
 gety=(sin(degInRad)*radius);
 //printf("getx %f, gety %f\n",getx,gety);
 getx+=(double)x;
 gety+=(double)y;
 //printf("getx %f, gety %f\n\n",getx,gety);
}

int g(int xcoord,int ycoord, double &x, double &y,int w)
{
 srand(xcoord+(ycoord*w));
 int degrees=rand()%360;
// printf("%i @ %i,%i\n",degrees,xcoord,ycoord);
 findcoord_circle(xcoord,ycoord,1.0,degrees,x,y);
}

double noise(double x,double y,int seed, int w)
{
 double floorx=(double)((int)x);
 double floory=(double)((int)y);
 //printf("%f, %f\n",floorx,floory);
 //printf("x=%f, y=%f\n",x, y);
 double x00,y00;
 g((int)floorx,(int)floory,x00,y00,w);
 double x01,y01;
 g((int)floorx,(int)(floory)+1,x01,y01,w);
 double x10,y10;
 g((int)(floorx)+1,(int)floory,x10,y10,w);
 //printf("x01= %f\n",x01);
 double x11,y11;
 g((int)(floorx)+1,(int)(floory)+1,x11,y11,w);
 double X00=x-floorx;
 double X10=x-(floorx+1.0);
 double X01=x-floorx;
 double X11=x-(floorx+1.0);
 double Y00=y-floory;
 double Y10=y-floory;
 double Y01=y-(floory+1.0);
 double Y11=y-(floory+1.0);
 double s,t,u,v;
 s=(x00*X00)+(y00*Y00); 
 t=(x10*X10)+(y10*Y10);
 u=(x01*X01)+(y01*Y01);
 v=(x11*X11)+(y11*Y11);
// printf("%f\n%f\n%f\n%f\n",s,t,u,v);
 double Sx = (3*pow(x-floorx,2.0))-(2*pow(x-floorx,3.0));
 double Sy = (3*pow(y-floory,2.0))-(2*pow(y-floory,3.0));
 double a=s+Sx*(t-s);
 double b=u+Sx*(v-u);
 double z=a+Sy*(b-a);
 if(z<-1.0)
  z=-1.0;
 if(z>1.0)
  z=1.0;
 return z;
}



void SetPixel ( SDL_Surface* pSurface , int x , int y , Uint8 r, Uint8 g, Uint8 b)
{
  //convert color
  Uint32 col = SDL_MapRGB ( pSurface->format ,r ,g ,b ) ;

  //determine position
  char* pPosition = ( char* ) pSurface->pixels ;

  //offset by y
  pPosition += ( pSurface->pitch * y ) ;
  

  //offset by x
  pPosition += ( pSurface->format->BytesPerPixel * x ) ;

  //copy pixel data
  memcpy ( pPosition , &col , pSurface->format->BytesPerPixel ) ;
}

SDL_Surface *Render_PerlinNoise(int w,int h,int seed,double zoom)
{
 int octaves=3;
 SDL_Surface *ret=SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,24,0,0,0,0);
 for(int y=0;y<h;y++)
 {
  for(int x=0;x<w;x++)
  {
    double getnoise = noise(((double)x)/zoom,((double)y)/zoom,seed,w);
    if((getnoise<(-1.0))||(getnoise>1.0))
     printf("error\n");
   //getnoise=1.0/getnoise;
   int color= (int)((getnoise*128.0)+128.0);
//   printf("%f ",getnoise);
   SetPixel(ret,x,y,color,color,color);
  }
  //printf("\n");
 }
 return ret;
}


#endif



Share this post


Link to post
Share on other sites
Advertisement
Hi there,

Because no one seems te respond I'm going to ask a different question. At Ken Perlins page Making Noise, u see a nice marble effect, now I wonder, can this be achieved by simply smoothing some non-coherent noise and editing it?

Thanks

Robin

Share this post


Link to post
Share on other sites
Take a look here. Its a very good site for noise. It has the calculations for it and example at the bottom of the page.

It shows some samples of what you can do with noise, and marble is one of them.

Share this post


Link to post
Share on other sites
Your color function is overflowing.
Your color buffer is 8bit, and can only display values in the range of 0-255.

If your getnoise returns 1.0, your color result is 256, which is seen as 0(black).

change your color function from this:
int color= (int)((getnoise*128.0)+128.0);

to this
int color= (int)((getnoise*127.0)+127.0);

you lose a little range, but you dont overflow.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!