Implementing random vectors on lattice points

This commit is contained in:
CJSatnarine
2024-07-09 20:20:42 -04:00
parent 3bbb02ee94
commit 176c33bcd7
5 changed files with 356497 additions and 356497 deletions

Binary file not shown.

712954
image.ppm

File diff suppressed because it is too large Load Diff

View File

@@ -6,9 +6,9 @@
class perlin {
public:
perlin() {
randFloat = new double[pointCount];
randVector = new vec3[pointCount];
for (int i = 0; i < pointCount; i++) {
randFloat[i] = randomDouble();
randVector[i] = unitVector(vec3::random(-1, 1));
}
permX = perlinGeneratePerm();
@@ -17,7 +17,7 @@ class perlin {
}
~perlin() {
delete[] randFloat;
delete[] randVector;
delete[] permX;
delete[] permY;
delete[] permZ;
@@ -27,19 +27,16 @@ class perlin {
auto u = p.x() - floor(p.x());
auto v = p.y() - floor(p.y());
auto w = p.z() - floor(p.z());
u = u*u*(3-2*u);
v = v*v*(3-2*v);
w = w*w*(3-2*w);
auto i = int(floor(p.x()));
auto j = int(floor(p.y()));
auto k = int(floor(p.z()));
double c[2][2][2];
vec3 c[2][2][2];
for (int di = 0; di < 2; di++) {
for (int dj = 0; dj < 2; dj++) {
for (int dk = 0; dk < 2; dk++) {
c[di][dj][dk] = randFloat[
c[di][dj][dk] = randVector[
permX[(i+di) & 255] ^
permY[(j+dj) & 255] ^
permZ[(k+dk) & 255]
@@ -47,12 +44,12 @@ class perlin {
}
}
}
return trileanerInterpolation(c, u, v, w);
return perlinInterpolation(c, u, v, w);
}
private:
static const int pointCount = 256;
double* randFloat;
vec3* randVector;
int* permX;
int* permY;
int* permZ;
@@ -78,19 +75,22 @@ class perlin {
}
}
static double trileanerInterpolation(double c[2][2][2], double u, double v, double w) {
static double perlinInterpolation(const vec3 c[2][2][2], double u, double v, double w) {
auto uu = u * u * (3 - 2 * u);
auto vv = v * v * (3 - 2 * v);
auto ww = w * w * (3 - 2 * w);
auto accum = 0.0;
for (int i =0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
accum += (i*u + (1-i)*(1-u))
* (j*v + (1-j)*(1-v))
* (k*w + (1-k)*(1-w))
* c[i][j][k];
}
}
vec3 weightV(u - i, v - j, w - k);
accum += (i * uu + (1 - i)*(1 - uu))
* (j * vv + (1 - j) * (1 - vv))
* (k * ww + (1 - k) * (1 - ww))
* dot(c[i][j][k], weightV);
}
return accum;
}
};

View File

@@ -80,7 +80,7 @@ class noiseTexture : public texture {
noiseTexture(double scale) : scale(scale) {}
colour value(double u, double v, const point3& p) const override {
return colour(1,1,1) * noise.noise(scale * p);
return colour(1, 1, 1) * 0.5 * (1.0 + noise.noise(scale * p));
}
private: