From 9608281164b8285913b98d5d5032d5f0ec063edb Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Sun, 29 Jan 2023 12:19:19 +0100 Subject: [PATCH] Adds seed to noise and CloudSettings --- common/noise/cloudnoise.cpp | 10 +++++----- common/noise/cloudnoise.h | 7 ++++++- common/noise/perlinnoise.cpp | 10 +++++++--- common/noise/perlinnoise.h | 8 +++++++- common/noise/worleynoise.cpp | 10 +++++++--- common/noise/worleynoise.h | 8 +++++++- fancy1.cpp | 10 +++++----- shader/cloudshader.cpp | 2 +- shader/cloudshader.h | 1 + 9 files changed, 46 insertions(+), 20 deletions(-) diff --git a/common/noise/cloudnoise.cpp b/common/noise/cloudnoise.cpp index 4b5a345..9469ff2 100644 --- a/common/noise/cloudnoise.cpp +++ b/common/noise/cloudnoise.cpp @@ -2,17 +2,17 @@ #include "worleynoise.h" #include "perlinnoise.h" -CloudNoise::CloudNoise(int size) : Noise(size) +CloudNoise::CloudNoise(int size, unsigned int seed) : Noise(size) { int minSize = std::min(32, size); // Some worley noises - WorleyNoise worleyNoise1(minSize, 3); - WorleyNoise worleyNoise3(size, 15); + WorleyNoise worleyNoise1(minSize, 3, seed); + WorleyNoise worleyNoise3(size, 15, seed); // Some perlin noises - PerlinNoise perlinNoise1(minSize, 3); - PerlinNoise perlinNoise2(size, 15); + PerlinNoise perlinNoise1(minSize, 3, seed); + PerlinNoise perlinNoise2(size, 15, seed); // Generate the noise for (int x = 0; x < size; x++) diff --git a/common/noise/cloudnoise.h b/common/noise/cloudnoise.h index 7ce7428..aa4d60d 100644 --- a/common/noise/cloudnoise.h +++ b/common/noise/cloudnoise.h @@ -7,7 +7,12 @@ class CloudNoise : public Noise { public: - CloudNoise(int size); + /** + * + * @param size + * @param seed 0 for random seed + */ + CloudNoise(int size, unsigned int seed = 0); }; diff --git a/common/noise/perlinnoise.cpp b/common/noise/perlinnoise.cpp index 3f8183a..a675bd1 100644 --- a/common/noise/perlinnoise.cpp +++ b/common/noise/perlinnoise.cpp @@ -56,11 +56,15 @@ float PerlinNoise::getGradientValue(int x, int y, int z) return noise; } -PerlinNoise::PerlinNoise(int size, int gridSize) : Noise(size), gridSize(gridSize) +PerlinNoise::PerlinNoise(int size, int gridSize, unsigned int seed) : Noise(size), gridSize(gridSize) { // Init random - std::random_device rd; - this->generator = std::mt19937(rd()); + if (seed == 0) + { + std::random_device rd; + seed = rd(); + } + this->generator = std::mt19937(seed); this->distribution = std::normal_distribution(0.0f, 1.0f); generateNoise(); diff --git a/common/noise/perlinnoise.h b/common/noise/perlinnoise.h index 7766f5b..9a4837c 100644 --- a/common/noise/perlinnoise.h +++ b/common/noise/perlinnoise.h @@ -8,7 +8,13 @@ class PerlinNoise : public Noise { public: - PerlinNoise(int size, int gridSize); + /** + * + * @param size + * @param gridSize + * @param seed 0 for random seed + */ + PerlinNoise(int size, int gridSize, unsigned int seed = 0); private: void generateNoise(); diff --git a/common/noise/worleynoise.cpp b/common/noise/worleynoise.cpp index 939b619..3d65749 100644 --- a/common/noise/worleynoise.cpp +++ b/common/noise/worleynoise.cpp @@ -66,11 +66,15 @@ void WorleyNoise::generateNoise() << duration.count() << " seconds" << std::endl; } -WorleyNoise::WorleyNoise(int size, int numberOfPoints) : numberOfPoints(numberOfPoints), Noise(size) +WorleyNoise::WorleyNoise(int size, int numberOfPoints, unsigned int seed) : numberOfPoints(numberOfPoints), Noise(size) { // Init random - std::random_device rd; - this->generator = std::mt19937(rd()); + if (seed == 0) + { + std::random_device rd; + seed = rd(); + } + this->generator = std::mt19937(seed); this->distribution = std::uniform_real_distribution(0.0f, 1.0f); generateNoise(); diff --git a/common/noise/worleynoise.h b/common/noise/worleynoise.h index 0bba3ed..3a37390 100644 --- a/common/noise/worleynoise.h +++ b/common/noise/worleynoise.h @@ -10,7 +10,13 @@ class WorleyNoise : public Noise { public: - WorleyNoise(int size, int numberOfPoints); + /** + * + * @param size + * @param numberOfPoints + * @param seed 0 for random seed + */ + WorleyNoise(int size, int numberOfPoints, unsigned int seed = 0); protected: int numberOfPoints; diff --git a/fancy1.cpp b/fancy1.cpp index 0a721d9..f55e46e 100644 --- a/fancy1.cpp +++ b/fancy1.cpp @@ -28,7 +28,7 @@ int main() { FastScene scene; - scene.setBackgroundColor(Color(0.529f, 0.808f, 0.922f) * 1.0f); + scene.setBackgroundColor(Color(0.2588f, 0.5098f, 0.9568f) * 1.0f); // scene.setBackgroundColor(Color(1, 0.79f, 0.62f) * 0.8f); // Add lights @@ -50,9 +50,9 @@ int main() // scene.add(std::make_shared(Vector3d(9.0f, 3.0f, 12.0f), Vector3d(3.0f, 3.0f, 3.0f), boxShader1)); // Add floor - auto floorShader = std::make_shared(Color(0.9f, 0.9f, 0.9f)); - scene.add(std::make_shared(Vector3d(0.0f, 0.0f, 0.0f), Vector3d(0.0f, 1.0f, 0.0f), - floorShader)); +// auto floorShader = std::make_shared(Color(0.9f, 0.9f, 0.9f)); +// scene.add(std::make_shared(Vector3d(0.0f, 0.0f, 0.0f), Vector3d(0.0f, 1.0f, 0.0f), +// floorShader)); // Add box for volume shader auto cloudSettings = CloudSettings(); @@ -72,7 +72,7 @@ int main() // Render the scene SuperRenderer sr; sr.setSuperSamplingFactor(1); - sr.renderImage(scene, camera, 256, 256).save("result.png"); + sr.renderImage(scene, camera, 512, 512).save("result.png"); return 0; } diff --git a/shader/cloudshader.cpp b/shader/cloudshader.cpp index 8491d58..dd8a11a 100644 --- a/shader/cloudshader.cpp +++ b/shader/cloudshader.cpp @@ -69,7 +69,7 @@ bool CloudShader::isTransparent() const } CloudShader::CloudShader(const CloudSettings &settings) : settings(settings), - cloudNoise(CloudNoise(NOISE_SIZE)) + cloudNoise(CloudNoise(NOISE_SIZE, settings.seed)) { cloudNoise.invert = true; } diff --git a/shader/cloudshader.h b/shader/cloudshader.h index 55ff38d..a85d394 100644 --- a/shader/cloudshader.h +++ b/shader/cloudshader.h @@ -12,6 +12,7 @@ float const TRANSMITTANCE_BREAK = 0.0001f; // If transmittance goes below this struct CloudSettings { + unsigned int seed = 0; // 0 for random seed float densitySteps = .2f; // .2f float scale = 30; // 30 float densityOffset = -.55f; // -.55f