Skip to content

simplex noise does not work for dvec3 #734

Open
@krux02

Description

@krux02

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions