framework2

Форк
0
193 строки · 6.2 Кб
1
/*
2
 *  PostProcessing.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 "PostProcessing.h"
33
#include "ofMain.h"
34

35
namespace itg
36
{
37
    void PostProcessing::init(unsigned width, unsigned height, bool arb)
38
    {
39
        this->width = width;
40
        this->height = height;
41
        this->arb = arb;
42
        
43
        ofFbo::Settings s;
44
        
45
        if (arb)
46
        {
47
            s.width = width;
48
            s.height = height;
49
            s.textureTarget = GL_TEXTURE_RECTANGLE_ARB;
50
        }
51
        else
52
        {
53
            s.width = ofNextPow2(width); // Test value: 2048
54
            s.height = ofNextPow2(height); // Test value: 1024
55
            s.textureTarget = GL_TEXTURE_2D;
56
        }
57
        this->widthFbo = s.width;
58
        this->heightFbo = s.height;
59
        
60
        // no need to use depth for ping pongs
61
        for (int i = 0; i < 2; ++i)
62
        {
63
            pingPong[i].allocate(s);
64
        }
65
        
66
        s.useDepth = true;
67
        s.depthStencilInternalFormat = GL_DEPTH_COMPONENT24;
68
        s.depthStencilAsTexture = true;
69
        raw.allocate(s);
70
        
71
        numProcessedPasses = 0;
72
        currentReadFbo = 0;
73
        flip = true;
74
    }
75
    
76
    void PostProcessing::begin()
77
    {
78
        raw.begin(OF_FBOMODE_NODEFAULTS);
79
        
80
        ofMatrixMode(OF_MATRIX_PROJECTION);
81
        ofPushMatrix();
82
        
83
        ofMatrixMode(OF_MATRIX_MODELVIEW);
84
        ofPushMatrix();
85
        
86
        ofViewport(0, 0, ofGetWindowWidth(), ofGetWindowHeight()); // Test values: 1800, 900
87
        
88
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
89
        
90
        ofPushStyle();
91
        glPushAttrib(GL_ENABLE_BIT);
92
    }
93
    
94
    void PostProcessing::begin(ofCamera& cam)
95
    {
96
        // update camera matrices
97

98
        raw.begin(OF_FBOMODE_NODEFAULTS);
99
        
100
        ofViewport(0, 0, ofGetWindowWidth(), ofGetWindowHeight()); // Test values: 1800, 900
101
        cam.begin();
102
        
103
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
104
        
105
        ofPushStyle();
106
        glPushAttrib(GL_ENABLE_BIT);
107
    }
108
    
109
    void PostProcessing::end(ofCamera& cam, bool autoDraw)
110
    {
111
        glPopAttrib();
112
        ofPopStyle();
113
        
114
        cam.end();
115
        raw.end();
116
        
117
        ofViewport(0, 0, ofGetWidth(), ofGetHeight());
118

119
        ofPushStyle();
120
        glPushAttrib(GL_ENABLE_BIT);
121
        glDisable(GL_LIGHTING);
122
        ofSetColor(255, 255, 255);
123
        process();
124
        if (autoDraw) draw();
125
        glPopAttrib();
126
        ofPopStyle();
127
    }
128
    
129
    void PostProcessing::debugDraw()
130
    {
131
        raw.getTexture().draw(10, 10, 300, 300);
132
        raw.getDepthTexture().draw(320, 10, 300, 300);
133
        pingPong[currentReadFbo].draw(630, 10, 300, 300);
134
    }
135
    
136
    void PostProcessing::draw(float x, float y) const
137
    {
138
        draw(x, y, widthFbo, heightFbo); // Test values: 2048, 1024
139
    }
140
    
141
    void PostProcessing::draw(float x, float y, float w, float h) const
142
    {
143
        if (flip)
144
        {
145
            ofPushMatrix();
146
            ofTranslate(x, y + h, 0);
147
            ofScale(1, -1, 1);
148
        }
149
        else glTranslatef(x, y, 0);
150
        if (numProcessedPasses == 0) raw.draw(0, 0, w, h);
151
        else pingPong[currentReadFbo].draw(0, 0, w, h);
152
        if (flip) ofPopMatrix();
153
    }
154
    
155
    ofTexture& PostProcessing::getProcessedTextureReference()
156
    {
157
        if (numProcessedPasses) return pingPong[currentReadFbo].getTexture();
158
        else return raw.getTexture();
159
    }
160
    
161
    // need to have depth enabled for some fx
162
    void PostProcessing::process(ofFbo& raw, bool hasDepthAsTexture)
163
    {
164
        numProcessedPasses = 0;
165
        for (int i = 0; i < passes.size(); ++i)
166
        {
167
            if (passes[i]->getEnabled())
168
            {
169
                if (arb && !passes[i]->hasArbShader()) ofLogError() << "Arb mode is enabled but pass " << passes[i]->getName() << " does not have an arb shader.";
170
                else
171
                {
172
                    if (hasDepthAsTexture)
173
                    {
174
                        if (numProcessedPasses == 0) passes[i]->render(raw, pingPong[1 - currentReadFbo], raw.getDepthTexture());
175
                        else passes[i]->render(pingPong[currentReadFbo], pingPong[1 - currentReadFbo], raw.getDepthTexture());
176
                    }
177
                    else
178
                    {
179
                        if (numProcessedPasses == 0) passes[i]->render(raw, pingPong[1 - currentReadFbo]);
180
                        else passes[i]->render(pingPong[currentReadFbo], pingPong[1 - currentReadFbo]);
181
                    }
182
                    currentReadFbo = 1 - currentReadFbo;
183
                    numProcessedPasses++;
184
                }
185
            }
186
        }
187
    }
188
    
189
    void PostProcessing::process()
190
    {
191
        process(raw);
192
    }
193
}
194

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

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

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

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