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