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 (GLint theTextureFormat = GL_RGBA8);
45 Standard_EXPORT virtual ~OpenGl_FrameBuffer();
47 //! Destroy object - will release GPU memory if any.
48 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx);
51 GLsizei GetSizeX() const
53 return myColorTexture->SizeX();
57 GLsizei GetSizeY() const
59 return myColorTexture->SizeY();
63 GLsizei GetVPSizeX() const
69 GLsizei GetVPSizeY() const
74 //! Returns true if current object was initialized
75 Standard_Boolean IsValid() const
77 return isValidFrameBuffer();
80 //! Notice! Obsolete hardware (GeForce FX etc)
81 //! doesn't support rectangular textures!
82 //! There are 3 possible results if you are trying
83 //! to create non power-of-two FBO on these cards:
84 //! 1) FBO creation will fail,
85 //! current implementation will try to generate compatible FBO;
86 //! 2) FBO rendering will be done in software mode (ForceWare 'hack');
87 //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers).
88 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
89 const GLsizei theViewportSizeX,
90 const GLsizei theViewportSizeY);
92 //! (Re-)initialize FBO with specified dimensions.
93 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
94 const GLsizei theViewportSizeX,
95 const GLsizei theViewportSizeY);
97 //! (Re-)initialize FBO with specified dimensions.
98 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
99 //! @param theGlCtx currently bound OpenGL context
100 //! @param theViewportSizeX required viewport size, the actual dimensions of FBO might be greater
101 //! @param theViewportSizeY required viewport size, the actual dimensions of FBO might be greater
102 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
103 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
104 const GLsizei theViewportSizeX,
105 const GLsizei theViewportSizeY,
106 const GLuint theColorRBufferFromWindow = 0);
108 //! Initialize class from currently bound FBO.
109 //! Retrieved OpenGL objects will not be destroyed on Release.
110 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
112 //! Setup viewport to render into FBO
113 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
115 //! Override viewport settings
116 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
117 const GLsizei theVPSizeY);
119 //! Bind frame buffer for drawing and reading (to render into the texture).
120 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
122 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
123 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
125 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
126 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
128 //! Unbind frame buffer.
129 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
131 //! Returns the color texture.
132 inline const Handle(OpenGl_Texture)& ColorTexture() const
134 return myColorTexture;
137 //! Returns the depth-stencil texture.
138 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
140 return myDepthStencilTexture;
143 //! Returns the color Render Buffer.
144 GLuint ColorRenderBuffer() const
146 return myGlColorRBufferId;
149 //! Returns the depth Render Buffer.
150 GLuint DepthStencilRenderBuffer() const
152 return myGlDepthRBufferId;
157 Standard_Boolean isValidFrameBuffer() const
159 return myGlFBufferId != NO_FRAMEBUFFER;
164 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
165 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
166 GLint myTextFormat; //!< GL_RGB, GL_RGBA,...
167 GLuint myGlFBufferId; //!< FBO object ID
168 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
169 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
170 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
171 Handle(OpenGl_Texture) myColorTexture; //!< color texture object
172 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
176 DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer, OpenGl_Resource) // Type definition
180 #endif // OPENGL_FRAME_BUFFER_H