0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[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 //! 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
35 public:
36
37   //! Empty constructor
38   Standard_EXPORT OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8);
39
40   //! Destructor
41   Standard_EXPORT virtual ~OpenGl_FrameBuffer();
42
43   //! Destroy object - will release GPU memory if any.
44   Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx);
45
46   //! Textures width.
47   GLsizei GetSizeX() const
48   {
49     return myColorTexture->SizeX();
50   }
51
52   //! Textures height.
53   GLsizei GetSizeY() const
54   {
55     return myColorTexture->SizeY();
56   }
57
58   //! Viewport width.
59   GLsizei GetVPSizeX() const
60   {
61     return myVPSizeX;
62   }
63
64   //! Viewport height.
65   GLsizei GetVPSizeY() const
66   {
67     return myVPSizeY;
68   }
69
70   //! Returns true if current object was initialized
71   Standard_Boolean IsValid() const
72   {
73     return isValidFrameBuffer();
74   }
75
76   //! Notice! Obsolete hardware (GeForce FX etc)
77   //! doesn't support rectangular textures!
78   //! There are 3 possible results if you are trying
79   //! to create non power-of-two FBO on these cards:
80   //! 1) FBO creation will fail,
81   //!    current implementation will try to generate compatible FBO;
82   //! 2) FBO rendering will be done in software mode (ForceWare 'hack');
83   //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers).
84   Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
85                                          const GLsizei                 theViewportSizeX,
86                                          const GLsizei                 theViewportSizeY);
87
88   //! Setup viewport to render into FBO
89   Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
90
91   //! Override viewport settings
92   Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
93                                        const GLsizei theVPSizeY);
94
95   //! Bind frame buffer (to render into the texture).
96   Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
97
98   //! Unbind frame buffer.
99   Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
100
101   //! Returns the color texture.
102   inline const Handle(OpenGl_Texture)& ColorTexture() const
103   {
104     return myColorTexture;
105   }
106
107   //! Returns the depth-stencil texture.
108   inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
109   {
110     return myDepthStencilTexture;
111   }
112
113 protected:
114
115   //! Generate textures with undefined data
116   Standard_Boolean initTrashTextures (const Handle(OpenGl_Context)& theGlContext);
117
118   Standard_Boolean isValidFrameBuffer() const
119   {
120     return myGlFBufferId != NO_FRAMEBUFFER;
121   }
122
123 protected:
124
125   GLsizei                myVPSizeX;             //!< viewport width  (should be <= texture width)
126   GLsizei                myVPSizeY;             //!< viewport height (should be <= texture height)
127   GLint                  myTextFormat;          //!< GL_RGB, GL_RGBA,...
128   GLuint                 myGlFBufferId;         //!< FBO object ID
129   Handle(OpenGl_Texture) myColorTexture;        //!< color texture object
130   Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
131
132 public:
133
134   DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer) // Type definition
135
136 };
137
138 DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource)
139
140 #endif // OPENGL_FRAME_BUFFER_H