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 class OpenGl_FrameBuffer;
23 DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource)
25 //! Class implements FrameBuffer Object (FBO) resource
26 //! intended for off-screen rendering.
27 class OpenGl_FrameBuffer : public OpenGl_Resource
33 static const GLuint NO_FRAMEBUFFER = 0;
34 static const GLuint NO_RENDERBUFFER = 0;
39 Standard_EXPORT OpenGl_FrameBuffer();
42 Standard_EXPORT virtual ~OpenGl_FrameBuffer();
44 //! Destroy object - will release GPU memory if any.
45 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
47 //! Number of multisampling samples.
48 GLsizei NbSamples() const
53 //! Return true if FBO has been created with color attachment.
56 return myColorFormat != 0;
59 //! Return true if FBO has been created with depth attachment.
62 return myDepthFormat != 0;
66 GLsizei GetSizeX() const
68 return myColorTexture->SizeX();
72 GLsizei GetSizeY() const
74 return myColorTexture->SizeY();
78 GLsizei GetVPSizeX() const
84 GLsizei GetVPSizeY() const
89 //! Returns true if current object was initialized
90 Standard_Boolean IsValid() const
92 return isValidFrameBuffer();
95 //! Initialize FBO for rendering into textures.
96 //! @param theGlCtx currently bound OpenGL context
97 //! @param theSizeX texture width
98 //! @param theSizeY texture height
99 //! @param theColorFormat color texture sized format (0 means no color attachment), e.g. GL_RGBA8
100 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
101 //! @param theNbSamples MSAA number of samples (0 means normal texture)
102 //! @return true on success
103 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
104 const GLsizei theSizeX,
105 const GLsizei theSizeY,
106 const GLint theColorFormat,
107 const GLint theDepthFormat,
108 const GLsizei theNbSamples = 0);
110 //! (Re-)initialize FBO with specified dimensions.
111 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
112 const GLsizei theViewportSizeX,
113 const GLsizei theViewportSizeY,
114 const GLint theColorFormat,
115 const GLint theDepthFormat,
116 const GLsizei theNbSamples = 0);
118 //! (Re-)initialize FBO with properties taken from another FBO.
119 Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
120 const OpenGl_FrameBuffer& theFbo)
122 return InitLazy (theGlCtx, theFbo.myVPSizeX, theFbo.myVPSizeY, theFbo.myColorFormat, theFbo.myDepthFormat, theFbo.myNbSamples);
125 //! (Re-)initialize FBO with specified dimensions.
126 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
127 //! @param theGlCtx currently bound OpenGL context
128 //! @param theSizeX render buffer width
129 //! @param theSizeY render buffer height
130 //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8
131 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
132 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
133 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
134 const GLsizei theSizeX,
135 const GLsizei theSizeY,
136 const GLint theColorFormat,
137 const GLint theDepthFormat,
138 const GLuint theColorRBufferFromWindow = 0);
140 //! Initialize class from currently bound FBO.
141 //! Retrieved OpenGL objects will not be destroyed on Release.
142 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
144 //! Setup viewport to render into FBO
145 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
147 //! Override viewport settings
148 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
149 const GLsizei theVPSizeY);
151 //! Bind frame buffer for drawing and reading (to render into the texture).
152 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
154 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
155 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
157 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
158 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
160 //! Unbind frame buffer.
161 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
163 //! Returns the color texture.
164 inline const Handle(OpenGl_Texture)& ColorTexture() const
166 return myColorTexture;
169 //! Returns the depth-stencil texture.
170 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
172 return myDepthStencilTexture;
175 //! Returns the color Render Buffer.
176 GLuint ColorRenderBuffer() const
178 return myGlColorRBufferId;
181 //! Returns the depth Render Buffer.
182 GLuint DepthStencilRenderBuffer() const
184 return myGlDepthRBufferId;
189 Standard_Boolean isValidFrameBuffer() const
191 return myGlFBufferId != NO_FRAMEBUFFER;
196 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
197 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
198 GLsizei myNbSamples; //!< number of MSAA samples
199 GLint myColorFormat; //!< sized format for color texture, GL_RGBA8 by default
200 GLint myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default
201 GLuint myGlFBufferId; //!< FBO object ID
202 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
203 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
204 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
205 Handle(OpenGl_Texture) myColorTexture; //!< color texture object
206 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
210 DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer,OpenGl_Resource) // Type definition
214 #endif // OPENGL_FRAME_BUFFER_H