framework2
85 строк · 3.8 Кб
1/*
2* HsbShiftPass.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 "HsbShiftPass.h"33
34namespace itg35{
36HsbShiftPass::HsbShiftPass(const ofVec2f& aspect, bool arb, float hueShift, float saturationShift, float brightnessShift) :37hueShift(hueShift), saturationShift(saturationShift), brightnessShift(brightnessShift), RenderPass(aspect, arb, "hsbshift")38{39string fragShaderSrc = STRINGIFY(40uniform sampler2D tex;41uniform float hueShift;42uniform float saturationShift;43uniform float brightnessShift;44
45// https://love2d.org/wiki/HSV_color46vec3 hsbToRgb(vec3 c) { return mix(vec3(1.),clamp((abs(fract(c.x+vec3(3.,2.,1.)/3.)*6.-3.)-1.),0.,1.),c.y)*c.z; }47// http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl48vec3 rgbToHsb(vec3 c)49{50vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);51vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));52vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));53
54float d = q.x - min(q.w, q.y);55float e = 1.0e-10;56return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);57}58
59void main()60{61vec3 hsb = rgbToHsb(texture2D(tex, gl_TexCoord[0].st).rgb);62vec3 rgb = hsbToRgb(vec3(hsb.x + hueShift, hsb.y + saturationShift, hsb.z + brightnessShift));63gl_FragColor = vec4(rgb, 1.0);64}65);66
67shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragShaderSrc);68shader.linkProgram();69}70
71void HsbShiftPass::render(ofFbo& readFbo, ofFbo& writeFbo, ofTexture& depth)72{73writeFbo.begin();74shader.begin();75shader.setUniformTexture("tex", readFbo.getTexture(), 0);76shader.setUniform1f("hueShift", hueShift);77shader.setUniform1f("saturationShift", saturationShift);78shader.setUniform1f("brightnessShift", brightnessShift);79
80texturedQuad(0, 0, writeFbo.getWidth(), writeFbo.getHeight());81
82shader.end();83writeFbo.end();84}85}