I'm trying to port a 1D perlin noise tutorial on C++












0















I'm trying to port a 1D perlin noise tutorial on C++ using SFMl lib : (tutorial link in javascript) https://codepen.io/Tobsta/post/procedural-generation-part-1-1d-perlin-noise



However this doesn't work, i don't get any error but this is what i get : https://i.imgur.com/2tAPhsH.png .
basically a straight line



And this is what i should get : https://i.imgur.com/GPnfsuK.png



Here's the ported code from the above link :



TerrainBorder constructor:



TerrainBorder::TerrainBorder(sf::RenderWindow &window) {

M = 4294967296;
A = 1664525;
C = 1;

std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int> dist(0, M);

Z = floor(dist(rng) * M);
x = 0;
y = window.getSize().y / 2.0f;
amp = 100;
wl = 100;
fq = 1.0f / wl;
a = rand();
b = rand();

ar = sf::VertexArray(sf::Points);
}


Functions:



double TerrainBorder::rand()
{
Z = (A * Z + C) % M;
return Z / M - 0.5;
}

double TerrainBorder::interpolate(double pa, double pb , double px) {
double ft = px * PI,
f = (1 - cos(ft)) * 0.5;
return pa * (1 - f) + pb * f;
}

void TerrainBorder::drawPoints(sf::RenderWindow &window) {
while (x < window.getSize().x) {

if (static_cast<int> (x) % wl == 0) {
a = b;
b = rand();
y = window.getSize().y / 2 + a * amp;
} else {
y = window.getSize().y / 2 + interpolate(a, b, static_cast<int> (x)
% wl / wl) * amp;
}
ar.append(sf::Vertex(sf::Vector2f(x, y)));
x += 1;
}
}


Then i'm drawing the sf::VectorArray (which contains all the sf::Vertex in the game loop










share|improve this question





























    0















    I'm trying to port a 1D perlin noise tutorial on C++ using SFMl lib : (tutorial link in javascript) https://codepen.io/Tobsta/post/procedural-generation-part-1-1d-perlin-noise



    However this doesn't work, i don't get any error but this is what i get : https://i.imgur.com/2tAPhsH.png .
    basically a straight line



    And this is what i should get : https://i.imgur.com/GPnfsuK.png



    Here's the ported code from the above link :



    TerrainBorder constructor:



    TerrainBorder::TerrainBorder(sf::RenderWindow &window) {

    M = 4294967296;
    A = 1664525;
    C = 1;

    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<int> dist(0, M);

    Z = floor(dist(rng) * M);
    x = 0;
    y = window.getSize().y / 2.0f;
    amp = 100;
    wl = 100;
    fq = 1.0f / wl;
    a = rand();
    b = rand();

    ar = sf::VertexArray(sf::Points);
    }


    Functions:



    double TerrainBorder::rand()
    {
    Z = (A * Z + C) % M;
    return Z / M - 0.5;
    }

    double TerrainBorder::interpolate(double pa, double pb , double px) {
    double ft = px * PI,
    f = (1 - cos(ft)) * 0.5;
    return pa * (1 - f) + pb * f;
    }

    void TerrainBorder::drawPoints(sf::RenderWindow &window) {
    while (x < window.getSize().x) {

    if (static_cast<int> (x) % wl == 0) {
    a = b;
    b = rand();
    y = window.getSize().y / 2 + a * amp;
    } else {
    y = window.getSize().y / 2 + interpolate(a, b, static_cast<int> (x)
    % wl / wl) * amp;
    }
    ar.append(sf::Vertex(sf::Vector2f(x, y)));
    x += 1;
    }
    }


    Then i'm drawing the sf::VectorArray (which contains all the sf::Vertex in the game loop










    share|improve this question



























      0












      0








      0








      I'm trying to port a 1D perlin noise tutorial on C++ using SFMl lib : (tutorial link in javascript) https://codepen.io/Tobsta/post/procedural-generation-part-1-1d-perlin-noise



      However this doesn't work, i don't get any error but this is what i get : https://i.imgur.com/2tAPhsH.png .
      basically a straight line



      And this is what i should get : https://i.imgur.com/GPnfsuK.png



      Here's the ported code from the above link :



      TerrainBorder constructor:



      TerrainBorder::TerrainBorder(sf::RenderWindow &window) {

      M = 4294967296;
      A = 1664525;
      C = 1;

      std::random_device rd;
      std::mt19937 rng(rd());
      std::uniform_int_distribution<int> dist(0, M);

      Z = floor(dist(rng) * M);
      x = 0;
      y = window.getSize().y / 2.0f;
      amp = 100;
      wl = 100;
      fq = 1.0f / wl;
      a = rand();
      b = rand();

      ar = sf::VertexArray(sf::Points);
      }


      Functions:



      double TerrainBorder::rand()
      {
      Z = (A * Z + C) % M;
      return Z / M - 0.5;
      }

      double TerrainBorder::interpolate(double pa, double pb , double px) {
      double ft = px * PI,
      f = (1 - cos(ft)) * 0.5;
      return pa * (1 - f) + pb * f;
      }

      void TerrainBorder::drawPoints(sf::RenderWindow &window) {
      while (x < window.getSize().x) {

      if (static_cast<int> (x) % wl == 0) {
      a = b;
      b = rand();
      y = window.getSize().y / 2 + a * amp;
      } else {
      y = window.getSize().y / 2 + interpolate(a, b, static_cast<int> (x)
      % wl / wl) * amp;
      }
      ar.append(sf::Vertex(sf::Vector2f(x, y)));
      x += 1;
      }
      }


      Then i'm drawing the sf::VectorArray (which contains all the sf::Vertex in the game loop










      share|improve this question
















      I'm trying to port a 1D perlin noise tutorial on C++ using SFMl lib : (tutorial link in javascript) https://codepen.io/Tobsta/post/procedural-generation-part-1-1d-perlin-noise



      However this doesn't work, i don't get any error but this is what i get : https://i.imgur.com/2tAPhsH.png .
      basically a straight line



      And this is what i should get : https://i.imgur.com/GPnfsuK.png



      Here's the ported code from the above link :



      TerrainBorder constructor:



      TerrainBorder::TerrainBorder(sf::RenderWindow &window) {

      M = 4294967296;
      A = 1664525;
      C = 1;

      std::random_device rd;
      std::mt19937 rng(rd());
      std::uniform_int_distribution<int> dist(0, M);

      Z = floor(dist(rng) * M);
      x = 0;
      y = window.getSize().y / 2.0f;
      amp = 100;
      wl = 100;
      fq = 1.0f / wl;
      a = rand();
      b = rand();

      ar = sf::VertexArray(sf::Points);
      }


      Functions:



      double TerrainBorder::rand()
      {
      Z = (A * Z + C) % M;
      return Z / M - 0.5;
      }

      double TerrainBorder::interpolate(double pa, double pb , double px) {
      double ft = px * PI,
      f = (1 - cos(ft)) * 0.5;
      return pa * (1 - f) + pb * f;
      }

      void TerrainBorder::drawPoints(sf::RenderWindow &window) {
      while (x < window.getSize().x) {

      if (static_cast<int> (x) % wl == 0) {
      a = b;
      b = rand();
      y = window.getSize().y / 2 + a * amp;
      } else {
      y = window.getSize().y / 2 + interpolate(a, b, static_cast<int> (x)
      % wl / wl) * amp;
      }
      ar.append(sf::Vertex(sf::Vector2f(x, y)));
      x += 1;
      }
      }


      Then i'm drawing the sf::VectorArray (which contains all the sf::Vertex in the game loop







      javascript c++ sfml perlin-noise






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 14:15









      matt

      1,042521




      1,042521










      asked Nov 13 '18 at 13:23









      KizekoKizeko

      112




      112
























          2 Answers
          2






          active

          oldest

          votes


















          1














          i solved my problem ty for the answer anyway :)
          I had to deal with types problems :p



          I figured out that :



          double c = x % 100 / 100;
          std::cout << c << std::endl; // 0


          !=



          double c = x % 100;
          std::cout << c / 100 << std::endl; // Some numbers depending on x


          If it can help anyone in the future :)






          share|improve this answer































            0














            C++ requires a careful choice for the types of the numeric variables, to avoid overflows and unexpected conversions.



            The snippet shown in OP's question doesn't specify the types of M, A and Z, but uses a std::uniform_int_distribution of int, while M is initialized with a value that's out of the range of an int in most implementations.



            It's also worth to be noted that the Standard Library already provides a std::linear_congruential_engine:



            #include <iostream>
            #include <random>

            int main()
            {
            std::random_device rd;
            std::mt19937 rng(rd());

            // Calculate the first value
            constexpr std::size_t M = 4294967296;
            std::uniform_int_distribution<std::size_t> ui_dist(0, M);
            std::size_t Z = ui_dist(rng);

            // Initialize the engine
            static std::linear_congruential_engine<std::size_t, 1664525, 1, M> lcg_dist(Z);

            // Generate the values
            for (int i = 0; i < 10; ++i)
            {
            Z = lcg_dist();
            std::cout << Z / double(M) << 'n'; // <- To avoid an integer division
            }
            }





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281999%2fim-trying-to-port-a-1d-perlin-noise-tutorial-on-c%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              i solved my problem ty for the answer anyway :)
              I had to deal with types problems :p



              I figured out that :



              double c = x % 100 / 100;
              std::cout << c << std::endl; // 0


              !=



              double c = x % 100;
              std::cout << c / 100 << std::endl; // Some numbers depending on x


              If it can help anyone in the future :)






              share|improve this answer




























                1














                i solved my problem ty for the answer anyway :)
                I had to deal with types problems :p



                I figured out that :



                double c = x % 100 / 100;
                std::cout << c << std::endl; // 0


                !=



                double c = x % 100;
                std::cout << c / 100 << std::endl; // Some numbers depending on x


                If it can help anyone in the future :)






                share|improve this answer


























                  1












                  1








                  1







                  i solved my problem ty for the answer anyway :)
                  I had to deal with types problems :p



                  I figured out that :



                  double c = x % 100 / 100;
                  std::cout << c << std::endl; // 0


                  !=



                  double c = x % 100;
                  std::cout << c / 100 << std::endl; // Some numbers depending on x


                  If it can help anyone in the future :)






                  share|improve this answer













                  i solved my problem ty for the answer anyway :)
                  I had to deal with types problems :p



                  I figured out that :



                  double c = x % 100 / 100;
                  std::cout << c << std::endl; // 0


                  !=



                  double c = x % 100;
                  std::cout << c / 100 << std::endl; // Some numbers depending on x


                  If it can help anyone in the future :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 16:37









                  KizekoKizeko

                  112




                  112

























                      0














                      C++ requires a careful choice for the types of the numeric variables, to avoid overflows and unexpected conversions.



                      The snippet shown in OP's question doesn't specify the types of M, A and Z, but uses a std::uniform_int_distribution of int, while M is initialized with a value that's out of the range of an int in most implementations.



                      It's also worth to be noted that the Standard Library already provides a std::linear_congruential_engine:



                      #include <iostream>
                      #include <random>

                      int main()
                      {
                      std::random_device rd;
                      std::mt19937 rng(rd());

                      // Calculate the first value
                      constexpr std::size_t M = 4294967296;
                      std::uniform_int_distribution<std::size_t> ui_dist(0, M);
                      std::size_t Z = ui_dist(rng);

                      // Initialize the engine
                      static std::linear_congruential_engine<std::size_t, 1664525, 1, M> lcg_dist(Z);

                      // Generate the values
                      for (int i = 0; i < 10; ++i)
                      {
                      Z = lcg_dist();
                      std::cout << Z / double(M) << 'n'; // <- To avoid an integer division
                      }
                      }





                      share|improve this answer






























                        0














                        C++ requires a careful choice for the types of the numeric variables, to avoid overflows and unexpected conversions.



                        The snippet shown in OP's question doesn't specify the types of M, A and Z, but uses a std::uniform_int_distribution of int, while M is initialized with a value that's out of the range of an int in most implementations.



                        It's also worth to be noted that the Standard Library already provides a std::linear_congruential_engine:



                        #include <iostream>
                        #include <random>

                        int main()
                        {
                        std::random_device rd;
                        std::mt19937 rng(rd());

                        // Calculate the first value
                        constexpr std::size_t M = 4294967296;
                        std::uniform_int_distribution<std::size_t> ui_dist(0, M);
                        std::size_t Z = ui_dist(rng);

                        // Initialize the engine
                        static std::linear_congruential_engine<std::size_t, 1664525, 1, M> lcg_dist(Z);

                        // Generate the values
                        for (int i = 0; i < 10; ++i)
                        {
                        Z = lcg_dist();
                        std::cout << Z / double(M) << 'n'; // <- To avoid an integer division
                        }
                        }





                        share|improve this answer




























                          0












                          0








                          0







                          C++ requires a careful choice for the types of the numeric variables, to avoid overflows and unexpected conversions.



                          The snippet shown in OP's question doesn't specify the types of M, A and Z, but uses a std::uniform_int_distribution of int, while M is initialized with a value that's out of the range of an int in most implementations.



                          It's also worth to be noted that the Standard Library already provides a std::linear_congruential_engine:



                          #include <iostream>
                          #include <random>

                          int main()
                          {
                          std::random_device rd;
                          std::mt19937 rng(rd());

                          // Calculate the first value
                          constexpr std::size_t M = 4294967296;
                          std::uniform_int_distribution<std::size_t> ui_dist(0, M);
                          std::size_t Z = ui_dist(rng);

                          // Initialize the engine
                          static std::linear_congruential_engine<std::size_t, 1664525, 1, M> lcg_dist(Z);

                          // Generate the values
                          for (int i = 0; i < 10; ++i)
                          {
                          Z = lcg_dist();
                          std::cout << Z / double(M) << 'n'; // <- To avoid an integer division
                          }
                          }





                          share|improve this answer















                          C++ requires a careful choice for the types of the numeric variables, to avoid overflows and unexpected conversions.



                          The snippet shown in OP's question doesn't specify the types of M, A and Z, but uses a std::uniform_int_distribution of int, while M is initialized with a value that's out of the range of an int in most implementations.



                          It's also worth to be noted that the Standard Library already provides a std::linear_congruential_engine:



                          #include <iostream>
                          #include <random>

                          int main()
                          {
                          std::random_device rd;
                          std::mt19937 rng(rd());

                          // Calculate the first value
                          constexpr std::size_t M = 4294967296;
                          std::uniform_int_distribution<std::size_t> ui_dist(0, M);
                          std::size_t Z = ui_dist(rng);

                          // Initialize the engine
                          static std::linear_congruential_engine<std::size_t, 1664525, 1, M> lcg_dist(Z);

                          // Generate the values
                          for (int i = 0; i < 10; ++i)
                          {
                          Z = lcg_dist();
                          std::cout << Z / double(M) << 'n'; // <- To avoid an integer division
                          }
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 13 '18 at 16:09

























                          answered Nov 13 '18 at 15:57









                          Bob__Bob__

                          4,89331425




                          4,89331425






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281999%2fim-trying-to-port-a-1d-perlin-noise-tutorial-on-c%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine