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