Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void PerlinNoise2D::generateNoise()
- {
- delete[]this->perlinNoise;
- this->perlinNoise = new float[width * height];
- for (int x = 0; x < width; ++x) {
- for (int y = 0; y < height; ++y) {
- float noise = 0.f;
- scale = 0.5f;
- float scaleAcc = 0.f;
- for (int o = 0; o < octave; ++o) {
- int pitchX = width >> o;
- int pitchY = height >> o;
- int sampleX1 = (x / pitchX) * pitchX;
- int sampleY1 = (y / pitchY) * pitchY;
- int sampleX2 = (sampleX1 + pitchX) % width;
- int sampleY2 = (sampleY1 + pitchY) % height;
- float blendX = (float)(x - sampleX1) / (float)pitchX;
- float blendY = (float)(y - sampleY1) / (float)pitchY;
- float sampleT = (1.f - blendX) * noiseSeed[sampleY1 * width + sampleX1] + blendX * noiseSeed[sampleY1 * width + sampleX2];
- float sampleB = (1.f - blendY) * noiseSeed[sampleY2 * width + sampleX1] + blendY * noiseSeed[sampleY2 * width + sampleX2];
- noise += ((blendY * blendX) * (sampleB - sampleT) + sampleT) * scale;
- scaleAcc += scale;
- scale = scale / scalingBias;
- }
- //std::cout << noise / scaleAcc << std::endl;
- perlinNoise[y * width + x] = noise / scaleAcc;
- }
- }
- }
- void PerlinNoise2D::generateNoiseToImage(sf::Image &image)
- {
- for (int x = 0; x < image.getSize().x; ++x) {
- for (int y = 0; y < image.getSize().y; ++y) {
- image.setPixel((unsigned)x, (unsigned)y, sf::Color::White);
- }
- }
- for (int x = 0; x < width; ++x) {
- for (int y = 0; y < height; ++y) {
- float value = perlinNoise[y * width + x];
- if (value > 0.8f) {
- image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(11,39,255,255));
- }
- else if (value > 0.6f && value < 0.8f) {
- image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(120,171,255,255));
- }
- else if(value > 0.4f && value < 0.6f) {
- image.setPixel((unsigned)x, (unsigned)y, sf::Color::Yellow);
- }
- else if(value > 0.2f && value < 0.4f) {
- image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(19,255,38,255));
- }
- else if (value > 0.f && value < 0.2f) {
- image.setPixel((unsigned)x, (unsigned)y, sf::Color::Color(12, 238, 7, 255));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment