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 | |
fd4a6963 |
26 | //! Class implements FrameBuffer Object (FBO) resource |
27 | //! intended for off-screen rendering. |
28 | class OpenGl_FrameBuffer : public OpenGl_Resource |
7fd59977 |
29 | { |
30 | |
31 | public: |
32 | |
33 | //! Helpful constants |
a2e4f780 |
34 | static const GLuint NO_FRAMEBUFFER = 0; |
35 | static const GLuint NO_RENDERBUFFER = 0; |
7fd59977 |
36 | |
7fd59977 |
37 | public: |
38 | |
fd4a6963 |
39 | //! Empty constructor |
40 | Standard_EXPORT OpenGl_FrameBuffer (GLint theTextureFormat = GL_RGBA8); |
7fd59977 |
41 | |
fd4a6963 |
42 | //! Destructor |
43 | Standard_EXPORT virtual ~OpenGl_FrameBuffer(); |
44 | |
45 | //! Destroy object - will release GPU memory if any. |
10b9c7df |
46 | Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx); |
7fd59977 |
47 | |
18f4e8e2 |
48 | //! Textures width. |
7fd59977 |
49 | GLsizei GetSizeX() const |
50 | { |
18f4e8e2 |
51 | return myColorTexture->SizeX(); |
7fd59977 |
52 | } |
53 | |
18f4e8e2 |
54 | //! Textures height. |
7fd59977 |
55 | GLsizei GetSizeY() const |
56 | { |
18f4e8e2 |
57 | return myColorTexture->SizeY(); |
7fd59977 |
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 | { |
18f4e8e2 |
75 | return isValidFrameBuffer(); |
7fd59977 |
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). |
fd4a6963 |
86 | Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, |
87 | const GLsizei theViewportSizeX, |
18f4e8e2 |
88 | const GLsizei theViewportSizeY); |
7fd59977 |
89 | |
38a0206f |
90 | //! (Re-)initialize FBO with specified dimensions. |
91 | Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
92 | const GLsizei theViewportSizeX, |
93 | const GLsizei theViewportSizeY); |
94 | |
a2e4f780 |
95 | //! (Re-)initialize FBO with specified dimensions. |
96 | //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures). |
97 | //! @param theGlCtx currently bound OpenGL context |
98 | //! @param theViewportSizeX required viewport size, the actual dimensions of FBO might be greater |
99 | //! @param theViewportSizeY required viewport size, the actual dimensions of FBO might be greater |
100 | //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class |
101 | Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx, |
102 | const GLsizei theViewportSizeX, |
103 | const GLsizei theViewportSizeY, |
104 | const GLuint theColorRBufferFromWindow = 0); |
105 | |
106 | //! Initialize class from currently bound FBO. |
107 | //! Retrieved OpenGL objects will not be destroyed on Release. |
108 | Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx); |
109 | |
7fd59977 |
110 | //! Setup viewport to render into FBO |
fd4a6963 |
111 | Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
112 | |
113 | //! Override viewport settings |
fd4a6963 |
114 | Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX, |
115 | const GLsizei theVPSizeY); |
7fd59977 |
116 | |
b86bb3df |
117 | //! Bind frame buffer for drawing and reading (to render into the texture). |
18f4e8e2 |
118 | Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
119 | |
b86bb3df |
120 | //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture). |
121 | Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx); |
122 | |
123 | //! Bind frame buffer for reading GL_READ_FRAMEBUFFER |
124 | Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx); |
125 | |
7fd59977 |
126 | //! Unbind frame buffer. |
18f4e8e2 |
127 | Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
128 | |
18f4e8e2 |
129 | //! Returns the color texture. |
130 | inline const Handle(OpenGl_Texture)& ColorTexture() const |
7fd59977 |
131 | { |
18f4e8e2 |
132 | return myColorTexture; |
7fd59977 |
133 | } |
134 | |
18f4e8e2 |
135 | //! Returns the depth-stencil texture. |
136 | inline const Handle(OpenGl_Texture)& DepthStencilTexture() const |
7fd59977 |
137 | { |
18f4e8e2 |
138 | return myDepthStencilTexture; |
7fd59977 |
139 | } |
140 | |
a2e4f780 |
141 | //! Returns the color Render Buffer. |
142 | GLuint ColorRenderBuffer() const |
143 | { |
144 | return myGlColorRBufferId; |
145 | } |
146 | |
147 | //! Returns the depth Render Buffer. |
148 | GLuint DepthStencilRenderBuffer() const |
149 | { |
150 | return myGlDepthRBufferId; |
151 | } |
152 | |
18f4e8e2 |
153 | protected: |
154 | |
18f4e8e2 |
155 | Standard_Boolean isValidFrameBuffer() const |
b859a34d |
156 | { |
18f4e8e2 |
157 | return myGlFBufferId != NO_FRAMEBUFFER; |
b859a34d |
158 | } |
159 | |
18f4e8e2 |
160 | protected: |
161 | |
162 | GLsizei myVPSizeX; //!< viewport width (should be <= texture width) |
163 | GLsizei myVPSizeY; //!< viewport height (should be <= texture height) |
164 | GLint myTextFormat; //!< GL_RGB, GL_RGBA,... |
165 | GLuint myGlFBufferId; //!< FBO object ID |
a2e4f780 |
166 | GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture) |
167 | GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture) |
168 | bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class |
18f4e8e2 |
169 | Handle(OpenGl_Texture) myColorTexture; //!< color texture object |
170 | Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object |
fd4a6963 |
171 | |
172 | public: |
173 | |
ec357c5c |
174 | DEFINE_STANDARD_RTTI(OpenGl_FrameBuffer, OpenGl_Resource) // Type definition |
7fd59977 |
175 | |
7fd59977 |
176 | }; |
177 | |
fd4a6963 |
178 | #endif // OPENGL_FRAME_BUFFER_H |