1 /*
2  *  Copyright (c) 2012-2014, Bruno Levy
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *  * Redistributions of source code must retain the above copyright notice,
9  *  this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright notice,
11  *  this list of conditions and the following disclaimer in the documentation
12  *  and/or other materials provided with the distribution.
13  *  * Neither the name of the ALICE Project-Team nor the names of its
14  *  contributors may be used to endorse or promote products derived from this
15  *  software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  If you modify this software, you should include a notice giving the
30  *  name of the person performing the modification, the date of modification,
31  *  and the reason for such modification.
32  *
33  *  Contact: Bruno Levy
34  *
35  *     Bruno.Levy@inria.fr
36  *     http://www.loria.fr/~levy
37  *
38  *     ALICE Project
39  *     LORIA, INRIA Lorraine,
40  *     Campus Scientifique, BP 239
41  *     54506 VANDOEUVRE LES NANCY CEDEX
42  *     FRANCE
43  *
44  */
45 
46 #ifndef H_GEOGRAM_GFX_BASIC_FBO_H
47 #define H_GEOGRAM_GFX_BASIC_FBO_H
48 
49 #include <geogram_gfx/basic/common.h>
50 
51 /**
52  * \file geogram_gfx/basic/frame_buffer_object.h
53  * \brief Helper class for manipulating OpenGL frame buffer objects.
54  */
55 
56 namespace GEO {
57 
58     /**
59      * \brief An OpenGL frame buffer object.
60      */
61     class GEOGRAM_GFX_API FrameBufferObject {
62     public:
63         /**
64          * \brief FrameBufferObject constructor.
65          * \details Creates an uninitialized FrameBufferObject.
66          */
67         FrameBufferObject();
68 
69         /**
70          * \brief FrameBufferObject destructor.
71          * \details Releases all the allocated OpenGL resources.
72          */
73         ~FrameBufferObject();
74 
75         /**
76          * \brief Initializes the FrameBufferObject.
77          * \param[in] width_in the width (in pixels)
78          * \param[in] height_in the height (in picels)
79          * \param[in] with_depth_buffer if true, a depth buffer is also created
80          * \param[in] internal_storage the OpenGL internal storage
81          * \param[in] mipmaps if true, the created textures have mipmaps
82          */
83         bool initialize(
84             index_t width_in,
85 	    index_t height_in,
86             bool with_depth_buffer,
87             GLint internal_storage,
88             bool mipmaps = false
89         );
90 
91         /**
92          * \brief Resizes the FrameBuferObject
93          * \param[in] new_width the new width, in pixels
94          * \param[in] new_height the new height, in pixels
95          */
96         void resize(index_t new_width, index_t new_height);
97 
98         /**
99          * \brief Binds this frame buffer as the input 2D texture.
100          */
101         void bind_as_texture();
102 
103         /**
104          * \brief Binds the depth buffer of this frame buffer as the
105          *  input 2D texture.
106          * \pre initialize() was called with with_depth_buffer=true
107          */
108         void bind_depth_buffer_as_texture();
109 
110         /**
111          * \brief Binds this framebuffer as the output of OpenGL rendering.
112 	 * \details This memorizes the currently bound framebuffer.
113          */
114         void bind_as_framebuffer();
115 
116 
117 	/**
118 	 * \brief Tests whether this framebuffer is bound as a framebuffer.
119 	 * \retval true if this framebuffer is bound, i.e. used for OpenGL
120 	 *   output.
121 	 * \retval false otherwise
122 	 */
123 	bool is_bound_as_framebuffer() const;
124 
125         /**
126          * \brief Unbind this framebuffer.
127          * \details This removes all the bindings (both as texture and
128          *  as target of OpenGL rendering). If the framebuffer was bound
129          *  as target of OpenGL rendering, this also restores the previously
130          *  bound framebuffer.
131          */
132         void unbind();
133 
134 
135 	/**
136 	 * \brief Tests whether this FrameBufferObject is initialized.
137 	 * \retval true if this FrameBufferObject is initialized.
138 	 * \retval false otherwise.
139 	 */
initialized()140 	bool initialized() {
141 	    return (frame_buffer_id != 0);
142 	}
143 
144         /**
145          * \brief The id of the frame buffer.
146          */
147         GLuint frame_buffer_id;
148 
149         /**
150          * \brief The id of the texture used for the depth buffer.
151          */
152         GLuint depth_buffer_id;
153 
154         /**
155          * \brief The id of the texture used for the color buffer.
156          */
157         GLuint offscreen_id;
158 
159         /**
160          * \brief The width of this frame buffer, in pixels.
161          */
162         index_t width;
163 
164         /**
165          * \brief The height of this frame buffer, in pixels.
166          */
167         index_t height;
168 
169         /**
170          * \brief The OpenGL internal storage for the color buffer.
171          */
172         GLint internal_storage;
173 
174         /**
175          * \brief The default frame buffer object associated with
176          *  the Opengl context.
177          */
178         GLuint previous_frame_buffer_id;
179     };
180 
181 }
182 
183 #endif
184