]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
Add a shortcut for specifying both texture filters
[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 filter(TextureFilter);
75                 void generate_mipmap(bool);
76                 void mag_filter(TextureFilter);
77                 void max_anisotropy(float);
78                 void min_filter(TextureFilter);
79                 void wrap(TextureWrap);
80                 void wrap_r(TextureWrap);
81                 void wrap_s(TextureWrap);
82                 void wrap_t(TextureWrap);
83         };
84
85         enum ParameterMask
86         {
87                 MIN_FILTER = 1,
88                 MAG_FILTER = 2,
89                 WRAP_S = 4,
90                 WRAP_T = 8,
91                 WRAP_R = 16,
92                 GENERATE_MIPMAP = 32,
93                 COMPARE = 64,
94                 COMPARE_FUNC = 128,
95                 MAX_ANISOTROPY = 256
96         };
97
98         unsigned id;
99         GLenum target;
100         TextureFilter min_filter;
101         TextureFilter mag_filter;
102         float max_anisotropy;
103         TextureWrap wrap_s;
104         TextureWrap wrap_t;
105         TextureWrap wrap_r;
106         bool gen_mipmap;
107         bool compare;
108         Predicate cmp_func;
109         mutable int dirty_params;
110
111         Texture(GLenum);
112         Texture(const Texture &);
113         Texture &operator=(const Texture &);
114 public:
115         ~Texture();
116
117 protected:
118         void update_parameter(int) const;
119 public:
120         void set_min_filter(TextureFilter);
121         void set_mag_filter(TextureFilter);
122
123         /** Sets filter for both minification and magnification.  Since mipmapping
124         is not applicable to magnification, LINEAR is used instead. */
125         void set_filter(TextureFilter);
126
127         void set_max_anisotropy(float);
128
129         /** Sets the wrapping mode for all coordinates. */
130         void set_wrap(TextureWrap);
131
132         void set_wrap_s(TextureWrap);
133         void set_wrap_t(TextureWrap);
134         void set_wrap_r(TextureWrap);
135
136         /** Sets automatic mipmap generation.  If enabled, mipmaps are generated
137         when a texture image is uploaded. */
138         void set_generate_mipmap(bool);
139
140         /** Sets depth texture comparison.  Has no effect on other formats.  When
141         comparison is enabled, the third component of the texture coordinate is
142         compared against the texel value, and the result is returned as the texture
143         sample. */
144         void set_compare_enabled(bool);
145
146         /** Sets the function to use for depth comparison. */
147         void set_compare_func(Predicate);
148
149         GLenum get_target() const { return target; }
150         unsigned get_id() const { return id; }
151
152         void bind() const;
153         void bind_to(unsigned) const;
154
155         static const Texture *current();
156         static void unbind();
157         static void unbind_from(unsigned);
158 };
159
160 } // namespace GL
161 } // namespace Msp
162
163 #endif