1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef OPENGL_FRAME_BUFFER_H
16 #define OPENGL_FRAME_BUFFER_H
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_Resource.hxx>
20 #include <OpenGl_Texture.hxx>
22 #include <NCollection_Vector.hxx>
24 class OpenGl_FrameBuffer;
25 DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource)
27 //! Short declaration of useful collection types.
28 typedef NCollection_Vector<GLint> OpenGl_ColorFormats;
30 //! Class implements FrameBuffer Object (FBO) resource
31 //! intended for off-screen rendering.
32 class OpenGl_FrameBuffer : public OpenGl_Resource
38 static const GLuint NO_FRAMEBUFFER = 0;
39 static const GLuint NO_RENDERBUFFER = 0;
44 Standard_EXPORT OpenGl_FrameBuffer();
47 Standard_EXPORT virtual ~OpenGl_FrameBuffer();
49 //! Destroy object - will release GPU memory if any.
50 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
52 //! Number of multisampling samples.
53 GLsizei NbSamples() const
58 //! Number of color buffers.
59 GLsizei NbColorBuffers() const
61 return myColorTextures.Length();
64 //! Return true if FBO has been created with color attachment.
67 return !myColorFormats.IsEmpty();
70 //! Return true if FBO has been created with depth attachment.
73 return myDepthFormat != 0;
77 GLsizei GetSizeX() const
79 return myColorTextures (0)->SizeX();
83 GLsizei GetSizeY() const
85 return myColorTextures (0)->SizeY();
89 GLsizei GetVPSizeX() const
95 GLsizei GetVPSizeY() const
100 //! Returns true if current object was initialized
101 Standard_Boolean IsValid() const
103 return isValidFrameBuffer();
106 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
107 //! @param theGlCtx currently bound OpenGL context
108 //! @param theSizeX texture width
109 //! @param theSizeY texture height
110 //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
111 //! @param theDepthStencilTexture depth-stencil texture
112 //! @param theNbSamples MSAA number of samples (0 means normal texture)
113 //! @return true on success
114 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
115 const GLsizei theSizeX,
116 const GLsizei theSizeY,
117 const OpenGl_ColorFormats& theColorFormats,
118 const Handle(OpenGl_Texture)& theDepthStencilTexture,
119 const GLsizei theNbSamples = 0);
121 //! Initialize FBO for rendering into textures.
122 //! @param theGlCtx currently bound OpenGL context
123 //! @param theSizeX texture width
124 //! @param theSizeY texture height
125 //! @param theColorFormat color texture sized format (0 means no color attachment), e.g. GL_RGBA8
126 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
127 //! @param theNbSamples MSAA number of samples (0 means normal texture)
128 //! @return true on success
129 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
130 const GLsizei theSizeX,
131 const GLsizei theSizeY,
132 const GLint theColorFormat,
133 const GLint theDepthFormat,
134 const GLsizei theNbSamples = 0);
136 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
137 //! @param theGlCtx currently bound OpenGL context
138 //! @param theSizeX texture width
139 //! @param theSizeY texture height
140 //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
141 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
142 //! @param theNbSamples MSAA number of samples (0 means normal texture)
143 //! @return true on success
144 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
145 const GLsizei theSizeX,
146 const GLsizei theSizeY,
147 const OpenGl_ColorFormats& theColorFormats,
148 const GLint theDepthFormat,
149 const GLsizei theNbSamples = 0);
151 //! (Re-)initialize FBO with specified dimensions.
152 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
153 const GLsizei theViewportSizeX,
154 const GLsizei theViewportSizeY,
155 const GLint theColorFormat,
156 const GLint theDepthFormat,
157 const GLsizei theNbSamples = 0);
159 //! (Re-)initialize FBO with specified dimensions.
160 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
161 const GLsizei theViewportSizeX,
162 const GLsizei theViewportSizeY,
163 const OpenGl_ColorFormats& theColorFormats,
164 const GLint theDepthFormat,
165 const GLsizei theNbSamples = 0);
167 //! (Re-)initialize FBO with properties taken from another FBO.
168 Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
169 const OpenGl_FrameBuffer& theFbo)
171 return InitLazy (theGlCtx, theFbo.myVPSizeX, theFbo.myVPSizeY, theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples);
174 //! (Re-)initialize FBO with specified dimensions.
175 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
176 //! @param theGlCtx currently bound OpenGL context
177 //! @param theSizeX render buffer width
178 //! @param theSizeY render buffer height
179 //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8
180 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
181 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
182 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
183 const GLsizei theSizeX,
184 const GLsizei theSizeY,
185 const GLint theColorFormat,
186 const GLint theDepthFormat,
187 const GLuint theColorRBufferFromWindow = 0);
189 //! Initialize class from currently bound FBO.
190 //! Retrieved OpenGL objects will not be destroyed on Release.
191 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
193 //! Setup viewport to render into FBO
194 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
196 //! Override viewport settings
197 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
198 const GLsizei theVPSizeY);
200 //! Bind frame buffer for drawing and reading (to render into the texture).
201 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
203 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
204 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
206 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
207 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
209 //! Unbind frame buffer.
210 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
212 //! Returns the color texture for the given color buffer index.
213 inline const Handle(OpenGl_Texture)& ColorTexture (const GLint theColorBufferIndex = 0) const
215 return myColorTextures (theColorBufferIndex);
218 //! Returns the depth-stencil texture.
219 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
221 return myDepthStencilTexture;
224 //! Returns the color Render Buffer.
225 GLuint ColorRenderBuffer() const
227 return myGlColorRBufferId;
230 //! Returns the depth Render Buffer.
231 GLuint DepthStencilRenderBuffer() const
233 return myGlDepthRBufferId;
238 Standard_Boolean isValidFrameBuffer() const
240 return myGlFBufferId != NO_FRAMEBUFFER;
245 typedef NCollection_Vector<Handle(OpenGl_Texture)> OpenGl_TextureArray;
249 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
250 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
251 GLsizei myNbSamples; //!< number of MSAA samples
252 OpenGl_ColorFormats myColorFormats; //!< sized format for color texture, GL_RGBA8 by default
253 GLint myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default
254 GLuint myGlFBufferId; //!< FBO object ID
255 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
256 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
257 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
258 bool myIsOwnDepth; //!< flag indicating that FBO should be deallocated by this class
259 OpenGl_TextureArray myColorTextures; //!< color texture objects
260 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
264 DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer,OpenGl_Resource) // Type definition
268 #endif // OPENGL_FRAME_BUFFER_H