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 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
93 //! @param theGlCtx currently bound OpenGL context
94 //! @param theViewportSizeX required viewport size, the actual dimensions of FBO might be greater
95 //! @param theViewportSizeY required viewport size, the actual dimensions of FBO might be greater
96 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
97 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
98 const GLsizei theViewportSizeX,
99 const GLsizei theViewportSizeY,
100 const GLuint theColorRBufferFromWindow = 0);
102 //! Initialize class from currently bound FBO.
103 //! Retrieved OpenGL objects will not be destroyed on Release.
104 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
106 //! Setup viewport to render into FBO
107 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
109 //! Override viewport settings
110 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
111 const GLsizei theVPSizeY);
113 //! Bind frame buffer for drawing and reading (to render into the texture).
114 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
116 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
117 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
119 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
120 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
122 //! Unbind frame buffer.
123 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
125 //! Returns the color texture.
126 inline const Handle(OpenGl_Texture)& ColorTexture() const
128 return myColorTexture;
131 //! Returns the depth-stencil texture.
132 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
134 return myDepthStencilTexture;
137 //! Returns the color Render Buffer.
138 GLuint ColorRenderBuffer() const
140 return myGlColorRBufferId;
143 //! Returns the depth Render Buffer.
144 GLuint DepthStencilRenderBuffer() const
146 return myGlDepthRBufferId;
151 //! Generate textures with undefined data
152 Standard_Boolean initTrashTextures (const Handle(OpenGl_Context)& theGlContext);
154 Standard_Boolean isValidFrameBuffer() const
156 return myGlFBufferId != NO_FRAMEBUFFER;
161 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
162 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
163 GLint myTextFormat; //!< GL_RGB, GL_RGBA,...
164 GLuint myGlFBufferId; //!< FBO object ID
165 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
166 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
167 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
168 Handle(OpenGl_Texture) myColorTexture; //!< color texture object
169 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
173 DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer) // Type definition
177 #endif // OPENGL_FRAME_BUFFER_H