]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.h
Update and improve documentation
[libs/gl.git] / source / core / texture2d.h
1 #ifndef MSP_GL_TEXTURE2D_H_
2 #define MSP_GL_TEXTURE2D_H_
3
4 #include <string>
5 #include <msp/linal/vector.h>
6 #include "texture2d_backend.h"
7
8 namespace Msp {
9 namespace GL {
10
11 /**
12 Two-dimensional texture, consisting of a rectangular array of texels.
13 */
14 class Texture2D: public Texture2DBackend
15 {
16         friend Texture2DBackend;
17
18 public:
19         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
20         {
21         public:
22                 Loader(Texture2D &);
23                 Loader(Texture2D &, Collection &);
24         private:
25                 void init();
26
27                 void raw_data(const std::string &);
28                 void storage(PixelFormat, unsigned, unsigned);
29                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned);
30         };
31
32 private:
33         unsigned width = 0;
34         unsigned height = 0;
35         unsigned levels = 0;
36
37 public:
38         virtual ~Texture2D();
39
40         /** Sets storage format and dimensions and allocates memory for the texture.
41         If lv is zero, a complete mipmap pyramid is automatically created.  Storage
42         cannot be changed once set. */
43         void storage(PixelFormat, unsigned wd, unsigned ht, unsigned lv = 0);
44
45         /** Replaces contents of an entire mipmap level.  Allocated storage must
46         exist.  The image data is interpreted according to the storage format and
47         must have size matching the selected mipmap level. */
48         virtual void image(unsigned level, const void *);
49
50         /** Replaces a rectangular region of the texture.  Allocated storage must
51         exist.  The image data is interpreted according to the storage format and
52         the region must be fully inside the selected mipmap level. */
53         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *);
54
55         virtual void image(const Graphics::Image &, unsigned = 0);
56
57         unsigned get_width() const { return width; }
58         unsigned get_height() const { return height; }
59
60 private:
61         unsigned get_n_levels() const;
62         LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
63
64 public:
65         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
66         virtual std::size_t get_data_size() const;
67         using Texture2DBackend::unload;
68 };
69
70 } // namespace GL
71 } // namespace Msp
72
73 #endif