skitzFist

Untitled

Oct 28th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. void PerlinNoise2D::generateNoise()
  2. {
  3.  
  4. delete[]this->perlinNoise;
  5. this->perlinNoise = new float[width * height];
  6.  
  7. for (int x = 0; x < width; ++x) {
  8.  
  9. for (int y = 0; y < height; ++y) {
  10.  
  11. float noise = 0.f;
  12. scale = 0.5f;
  13. float scaleAcc = 0.f;
  14.  
  15. for (int o = 0; o < octave; ++o) {
  16. int pitchX = width >> o;
  17. int pitchY = height >> o;
  18.  
  19. int sampleX1 = (x / pitchX) * pitchX;
  20. int sampleY1 = (y / pitchY) * pitchY;
  21.  
  22. int sampleX2 = (sampleX1 + pitchX) % width;
  23. int sampleY2 = (sampleY1 + pitchY) % height;
  24.  
  25. float blendX = (float)(x - sampleX1) / (float)pitchX;
  26. float blendY = (float)(y - sampleY1) / (float)pitchY;
  27.  
  28. float sampleT = (1.f - blendX) * noiseSeed[sampleY1 * width + sampleX1] + blendX * noiseSeed[sampleY1 * width + sampleX2];
  29. float sampleB = (1.f - blendY) * noiseSeed[sampleY2 * width + sampleX1] + blendY * noiseSeed[sampleY2 * width + sampleX2];
  30.  
  31.  
  32. noise += ((blendY * blendX) * (sampleB - sampleT) + sampleT) * scale;
  33. scaleAcc += scale;
  34. scale = scale / scalingBias;
  35.  
  36.  
  37. }
  38. //std::cout << noise / scaleAcc << std::endl;
  39. perlinNoise[y * width + x] = noise / scaleAcc;
  40. }
  41.  
  42. }
  43. }
  44.  
  45. void PerlinNoise2D::generateNoiseToImage(sf::Image &image)
  46. {
  47. for (int x = 0; x < image.getSize().x; ++x) {
  48. for (int y = 0; y < image.getSize().y; ++y) {
  49. image.setPixel((unsigned)x, (unsigned)y, sf::Color::White);
  50. }
  51. }
  52.  
  53. for (int x = 0; x < width; ++x) {
  54. for (int y = 0; y < height; ++y) {
  55.  
  56. float value = perlinNoise[y * width + x];
  57.  
  58. if (value > 0.8f) {
  59. image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(11,39,255,255));
  60. }
  61. else if (value > 0.6f && value < 0.8f) {
  62. image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(120,171,255,255));
  63. }
  64. else if(value > 0.4f && value < 0.6f) {
  65. image.setPixel((unsigned)x, (unsigned)y, sf::Color::Yellow);
  66. }
  67. else if(value > 0.2f && value < 0.4f) {
  68. image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(19,255,38,255));
  69. }
  70. else if (value > 0.f && value < 0.2f) {
  71. image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(12, 238, 7, 255));
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment