From 02a026116da22aba5e7715ad823769f9d33955c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 4 Jan 2016 20:26:40 +0100 Subject: [PATCH] Fixed noise scaling in opengl --- src/basics/NoiseFunctionSimplex.cpp | 31 ++++++++++++++++++---------- src/interface/commandline/tests.cpp | 30 ++++++++++++++++++++------- src/render/opengl/shaders/noise.frag | 4 ++-- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/basics/NoiseFunctionSimplex.cpp b/src/basics/NoiseFunctionSimplex.cpp index a38a21a..fc8b4dc 100644 --- a/src/basics/NoiseFunctionSimplex.cpp +++ b/src/basics/NoiseFunctionSimplex.cpp @@ -476,18 +476,22 @@ double NoiseFunctionSimplex::getBase3d(double x, double y, double z) const { return noiseSimplexGet3DValue(x, y, z) - 0.5; } +static constexpr double TEXTURE_SCALING = 15.0; static Texture2D *_valueTexture = NULL; const Texture2D *NoiseFunctionSimplex::getValueTexture() { if (!_valueTexture) { - const int width = 1024; - const int height = 1024; + const int width = 2048; + const int height = 2048; _valueTexture = new Texture2D(width, height); for (int x = 0; x < width; x++) { for (int z = 0; z < height; z++) { - double val = noiseSimplexGet2DValue(to_double(x), to_double(z)) - 0.5; + // TODO Make texture tileable + double dx = to_double(x) / to_double(width); + double dz = to_double(z) / to_double(height); + double val = noiseSimplexGet2DValue(TEXTURE_SCALING * dx, TEXTURE_SCALING * dz); _valueTexture->setPixel(x, z, Color(val, val, val)); } } @@ -500,22 +504,27 @@ static Texture2D *_normalTexture = NULL; const Texture2D *NoiseFunctionSimplex::getNormalTexture() { if (!_normalTexture) { - const int width = 1024; - const int height = 1024; + const int width = 2048; + const int height = 2048; _normalTexture = new Texture2D(width, height); + double scale = TEXTURE_SCALING; + double offset = scale * 0.1; + for (int x = 0; x < width; x++) { for (int z = 0; z < height; z++) { // TODO Make texture tileable - double vcenter = noiseSimplexGet2DValue(0.01 * to_double(x), 0.01 * to_double(z)) - 0.5; - double vsouth = noiseSimplexGet2DValue(0.01 * to_double(x), 0.01 * to_double(z) + 0.001) - 0.5; - double veast = noiseSimplexGet2DValue(0.01 * to_double(x) + 0.001, 0.01 * to_double(z)) - 0.5; + double dx = to_double(x) / to_double(width); + double dz = to_double(z) / to_double(height); + double vcenter = noiseSimplexGet2DValue(scale * dx, scale * dz) - 0.5; + double vsouth = noiseSimplexGet2DValue(scale * dx, scale * dz + offset) - 0.5; + double veast = noiseSimplexGet2DValue(scale * dx + offset, scale * dz) - 0.5; - Vector3 normal = Geometry::getNormalFromTriangle(Vector3(0.0, vcenter, 0.0), Vector3(0.0, vsouth, 0.01), - Vector3(0.01, veast, 0.0)); + Vector3 normal = Geometry::getNormalFromTriangle( + Vector3(0.0, vcenter, 0.0), Vector3(0.0, vsouth, offset), Vector3(offset, veast, 0.0)); - _normalTexture->setPixel(x, z, Color(normal.x, normal.y, normal.z)); + _normalTexture->setPixel(x, z, Color(normal.x + 0.5, normal.y + 0.5, normal.z + 0.5)); } } } diff --git a/src/interface/commandline/tests.cpp b/src/interface/commandline/tests.cpp index ca81b98..b8bdee6 100644 --- a/src/interface/commandline/tests.cpp +++ b/src/interface/commandline/tests.cpp @@ -27,7 +27,10 @@ #include "RayCastingResult.h" #include "OpenGLVegetationImpostor.h" #include "Texture2D.h" +#include "NoiseNode.h" +#include "FractalNoise.h" #include "RandomGenerator.h" +#include "NoiseFunctionSimplex.h" #include #include @@ -53,6 +56,18 @@ static void startTestRender(SoftwareCanvasRenderer *renderer, const string &name startRender(renderer, getFileName(name, iteration).data()); } +static void testNoise() { + NoiseFunctionSimplex noise; + + string filename = getFileName("noise_simplex_cache_value"); + cout << "Rendering " << filename << "..." << endl; + noise.getValueTexture()->saveToFile(filename); + + filename = getFileName("noise_simplex_cache_normal"); + cout << "Rendering " << filename << "..." << endl; + noise.getNormalTexture()->saveToFile(filename); +} + static void testGroundShadowQuality() { Scenery scenery; RandomGenerator random(5); @@ -345,13 +360,14 @@ static void testOpenGLVegetationImpostor() { } void runTestSuite() { - testGroundShadowQuality(); - testRasterizationQuality(); - testCloudQuality(); - testGodRays(); - testNearFrustum(); - testCloudsNearGround(); testAtmosphereBruneton(); - testVegetationModels(); + testCloudQuality(); + testCloudsNearGround(); + testGodRays(); + testGroundShadowQuality(); + testNearFrustum(); + testNoise(); testOpenGLVegetationImpostor(); + testRasterizationQuality(); + testVegetationModels(); } diff --git a/src/render/opengl/shaders/noise.frag b/src/render/opengl/shaders/noise.frag index af0bd99..d466fc6 100644 --- a/src/render/opengl/shaders/noise.frag +++ b/src/render/opengl/shaders/noise.frag @@ -10,8 +10,8 @@ vec3 noiseNormal2d(float[4] data, vec2 location, float detail) while (height > detail) { // TODO offsets - // TODO parametrized texture scaling (0.01) - normal += texture(simplexSampler, location * 0.01 * scaling).xyz; + // TODO parametrized texture scaling + normal += texture(simplexSampler, location * scaling / 15.0).xyz - vec3(0.5); scaling *= step_scaling; height *= step_height; }