framework2
107 строк · 4.1 Кб
1/*
2* ZoomBlurPass.cpp
3*
4* Copyright (c) 2013, satcy, http://satcy.net
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 "ZoomBlurPass.h"
33#include "ofMain.h"
34
35namespace itg
36{
37ZoomBlurPass::ZoomBlurPass(const ofVec2f& aspect, bool arb, float centerX, float centerY,
38float exposure, float decay, float density,
39float weight, float clamp) :
40centerX(centerX), centerY(centerY), exposure(exposure), decay(decay), density(density), weight(weight), clamp(clamp), RenderPass(aspect, arb, "zoomblur")
41{
42
43string fragShaderSrc = STRINGIFY(
44uniform sampler2D tDiffuse;
45
46uniform float fX;
47uniform float fY;
48uniform float fExposure;
49uniform float fDecay;
50uniform float fDensity;
51uniform float fWeight;
52uniform float fClamp;
53
54const int iSamples = 20;
55
56void main()
57{
58vec2 vUv = gl_TexCoord[0].st;
59vec2 deltaTextCoord = vec2(vUv - vec2(fX,fY));
60deltaTextCoord *= 1.0 / float(iSamples) * fDensity;
61vec2 coord = vUv;
62float illuminationDecay = 1.0;
63vec4 FragColor = vec4(0.0);
64
65for(int i=0; i < iSamples ; i++)
66{
67coord -= deltaTextCoord;
68vec4 texel = texture2D(tDiffuse, coord);
69texel *= illuminationDecay * fWeight;
70
71FragColor += texel;
72
73illuminationDecay *= fDecay;
74}
75FragColor *= fExposure;
76FragColor = clamp(FragColor, 0.0, fClamp);
77gl_FragColor = FragColor;
78}
79);
80
81shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragShaderSrc);
82shader.linkProgram();
83
84}
85
86void ZoomBlurPass::render(ofFbo& readFbo, ofFbo& writeFbo, ofTexture& depthTex)
87{
88writeFbo.begin();
89
90
91shader.begin();
92
93shader.setUniformTexture("tDiffuse", readFbo.getTexture(), 0);
94shader.setUniform1f("fX", centerX);
95shader.setUniform1f("fY", centerY);
96shader.setUniform1f("fExposure", exposure);
97shader.setUniform1f("fDecay", decay);
98shader.setUniform1f("fDensity", density);
99shader.setUniform1f("fWeight", weight);
100shader.setUniform1f("fClamp", clamp);
101
102texturedQuad(0, 0, writeFbo.getWidth(), writeFbo.getHeight());
103
104shader.end();
105writeFbo.end();
106}
107}