FrameBuffer.hh 10.8 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

Robert Menzel's avatar
Robert Menzel committed
6
7
8
#ifndef ACGL_RESOURCE_FRAMEBUFFER_HH
#define ACGL_RESOURCE_FRAMEBUFFER_HH

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * This FrameBuffer class encapsulates an OpenGL frame buffer object (FBO).
 * A FrameBuffer is a target for rendering and thus consists of different "layers":
 *
 * one or no depthbuffer
 * one or no stencilbuffer
 * one (OpenGL ES) to many (hardware dependent limit) colorbuffers
 *
 * These buffers get attached to the FrameBuffer.
 *
 * There exists one system-provided frame buffer object for rendering to the screen
 * and optionaly multiple user defined frame buffer objects for offscreen rendering.
 *
 * This class does not encapsulate the system-provided FBO.
 */

#include <ACGL/ACGL.hh>
Robert Menzel's avatar
Robert Menzel committed
26
27
28
29
30
31
#include <ACGL/Resource/ResourceTypes.hh>
#include <ACGL/Resource/RenderBuffer.hh>
#include <ACGL/Resource/Texture.hh>
#include <ACGL/Utils/Log.hh>
#include <ACGL/Utils/StringOperations.hh>
#include <ACGL/GL.hh>
32
#include <ACGL/GLUtils/Tools.hh>
Robert Menzel's avatar
Robert Menzel committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

namespace ACGL{
namespace Resource{

class FrameBuffer
{
    // =================================================================================================== \/
    // ============================================================================================ STATIC \/
    // =================================================================================================== \/
private:
    static GLuint msBuffers[8];

    // ==================================================================================================== \/
    // ============================================================================================ STRUCTS \/
    // ==================================================================================================== \/
public:
    //! An attachment can be a texture or a render buffer
    struct Attachment
    {
        std::string name;
        SharedTexture texture;
        SharedRenderBuffer renderBuffer;
    };

57
58
59
60
61
62
63
64
    // Per default an FBO is used for reading (glReadPixels etc) and drawing (glDraw*) but a
    // read-only/draw-only setup is also possible.
    enum TargetType {
        READ_FRAMEBUFFER      = GL_READ_FRAMEBUFFER,
        DRAW_FRAMEBUFFER      = GL_DRAW_FRAMEBUFFER,
        READ_DRAW_FRAMEBUFFER = GL_FRAMEBUFFER
    };

Robert Menzel's avatar
Robert Menzel committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    // ===================================================================================================== \/
    // ============================================================================================ TYPEDEFS \/
    // ===================================================================================================== \/
public:
    typedef std::vector< Attachment > AttachmentVec;

	// ========================================================================================================= \/
	// ============================================================================================ CONSTRUCTORS \/
	// ========================================================================================================= \/
public:
    FrameBuffer(GLsizei _width, GLsizei _height)
    :   mContext(0),
        mWidth(_width),
        mHeight(_height),
        mDrawBuffers(0),
        mColorAttachments(),
        mDepthAttachment()
    {
        glGenFramebuffers(1, &mContext);
84
85
86
87
        if (openGLCriticalErrorOccured() ) {
            ACGL::Utils::error() << "could not generate framebuffer!" << std::endl;
            return;
        }
Robert Menzel's avatar
Robert Menzel committed
88
89
90
91
92
93
94
        mDepthAttachment.name = "";
        mDepthAttachment.texture = SharedTexture();
        mDepthAttachment.renderBuffer = SharedRenderBuffer();
    }

    virtual ~FrameBuffer(void)
    {
95
96
        // buffer 0 will get ignored by OpenGL
        glDeleteFramebuffers(1, &mContext);
Robert Menzel's avatar
Robert Menzel committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    }

	// ==================================================================================================== \/
	// ============================================================================================ GETTERS \/
	// ==================================================================================================== \/
public:
    inline       GLuint         getContext          (void) const { return mContext;          }
    inline       GLsizei        getWidth            (void) const { return mWidth;            }
    inline       GLsizei        getHeight           (void) const { return mHeight;           }
    inline       GLsizei        getDrawBuffers      (void) const { return mDrawBuffers;      }
    inline const AttachmentVec& getColorAttachments (void) const { return mColorAttachments; }
    inline const Attachment&    getDepthAttachment  (void) const { return mDepthAttachment;  }

	// ===================================================================================================== \/
	// ============================================================================================ WRAPPERS \/
	// ===================================================================================================== \/
public:
    inline void bindAsRenderTarget(void) const
    {
116
117
        bind();
        setDrawBuffers();
Robert Menzel's avatar
Robert Menzel committed
118
119
    }

120
121
122
123
124
    /**
     * Per default a FrameBuffer gets used for read/write operations, but we can
     * bind two different FrameBuffers for these operations!
     */
    inline void bind( TargetType _type = READ_DRAW_FRAMEBUFFER ) const
Robert Menzel's avatar
Robert Menzel committed
125
    {
126
127
        glBindFramebuffer(_type, mContext);
        openGLRareError(); // glBindFramebuffer can only fail if the contect is no valid FBO which shouldn't happen using this framework
Robert Menzel's avatar
Robert Menzel committed
128
129
130
131
132
    }

