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