0023022: This is desirable to access OpenGl extensions and core API (1.2+) in one...
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.hxx
1 // File:      OpenGl_FrameBuffer.hxx
2 // Author:    Kirill GAVRILOV
3 // Copyright: OPEN CASCADE 2011
4
5 #ifndef OPENGL_FRAME_BUFFER_H
6 #define OPENGL_FRAME_BUFFER_H
7
8 #include <OpenGl_Context.hxx>
9 #include <OpenGl_ExtFBO.hxx>
10
11 #include <Standard_Boolean.hxx>
12 #include <InterfaceGraphic.hxx>
13
14 class OpenGl_FrameBuffer
15 {
16
17 public:
18
19   //! Helpful constants
20   static const GLuint NO_TEXTURE = 0;
21   static const GLuint NO_FRAMEBUFFER = 0;
22   static const GLuint NO_RENDERBUFFER = 0;
23
24 public:
25
26   OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8);
27
28   virtual ~OpenGl_FrameBuffer()
29   {
30     Release (Handle(OpenGl_Context)());
31   }
32
33   //! Texture width.
34   GLsizei GetSizeX() const
35   {
36     return mySizeX;
37   }
38
39   //! Texture height.
40   GLsizei GetSizeY() const
41   {
42     return mySizeY;
43   }
44
45   //! Viewport width.
46   GLsizei GetVPSizeX() const
47   {
48     return myVPSizeX;
49   }
50
51   //! Viewport height.
52   GLsizei GetVPSizeY() const
53   {
54     return myVPSizeY;
55   }
56
57   //! Returns true if current object was initialized
58   Standard_Boolean IsValid() const
59   {
60     return IsValidFrameBuffer() && IsValidTexture() && IsValidDepthBuffer();
61   }
62
63   //! Notice! Obsolete hardware (GeForce FX etc)
64   //! doesn't support rectangular textures!
65   //! There are 3 possible results if you are trying
66   //! to create non power-of-two FBO on these cards:
67   //! 1) FBO creation will fail,
68   //!    current implementation will try to generate compatible FBO;
69   //! 2) FBO rendering will be done in software mode (ForceWare 'hack');
70   //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers).
71   Standard_Boolean Init (const Handle(OpenGl_Context)& theGlContext,
72                          GLsizei theViewportSizeX,
73                          GLsizei theViewportSizeY,
74                          GLboolean toForcePowerOfTwo = GL_FALSE);
75
76   //! Release GL objects
77   void Release (const Handle(OpenGl_Context)& theGlContext);
78
79   //! Setup viewport to render into FBO
80   void SetupViewport()
81   {
82     glViewport (0, 0, myVPSizeX, myVPSizeY);
83   }
84
85   //! Override viewport settings
86   void ChangeViewport (const GLsizei theVPSizeX,
87                        const GLsizei theVPSizeY)
88   {
89     myVPSizeX = theVPSizeX;
90     myVPSizeY = theVPSizeY;
91   }
92
93   //! Bind frame buffer (to render into the texture).
94   void BindBuffer (const Handle(OpenGl_Context)& theGlContext)
95   {
96     theGlContext->extFBO->glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, myGlFBufferId);
97   }
98
99   //! Unbind frame buffer.
100   void UnbindBuffer (const Handle(OpenGl_Context)& theGlContext)
101   {
102     theGlContext->extFBO->glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, NO_FRAMEBUFFER);
103   }
104
105   //! Bind the texture.
106   void BindTexture ()
107   {
108     glEnable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering
109     glBindTexture (GL_TEXTURE_2D, myGlTextureId);
110   }
111
112   //! Unbind the texture.
113   void UnbindTexture()
114   {
115     glBindTexture (GL_TEXTURE_2D, NO_TEXTURE);
116     glDisable (GL_TEXTURE_2D); // needed only for fixed pipeline rendering
117   }
118
119 private:
120
121   //! Check texture could be created
122   Standard_Boolean IsProxySuccess() const;
123
124   //! Generate texture with undefined data
125   Standard_Boolean InitTrashTexture (const Handle(OpenGl_Context)& theGlContext);
126
127   Standard_Boolean IsValidTexture() const
128   {
129     return myGlTextureId != NO_TEXTURE;
130   }
131
132   Standard_Boolean IsValidFrameBuffer() const
133   {
134     return myGlFBufferId != NO_FRAMEBUFFER;
135   }
136
137   Standard_Boolean IsValidDepthBuffer() const
138   {
139     return myGlTextureId != NO_RENDERBUFFER;
140   }
141
142 private:
143
144   GLsizei      mySizeX; // texture width
145   GLsizei      mySizeY; // texture height
146   GLsizei    myVPSizeX; // viewport width (should be <= texture width)
147   GLsizei    myVPSizeY; // viewport height (should be <= texture height)
148   GLint   myTextFormat; // GL_RGB, GL_RGBA,...
149   GLuint myGlTextureId; // GL texture ID
150   GLuint myGlFBufferId; // FBO object ID
151   GLuint myGlDepthRBId; // RenderBuffer object for depth ID
152
153 };
154
155 #endif //OPENGL_FRAME_BUFFER_H