Line data Source code
1 :
2 : /* Copyright (c) 2008-2015, Cedric Stalder <cedric.stalder@gmail.com>
3 : * Stefan Eilemann <eile@equalizergraphics.com>
4 : * Daniel Nachbaur <danielnachbaur@gmail.com>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #ifndef EQUTIL_FRAMEBUFFEROBJECT_H
21 : #define EQUTIL_FRAMEBUFFEROBJECT_H
22 :
23 : #include <eq/util/texture.h> // member
24 : #include <eq/util/types.h>
25 :
26 : #include <vector>
27 :
28 : namespace eq
29 : {
30 : namespace util
31 : {
32 : /** A C++ class to abstract OpenGL frame buffer objects */
33 : class FrameBufferObject
34 : {
35 : public:
36 : /**
37 : * Construct a new Frame Buffer Object. The first color texture is
38 : * automatically created.
39 : * @version 1.0
40 : */
41 : EQ_API FrameBufferObject(const GLEWContext* const glewContext,
42 : const unsigned textureTarget = 0x84F5
43 : /*GL_TEXTURE_RECTANGLE_ARB*/);
44 :
45 : /** Destruct the Frame Buffer Object. @version 1.0 */
46 : EQ_API ~FrameBufferObject();
47 :
48 : /**
49 : * Add one color texture to the FBO.
50 : *
51 : * The maximum number of textures per FBO is 16. Added color textures will
52 : * have the same format as the existing texture(s). This method has to be
53 : * called on an uninitialized FBO.
54 : *
55 : * @return false if color texture can't be added, otherwise true.
56 : * @version 1.0
57 : */
58 : EQ_API bool addColorTexture();
59 :
60 : /**
61 : * Initialize the Frame Buffer Object.
62 : *
63 : * On successful initialization, the FBO is bound.
64 : *
65 : * @param width the initial width of the rendering buffer.
66 : * @param height the initial height of the rendering buffer.
67 : * @param colorFormat The internal color texture format, e.g., GL_RGBA.
68 : * @param depthSize The bit depth of the depth attachment.
69 : * @param stencilSize The bit depth of the stencil attachment.
70 : * @param samplesSize The number of multisamples.
71 : * @return ERROR_NONE on success, Error code on failure.
72 : * @sa resize()
73 : * @version 1.0
74 : */
75 : EQ_API Error init(const int32_t width, const int32_t height,
76 : const unsigned colorFormat, const int32_t depthSize,
77 : const int32_t stencilSize, const int32_t samplesSize = 1);
78 :
79 : /** De-initialize the Frame Buffer Object. @version 1.0 */
80 : EQ_API void exit();
81 :
82 : /**
83 : * Bind to the Frame Buffer Object.
84 : *
85 : * The FBO becomes the read and/or draw buffer of the current context,
86 : * depending on the given target.
87 : * @param target the framebuffer target to bind, default read and draw
88 : * @version 1.0
89 : */
90 : EQ_API void bind(const uint32_t target = 0x8D40 /*GL_FRAMEBUFFER_EXT*/);
91 :
92 : /**
93 : * Unbind any Frame Buffer Object and use the default drawable for the
94 : * current context.
95 : * @param target the framebuffer target to unbind, default read and draw
96 : * @version 1.0
97 : */
98 : EQ_API void unbind(const uint32_t target = 0x8D40 /*GL_FRAMEBUFFER_EXT*/);
99 :
100 : /**
101 : * Resize the FBO.
102 : *
103 : * The FBO has to be initialized and bound. It is not changed if the size
104 : * does not change.
105 : *
106 : * @return true on success, false on error.
107 : * @return ERROR_NONE on success, Error code on failure.
108 : * @version 1.0
109 : */
110 : EQ_API Error resize(const int32_t width, const int32_t height);
111 :
112 : /** @return the current width. @version 1.0 */
113 0 : int32_t getWidth() const
114 : {
115 0 : LBASSERT(!_colors.empty());
116 0 : return _colors.front()->getWidth();
117 : }
118 :
119 : /** @return the current height. @version 1.0 */
120 0 : int32_t getHeight() const
121 : {
122 0 : LBASSERT(!_colors.empty());
123 0 : return _colors.front()->getHeight();
124 : }
125 :
126 : /** @return the vector of color textures. @version 1.0 */
127 0 : const Textures& getColorTextures() const { return _colors; }
128 : /** @return the depth texture. @version 1.0 */
129 0 : const Texture& getDepthTexture() const { return _depth; }
130 : /** @return the GLEW context. @version 1.0 */
131 2 : const GLEWContext* glewGetContext() const { return _glewContext; }
132 : /** @return true if the fbo is valid. @version 1.0 */
133 0 : bool isValid() const { return _valid; }
134 : private:
135 : unsigned _fboID; //!< the FBO GL name
136 :
137 : Textures _colors; //!< Multiple color textures
138 : Texture _depth;
139 :
140 : const GLEWContext* const _glewContext;
141 :
142 : bool _valid;
143 :
144 2 : LB_TS_VAR(_thread);
145 :
146 : /** Check the result after changes to an FBO and set the _valid flag. */
147 : Error _checkStatus();
148 : };
149 : }
150 : }
151 :
152 : #endif // EQUTIL_FRAMEBUFFEROBJECT_H
|