[ad_1]
I am at present making a sport that’s going to contain terrain era. I’ve already tried terrain era utilizing FastNoise2, and it “labored” and truthfully can be sooner than the “Roll your personal” I’ll strive, nevertheless it’d be enjoyable to roll my very own anyway.
I managed to get regular 2D perlin noise working like so:
float Noise2D (int x, int y, int seed) {
Random::gen.seed(x + y * 57 + seed); // <-- Random::gen is an occasion of std::mt19937
auto n = fmod(Random::RandomFloat(), 1); // <-- RandomFloat is a random vary perform; with no parameters it simply makes the vary 0-[whatever the default max float value in <random> is]
Random::gen.seed(seed);
return n;
}
float PerlinNoise2D(float x, float y, int seed) {
auto floor_x = flooring(x), floor_y = flooring(y);
auto s = Noise2D(floor_x, floor_y, seed);
auto t = Noise2D(floor_x + 1, floor_y, seed);
auto u = Noise2D(floor_x, floor_y + 1, seed);
auto v = Noise2D(floor_x + 1, floor_y + 1, seed);
auto frac_x = x- floor_x;
auto frac_y = y - floor_y;
auto worth = s * (1 - frac_x) + t * frac_x;
worth += (u - s) * frac_y * (1 - frac_x);
worth += (v - t) * frac_x * frac_y;
return worth;
}
And to check it:
void TestPerlinNoise() {
int seed = Random::RandomFloat(0, 1000000);
const int SIZE = 96;
SDL_Surface* floor = SDL_CreateRGBSurface(0, SIZE, SIZE, 32, 0, 0, 0, 0);
//float take a look at[SIZE][SIZE];
auto scale = (10.0f / float(SIZE));
for (int y = 0; y < SIZE; y++) {
for (int x = 0; x < SIZE; x++) {
//take a look at[y][x] = PerlinNoise2D(x, y, seed);
auto noise = PerlinNoise2D(x * scale, y * scale, seed);
Uint32 coloration = RGBA((uint8_t)(noise * 255), (uint8_t)(noise * 255), (uint8_t)(noise * 255), 255);
SDLSetPixel(floor, x, y, coloration);
}
}
IMG_SavePNG(floor, "take a look at.png");
SDL_FreeSurface(floor);
}
However how do I make the output of this perform tileable?
[ad_2]