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