0026165: Visualization, TKOpenGl - fix FBO blitting on some mobile devices
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2011-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef OPENGL_FRAME_BUFFER_H
16 #define OPENGL_FRAME_BUFFER_H
17
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_Resource.hxx>
20 #include <OpenGl_Texture.hxx>
21
22 #include <Standard_Boolean.hxx>
23 #include <InterfaceGraphic.hxx>
24
25 #include <Handle_OpenGl_FrameBuffer.hxx>
26
27 //! Class implements FrameBuffer Object (FBO) resource
28 //! intended for off-screen rendering.
29 class OpenGl_FrameBuffer : public OpenGl_Resource
30 {
31
32 public:
33
34   //! Helpful constants
35   static const GLuint NO_FRAMEBUFFER  = 0;
36   static const GLuint NO_RENDERBUFFER = 0;
37
38 public:
39
40   //! Empty constructor
41   Standard_EXPORT OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8);
42
43   //! Destructor
44   Standard_EXPORT virtual ~OpenGl_FrameBuffer();
45
46   //! Destroy object - will release GPU memory if any.
47   Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx);
48
49   //! Textures width.
50   GLsizei GetSizeX() const
51   {
52     return myColorTexture->SizeX();
53   }
54
55   //! Textures height.
56   GLsizei GetSizeY() const
57   {
58     return myColorTexture->SizeY();
59   }
60
61   //! Viewport width.
62   GLsizei GetVPSizeX() const
63   {
64     return myVPSizeX;
65   }
66
67   //! Viewport height.
68   GLsizei GetVPSizeY() const
69   {
70     return myVPSizeY;
71   }
72
73   //! Returns true if current object was initialized
74   Standard_Boolean IsValid() const
75   {
76     return isValidFrameBuffer();
77   }
78
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);
90
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);
95
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);
106
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);
110
111   //! Setup viewport to render into FBO
112   Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
113
114   //! Override viewport settings
115   Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
116                                        const GLsizei theVPSizeY);
117
118   //! Bind frame buffer for drawing and reading (to render into the texture).
119   Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
120
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);
123
124   //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
125   Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
126
127   //! Unbind frame buffer.
128   Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
129
130   //! Returns the color texture.
131   inline const Handle(OpenGl_Texture)& ColorTexture() const
132   {
133     return myColorTexture;
134   }
135
136   //! Returns the depth-stencil texture.
137   inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
138   {
139     return myDepthStencilTexture;
140   }
141
142   //! Returns the color Render Buffer.
143   GLuint ColorRenderBuffer() const
144   {
145     return myGlColorRBufferId;
146   }
147
148   //! Returns the depth Render Buffer.
149   GLuint DepthStencilRenderBuffer() const
150   {
151     return myGlDepthRBufferId;
152   }
153
154 protected:
155
156   Standard_Boolean isValidFrameBuffer() const
157   {
158     return myGlFBufferId != NO_FRAMEBUFFER;
159   }
160
161 protected:
162
163   GLsizei                myVPSizeX;             //!< viewport width  (should be <= texture width)
164   GLsizei                myVPSizeY;             //!< viewport height (should be <= texture height)
165   GLint                  myTextFormat;          //!< GL_RGB, GL_RGBA,...
166   GLuint                 myGlFBufferId;         //!< FBO object ID
167   GLuint                 myGlColorRBufferId;    //!< color         Render Buffer object (alternative to myColorTexture)
168   GLuint                 myGlDepthRBufferId;    //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
169   bool                   myIsOwnBuffer;         //!< flag indicating that FBO should be deallocated by this class
170   Handle(OpenGl_Texture) myColorTexture;        //!< color texture object
171   Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
172
173 public:
174
175   DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer) // Type definition
176
177 };
178
179 #endif // OPENGL_FRAME_BUFFER_H