0022734: Memory allocation error in OpenGl
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2011-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19
20 #ifndef OPENGL_FRAME_BUFFER_H
21 #define OPENGL_FRAME_BUFFER_H
22
23 #include <OpenGl_Context.hxx>
24 #include <OpenGl_ExtFBO.hxx>
25
26 #include <Standard_Boolean.hxx>
27 #include <InterfaceGraphic.hxx>
28
29 class OpenGl_FrameBuffer
30 {
31
32 public:
33
34   //! Helpful constants
35   static const GLuint NO_TEXTURE = 0;
36   static const GLuint NO_FRAMEBUFFER = 0;
37   static const GLuint NO_RENDERBUFFER = 0;
38
39 public:
40
41   OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8);
42
43   virtual ~OpenGl_FrameBuffer()
44   {
45     Release (Handle(OpenGl_Context)());
46   }
47
48   //! Texture width.
49   GLsizei GetSizeX() const
50   {
51     return mySizeX;
52   }
53
54   //! Texture height.
55   GLsizei GetSizeY() const
56   {
57     return mySizeY;
58   }
59
60   //! Viewport width.
61   GLsizei GetVPSizeX() const
62   {
63     return myVPSizeX;
64   }
65
66   //! Viewport height.
67   GLsizei GetVPSizeY() const
68   {
69     return myVPSizeY;
70   }
71
72   //! Returns true if current object was initialized
73   Standard_Boolean IsValid() const
74   {
75     return IsValidFrameBuffer() && IsValidTexture() && IsValidDepthBuffer();
76   }
77
78   //! Notice! Obsolete hardware (GeForce FX etc)
79   //! doesn't support rectangular textures!
80   //! There are 3 possible results if you are trying
81   //! to create non power-of-two FBO on these cards:
82   //! 1) FBO creation will fail,
83   //!    current implementation will try to generate compatible FBO;
84   //! 2) FBO rendering will be done in software mode (ForceWare 'hack');
85   //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers).
86   Standard_Boolean Init (const Handle(OpenGl_Context)& theGlContext,
87                          GLsizei theViewportSizeX,
88                          GLsizei theViewportSizeY,
89                          GLboolean toForcePowerOfTwo = GL_FALSE);
90
91   //! Release GL objects
92   void Release (const Handle(OpenGl_Context)& theGlContext);
93
94   //! Setup viewport to render into FBO
95   void SetupViewport()
96   {
97     glViewport (0, 0, myVPSizeX, myVPSizeY);
98   }
99
100   //! Override viewport settings
101   void ChangeViewport (const GLsizei theVPSizeX,
102                        const GLsizei theVPSizeY)
103   {
104     myVPSizeX = theVPSizeX;
105     myVPSizeY = theVPSizeY;
106   }
107
108   //! Bind frame buffer (to render into the texture).
109   void BindBuffer (const Handle(OpenGl_Context)& theGlContext)
110   {
111     theGlContext->extFBO->glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, myGlFBufferId);
112   }
113
114   //! Unbind frame buffer.
115   void UnbindBuffer (const Handle(OpenGl_Context)& theGlContext)
116   {
117     theGlContext->extFBO->glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, NO_FRAMEBUFFER);
118   }
119
120   //! Bind the texture.
121   void BindTexture ()
122   {
123     glEnable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering
124     glBindTexture (GL_TEXTURE_2D, myGlTextureId);
125   }
126
127   //! Unbind the texture.
128   void UnbindTexture()
129   {
130     glBindTexture (GL_TEXTURE_2D, NO_TEXTURE);
131     glDisable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering
132   }
133
134 private:
135
136   //! Check texture could be created
137   Standard_Boolean IsProxySuccess() const;
138
139   //! Generate texture with undefined data
140   Standard_Boolean InitTrashTexture (const Handle(OpenGl_Context)& theGlContext);
141
142   Standard_Boolean IsValidTexture() const
143   {
144     return myGlTextureId != NO_TEXTURE;
145   }
146
147   Standard_Boolean IsValidFrameBuffer() const
148   {
149     return myGlFBufferId != NO_FRAMEBUFFER;
150   }
151
152   Standard_Boolean IsValidDepthBuffer() const
153   {
154     return myGlDepthRBId != NO_RENDERBUFFER;
155   }
156
157 private:
158
159   GLsizei      mySizeX; // texture width
160   GLsizei      mySizeY; // texture height
161   GLsizei    myVPSizeX; // viewport width (should be <= texture width)
162   GLsizei    myVPSizeY; // viewport height (should be <= texture height)
163   GLint   myTextFormat; // GL_RGB, GL_RGBA,...
164   GLuint myGlTextureId; // GL texture ID
165   GLuint myGlFBufferId; // FBO object ID
166   GLuint myGlDepthRBId; // RenderBuffer object for depth ID
167
168 };
169
170 #endif //OPENGL_FRAME_BUFFER_H