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