]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
Refactor texture external image loading into a helper function
[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         protected:
43                 void load_external_image(Graphics::Image &, const std::string &);
44
45         private:
46                 void external_image(const std::string &);
47                 void filter(TextureFilter);
48                 void generate_mipmap(bool);
49                 void image_data(const std::string &);
50                 void mag_filter(TextureFilter);
51                 void max_anisotropy(float);
52                 void min_filter(TextureFilter);
53                 void mipmap_levels(unsigned);
54                 void sampler();
55                 void wrap(TextureWrap);
56                 void wrap_r(TextureWrap);
57                 void wrap_s(TextureWrap);
58                 void wrap_t(TextureWrap);
59         };
60
61         enum ParameterMask
62         {
63                 FORMAT_SWIZZLE = 512
64         };
65
66         enum FormatSwizzle
67         {
68                 NO_SWIZZLE,
69                 R_TO_LUMINANCE,
70                 RG_TO_LUMINANCE_ALPHA,
71                 RGB_TO_BGR
72         };
73
74         unsigned id;
75         GLenum target;
76         PixelFormat format;
77         PixelFormat storage_fmt;
78         FormatSwizzle swizzle;
79         bool auto_gen_mipmap;
80         Sampler default_sampler;
81
82         static int swizzle_orders[];
83
84         Texture(GLenum, ResourceManager * = 0);
85         Texture(const Texture &);
86         Texture &operator=(const Texture &);
87 public:
88         ~Texture();
89
90 protected:
91         void set_format(PixelFormat);
92         void apply_swizzle();
93         void set_parameter_i(GLenum, int) const;
94
95 public:
96         Sampler &get_default_sampler() { return default_sampler; }
97         const Sampler &get_default_sampler() const { return default_sampler; }
98
99         DEPRECATED void set_min_filter(TextureFilter);
100         DEPRECATED void set_mag_filter(TextureFilter);
101
102         /** Sets filter for both minification and magnification.  Since mipmapping
103         is not applicable to magnification, LINEAR is used instead. */
104         DEPRECATED void set_filter(TextureFilter);
105
106         DEPRECATED void set_mipmap_levels(unsigned) { }
107
108         DEPRECATED void set_max_anisotropy(float);
109
110         /** Sets the wrapping mode for all coordinates. */
111         DEPRECATED void set_wrap(TextureWrap);
112
113         DEPRECATED void set_wrap_s(TextureWrap);
114         DEPRECATED void set_wrap_t(TextureWrap);
115         DEPRECATED void set_wrap_r(TextureWrap);
116
117         static bool can_generate_mipmap();
118
119         void generate_mipmap();
120
121         /** Sets automatic mipmap generation.  If enabled, mipmaps are generated
122         when a texture image is uploaded. */
123         void set_auto_generate_mipmap(bool);
124
125         /// Deprecated.  Use set_auto_generate_mipmap instead.
126         DEPRECATED void set_generate_mipmap(bool g) { set_auto_generate_mipmap(g); }
127
128         /** Sets depth texture comparison.  Has no effect on other formats.  When
129         comparison is enabled, the third component of the texture coordinate is
130         compared against the texel value, and the result is returned as the texture
131         sample. */
132         DEPRECATED void set_compare_enabled(bool);
133
134         /** Sets the function to use for depth comparison. */
135         DEPRECATED void set_compare_func(Predicate);
136
137         /// Loads a Graphics::Image from a file and uploads it to the texture.
138         virtual void load_image(const std::string &, bool srgb = false);
139
140         virtual void load_image(const std::string &, unsigned, bool srgb = false);
141
142         /** Uploads an image to the texture.  If storage has not been defined, it
143         will be set to match the image.  Otherwise the image must be compatible
144         with the defined storage.  Semantics depend on the type of texture.
145
146         If srgb is true and storage is determined by this call, then an sRGB pixel
147         format will be used. */
148         virtual void image(const Graphics::Image &, bool srgb = false);
149
150         virtual void image(const Graphics::Image &, unsigned, bool srgb = false) = 0;
151
152         GLenum get_target() const { return target; }
153         unsigned get_id() const { return id; }
154
155         void bind() const { bind_to(0); }
156         void bind_to(unsigned) const;
157
158         static const Texture *current(unsigned = 0);
159         static void unbind() { unbind_from(0); }
160         static void unbind_from(unsigned);
161
162         virtual UInt64 get_data_size() const { return 0; }
163 };
164
165 } // namespace GL
166 } // namespace Msp
167
168 #endif