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