framework2

Форк
0
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

34
namespace itg
35
{
36
    NoiseWarpPass::NoiseWarpPass(const ofVec2f& aspect, bool arb, float frequency, float amplitude, float speed) :
37
        frequency(frequency), amplitude(amplitude), speed(speed), RenderPass(aspect, arb, "noisewarp")
38
    {
39
        string fragShaderSrc = STRINGIFY(
40
            uniform sampler2D tex;
41

42
            uniform float frequency;
43
            uniform float amplitude;
44
            uniform float time;
45
            uniform float speed;
46

47
            //
48
            // Description : Array and textureless GLSL 2D/3D/4D simplex
49
            //               noise functions.
50
            //      Author : Ian McEwan, Ashima Arts.
51
            //  Maintainer : ijm
52
            //     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-noise
56
            //
57

58
            vec3 mod289(vec3 x) {
59
             return x - floor(x * (1.0 / 289.0)) * 289.0;
60
            }
61

62
            vec4 mod289(vec4 x) {
63
             return x - floor(x * (1.0 / 289.0)) * 289.0;
64
            }
65

66
            vec4 permute(vec4 x) {
67
             return mod289(((x*34.0)+1.0)*x);
68
            }
69

70
            vec4 taylorInvSqrt(vec4 r)
71
            {
72
                return 1.79284291400159 - 0.85373472095314 * r;
73
            }
74
                                             
75
            float snoise(vec3 v)
76
            {
77
                const vec2  C = vec2(1.0/6.0, 1.0/3.0) ;
78
                const vec4  D = vec4(0.0, 0.5, 1.0, 2.0);
79
                
80
                // First corner
81
                vec3 i  = floor(v + dot(v, C.yyy) );
82
                vec3 x0 =   v - i + dot(i, C.xxx) ;
83
                
84
                // Other corners
85
                vec3 g = step(x0.yzx, x0.xyz);
86
                vec3 l = 1.0 - g;
87
                vec3 i1 = min( g.xyz, l.zxy );
88
                vec3 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;
94
                vec3 x1 = x0 - i1 + C.xxx;
95
                vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
96
                vec3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
97
                
98
                // Permutations
99
                i = mod289(i);
100
                vec4 p = permute( permute( permute(
101
                                                   i.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)
107
                float n_ = 0.142857142857; // 1.0/7.0
108
                vec3  ns = n_ * D.wyz - D.xzx;
109
                
110
                vec4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  mod(p,7*7)
111
                
112
                vec4 x_ = floor(j * ns.z);
113
                vec4 y_ = floor(j - 7.0 * x_ );    // mod(j,N)
114
                
115
                vec4 x = x_ *ns.x + ns.yyyy;
116
                vec4 y = y_ *ns.x + ns.yyyy;
117
                vec4 h = 1.0 - abs(x) - abs(y);
118
                
119
                vec4 b0 = vec4( x.xy, y.xy );
120
                vec4 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;
124
                vec4 s0 = floor(b0)*2.0 + 1.0;
125
                vec4 s1 = floor(b1)*2.0 + 1.0;
126
                vec4 sh = -step(h, vec4(0.0));
127
                
128
                vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
129
                vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
130
                
131
                vec3 p0 = vec3(a0.xy,h.x);
132
                vec3 p1 = vec3(a0.zw,h.y);
133
                vec3 p2 = vec3(a1.xy,h.z);
134
                vec3 p3 = vec3(a1.zw,h.w);
135
                
136
                //Normalise gradients
137
                vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
138
                p0 *= norm.x;
139
                p1 *= norm.y;
140
                p2 *= norm.z;
141
                p3 *= norm.w;
142
                
143
                // Mix final noise value
144
                vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
145
                m = m * m;
146
                return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), 
147
                                             dot(p2,x2), dot(p3,x3) ) );
148
            }
149
            // end of noise functions
150
                                         
151
            void main()
152
            {
153
                vec2 texCoords = gl_TexCoord[0].st + vec2(
154
                    amplitude * (snoise(vec3(frequency * gl_TexCoord[0].s, frequency * gl_TexCoord[0].t, speed * time))),
155
                    amplitude * (snoise(vec3(frequency * gl_TexCoord[0].s + 17.0, frequency * gl_TexCoord[0].t, speed * time)))
156
                );
157
                gl_FragColor = texture2D(tex, texCoords);
158
            }
159
        );
160
        
161
        shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragShaderSrc);
162
        shader.linkProgram();
163
#ifdef _ITG_TWEAKABLE
164
        addParameter("amplitude", this->amplitude, "min=0 max=10");
165
        addParameter("frequency", this->frequency, "min=0 max=20");
166
        addParameter("speed", this->speed, "min=0 max=10");
167
#endif
168
    }
169
    
170
    void NoiseWarpPass::render(ofFbo& readFbo, ofFbo& writeFbo, ofTexture& depth)
171
    {
172
        writeFbo.begin();
173
        shader.begin();
174
        shader.setUniform1f("time", ofGetElapsedTimef());
175
        shader.setUniformTexture("tex", readFbo.getTexture(), 0);
176
        shader.setUniform1f("frequency", frequency);
177
        shader.setUniform1f("amplitude", amplitude);
178
        shader.setUniform1f("speed", speed);
179
        
180
        texturedQuad(0, 0, writeFbo.getWidth(), writeFbo.getHeight());
181
        
182
        shader.end();
183
        writeFbo.end();
184
    }
185
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.