]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.h
Load textures in a type-generic way
[libs/gl.git] / source / core / texture.h
1 #ifndef MSP_GL_TEXTURE_H_
2 #define MSP_GL_TEXTURE_H_
3
4 #include <msp/datafile/dynamicobjectloader.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/graphics/image.h>
7 #include "pixelformat.h"
8 #include "resource.h"
9 #include "texture_backend.h"
10
11 namespace Msp {
12 namespace GL {
13
14 /**
15 Base class for textures.  This class only defines operations common for all
16 texture types and is not instantiable.  For specifying images for textures,
17 see one of the dimensioned texture classes.
18
19 A texture can consinst of a stack of images, called a mipmap.  The dimensions
20 of each mipmap level are half that of the previous level.  The mipmap stack
21 can be used for texture minification; see the Sampler class for details.
22 */
23 class Texture: public TextureBackend, public Resource
24 {
25         friend TextureBackend;
26
27 protected:
28         class Loader: public DataFile::CollectionObjectLoader<Texture>
29         {
30         protected:
31                 unsigned levels;
32
33         public:
34                 Loader(Texture &t): Loader(t, 0) { }
35                 Loader(Texture &t, Collection &c): Loader(t, &c) { }
36         private:
37                 Loader(Texture &, Collection *);
38
39                 virtual void finish();
40
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 generate_mipmap(bool);
49                 void image_data(const std::string &);
50                 void mipmap_levels(unsigned);
51         };
52
53 public:
54         class GenericLoader: public DataFile::DynamicObjectLoader<Texture>
55         {
56                 friend class Texture;
57
58         public:
59                 GenericLoader(Collection &c): DynamicObjectLoader<Texture>(&c) { }
60
61         protected:
62                 virtual const TypeRegistry &get_type_registry() const { return get_texture_registry(); }
63         };
64
65 protected:
66         enum FormatSwizzle
67         {
68                 NO_SWIZZLE,
69                 R_TO_LUMINANCE,
70                 RG_TO_LUMINANCE_ALPHA,
71                 RGB_TO_BGR
72         };
73
74         PixelFormat format;
75         PixelFormat storage_fmt;
76         FormatSwizzle swizzle;
77         bool use_srgb_format;
78         bool auto_gen_mipmap;
79
80         Texture(unsigned);
81
82         void set_format(PixelFormat);
83
84 public:
85         PixelFormat get_format() const { return format; }
86
87         using TextureBackend::generate_mipmap;
88
89         /// Loads a Graphics::Image from a file and uploads it to the texture.
90         virtual void load_image(const std::string &, unsigned = 0);
91
92         /** Uploads an image to the texture.  If storage has not been defined, it
93         will be set to match the image.  Otherwise the image must be compatible
94         with the defined storage.  Semantics depend on the type of texture.  */
95         virtual void image(const Graphics::Image &, unsigned = 0) = 0;
96
97         virtual std::uint64_t get_data_size() const { return 0; }
98
99         using TextureBackend::set_debug_name;
100
101 private:
102         static GenericLoader::TypeRegistry &get_texture_registry();
103 };
104
105 } // namespace GL
106 } // namespace Msp
107
108 #endif