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 #include <Handle_OpenGl_FrameBuffer.hxx>
27 //! Class implements FrameBuffer Object (FBO) resource
28 //! intended for off-screen rendering.
29 class OpenGl_FrameBuffer : public OpenGl_Resource
35 static const GLuint NO_FRAMEBUFFER = 0;
36 static const GLuint NO_RENDERBUFFER = 0;
41 Standard_EXPORT OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8);
44 Standard_EXPORT virtual ~OpenGl_FrameBuffer();
46 //! Destroy object - will release GPU memory if any.
47 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx);
50 GLsizei GetSizeX() const
52 return myColorTexture->SizeX();
56 GLsizei GetSizeY() const
58 return myColorTexture->SizeY();
62 GLsizei GetVPSizeX() const
68 GLsizei GetVPSizeY() const
73 //! Returns true if current object was initialized
74 Standard_Boolean IsValid() const
76 return isValidFrameBuffer();
79 //! Notice! Obsolete hardware (GeForce FX etc)
80 //! doesn't support rectangular textures!
81 //! There are 3 possible results if you are trying
82 //! to create non power-of-two FBO on these cards:
83 //! 1) FBO creation will fail,
84 //! current implementation will try to generate compatible FBO;
85 //! 2) FBO rendering will be done in software mode (ForceWare 'hack');
86 //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers).
87 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
88 const GLsizei theViewportSizeX,
89 const GLsizei theViewportSizeY);
91 //! (Re-)initialize FBO with specified dimensions.
92 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
93 const GLsizei theViewportSizeX,
94 const GLsizei theViewportSizeY);
96 //! (Re-)initialize FBO with specified dimensions.
97 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
98 //! @param theGlCtx currently bound OpenGL context
99 //! @param theViewportSizeX required viewport size, the actual dimensions of FBO might be greater
100 //! @param theViewportSizeY required viewport size, the actual dimensions of FBO might be greater
101 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
102 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
103 const GLsizei theViewportSizeX,
104 const GLsizei theViewportSizeY,
105 const GLuint theColorRBufferFromWindow = 0);
107 //! Initialize class from currently bound FBO.
108 //! Retrieved OpenGL objects will not be destroyed on Release.
109 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
111 //! Setup viewport to render into FBO
112 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
114 //! Override viewport settings
115 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
116 const GLsizei theVPSizeY);
118 //! Bind frame buffer for drawing and reading (to render into the texture).
119 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
121 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
122 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
124 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
125 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
127 //! Unbind frame buffer.
128 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
130 //! Returns the color texture.
131 inline const Handle(OpenGl_Texture)& ColorTexture() const
133 return myColorTexture;
136 //! Returns the depth-stencil texture.
137 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
139 return myDepthStencilTexture;
142 //! Returns the color Render Buffer.
143 GLuint ColorRenderBuffer() const
145 return myGlColorRBufferId;
148 //! Returns the depth Render Buffer.
149 GLuint DepthStencilRenderBuffer() const
151 return myGlDepthRBufferId;
156 //! Generate textures with undefined data
157 Standard_Boolean initTrashTextures (const Handle(OpenGl_Context)& theGlContext);
159 Standard_Boolean isValidFrameBuffer() const
161 return myGlFBufferId != NO_FRAMEBUFFER;
166 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
167 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
168 GLint myTextFormat; //!< GL_RGB, GL_RGBA,...
169 GLuint myGlFBufferId; //!< FBO object ID
170 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
171 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
172 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
173 Handle(OpenGl_Texture) myColorTexture; //!< color texture object
174 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
178 DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer) // Type definition
182 #endif // OPENGL_FRAME_BUFFER_H