framework2

Форк
0
120 строк · 4.3 Кб
1
/*
2
 *  BloomPass.cpp
3
 *
4
 *  Copyright (c) 2012, 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 "BloomPass.h"
33
#include "ofMain.h"
34

35
namespace itg
36
{
37
    BloomPass::BloomPass(const ofVec2f& aspect, bool arb, const ofVec2f& xBlur, const ofVec2f& yBlur, unsigned resolution, bool aspectCorrect) : RenderPass(aspect, arb, "bloom")
38
    {
39
        currentReadFbo = 0;
40
        if (resolution != ofNextPow2(resolution)) ofLogWarning() << "Resolution " << resolution << " is not a power of two, using " << ofNextPow2(resolution);
41
        
42
        xConv = ConvolutionPass::Ptr(new ConvolutionPass(aspect, arb, xBlur));
43
        yConv = ConvolutionPass::Ptr(new ConvolutionPass(aspect, arb, (aspectCorrect?aspect.x / aspect.y:1.f) * yBlur));
44
        
45
        ofFbo::Settings s;
46
        if (arb)
47
        {
48
            s.width = resolution;
49
            s.height = resolution * aspect.y / aspect.x;
50
            s.textureTarget = GL_TEXTURE_RECTANGLE_ARB;
51
        }
52
        else
53
        {
54
            s.width = ofNextPow2(resolution);
55
            s.height = ofNextPow2(resolution);
56
            s.textureTarget = GL_TEXTURE_2D;
57
            
58
        }
59
        s.useDepth = true;
60
        
61
        for (int i = 0; i < 2; ++i) fbos[i].allocate(s);
62
    }
63
    
64
    void BloomPass::allocateSelectiveGlow(unsigned w, unsigned h)
65
    {
66
        this->w = w;
67
        this->h = h;
68
        
69
        ofFbo::Settings s;
70
        s.textureTarget = GL_TEXTURE_2D;
71
        s.width = ofNextPow2(w);
72
        s.height = ofNextPow2(h);
73
        s.useDepth = true;
74
        selectiveGlow.allocate(s);
75
        selectiveGlow.begin();
76
        ofClear(0,0,0,255);
77
        selectiveGlow.end();
78
    }
79
    
80
    void BloomPass::beginSelectiveGlow(bool clear)
81
    {
82
        selectiveGlow.begin();
83
        glPushMatrix();
84
        glScalef(1, -1, 1);
85
        glTranslatef(0, -ofNextPow2(h), 0);
86
        if (clear) ofClear(0,0,0, 255);
87
    }
88
    
89
    void BloomPass::endSelectiveGlow()
90
    {
91
        glPopMatrix();
92
        selectiveGlow.end();
93
    }
94
    
95
    void BloomPass::debugDraw()
96
    {
97
        glPushMatrix();
98
        glScalef(1, -1, 1);
99
        selectiveGlow.draw(0, -selectiveGlow.getHeight());
100
        glPopMatrix();
101
    }
102
    
103
    void BloomPass::render(ofFbo& readFbo, ofFbo& writeFbo)
104
    {
105
        if (selectiveGlow.isAllocated()) xConv->render(selectiveGlow, fbos[0]);
106
        else xConv->render(readFbo, fbos[0]);
107
        yConv->render(fbos[0], fbos[1]);
108
        
109
        writeFbo.begin();
110
        ofClear(0, 0, 0, 255);
111
        ofSetColor(255, 255, 255);
112
        readFbo.draw(0, 0);
113
        ofEnableAlphaBlending();
114
        glBlendFunc(GL_ONE, GL_ONE);
115
        fbos[1].draw(0, 0, writeFbo.getWidth(), writeFbo.getHeight());
116
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117
        ofDisableAlphaBlending();
118
        writeFbo.end();
119
    }
120
}

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

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

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

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