0031082: Visualization - crash on display if there are no lights in the view
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.hxx
CommitLineData
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
6cde53c4 22#include <Graphic3d_BufferType.hxx>
a1073ae2 23#include <NCollection_Vector.hxx>
24
c04c30b3 25class OpenGl_FrameBuffer;
26DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource)
a2e4f780 27
a1073ae2 28//! Short declaration of useful collection types.
29typedef NCollection_Vector<GLint> OpenGl_ColorFormats;
30
fd4a6963 31//! Class implements FrameBuffer Object (FBO) resource
32//! intended for off-screen rendering.
33class OpenGl_FrameBuffer : public OpenGl_Resource
7fd59977 34{
35
36public:
37
38 //! Helpful constants
a2e4f780 39 static const GLuint NO_FRAMEBUFFER = 0;
40 static const GLuint NO_RENDERBUFFER = 0;
7fd59977 41
6cde53c4 42public:
43
44 //! Dump content into image.
45 //! @param theGlCtx bound OpenGL context
46 //! @param theFbo FBO to dump (or window buffer, if NULL)
47 //! @param theImage target image
48 //! @param theBufferType buffer type (attachment) to dump
49 //! @return TRUE on success
50 Standard_EXPORT static Standard_Boolean BufferDump (const Handle(OpenGl_Context)& theGlCtx,
51 const Handle(OpenGl_FrameBuffer)& theFbo,
52 Image_PixMap& theImage,
53 Graphic3d_BufferType theBufferType);
54
7fd59977 55public:
56
fd4a6963 57 //! Empty constructor
3c4b62a4 58 Standard_EXPORT OpenGl_FrameBuffer();
7fd59977 59
fd4a6963 60 //! Destructor
61 Standard_EXPORT virtual ~OpenGl_FrameBuffer();
62
63 //! Destroy object - will release GPU memory if any.
79104795 64 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
7fd59977 65
3c4b62a4 66 //! Number of multisampling samples.
67 GLsizei NbSamples() const
68 {
69 return myNbSamples;
70 }
71
a1073ae2 72 //! Number of color buffers.
73 GLsizei NbColorBuffers() const
74 {
75 return myColorTextures.Length();
76 }
77
3c4b62a4 78 //! Return true if FBO has been created with color attachment.
79 bool HasColor() const
80 {
a1073ae2 81 return !myColorFormats.IsEmpty();
3c4b62a4 82 }
83
84 //! Return true if FBO has been created with depth attachment.
85 bool HasDepth() const
86 {
87 return myDepthFormat != 0;
88 }
89
18f4e8e2 90 //! Textures width.
7fd59977 91 GLsizei GetSizeX() const
92 {
a1073ae2 93 return myColorTextures (0)->SizeX();
7fd59977 94 }
95
18f4e8e2 96 //! Textures height.
7fd59977 97 GLsizei GetSizeY() const
98 {
a1073ae2 99 return myColorTextures (0)->SizeY();
7fd59977 100 }
101
102 //! Viewport width.
103 GLsizei GetVPSizeX() const
104 {
105 return myVPSizeX;
106 }
107
108 //! Viewport height.
109 GLsizei GetVPSizeY() const
110 {
111 return myVPSizeY;
112 }
113
a0b49de4 114 //! Viewport width.
115 GLsizei GetInitVPSizeX() const
116 {
117 return myInitVPSizeX;
118 }
119
120 //! Viewport height.
121 GLsizei GetInitVPSizeY() const
122 {
123 return myInitVPSizeY;
124 }
125
7fd59977 126 //! Returns true if current object was initialized
127 Standard_Boolean IsValid() const
128 {
18f4e8e2 129 return isValidFrameBuffer();
7fd59977 130 }
131
a1073ae2 132 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
133 //! @param theGlCtx currently bound OpenGL context
134 //! @param theSizeX texture width
135 //! @param theSizeY texture height
136 //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
137 //! @param theDepthStencilTexture depth-stencil texture
138 //! @param theNbSamples MSAA number of samples (0 means normal texture)
139 //! @return true on success
140 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
141 const GLsizei theSizeX,
142 const GLsizei theSizeY,
143 const OpenGl_ColorFormats& theColorFormats,
144 const Handle(OpenGl_Texture)& theDepthStencilTexture,
145 const GLsizei theNbSamples = 0);
146
3c4b62a4 147 //! Initialize FBO for rendering into textures.
148 //! @param theGlCtx currently bound OpenGL context
149 //! @param theSizeX texture width
150 //! @param theSizeY texture height
151 //! @param theColorFormat color texture sized format (0 means no color attachment), e.g. GL_RGBA8
152 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
153 //! @param theNbSamples MSAA number of samples (0 means normal texture)
154 //! @return true on success
fd4a6963 155 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
3c4b62a4 156 const GLsizei theSizeX,
157 const GLsizei theSizeY,
158 const GLint theColorFormat,
159 const GLint theDepthFormat,
160 const GLsizei theNbSamples = 0);
7fd59977 161
a1073ae2 162 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
163 //! @param theGlCtx currently bound OpenGL context
164 //! @param theSizeX texture width
165 //! @param theSizeY texture height
166 //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
167 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
168 //! @param theNbSamples MSAA number of samples (0 means normal texture)
169 //! @return true on success
170 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
171 const GLsizei theSizeX,
172 const GLsizei theSizeY,
173 const OpenGl_ColorFormats& theColorFormats,
174 const GLint theDepthFormat,
175 const GLsizei theNbSamples = 0);
176
38a0206f 177 //! (Re-)initialize FBO with specified dimensions.
178 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
179 const GLsizei theViewportSizeX,
3c4b62a4 180 const GLsizei theViewportSizeY,
181 const GLint theColorFormat,
182 const GLint theDepthFormat,
183 const GLsizei theNbSamples = 0);
184
a1073ae2 185 //! (Re-)initialize FBO with specified dimensions.
186 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
187 const GLsizei theViewportSizeX,
188 const GLsizei theViewportSizeY,
189 const OpenGl_ColorFormats& theColorFormats,
190 const GLint theDepthFormat,
191 const GLsizei theNbSamples = 0);
192
3c4b62a4 193 //! (Re-)initialize FBO with properties taken from another FBO.
194 Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
195 const OpenGl_FrameBuffer& theFbo)
196 {
a1073ae2 197 return InitLazy (theGlCtx, theFbo.myVPSizeX, theFbo.myVPSizeY, theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples);
3c4b62a4 198 }
38a0206f 199
a2e4f780 200 //! (Re-)initialize FBO with specified dimensions.
201 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
3c4b62a4 202 //! @param theGlCtx currently bound OpenGL context
203 //! @param theSizeX render buffer width
204 //! @param theSizeY render buffer height
205 //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8
206 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
a2e4f780 207 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
208 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
3c4b62a4 209 const GLsizei theSizeX,
210 const GLsizei theSizeY,
211 const GLint theColorFormat,
212 const GLint theDepthFormat,
a2e4f780 213 const GLuint theColorRBufferFromWindow = 0);
214
215 //! Initialize class from currently bound FBO.
216 //! Retrieved OpenGL objects will not be destroyed on Release.
217 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
218
7fd59977 219 //! Setup viewport to render into FBO
fd4a6963 220 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
7fd59977 221
222 //! Override viewport settings
fd4a6963 223 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
224 const GLsizei theVPSizeY);
7fd59977 225
b86bb3df 226 //! Bind frame buffer for drawing and reading (to render into the texture).
18f4e8e2 227 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
7fd59977 228
b86bb3df 229 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
230 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
231
232 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
233 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
234
7fd59977 235 //! Unbind frame buffer.
18f4e8e2 236 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
7fd59977 237
a1073ae2 238 //! Returns the color texture for the given color buffer index.
239 inline const Handle(OpenGl_Texture)& ColorTexture (const GLint theColorBufferIndex = 0) const
7fd59977 240 {
a1073ae2 241 return myColorTextures (theColorBufferIndex);
7fd59977 242 }
243
18f4e8e2 244 //! Returns the depth-stencil texture.
245 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
7fd59977 246 {
18f4e8e2 247 return myDepthStencilTexture;
7fd59977 248 }
249
a2e4f780 250 //! Returns the color Render Buffer.
251 GLuint ColorRenderBuffer() const
252 {
253 return myGlColorRBufferId;
254 }
255
256 //! Returns the depth Render Buffer.
257 GLuint DepthStencilRenderBuffer() const
258 {
259 return myGlDepthRBufferId;
260 }
261
15669413 262 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
263 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
264
18f4e8e2 265protected:
266
18f4e8e2 267 Standard_Boolean isValidFrameBuffer() const
b859a34d 268 {
18f4e8e2 269 return myGlFBufferId != NO_FRAMEBUFFER;
b859a34d 270 }
271
a1073ae2 272protected:
273
274 typedef NCollection_Vector<Handle(OpenGl_Texture)> OpenGl_TextureArray;
275
18f4e8e2 276protected:
277
a0b49de4 278 GLsizei myInitVPSizeX; //!< viewport width specified during initialization (kept even on failure)
279 GLsizei myInitVPSizeY; //!< viewport height specified during initialization (kept even on failure)
18f4e8e2 280 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
281 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
3c4b62a4 282 GLsizei myNbSamples; //!< number of MSAA samples
a1073ae2 283 OpenGl_ColorFormats myColorFormats; //!< sized format for color texture, GL_RGBA8 by default
3c4b62a4 284 GLint myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default
18f4e8e2 285 GLuint myGlFBufferId; //!< FBO object ID
a2e4f780 286 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
287 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
288 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
a1073ae2 289 bool myIsOwnDepth; //!< flag indicating that FBO should be deallocated by this class
290 OpenGl_TextureArray myColorTextures; //!< color texture objects
18f4e8e2 291 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
fd4a6963 292
293public:
294
92efcf78 295 DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer,OpenGl_Resource) // Type definition
7fd59977 296
7fd59977 297};
298
fd4a6963 299#endif // OPENGL_FRAME_BUFFER_H