Ocean
platform/gl/Texture.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_GL_TEXTURE_H
9 #define META_OCEAN_PLATFORM_GL_GL_TEXTURE_H
10 
11 #include "ocean/platform/gl/GL.h"
13 
14 #include "ocean/base/Frame.h"
15 
16 namespace Ocean
17 {
18 
19 namespace Platform
20 {
21 
22 namespace GL
23 {
24 
25 /**
26  * This class implements a 2D OpenGL texture.
27  * @ingroup platformgl
28  */
29 class OCEAN_PLATFORM_GL_EXPORT Texture : public ContextAssociated
30 {
31  public:
32 
33  /**
34  * Creates a new OpenGL texture object.
35  */
37 
38  /**
39  * Creates a new OpenGL texture object.
40  * @param frame The frame for the texture
41  */
42  explicit Texture(const Frame& frame);
43 
44  /**
45  * Destructs a texture.
46  */
48 
49  /**
50  * Returns the id of the texture.
51  * @return The texture's id, 0 if the texture has not been initialized yet
52  */
53  inline GLuint id() const;
54 
55  /**
56  * Resizes the texture. without uploading any texture data.
57  * @param frameType The frameType to set
58  * @param enableFiltering If set, the best available filtering (either tri-linear filtering or anisotropic filtering) is enabled; otherwise nearest neighbor sampling is used.
59  * @return True, if succeeded
60  */
61  bool resize(const FrameType& frameType, const bool enableFiltering = true);
62 
63  /**
64  * Sets the image content of the texture.
65  * Beware: If filtering is disabled, then no mipmaps are generated.
66  * @param frame The frame to set
67  * @param enableFiltering If set, the best available filtering (either tri-linear filtering or anisotropic filtering) is enabled; otherwise nearest neighbor sampling is used.
68  * @return True, if succeeded
69  */
70  bool update(const Frame& frame, const bool enableFiltering = true);
71 
72  /**
73  * Generates mipmaps.
74  */
75  void updateMipmap();
76 
77  /**
78  * Bind this texture.
79  */
80  inline void bind();
81 
82  /**
83  * Bind this texture to a given shader program.
84  * @param programId The id of the shader program
85  * @param attributeName The name of the texture attribute
86  * @param index The index of the texture
87  * @return True, if succeeded
88  */
89  bool bindToProgram(const GLuint programId, const std::string& attributeName, const unsigned int index = 0u);
90 
91  /**
92  * Unbinds this texture.
93  */
94  inline void unbind();
95 
96  /**
97  * Releases the texture.
98  */
99  void release();
100 
101  /**
102  * Returns the frame type of this texture object.
103  * @return The frame type
104  */
105  inline const FrameType& frameType() const;
106 
107  /**
108  * Returns whether this object holds a valid OpenGL texture object.
109  * @return True, if so
110  */
111  explicit inline operator bool() const;
112 
113  /**
114  * Translates a pixel format to a corresponding OpenGL texture format.
115  * @param pixelFormat The pixel format to translate
116  * @param internalFormat Optional the corresponding internal texture format
117  * @return The corresponding texture format, 0 if no corresponding format exists
118  */
119  static GLenum pixelFormat2textureFormat(const FrameType::PixelFormat pixelFormat, GLint* internalFormat = nullptr);
120 
121  protected:
122 
123  /// The OpenGL texture id.
124  GLuint textureId;
125 
126  /// OpenGL texture (pixel) format.
128 
129  /// The frame type of the texture.
131 };
132 
133 inline GLuint Texture::id() const
134 {
135  return textureId;
136 }
137 
138 inline void Texture::bind()
139 {
140  ocean_assert(GL_NO_ERROR == glGetError());
141  glBindTexture(GL_TEXTURE_2D, textureId);
142  ocean_assert(GL_NO_ERROR == glGetError());
143 }
144 
145 inline void Texture::unbind()
146 {
147  ocean_assert(GL_NO_ERROR == glGetError());
148  glBindTexture(GL_TEXTURE_2D, 0);
149  ocean_assert(GL_NO_ERROR == glGetError());
150 }
151 
152 inline const FrameType& Texture::frameType() const
153 {
154  return textureFrameType;
155 }
156 
157 inline Texture::operator bool() const
158 {
159  return textureId != 0u;
160 }
161 
162 }
163 
164 }
165 
166 }
167 
168 #endif // META_OCEAN_PLATFORM_GL_GL_TEXTURE_H
This class implements Ocean's image class.
Definition: Frame.h:1760
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition: Frame.h:30
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
This class implements the base class for all object which have an associated context.
Definition: ContextAssociated.h:31
This class implements a 2D OpenGL texture.
Definition: platform/gl/Texture.h:30
~Texture()
Destructs a texture.
GLuint id() const
Returns the id of the texture.
Definition: platform/gl/Texture.h:133
bool update(const Frame &frame, const bool enableFiltering=true)
Sets the image content of the texture.
void updateMipmap()
Generates mipmaps.
FrameType textureFrameType
The frame type of the texture.
Definition: platform/gl/Texture.h:130
GLuint textureId
The OpenGL texture id.
Definition: platform/gl/Texture.h:124
static GLenum pixelFormat2textureFormat(const FrameType::PixelFormat pixelFormat, GLint *internalFormat=nullptr)
Translates a pixel format to a corresponding OpenGL texture format.
Texture(const Frame &frame)
Creates a new OpenGL texture object.
void release()
Releases the texture.
void bind()
Bind this texture.
Definition: platform/gl/Texture.h:138
Texture()
Creates a new OpenGL texture object.
const FrameType & frameType() const
Returns the frame type of this texture object.
Definition: platform/gl/Texture.h:152
bool bindToProgram(const GLuint programId, const std::string &attributeName, const unsigned int index=0u)
Bind this texture to a given shader program.
bool resize(const FrameType &frameType, const bool enableFiltering=true)
Resizes the texture.
GLuint textureFormat
OpenGL texture (pixel) format.
Definition: platform/gl/Texture.h:127
void unbind()
Unbinds this texture.
Definition: platform/gl/Texture.h:145
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15