|
5 | 5 | #include <GL/glew.h>
|
6 | 6 | #include <glm/vec3.hpp>
|
7 | 7 |
|
| 8 | +#include "Core/Core.h" |
8 | 9 | #include "Core/PointerDefs.h"
|
9 | 10 |
|
10 | 11 | namespace ej
|
11 | 12 | {
|
| 13 | + class RenderingState; |
| 14 | + |
| 15 | + /** |
| 16 | + * \brief Image living on the graphics card that can be used for drawing |
| 17 | + */ |
12 | 18 | class Texture final : public PointerDefs<Texture>
|
13 | 19 | {
|
14 | 20 | public:
|
15 |
| - Texture(GLenum target = GL_TEXTURE_2D); |
| 21 | + /** |
| 22 | + * \param core Main core object |
| 23 | + * \param target Target to which the texture is bound. Must be |
| 24 | + * one of GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, |
| 25 | + * GL_TEXTURE_1D_ARRAY, GL_TEXTURE_2D_ARRAY, GL_TEXTURE_RECTANGLE, |
| 26 | + * GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BUFFER, |
| 27 | + * GL_TEXTURE_2D_MULTISAMPLE or GL_TEXTURE_2D_MULTISAMPLE_ARRAY |
| 28 | + */ |
| 29 | + explicit Texture(const Core& core, GLenum target = GL_TEXTURE_2D); |
16 | 30 | ~Texture();
|
17 | 31 |
|
18 |
| - // init as 1D texture |
| 32 | + /** |
| 33 | + * \brief Initialize as 1D texture |
| 34 | + * |
| 35 | + * Texture can be initialized only once. |
| 36 | + * |
| 37 | + * https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage1D.xhtml |
| 38 | + * |
| 39 | + * \param width Texture width in pixels |
| 40 | + * \param internalFormat number of color components in the texture |
| 41 | + * \param format format of the pixel data. Must be one of GL_RED, |
| 42 | + * GL_RG, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_RED_INTEGER, |
| 43 | + * GL_RG_INTEGER, GL_RGB_INTEGER, GL_BGR_INTEGER, GL_RGBA_INTEGER, |
| 44 | + * GL_BGRA_INTEGER, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT or |
| 45 | + * GL_DEPTH_STENCIL |
| 46 | + * \param type Data type of the pixel data. Must be one of |
| 47 | + * GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, |
| 48 | + * GL_UNSIGNED_INT, GL_INT, GL_HALF_FLOAT, GL_FLOAT and etc. |
| 49 | + * \param data Pointer to the image data in memory |
| 50 | + * \return true if successfully initialized |
| 51 | + */ |
19 | 52 | bool init(unsigned int width,
|
20 | 53 | GLenum internalFormat, GLenum format, GLenum type, const void* data);
|
21 | 54 |
|
22 |
| - // init as 2D texture |
| 55 | + /** |
| 56 | + * \brief Initialize as 2D texture |
| 57 | + * |
| 58 | + * Texture can be initialized only once. |
| 59 | + * |
| 60 | + * https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml |
| 61 | + * |
| 62 | + * \param width Texture width in pixels |
| 63 | + * \param height Texture height in pixels |
| 64 | + * \param internalFormat number of color components in the texture |
| 65 | + * \param format format of the pixel data. Must be one of GL_RED, |
| 66 | + * GL_RG, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_RED_INTEGER, |
| 67 | + * GL_RG_INTEGER, GL_RGB_INTEGER, GL_BGR_INTEGER, GL_RGBA_INTEGER, |
| 68 | + * GL_BGRA_INTEGER, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT or |
| 69 | + * GL_DEPTH_STENCIL |
| 70 | + * \param type Data type of the pixel data. Must be one of |
| 71 | + * GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, |
| 72 | + * GL_UNSIGNED_INT, GL_INT, GL_HALF_FLOAT, GL_FLOAT and etc. |
| 73 | + * \param data Pointer to the image data in memory |
| 74 | + * \return true if successfully initialized |
| 75 | + */ |
23 | 76 | bool init(unsigned int width, unsigned int height,
|
24 | 77 | GLenum internalFormat, GLenum format, GLenum type, const void* data);
|
25 | 78 |
|
26 |
| - // init as 3D texture |
| 79 | + /** |
| 80 | + * \brief Initialize as 3D texture |
| 81 | + * |
| 82 | + * Texture can be initialized only once. |
| 83 | + * |
| 84 | + * https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage3D.xhtml |
| 85 | + * |
| 86 | + * \param width Texture width in pixels. Must be > 1 |
| 87 | + * \param height Texture height in pixels. Must be > 1 |
| 88 | + * \param depth Texture depth in pixels. Must be > 1 |
| 89 | + * \param internalFormat number of color components in the texture |
| 90 | + * \param format format of the pixel data. Must be one of GL_RED, |
| 91 | + * GL_RG, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA, GL_RED_INTEGER, |
| 92 | + * GL_RG_INTEGER, GL_RGB_INTEGER, GL_BGR_INTEGER, GL_RGBA_INTEGER, |
| 93 | + * GL_BGRA_INTEGER, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT or |
| 94 | + * GL_DEPTH_STENCIL |
| 95 | + * \param type Data type of the pixel data. Must be one of |
| 96 | + * GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, |
| 97 | + * GL_UNSIGNED_INT, GL_INT, GL_HALF_FLOAT, GL_FLOAT and etc. |
| 98 | + * \param data Pointer to the image data in memory |
| 99 | + * \return true if successfully initialized |
| 100 | + */ |
27 | 101 | bool init(unsigned int width, unsigned int height, unsigned int depth,
|
28 | 102 | GLenum internalFormat, GLenum format, GLenum type, const void* data);
|
29 | 103 |
|
| 104 | + /** |
| 105 | + * \brief Change size of texture |
| 106 | + * |
| 107 | + * Only needed components will be used. |
| 108 | + * E.g. for TEXTURE_2D - width and height. |
| 109 | + * |
| 110 | + * \param width New texture with in pixels |
| 111 | + * \param height New texture height in pixels |
| 112 | + * \param depth New texture depth in pixels |
| 113 | + */ |
30 | 114 | void resize(unsigned int width, unsigned int height = 0, unsigned int depth = 0);
|
31 | 115 |
|
32 |
| - void setFilters(GLenum minFilter, GLenum maxFilter, bool bind = true); |
33 |
| - void setWrapMode(GLenum wrapMode, bool bind = true); |
34 |
| - |
| 116 | + /** |
| 117 | + * \brief Change sampling arithmetic |
| 118 | + * |
| 119 | + * When this function is called before initialization, it just set |
| 120 | + * parameters locally. When it is called after initialization it will |
| 121 | + * change OpenGL state of this texture. |
| 122 | + * |
| 123 | + * Filter parameters must be one of: GL_NEAREST, GL_LINEAR |
| 124 | + * |
| 125 | + * \param minFilter Filter used for texture minimization |
| 126 | + * \param maxFilter Filter used for texture magnification |
| 127 | + */ |
| 128 | + void setFilters(GLenum minFilter, GLenum maxFilter); |
| 129 | + |
| 130 | + /** |
| 131 | + * \brief Change texture wrapping for all dimensions |
| 132 | + * |
| 133 | + * When this function is called before initialization, it just set |
| 134 | + * parameters locally. When it is called after initialization it will |
| 135 | + * change OpenGL state of this texture. |
| 136 | + * |
| 137 | + * \param wrapMode Component values arithmetic outside [0, 1]. Must |
| 138 | + * be one of: GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT, |
| 139 | + * GL_REPEAT, or GL_MIRROR_CLAMP_TO_EDGE |
| 140 | + */ |
| 141 | + void setWrapMode(GLenum wrapMode); |
| 142 | + |
| 143 | + /** |
| 144 | + * \brief Change texture wrapping for all dimensions |
| 145 | + * |
| 146 | + * When this function is called before initialization, it just set |
| 147 | + * parameters locally. When it is called after initialization it will |
| 148 | + * change OpenGL state of this texture. |
| 149 | + * |
| 150 | + * \param wrapMode Component values arithmetic outside [0, 1]. Must |
| 151 | + * be one of: GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT, |
| 152 | + * GL_REPEAT, or GL_MIRROR_CLAMP_TO_EDGE |
| 153 | + * \param component Component number. 0 - width, 1 - height, 2 - depth |
| 154 | + */ |
| 155 | + void setWrapMode(GLenum wrapMode, size_t component); |
| 156 | + |
| 157 | + /** |
| 158 | + * \brief Generates LOD textures |
| 159 | + * |
| 160 | + * Must be called only after initialization |
| 161 | + */ |
35 | 162 | void generateMipmap() const;
|
36 | 163 |
|
| 164 | + /** |
| 165 | + * \return Native OpenGL handle |
| 166 | + */ |
37 | 167 | GLuint getHandle() const;
|
| 168 | + |
| 169 | + /** |
| 170 | + * \return Target to which the texture is bound |
| 171 | + */ |
38 | 172 | GLenum getTarget() const;
|
39 | 173 |
|
40 |
| - glm::ivec3 getSize() const; |
| 174 | + /** |
| 175 | + * \return Texture dimensions. Useless components will be zeroed |
| 176 | + */ |
| 177 | + glm::uvec3 getSize() const; |
41 | 178 |
|
42 | 179 | private:
|
43 |
| - GLuint m_id; |
| 180 | + static const unsigned DEFAULT_TEXTURE_UNIT = 16; |
| 181 | + |
| 182 | + RenderingState* m_renderingState = nullptr; |
| 183 | + |
| 184 | + GLuint m_id = 0; |
44 | 185 | GLenum m_target;
|
45 | 186 |
|
46 |
| - GLenum m_internalFormat; |
47 |
| - GLenum m_format; |
48 |
| - GLenum m_type; |
| 187 | + GLenum m_internalFormat = GL_RGBA; |
| 188 | + GLenum m_format = GL_RGBA; |
| 189 | + GLenum m_type = GL_UNSIGNED_BYTE; |
49 | 190 |
|
50 |
| - GLenum m_minFilter; |
51 |
| - GLenum m_maxFilter; |
| 191 | + GLenum m_minFilter = GL_LINEAR; |
| 192 | + GLenum m_maxFilter = GL_LINEAR; |
52 | 193 |
|
53 |
| - GLenum m_wrapS; |
54 |
| - GLenum m_wrapT; |
55 |
| - GLenum m_wrapR; |
| 194 | + GLenum m_wrapS = GL_REPEAT; |
| 195 | + GLenum m_wrapT = GL_REPEAT; |
| 196 | + GLenum m_wrapR = GL_REPEAT; |
56 | 197 |
|
57 |
| - glm::ivec3 m_size; |
| 198 | + glm::uvec3 m_size{}; |
58 | 199 |
|
59 |
| - bool m_initialized; |
| 200 | + bool m_initialized = false; |
60 | 201 | };
|
61 | 202 | }
|
0 commit comments