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