]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture.h
Use constructor delegation instead of init functions when possible
[libs/gl.git] / source / core / 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 "pixelformat.h"
7 #include "resource.h"
8 #include "texture_backend.h"
9
10 namespace Msp {
11 namespace GL {
12
13 /**
14 Base class for textures.  This class only defines operations common for all
15 texture types and is not instantiable.  For specifying images for textures,
16 see one of the dimensioned texture classes.
17
18 A texture can consinst of a stack of images, called a mipmap.  The dimensions
19 of each mipmap level are half that of the previous level.  The mipmap stack
20 can be used for texture minification; see the Sampler class for details.
21 */
22 class Texture: public TextureBackend, public Resource
23 {
24         friend TextureBackend;
25
26 protected:
27         class Loader: public DataFile::CollectionObjectLoader<Texture>
28         {
29         protected:
30                 unsigned levels;
31
32         public:
33                 Loader(Texture &t): Loader(t, 0) { }
34                 Loader(Texture &t, Collection &c): Loader(t, &c) { }
35         private:
36                 Loader(Texture &, Collection *);
37
38                 virtual void finish();
39
40         protected:
41                 void load_external_image(Graphics::Image &, const std::string &);
42
43         private:
44                 void external_image(const std::string &);
45                 void external_image_srgb(const std::string &);
46                 void external_image_common(const std::string &);
47                 void generate_mipmap(bool);
48                 void image_data(const std::string &);
49                 void mipmap_levels(unsigned);
50         };
51
52         enum FormatSwizzle
53         {
54                 NO_SWIZZLE,
55                 R_TO_LUMINANCE,
56                 RG_TO_LUMINANCE_ALPHA,
57                 RGB_TO_BGR
58         };
59
60         PixelFormat format;
61         PixelFormat storage_fmt;
62         FormatSwizzle swizzle;
63         bool use_srgb_format;
64         bool auto_gen_mipmap;
65
66         Texture(unsigned, ResourceManager * = 0);
67
68         void set_format(PixelFormat);
69
70 public:
71         PixelFormat get_format() const { return format; }
72
73         using TextureBackend::generate_mipmap;
74
75         /// Loads a Graphics::Image from a file and uploads it to the texture.
76         virtual void load_image(const std::string &, unsigned = 0);
77
78         /** Uploads an image to the texture.  If storage has not been defined, it
79         will be set to match the image.  Otherwise the image must be compatible
80         with the defined storage.  Semantics depend on the type of texture.  */
81         virtual void image(const Graphics::Image &, unsigned = 0) = 0;
82
83         virtual std::uint64_t get_data_size() const { return 0; }
84
85         using TextureBackend::set_debug_name;
86 };
87
88 } // namespace GL
89 } // namespace Msp
90
91 #endif