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