]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
Enable resource management on Texture2D
[libs/gl.git] / source / texture.h
1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "gl.h"
6 #include "predicate.h"
7 #include "resource.h"
8
9 namespace Msp {
10 namespace GL {
11
12 enum TextureFilter
13 {
14         /// No filtering
15         NEAREST = GL_NEAREST,
16
17         /// Bilinear filtering
18         LINEAR = GL_LINEAR,
19
20         /// Mipmapping without filtering
21         NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
22
23         /// Linear filtering between two mipmap levels
24         NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
25
26         /// Bilinear filtering on the closest mipmap level
27         LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
28
29         /// Trilinear filtering between two mipmap levels
30         LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR
31 };
32
33
34 enum TextureWrap
35 {
36         /// Tile the texture infinitely
37         REPEAT = GL_REPEAT,
38
39         /// Extend the texels at the edge of the texture to infinity
40         CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
41
42         /// Tile the texture, with every other repetition mirrored
43         MIRRORED_REPEAT = GL_MIRRORED_REPEAT
44 };
45
46
47 /**
48 Base class for textures.  This class only defines operations common for all
49 texture types and is not instantiable.  For specifying images for textures, see
50 one of the dimensioned texture classes.
51
52 A texture is generally rendered at a size that's either smaller or larger than
53 its native size, so that the texture coordinates do not exactly correspond to
54 the texels of the texture.  The kind of filtering used, if any, is determined
55 by the minification and magnification filter parameters.  The default is LINEAR
56 for magnification and NEAREST_MIPMAP_LINEAR for minification.
57
58 When a mipmapped filter is in use, the texture consists of a stack of mipmap
59 images.  Level 0 is the base image.  Each level above 0 has half the size of
60 the previous level, rounded down and clamped to 1.  The level with size 1 in
61 all dimensions is the last mipmap level.  All levels must be allocated for the
62 texture to be usable.
63
64 If texture coordinates fall outside of the principal range of the texture,
65 wrapping is applied.  The default for all directions is REPEAT.
66 */
67 class Texture: public Resource
68 {
69 protected:
70         class Loader: public DataFile::CollectionObjectLoader<Texture>
71         {
72         protected:
73                 bool srgb;
74
75         public:
76                 Loader(Texture &);
77                 Loader(Texture &, Collection &);
78         private:
79                 void init();
80
81                 void filter(TextureFilter);
82                 void generate_mipmap(bool);
83                 void mag_filter(TextureFilter);
84                 void max_anisotropy(float);
85                 void min_filter(TextureFilter);
86                 void wrap(TextureWrap);
87                 void wrap_r(TextureWrap);
88                 void wrap_s(TextureWrap);
89                 void wrap_t(TextureWrap);
90         };
91
92         enum ParameterMask
93         {
94                 MIN_FILTER = 1,
95                 MAG_FILTER = 2,
96                 WRAP_S = 4,
97                 WRAP_T = 8,
98                 WRAP_R = 16,
99                 GENERATE_MIPMAP = 32,
100                 COMPARE = 64,
101                 COMPARE_FUNC = 128,
102                 MAX_ANISOTROPY = 256
103         };
104
105         unsigned id;
106         GLenum target;
107         TextureFilter min_filter;
108         TextureFilter mag_filter;
109         float max_anisotropy;
110         TextureWrap wrap_s;
111         TextureWrap wrap_t;
112         TextureWrap wrap_r;
113         bool gen_mipmap;
114         bool compare;
115         Predicate cmp_func;
116         mutable int dirty_params;
117
118         Texture(GLenum, ResourceManager * = 0);
119         Texture(const Texture &);
120         Texture &operator=(const Texture &);
121 public:
122         ~Texture();
123
124 protected:
125         void update_parameter(int) const;
126 public:
127         void set_min_filter(TextureFilter);
128         void set_mag_filter(TextureFilter);
129
130         /** Sets filter for both minification and magnification.  Since mipmapping
131         is not applicable to magnification, LINEAR is used instead. */
132         void set_filter(TextureFilter);
133
134         void set_max_anisotropy(float);
135
136         /** Sets the wrapping mode for all coordinates. */
137         void set_wrap(TextureWrap);
138
139         void set_wrap_s(TextureWrap);
140         void set_wrap_t(TextureWrap);
141         void set_wrap_r(TextureWrap);
142
143         /** Sets automatic mipmap generation.  If enabled, mipmaps are generated
144         when a texture image is uploaded. */
145         void set_generate_mipmap(bool);
146
147         /** Sets depth texture comparison.  Has no effect on other formats.  When
148         comparison is enabled, the third component of the texture coordinate is
149         compared against the texel value, and the result is returned as the texture
150         sample. */
151         void set_compare_enabled(bool);
152
153         /** Sets the function to use for depth comparison. */
154         void set_compare_func(Predicate);
155
156         GLenum get_target() const { return target; }
157         unsigned get_id() const { return id; }
158
159         void bind() const { bind_to(0); }
160         void bind_to(unsigned) const;
161
162         static const Texture *current(unsigned = 0);
163         static void unbind() { unbind_from(0); }
164         static void unbind_from(unsigned);
165 };
166
167 } // namespace GL
168 } // namespace Msp
169
170 #endif