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