framework2

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

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

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

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

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

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