From e79692bb5d2da78299178b0c6f4315a33dbd5588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Tue, 21 Jan 2014 21:41:15 +0100 Subject: [PATCH] Refactored noise shader --- src/render/opengl/OpenGLWater.cpp | 1 + src/render/opengl/opengl.pro | 3 ++- src/render/opengl/shaders/noise.frag | 15 +++++++++++++++ src/render/opengl/shaders/resources.qrc | 1 + src/render/opengl/shaders/water.frag | 8 +------- 5 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 src/render/opengl/shaders/noise.frag diff --git a/src/render/opengl/OpenGLWater.cpp b/src/render/opengl/OpenGLWater.cpp index 9868d75..fa57d2c 100644 --- a/src/render/opengl/OpenGLWater.cpp +++ b/src/render/opengl/OpenGLWater.cpp @@ -27,6 +27,7 @@ void OpenGLWater::initialize() program->addFragmentSource("bruneton"); program->addFragmentSource("tonemapping"); program->addFragmentSource("fadeout"); + program->addFragmentSource("noise"); program->addFragmentSource("water"); setVertex(0, -1.0f, 0.0f, -1.0f); diff --git a/src/render/opengl/opengl.pro b/src/render/opengl/opengl.pro index e08d5a8..ce42fbd 100644 --- a/src/render/opengl/opengl.pro +++ b/src/render/opengl/opengl.pro @@ -87,4 +87,5 @@ OTHER_FILES += \ shaders/tonemapping.frag \ shaders/terrain.frag \ shaders/terrain.vert \ - shaders/fadeout.frag + shaders/fadeout.frag \ + shaders/noise.frag diff --git a/src/render/opengl/shaders/noise.frag b/src/render/opengl/shaders/noise.frag new file mode 100644 index 0000000..02995cf --- /dev/null +++ b/src/render/opengl/shaders/noise.frag @@ -0,0 +1,15 @@ +uniform float noiseInitScaling; +uniform float noiseInitHeight; +uniform float noiseStepScaling; +uniform float noiseStepHeight; +uniform sampler2D simplexSampler; + +vec3 noiseNormal2d(vec2 location, float detail) +{ + vec3 normal = vec3(0.0, 0.0, 0.0); + for (float scaling = 1.0; scaling < 400.0; scaling *= 1.5) + { + normal += texture2D(simplexSampler, location * 0.01 * scaling).xyz; + } + return normalize(normal); +} diff --git a/src/render/opengl/shaders/resources.qrc b/src/render/opengl/shaders/resources.qrc index 5dc91be..72a4bb6 100644 --- a/src/render/opengl/shaders/resources.qrc +++ b/src/render/opengl/shaders/resources.qrc @@ -9,5 +9,6 @@ terrain.frag terrain.vert fadeout.frag + noise.frag diff --git a/src/render/opengl/shaders/water.frag b/src/render/opengl/shaders/water.frag index 69d9efc..4c00fe8 100644 --- a/src/render/opengl/shaders/water.frag +++ b/src/render/opengl/shaders/water.frag @@ -1,5 +1,4 @@ uniform vec4 waterColor; -uniform sampler2D simplexSampler; uniform float waterReflection; vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess) @@ -25,12 +24,7 @@ vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess) void main(void) { - vec3 normal = vec3(0.0, 0.0, 0.0); - for (float scaling = 1.0; scaling < 400.0; scaling *= 1.5) - { - normal += texture2D(simplexSampler, unprojected.xz * 0.01 * scaling).xyz; - } - normal = normalize(normal); + vec3 normal = noiseNormal2d(unprojected.xz, 0.001); gl_FragColor = applyLighting(unprojected, normal, waterColor, 100.0);