b311480e |
1 | // Created by: Kirill GAVRILOV |
973c2be1 |
2 | // Copyright (c) 2011-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | #ifndef OPENGL_FRAME_BUFFER_H |
16 | #define OPENGL_FRAME_BUFFER_H |
17 | |
2166f0fa |
18 | #include <OpenGl_Context.hxx> |
fd4a6963 |
19 | #include <OpenGl_Resource.hxx> |
18f4e8e2 |
20 | #include <OpenGl_Texture.hxx> |
7fd59977 |
21 | |
22 | #include <Standard_Boolean.hxx> |
23 | #include <InterfaceGraphic.hxx> |
24 | |
a2e4f780 |
25 | #include <Handle_OpenGl_FrameBuffer.hxx> |
26 | |
fd4a6963 |
27 | //! Class implements FrameBuffer Object (FBO) resource |
28 | //! intended for off-screen rendering. |
29 | class OpenGl_FrameBuffer : public OpenGl_Resource |
7fd59977 |
30 | { |
31 | |
32 | public: |
33 | |
34 | //! Helpful constants |
a2e4f780 |
35 | static const GLuint NO_FRAMEBUFFER = 0; |
36 | static const GLuint NO_RENDERBUFFER = 0; |
7fd59977 |
37 | |
7fd59977 |
38 | public: |
39 | |
fd4a6963 |
40 | //! Empty constructor |
41 | Standard_EXPORT OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8); |
7fd59977 |
42 | |
fd4a6963 |
43 | //! Destructor |
44 | Standard_EXPORT virtual ~OpenGl_FrameBuffer(); |
45 | |
46 | //! Destroy object - will release GPU memory if any. |
10b9c7df |
47 | Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx); |
7fd59977 |
48 | |
18f4e8e2 |
49 | //! Textures width. |
7fd59977 |
50 | GLsizei GetSizeX() const |
51 | { |
18f4e8e2 |
52 | return myColorTexture->SizeX(); |
7fd59977 |
53 | } |
54 | |
18f4e8e2 |
55 | //! Textures height. |
7fd59977 |
56 | GLsizei GetSizeY() const |
57 | { |
18f4e8e2 |
58 | return myColorTexture->SizeY(); |
7fd59977 |
59 | } |
60 | |
61 | //! Viewport width. |
62 | GLsizei GetVPSizeX() const |
63 | { |
64 | return myVPSizeX; |
65 | } |
66 | |
67 | //! Viewport height. |
68 | GLsizei GetVPSizeY() const |
69 | { |
70 | return myVPSizeY; |
71 | } |
72 | |
73 | //! Returns true if current object was initialized |
74 | Standard_Boolean IsValid() const |
75 | { |
18f4e8e2 |
76 | return isValidFrameBuffer(); |
7fd59977 |
77 | } |
78 | |
79 | //! Notice! Obsolete hardware (GeForce FX etc) |
80 | //! doesn't support rectangular textures! |
81 | //! There are 3 possible results if you are trying |
82 | //! to create non power-of-two FBO on these cards: |
83 | //! 1) FBO creation will fail, |
84 | //! current implementation will try to generate compatible FBO; |
85 | //! 2) FBO rendering will be done in software mode (ForceWare 'hack'); |
86 | //! 3) FBO rendering will be incorrect (some obsolete Catalyst drivers). |
fd4a6963 |
87 | Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, |
88 | const GLsizei theViewportSizeX, |
18f4e8e2 |
89 | const GLsizei theViewportSizeY); |
7fd59977 |
90 | |
38a0206f |
91 | //! (Re-)initialize FBO with specified dimensions. |
92 | Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
93 | const GLsizei theViewportSizeX, |
94 | const GLsizei theViewportSizeY); |
95 | |
a2e4f780 |
96 | //! (Re-)initialize FBO with specified dimensions. |
97 | //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures). |
98 | //! @param theGlCtx currently bound OpenGL context |
99 | //! @param theViewportSizeX required viewport size, the actual dimensions of FBO might be greater |
100 | //! @param theViewportSizeY required viewport size, the actual dimensions of FBO might be greater |
101 | //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class |
102 | Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx, |
103 | const GLsizei theViewportSizeX, |
104 | const GLsizei theViewportSizeY, |
105 | const GLuint theColorRBufferFromWindow = 0); |
106 | |
107 | //! Initialize class from currently bound FBO. |
108 | //! Retrieved OpenGL objects will not be destroyed on Release. |
109 | Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx); |
110 | |
7fd59977 |
111 | //! Setup viewport to render into FBO |
fd4a6963 |
112 | Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
113 | |
114 | //! Override viewport settings |
fd4a6963 |
115 | Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX, |
116 | const GLsizei theVPSizeY); |
7fd59977 |
117 | |
b86bb3df |
118 | //! Bind frame buffer for drawing and reading (to render into the texture). |
18f4e8e2 |
119 | Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
120 | |
b86bb3df |
121 | //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture). |
122 | Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx); |
123 | |
124 | //! Bind frame buffer for reading GL_READ_FRAMEBUFFER |
125 | Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx); |
126 | |
7fd59977 |
127 | //! Unbind frame buffer. |
18f4e8e2 |
128 | Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
129 | |
18f4e8e2 |
130 | //! Returns the color texture. |
131 | inline const Handle(OpenGl_Texture)& ColorTexture() const |
7fd59977 |
132 | { |
18f4e8e2 |
133 | return myColorTexture; |
7fd59977 |
134 | } |
135 | |
18f4e8e2 |
136 | //! Returns the depth-stencil texture. |
137 | inline const Handle(OpenGl_Texture)& DepthStencilTexture() const |
7fd59977 |
138 | { |
18f4e8e2 |
139 | return myDepthStencilTexture; |
7fd59977 |
140 | } |
141 | |
a2e4f780 |
142 | //! Returns the color Render Buffer. |
143 | GLuint ColorRenderBuffer() const |
144 | { |
145 | return myGlColorRBufferId; |
146 | } |
147 | |
148 | //! Returns the depth Render Buffer. |
149 | GLuint DepthStencilRenderBuffer() const |
150 | { |
151 | return myGlDepthRBufferId; |
152 | } |
153 | |
18f4e8e2 |
154 | protected: |
155 | |
18f4e8e2 |
156 | Standard_Boolean isValidFrameBuffer() const |
b859a34d |
157 | { |
18f4e8e2 |
158 | return myGlFBufferId != NO_FRAMEBUFFER; |
b859a34d |
159 | } |
160 | |
18f4e8e2 |
161 | protected: |
162 | |
163 | GLsizei myVPSizeX; //!< viewport width (should be <= texture width) |
164 | GLsizei myVPSizeY; //!< viewport height (should be <= texture height) |
165 | GLint myTextFormat; //!< GL_RGB, GL_RGBA,... |
166 | GLuint myGlFBufferId; //!< FBO object ID |
a2e4f780 |
167 | GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture) |
168 | GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture) |
169 | bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class |
18f4e8e2 |
170 | Handle(OpenGl_Texture) myColorTexture; //!< color texture object |
171 | Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object |
fd4a6963 |
172 | |
173 | public: |
174 | |
175 | DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer) // Type definition |
7fd59977 |
176 | |
7fd59977 |
177 | }; |
178 | |
fd4a6963 |
179 | #endif // OPENGL_FRAME_BUFFER_H |