]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
ad41e573ed5d53b409f82794ae44afec96a1a23e
[libs/gl.git] / source / texture.h
1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
3
4 #include <msp/core/attributes.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/graphics/image.h>
7 #include "datatype.h"
8 #include "gl.h"
9 #include "pixelformat.h"
10 #include "predicate.h"
11 #include "sampler.h"
12 #include "resource.h"
13
14 namespace Msp {
15 namespace GL {
16
17 /**
18 Base class for textures.  This class only defines operations common for all
19 texture types and is not instantiable.  For specifying images for textures,
20 see one of the dimensioned texture classes.
21
22 A texture can consinst of a stack of images, called a mipmap.  The dimensions
23 of each mipmap level are half that of the previous level.  The mipmap stack
24 can be used for texture minification; see the Sampler class for details.
25 */
26 class Texture: public Resource
27 {
28 protected:
29         class Loader: public DataFile::CollectionObjectLoader<Texture>
30         {
31         protected:
32                 unsigned levels;
33                 bool srgb;
34
35         public:
36                 Loader(Texture &);
37                 Loader(Texture &, Collection &);
38         private:
39                 void init();
40
41                 unsigned get_levels() const;
42
43                 void external_image(const std::string &);
44                 void filter(TextureFilter);
45                 void generate_mipmap(bool);
46                 void image_data(const std::string &);
47                 void mag_filter(TextureFilter);
48                 void max_anisotropy(float);
49                 void min_filter(TextureFilter);
50                 void mipmap_levels(unsigned);
51                 void sampler();
52                 void wrap(TextureWrap);
53                 void wrap_r(TextureWrap);
54                 void wrap_s(TextureWrap);
55                 void wrap_t(TextureWrap);
56         };
57
58         enum ParameterMask
59         {
60                 FORMAT_SWIZZLE = 512
61         };
62
63         enum FormatSwizzle
64         {
65                 NO_SWIZZLE,
66                 R_TO_LUMINANCE,
67                 RG_TO_LUMINANCE_ALPHA
68         };
69
70         unsigned id;
71         GLenum target;
72         PixelFormat ifmt;
73         FormatSwizzle swizzle;
74         bool auto_gen_mipmap;
75         mutable int dirty_params;
76         Sampler default_sampler;
77
78         static int swizzle_orders[];
79
80         Texture(GLenum, ResourceManager * = 0);
81         Texture(const Texture &);
82         Texture &operator=(const Texture &);
83 public:
84         ~Texture();
85
86 protected:
87         static DataType get_alloc_type(PixelFormat);
88         void set_internal_format(PixelFormat);
89         PixelFormat get_upload_format(PixelFormat) const;
90
91 public:
92         Sampler &get_default_sampler() { return default_sampler; }
93         const Sampler &get_default_sampler() const { return default_sampler; }
94
95 protected:
96         void update_parameter(int) const;
97         void set_parameter_i(GLenum, int) const;
98         void set_parameter_f(GLenum, float) const;
99 public:
100         DEPRECATED void set_min_filter(TextureFilter);
101         DEPRECATED void set_mag_filter(TextureFilter);
102
103         /** Sets filter for both minification and magnification.  Since mipmapping
104         is not applicable to magnification, LINEAR is used instead. */
105         DEPRECATED void set_filter(TextureFilter);
106
107         DEPRECATED void set_mipmap_levels(unsigned) { }
108
109         DEPRECATED void set_max_anisotropy(float);
110
111         /** Sets the wrapping mode for all coordinates. */
112         DEPRECATED void set_wrap(TextureWrap);
113
114         DEPRECATED void set_wrap_s(TextureWrap);
115         DEPRECATED void set_wrap_t(TextureWrap);
116         DEPRECATED void set_wrap_r(TextureWrap);
117
118         static bool can_generate_mipmap();
119
120         void generate_mipmap();
121
122         /** Sets automatic mipmap generation.  If enabled, mipmaps are generated
123         when a texture image is uploaded. */
124         void set_auto_generate_mipmap(bool);
125
126         /// Deprecated.  Use set_auto_generate_mipmap instead.
127         DEPRECATED void set_generate_mipmap(bool g) { set_auto_generate_mipmap(g); }
128
129         /** Sets depth texture comparison.  Has no effect on other formats.  When
130         comparison is enabled, the third component of the texture coordinate is
131         compared against the texel value, and the result is returned as the texture
132         sample. */
133         DEPRECATED void set_compare_enabled(bool);
134
135         /** Sets the function to use for depth comparison. */
136         DEPRECATED void set_compare_func(Predicate);
137
138         /// Loads a Graphics::Image from a file and uploads it to the texture.
139         virtual void load_image(const std::string &, bool srgb = false);
140
141         virtual void load_image(const std::string &, unsigned, bool srgb = false);
142
143         /** Uploads an image to the texture.  If storage has not been defined, it
144         will be set to match the image.  Otherwise the image must be compatible
145         with the defined storage.  Semantics depend on the type of texture.
146
147         If srgb is true and storage is determined by this call, then an sRGB pixel
148         format will be used. */
149         virtual void image(const Graphics::Image &, bool srgb = false);
150
151         virtual void image(const Graphics::Image &, unsigned, bool srgb = false) = 0;
152
153         GLenum get_target() const { return target; }
154         unsigned get_id() const { return id; }
155
156         void bind() const { bind_to(0); }
157         void bind_to(unsigned) const;
158
159         static const Texture *current(unsigned = 0);
160         static void unbind() { unbind_from(0); }
161         static void unbind_from(unsigned);
162
163         virtual UInt64 get_data_size() const { return 0; }
164 };
165
166 } // namespace GL
167 } // namespace Msp
168
169 #endif