    inline void setDrawBuffers(void) const
    {
        glDrawBuffers(mDrawBuffers, msBuffers);
133
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    }

    inline void setViewport(void) const
    {
        glViewport(0, 0, mWidth, mHeight);
    }

    inline bool attachColorRenderBuffer(const SharedRenderBuffer& _renderBuffer)
    {
        if(_renderBuffer->getWidth() != mWidth && _renderBuffer->getHeight() != mHeight)
        {
            Utils::error() << "Attached render buffer for color has a wrong size!" << std::endl;
            return false;
        }
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + mDrawBuffers, GL_RENDERBUFFER, _renderBuffer->getContext());
149
150
151
152
        if (openGLCommonErrorOccured()) {
            Utils::error() << "Attaching of texture to the FBO failed" << std::endl;
            return false;
        }
Robert Menzel's avatar
Robert Menzel committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        mDrawBuffers++;
        Attachment attachment = {"", SharedTexture(), _renderBuffer};
        mColorAttachments.push_back(attachment);
        return true;
    }

    inline bool attachColorTexture(const SharedTexture& _texture)
    {
        if(_texture->getWidth() != mWidth && _texture->getHeight() != mHeight)
        {
            Utils::error() << "Attached texture for color has a wrong size!" << std::endl;
            return false;
        }
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + mDrawBuffers, _texture->getTarget(), _texture->getContext(), 0);
167
168
169
170
        if (openGLCommonErrorOccured()) {
            Utils::error() << "Attaching of texture to the FBO failed" << std::endl;
            return false;
        }
Robert Menzel's avatar
Robert Menzel committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
        mDrawBuffers++;
        Attachment attachment = {"", _texture, SharedRenderBuffer()};
        mColorAttachments.push_back(attachment);
        return true;
    }

    inline bool setDepthRenderBuffer(const SharedRenderBuffer& _renderBuffer)
    {
        if(_renderBuffer->getWidth() != mWidth && _renderBuffer->getHeight() != mHeight)
        {
            Utils::error() << "Attached render buffer for depth has a wrong size!" << std::endl;
            return false;
        }
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext());
        #ifdef OPENGL_ES
            if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8_OES ||
                _renderBuffer->getInternalFormat() == GL_DEPTH_STENCIL_OES)
                glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext());
        #else
            if( _renderBuffer->getInternalFormat() == GL_DEPTH24_STENCIL8 ||
                _renderBuffer->getInternalFormat() == GL_DEPTH_STENCIL)
                glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer->getContext());
        #endif
        mDepthAttachment.renderBuffer = _renderBuffer;
        return true;
    }

    inline bool setDepthTexture(const SharedTexture& _texture)
    {
        if(_texture->getWidth() != mWidth && _texture->getHeight() != mHeight)
        {
            Utils::error() << "Attached texture for depth has a wrong size!" << std::endl;
            return false;
        }
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0);
        #ifdef OPENGL_ES
            if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8_OES ||
                _texture->getInternalFormat() == GL_DEPTH_STENCIL_OES)
                glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0);
        #else
            if( _texture->getInternalFormat() == GL_DEPTH24_STENCIL8 ||
                _texture->getInternalFormat() == GL_DEPTH_STENCIL)
                glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, _texture->getTarget(), _texture->getContext(), 0);
        #endif
        mDepthAttachment.texture = _texture;
        return true;
    }

    inline bool isFrameBufferComplete(void)
    {
        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        {
            Utils::error() << "Failed to make complete FrameBuffer object: " << (glCheckFramebufferStatus(GL_FRAMEBUFFER)) << std::endl;
            return false;
        }
        return true;
    }

	// ==================================================================================================== \/
	// ============================================================================================ METHODS \/
	// ==================================================================================================== \/
protected:
    void clean(void);

	// =================================================================================================== \/
	// ============================================================================================ FIELDS \/
	// =================================================================================================== \/
protected:
    GLuint        mContext;
    GLsizei       mWidth;
    GLsizei       mHeight;
    GLsizei       mDrawBuffers;
    AttachmentVec mColorAttachments;
244
    Attachment    mDepthAttachment;  // depth and stencil are combined
Robert Menzel's avatar
Robert Menzel committed
245
246
247
248
249
250
};

} // Resource
} // ACGL

#endif // ACGL_RESOURCE_FRAMEBUFFER_HH