0027925: Visualization - implement order-independent transparency algorithm within...
[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 <NCollection_Vector.hxx>
23
24 class OpenGl_FrameBuffer;
25 DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource)
26
27 //! Short declaration of useful collection types.
28 typedef NCollection_Vector<GLint> OpenGl_ColorFormats;
29
30 //! Class implements FrameBuffer Object (FBO) resource
31 //! intended for off-screen rendering.
32 class OpenGl_FrameBuffer : public OpenGl_Resource
33 {
34
35 public:
36
37   //! Helpful constants
38   static const GLuint NO_FRAMEBUFFER  = 0;
39   static const GLuint NO_RENDERBUFFER = 0;
40
41 public:
42
43   //! Empty constructor
44   Standard_EXPORT OpenGl_FrameBuffer();
45
46   //! Destructor
47   Standard_EXPORT virtual ~OpenGl_FrameBuffer();
48
49   //! Destroy object - will release GPU memory if any.
50   Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
51
52   //! Number of multisampling samples.
53   GLsizei NbSamples() const
54   {
55     return myNbSamples;
56   }
57
58   //! Number of color buffers.
59   GLsizei NbColorBuffers() const
60   {
61     return myColorTextures.Length();
62   }
63
64   //! Return true if FBO has been created with color attachment.
65   bool HasColor() const
66   {
67     return !myColorFormats.IsEmpty();
68   }
69
70   //! Return true if FBO has been created with depth attachment.
71   bool HasDepth() const
72   {
73     return myDepthFormat != 0;
74   }
75
76   //! Textures width.
77   GLsizei GetSizeX() const
78   {
79     return myColorTextures (0)->SizeX();
80   }
81
82   //! Textures height.
83   GLsizei GetSizeY() const
84   {
85     return myColorTextures (0)->SizeY();
86   }
87
88   //! Viewport width.
89   GLsizei GetVPSizeX() const
90   {
91     return myVPSizeX;
92   }
93
94   //! Viewport height.
95   GLsizei GetVPSizeY() const
96   {
97     return myVPSizeY;
98   }
99
100   //! Returns true if current object was initialized
101   Standard_Boolean IsValid() const
102   {
103     return isValidFrameBuffer();
104   }
105
106   //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
107   //! @param theGlCtx               currently bound OpenGL context
108   //! @param theSizeX               texture width
109   //! @param theSizeY               texture height
110   //! @param theColorFormats        list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
111   //! @param theDepthStencilTexture depth-stencil texture
112   //! @param theNbSamples           MSAA number of samples (0 means normal texture)
113   //! @return true on success
114   Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
115                                          const GLsizei                 theSizeX,
116                                          const GLsizei                 theSizeY,
117                                          const OpenGl_ColorFormats&    theColorFormats,
118                                          const Handle(OpenGl_Texture)& theDepthStencilTexture,
119                                          const GLsizei                 theNbSamples = 0);
120
121   //! Initialize FBO for rendering into textures.
122   //! @param theGlCtx       currently bound OpenGL context
123   //! @param theSizeX       texture width
124   //! @param theSizeY       texture height
125   //! @param theColorFormat color         texture sized format (0 means no color attachment), e.g. GL_RGBA8
126   //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
127   //! @param theNbSamples   MSAA number of samples (0 means normal texture)
128   //! @return true on success
129   Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
130                                          const GLsizei                 theSizeX,
131                                          const GLsizei                 theSizeY,
132                                          const GLint                   theColorFormat,
133                                          const GLint                   theDepthFormat,
134                                          const GLsizei                 theNbSamples = 0);
135
136   //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
137   //! @param theGlCtx        currently bound OpenGL context
138   //! @param theSizeX        texture width
139   //! @param theSizeY        texture height
140   //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
141   //! @param theDepthFormat  depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
142   //! @param theNbSamples    MSAA number of samples (0 means normal texture)
143   //! @return true on success
144   Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
145                                          const GLsizei                 theSizeX,
146                                          const GLsizei                 theSizeY,
147                                          const OpenGl_ColorFormats&    theColorFormats,
148                                          const GLint                   theDepthFormat,
149                                          const GLsizei                 theNbSamples = 0);
150
151   //! (Re-)initialize FBO with specified dimensions.
152   Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
153                                              const GLsizei                 theViewportSizeX,
154                                              const GLsizei                 theViewportSizeY,
155                                              const GLint                   theColorFormat,
156                                              const GLint                   theDepthFormat,
157                                              const GLsizei                 theNbSamples = 0);
158
159   //! (Re-)initialize FBO with specified dimensions.
160   Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
161                                              const GLsizei                 theViewportSizeX,
162                                              const GLsizei                 theViewportSizeY,
163                                              const OpenGl_ColorFormats&    theColorFormats,
164                                              const GLint                   theDepthFormat,
165                                              const GLsizei                 theNbSamples = 0);
166
167   //! (Re-)initialize FBO with properties taken from another FBO.
168   Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
169                              const OpenGl_FrameBuffer&     theFbo)
170   {
171     return InitLazy (theGlCtx, theFbo.myVPSizeX, theFbo.myVPSizeY, theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples);
172   }
173
174   //! (Re-)initialize FBO with specified dimensions.
175   //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
176   //! @param theGlCtx       currently bound OpenGL context
177   //! @param theSizeX       render buffer width
178   //! @param theSizeY       render buffer height
179   //! @param theColorFormat color         render buffer sized format, e.g. GL_RGBA8
180   //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
181   //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
182   Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
183                                                const GLsizei                 theSizeX,
184                                                const GLsizei                 theSizeY,
185                                                const GLint                   theColorFormat,
186                                                const GLint                   theDepthFormat,
187                                                const GLuint                  theColorRBufferFromWindow = 0);
188
189   //! Initialize class from currently bound FBO.
190   //! Retrieved OpenGL objects will not be destroyed on Release.
191   Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
192
193   //! Setup viewport to render into FBO
194   Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
195
196   //! Override viewport settings
197   Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
198                                        const GLsizei theVPSizeY);
199
200   //! Bind frame buffer for drawing and reading (to render into the texture).
201   Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
202
203   //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
204   Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
205
206   //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
207   Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
208
209   //! Unbind frame buffer.
210   Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
211
212   //! Returns the color texture for the given color buffer index.
213   inline const Handle(OpenGl_Texture)& ColorTexture (const GLint theColorBufferIndex = 0) const
214   {
215     return myColorTextures (theColorBufferIndex);
216   }
217
218   //! Returns the depth-stencil texture.
219   inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
220   {
221     return myDepthStencilTexture;
222   }
223
224   //! Returns the color Render Buffer.
225   GLuint ColorRenderBuffer() const
226   {
227     return myGlColorRBufferId;
228   }
229
230   //! Returns the depth Render Buffer.
231   GLuint DepthStencilRenderBuffer() const
232   {
233     return myGlDepthRBufferId;
234   }
235
236 protected:
237
238   Standard_Boolean isValidFrameBuffer() const
239   {
240     return myGlFBufferId != NO_FRAMEBUFFER;
241   }
242
243 protected:
244
245   typedef NCollection_Vector<Handle(OpenGl_Texture)> OpenGl_TextureArray;
246
247 protected:
248
249   GLsizei                myVPSizeX;             //!< viewport width  (should be <= texture width)
250   GLsizei                myVPSizeY;             //!< viewport height (should be <= texture height)
251   GLsizei                myNbSamples;           //!< number of MSAA samples
252   OpenGl_ColorFormats    myColorFormats;        //!< sized format for color         texture, GL_RGBA8 by default
253   GLint                  myDepthFormat;         //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default
254   GLuint                 myGlFBufferId;         //!< FBO object ID
255   GLuint                 myGlColorRBufferId;    //!< color         Render Buffer object (alternative to myColorTexture)
256   GLuint                 myGlDepthRBufferId;    //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
257   bool                   myIsOwnBuffer;         //!< flag indicating that FBO should be deallocated by this class
258   bool                   myIsOwnDepth;          //!< flag indicating that FBO should be deallocated by this class
259   OpenGl_TextureArray    myColorTextures;       //!< color texture objects
260   Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
261
262 public:
263
264   DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer,OpenGl_Resource) // Type definition
265
266 };
267
268 #endif // OPENGL_FRAME_BUFFER_H