Guest User

Untitled

a guest
Oct 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. float rand1(int x, int y) {
  2. x=(x+y*57);
  3. x=(x<<13)^x;
  4. return (1.0f-((x*(x*x*15731+789221)+1376312589)&0x7fffffff)/1073741824.0f);
  5. }
  6.  
  7. float interpolate(float a, float b, float x) {
  8. x=(1-cos(x*PI))*0.5f;
  9. return a*(1-x)+b*x;
  10. }
  11.  
  12.  
  13. float smooth(int x, int y) {
  14. float corn = (rand1(x-1,y-1)+rand1(x+1,y-1)+rand1(x+1,y+1)+rand1(x-1,y+1))/16.0f;
  15. float side = (rand1(x-1,y)+rand1(x+1,y)+rand1(x,y-1)+rand1(x,y+1))/8.0f;
  16. return corn+side+rand1(x,y)/4.0f;
  17. }
  18.  
  19. float sAndI(float x, float y) {
  20. int integer_X = (int)x;
  21. float fractional_X = x - integer_X;
  22. int integer_Y = (int)y;
  23. float fractional_Y = y - integer_Y;
  24. float v1,v2,v3,v4,i1,i2;
  25. v1 = smooth(integer_X, integer_Y);
  26. v2 = smooth(integer_X + 1, integer_Y);
  27. v3 = smooth(integer_X, integer_Y + 1);
  28. v4 = smooth(integer_X + 1, integer_Y + 1);
  29. i1 = interpolate(v1 , v2 , fractional_X);
  30. i2 = interpolate(v3 , v4 , fractional_X);
  31. return interpolate(i1 , i2 , fractional_Y);
  32. }
  33.  
  34. float noise(float persistance, int iter, int x, int y) {
  35. float tot,freq,amp;
  36. tot=0; freq=amp=1;
  37. for (int c=0;c<iter;c++) {
  38. tot+=sAndI(x*freq,y*freq)*amp;
  39. freq*=2; amp*=persistance;
  40. };
  41. return tot;
  42. };
Add Comment
Please, Sign In to add comment