Advertisement
Guest User

Untitled

a guest
Apr 26th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | Source Code | 0 0
  1. #include <cstdint>
  2. #include <cmath>
  3. #include "glm/glm/glm.hpp"
  4. #include <iostream>
  5. // This is an implementation of 2D perlin noise(3D noise is not that much more complex, but this should be easier to understand).
  6. // MurmurHash implementation for a 2-dimensional unsigned integer input vector.
  7. uint32_t hash2D(uint32_t x, uint32_t y, uint32_t seed) {
  8.     const uint32_t m = 0x5bd1e995U;
  9.     uint32_t hash = seed;
  10.     uint32_t k1 = x;
  11.     k1 *= m;
  12.     k1 ^= k1 >> 24;
  13.     k1 *= m;
  14.     hash *= m;
  15.     hash ^= k1;
  16.    
  17.     uint32_t k2 = y;
  18.     k2 *= m;
  19.     k2 ^= k2 >> 24;
  20.     k2 *= m;
  21.     hash *= m;
  22.     hash ^= k2;
  23.  
  24.     hash ^= hash >> 13;
  25.     hash *= m;
  26.     hash ^= hash >> 15;
  27.     return hash;
  28. }
  29.  
  30. // Gradient direction based on hash value
  31. glm::vec2 gradientDirection(uint32_t hash) {
  32.     switch (int(hash) & 3) {
  33.     case 0:
  34.         return glm::vec2(1.0f, 1.0f);
  35.     case 1:
  36.         return glm::vec2(-1.0f, 1.0f);
  37.     case 2:
  38.         return glm::vec2(1.0f, -1.0f);
  39.     case 3:
  40.         return glm::vec2(-1.0f, -1.0f);
  41.     default:
  42.         return glm::vec2(0.0f);
  43.     }
  44. }
  45.  
  46. // Linear interpolation function
  47. float interpolate(float value1, float value2, float value3, float value4, glm::vec2 t) {
  48.     return glm::mix(glm::mix(value1, value2, t.x), glm::mix(value3, value4, t.x), t.y);
  49. }
  50.  
  51. // Fade function for smooth interpolation
  52. glm::vec2 fade(glm::vec2 t) {
  53.     return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
  54. }
  55.  
  56. // Perlin noise function
  57. float perlinNoise(glm::vec2 position, uint32_t seed) {
  58.     glm::vec2 floorPosition = glm::floor(position);
  59.     glm::vec2 fractPosition = position - floorPosition;
  60.     uint32_t x0 = static_cast<uint32_t>(floorPosition.x);
  61.     uint32_t y0 = static_cast<uint32_t>(floorPosition.y);
  62.     uint32_t x1 = x0 + 1;
  63.     uint32_t y1 = y0 + 1;
  64.  
  65.     float value1 = glm::dot(gradientDirection(hash2D(x0, y0, seed)), fractPosition);
  66.     float value2 = glm::dot(gradientDirection(hash2D(x1, y0, seed)), fractPosition - glm::vec2(1.0f, 0.0f));
  67.     float value3 = glm::dot(gradientDirection(hash2D(x0, y1, seed)), fractPosition - glm::vec2(0.0f, 1.0f));
  68.     float value4 = glm::dot(gradientDirection(hash2D(x1, y1, seed)), fractPosition - glm::vec2(1.0f, 1.0f));
  69.  
  70.     return interpolate(value1, value2, value3, value4, fade(fractPosition));
  71. }
  72. // Noise seed - change to get different wind patterns
  73. const uint SEED = 0xDEADBABE;
  74. glm::vec2  GetWindGradient(glm::vec2  pos){
  75.  
  76.   // Sample the noise at the current position
  77.   float c = perlinNoise(pos,0xDEAD);
  78.   // Sample the noise offset in the x direction
  79.   float x = perlinNoise(pos + glm::vec2(0.001,0.0),SEED);
  80.   // Sample the noise offset in the x direction
  81.     float y = perlinNoise(pos + glm::vec2(0.0,0.001),SEED);
  82.     return glm::vec2 (c - x,c - y)*(1.0f/0.001f);
  83. }
  84.  
  85. //
  86. // This is the bechmark function
  87. int main(){
  88.     // I use this to prevent the compiler from optimizing the noise sampling away.
  89.     // It also serves as a small proof that the wind works: it checks that the direction of the wind was up&right 25% of the time.
  90.     unsigned int prevent_opts = 0;
  91.     for(int iy = 0; iy < 10000; iy++){
  92.         for(int ix = 0; ix < 10000; ix++){
  93.             float x = (float)ix;
  94.             float y = (float)iy;
  95.             glm::vec2 gradient = GetWindGradient(glm::vec2(x,y));
  96.             //std::cout<<gradient.x<<","<<gradient.y<<std::endl;
  97.             if (gradient.x > 0 && gradient.y > 0){
  98.                 prevent_opts += 1;
  99.             }
  100.         }
  101.     }
  102.     std::cout<<prevent_opts<<std::endl;
  103. }
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement