Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

simplex noise does not work for dvec3 #734

Open
krux02 opened this issue Feb 13, 2018 · 1 comment
Open

simplex noise does not work for dvec3 #734

krux02 opened this issue Feb 13, 2018 · 1 comment

Comments

@krux02
Copy link

krux02 commented Feb 13, 2018

simplex noise in 3D with double precision integers produces a wrong noise patters that is not in the bounds of [-1,1].

here is an example that samples the noise at a coordinate where it is out of range. Additionally I create an image where you can see the difference between the float and the double version of the noise function.

#include <glm/glm.hpp>
#include <glm/gtc/noise.hpp>
#include <cstdlib>
#include <cstdio>

using namespace glm;
using namespace std;

int main() {
  printf("%f\n", simplex( vec3(1.4, 2.7, 0.51))); // prints 0.438957
  printf("%f\n", simplex(dvec3(1.4, 2.7, 0.51))); // prints 3.93985

  const int dimx = 800, dimy = 800;

  FILE *fp = fopen("noise-dvec3-error.ppm", "wb"); // b - binary mode
  fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy);
  for (int j = 0; j < dimy; ++j) {
    for (int i = 0; i < dimx; ++i) {
      dvec3 pos(float(i) * 0.01, float(j) * 0.01, 0.51);

      float noisef = simplex(vec3(pos));
      double noised = simplex(pos);

      unsigned char a = (unsigned char)(glm::clamp<float>((noisef * 0.5 + 0.5) * 256, 0.0f, 255.0f));
      unsigned char b = (unsigned char)(glm::clamp<double>((noised * 0.5 + 0.5) * 256, 0.0d, 255.0d));

      static unsigned char color[3];

      color[0] = a;
      color[1] = b;

      // make out of range values more visible
      if(b == 0 || b == 255)
        color[2] = 255 - b;
      else
        color[2] = b;

      (void) fwrite(color, 1, 3, fp);
    }
  }
  fclose(fp);
  return EXIT_SUCCESS;
}

Output Image:
vec3d-error

  • red channel is the result of simplex(vec3)
  • green/blue channel is the result of simplex(dvec3)
  • blue channel is inverted if simplex(dvec3) needs clamping (is out of range)
  • A grayscale pixel means that simplex(vec3) and simplex(dvec3) as the same value and no clipping occurs
  • A colored pixel means simplex(vec3) and simplex(dvec3) disagrees on the result.

EDIT:
simplex of dvec4 does not even compile.

@Retr111
Copy link

Retr111 commented May 17, 2019

bug still exists, e.g.

auto value = glm::simplex(glm::dvec3 { 0.71805937418432031, -0.65074130785454032, -0.24683290987586010 });
// value == -1.0236474215821429, value not in [-1.0, 1.0], 

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants