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