framework2
185 строк · 7.7 Кб
1/*
2* NoiseWarpPass.cpp
3*
4* Copyright (c) 2013, Neil Mendoza, http://www.neilmendoza.com
5* All rights reserved.
6*
7* Redistribution and use in source and binary forms, with or without
8* modification, are permitted provided that the following conditions are met:
9*
10* * Redistributions of source code must retain the above copyright notice,
11* this list of conditions and the following disclaimer.
12* * Redistributions in binary form must reproduce the above copyright
13* notice, this list of conditions and the following disclaimer in the
14* documentation and/or other materials provided with the distribution.
15* * Neither the name of Neil Mendoza nor the names of its contributors may be used
16* to endorse or promote products derived from this software without
17* specific prior written permission.
18*
19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29* POSSIBILITY OF SUCH DAMAGE.
30*
31*/
32#include "NoiseWarpPass.h"33
34namespace itg35{
36NoiseWarpPass::NoiseWarpPass(const ofVec2f& aspect, bool arb, float frequency, float amplitude, float speed) :37frequency(frequency), amplitude(amplitude), speed(speed), RenderPass(aspect, arb, "noisewarp")38{39string fragShaderSrc = STRINGIFY(40uniform sampler2D tex;41
42uniform float frequency;43uniform float amplitude;44uniform float time;45uniform float speed;46
47//48// Description : Array and textureless GLSL 2D/3D/4D simplex49// noise functions.50// Author : Ian McEwan, Ashima Arts.51// Maintainer : ijm52// Lastmod : 20110822 (ijm)53// License : Copyright (C) 2011 Ashima Arts. All rights reserved.54// Distributed under the MIT License. See LICENSE file.55// https://github.com/ashima/webgl-noise56//57
58vec3 mod289(vec3 x) {59return x - floor(x * (1.0 / 289.0)) * 289.0;60}61
62vec4 mod289(vec4 x) {63return x - floor(x * (1.0 / 289.0)) * 289.0;64}65
66vec4 permute(vec4 x) {67return mod289(((x*34.0)+1.0)*x);68}69
70vec4 taylorInvSqrt(vec4 r)71{72return 1.79284291400159 - 0.85373472095314 * r;73}74
75float snoise(vec3 v)76{77const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;78const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);79
80// First corner81vec3 i = floor(v + dot(v, C.yyy) );82vec3 x0 = v - i + dot(i, C.xxx) ;83
84// Other corners85vec3 g = step(x0.yzx, x0.xyz);86vec3 l = 1.0 - g;87vec3 i1 = min( g.xyz, l.zxy );88vec3 i2 = max( g.xyz, l.zxy );89
90// x0 = x0 - 0.0 + 0.0 * C.xxx;91// x1 = x0 - i1 + 1.0 * C.xxx;92// x2 = x0 - i2 + 2.0 * C.xxx;93// x3 = x0 - 1.0 + 3.0 * C.xxx;94vec3 x1 = x0 - i1 + C.xxx;95vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y96vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y97
98// Permutations99i = mod289(i);100vec4 p = permute( permute( permute(101i.z + vec4(0.0, i1.z, i2.z, 1.0 ))102+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))103+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));104
105// Gradients: 7x7 points over a square, mapped onto an octahedron.106// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)107float n_ = 0.142857142857; // 1.0/7.0108vec3 ns = n_ * D.wyz - D.xzx;109
110vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)111
112vec4 x_ = floor(j * ns.z);113vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)114
115vec4 x = x_ *ns.x + ns.yyyy;116vec4 y = y_ *ns.x + ns.yyyy;117vec4 h = 1.0 - abs(x) - abs(y);118
119vec4 b0 = vec4( x.xy, y.xy );120vec4 b1 = vec4( x.zw, y.zw );121
122//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;123//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;124vec4 s0 = floor(b0)*2.0 + 1.0;125vec4 s1 = floor(b1)*2.0 + 1.0;126vec4 sh = -step(h, vec4(0.0));127
128vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;129vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;130
131vec3 p0 = vec3(a0.xy,h.x);132vec3 p1 = vec3(a0.zw,h.y);133vec3 p2 = vec3(a1.xy,h.z);134vec3 p3 = vec3(a1.zw,h.w);135
136//Normalise gradients137vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));138p0 *= norm.x;139p1 *= norm.y;140p2 *= norm.z;141p3 *= norm.w;142
143// Mix final noise value144vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);145m = m * m;146return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),147dot(p2,x2), dot(p3,x3) ) );148}149// end of noise functions150
151void main()152{153vec2 texCoords = gl_TexCoord[0].st + vec2(154amplitude * (snoise(vec3(frequency * gl_TexCoord[0].s, frequency * gl_TexCoord[0].t, speed * time))),155amplitude * (snoise(vec3(frequency * gl_TexCoord[0].s + 17.0, frequency * gl_TexCoord[0].t, speed * time)))156);157gl_FragColor = texture2D(tex, texCoords);158}159);160
161shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragShaderSrc);162shader.linkProgram();163#ifdef _ITG_TWEAKABLE164addParameter("amplitude", this->amplitude, "min=0 max=10");165addParameter("frequency", this->frequency, "min=0 max=20");166addParameter("speed", this->speed, "min=0 max=10");167#endif168}169
170void NoiseWarpPass::render(ofFbo& readFbo, ofFbo& writeFbo, ofTexture& depth)171{172writeFbo.begin();173shader.begin();174shader.setUniform1f("time", ofGetElapsedTimef());175shader.setUniformTexture("tex", readFbo.getTexture(), 0);176shader.setUniform1f("frequency", frequency);177shader.setUniform1f("amplitude", amplitude);178shader.setUniform1f("speed", speed);179
180texturedQuad(0, 0, writeFbo.getWidth(), writeFbo.getHeight());181
182shader.end();183writeFbo.end();184}185}