Ocean
platform/gl/TextureFramebuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_GL_TEXTURE_FRAMEBUFFER_H
9 #define META_OCEAN_PLATFORM_GL_TEXTURE_FRAMEBUFFER_H
10 
11 #include "ocean/platform/gl/GL.h"
13 
14 namespace Ocean
15 {
16 
17 namespace Platform
18 {
19 
20 namespace GL
21 {
22 
23 /**
24  * This class implements a texture framebuffer allowing to render into a texture object.
25  * @ingroup platformgl
26  */
27 class OCEAN_PLATFORM_GL_EXPORT TextureFramebuffer : public Framebuffer
28 {
29  public:
30 
31  /**
32  * Creates a new framebuffer object.
33  */
34  TextureFramebuffer() = default;
35 
36  /**
37  * Creates a new framebuffer object with know associated context.
38  * @param context The associated context of this framebuffer
39  */
40  explicit TextureFramebuffer(Context& context);
41 
42  /**
43  * Returns the number of samples.
44  * @return The number of samples, with range [1, infinity), 1 by default
45  */
46  inline unsigned int samples() const;
47 
48  /**
49  * Returns the id of the texture of this framebuffer.
50  * @return The texture's id
51  */
52  inline GLuint textureId() const;
53 
54  /**
55  * (Re-)sets the number of samples of this texture framebuffer.
56  * @param samples The number of samples to set, with range [1, infinity)
57  * @return True, if succeeded
58  */
59  bool setSamples(const unsigned int samples);
60 
61  /**
62  * Resizes the off-screen framebuffer.
63  * @see Framebuffer::resize().
64  */
65  virtual bool resize(const unsigned int width, const unsigned int height, const GLenum internalFormat = GL_RGBA);
66 
67  /**
68  * Binds the framebuffer as target framebuffer.
69  * @see Framebuffer::bind().
70  */
71  virtual bool bind();
72 
73  /**
74  * Bind this texture to a given shader program.
75  * @param programId The id of the shader program
76  * @param attributeName The name of the texture attribute
77  * @param index The index of the texture
78  * @return True, if succeeded
79  */
80  bool bindToProgram(const GLuint programId, const std::string& attributeName, const unsigned int index = 0u);
81 
82  /**
83  * Unbinds the framebuffer as target framebuffer.
84  * @see Framebuffer::unbind().
85  */
86  virtual bool unbind();
87 
88  /**
89  * Releases this framebuffer.
90  * @return True, if succeeded
91  */
92  bool release();
93 
94  /**
95  * Returns whether this framebuffer holds a valid texture.
96  * @return True, if so
97  */
98  explicit inline operator bool() const;
99 
100  protected:
101 
102  /// The id of the framebuffer object.
103  GLuint objectId_ = 0u;
104 
105  /// The id of the texture.
106  GLuint textureId_ = 0u;
107 
108  /// The number of samples.
109  unsigned int samples_ = 1u;
110 };
111 
112 inline unsigned int TextureFramebuffer::samples() const
113 {
114  return samples_;
115 }
116 
117 inline GLuint TextureFramebuffer::textureId() const
118 {
119  return textureId_;
120 }
121 
122 inline TextureFramebuffer::operator bool() const
123 {
124  ocean_assert(objectId_ == 0u && textureId_ == 0u || objectId_ != 0u && textureId_ != 0u);
125  return textureId_ != 0u;
126 }
127 
128 }
129 
130 }
131 
132 }
133 
134 #endif // META_OCEAN_PLATFORM_GL_TEXTURE_FRAMEBUFFER_H
This class encapsulates an OpenGL context.
Definition: platform/gl/Context.h:29
This class implements the base class for an OpenGL framebuffers.
Definition: platform/gl/Framebuffer.h:30
This class implements a texture framebuffer allowing to render into a texture object.
Definition: platform/gl/TextureFramebuffer.h:28
TextureFramebuffer()=default
Creates a new framebuffer object.
bool release()
Releases this framebuffer.
virtual bool resize(const unsigned int width, const unsigned int height, const GLenum internalFormat=GL_RGBA)
Resizes the off-screen framebuffer.
GLuint textureId() const
Returns the id of the texture of this framebuffer.
Definition: platform/gl/TextureFramebuffer.h:117
unsigned int samples() const
Returns the number of samples.
Definition: platform/gl/TextureFramebuffer.h:112
unsigned int samples_
The number of samples.
Definition: platform/gl/TextureFramebuffer.h:109
virtual bool unbind()
Unbinds the framebuffer as target framebuffer.
bool setSamples(const unsigned int samples)
(Re-)sets the number of samples of this texture framebuffer.
virtual bool bind()
Binds the framebuffer as target framebuffer.
TextureFramebuffer(Context &context)
Creates a new framebuffer object with know associated context.
bool bindToProgram(const GLuint programId, const std::string &attributeName, const unsigned int index=0u)
Bind this texture to a given shader program.
GLuint textureId_
The id of the texture.
Definition: platform/gl/TextureFramebuffer.h:106
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15