]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2d.h
4f0bde26960c0db5f4dee5c9a0edd381f86e285e
[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.  Consists of an array of texels in the shape of a
13 rectangle.  Texture coordinate have a range of [0, 1].  Coordinates outside of
14 this range are subject to wrapping.  This is the most common type of texture.
15 */
16 class Texture2D: public Texture2DBackend
17 {
18         friend Texture2DBackend;
19
20 public:
21         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
22         {
23         public:
24                 Loader(Texture2D &);
25                 Loader(Texture2D &, Collection &);
26         private:
27                 void init();
28
29                 void raw_data(const std::string &);
30                 void storage(PixelFormat, unsigned, unsigned);
31                 void storage_levels(PixelFormat, unsigned, unsigned, unsigned);
32         };
33
34 private:
35         unsigned width;
36         unsigned height;
37         unsigned levels;
38
39 public:
40         Texture2D();
41         virtual ~Texture2D();
42
43         /** Defines storage structure for the texture.  If lv is zero, the number
44         of mipmap levels is automatically determined from storage dimensions.
45
46         Must be called before an image can be uploaded.  Once storage is defined,
47         it can't be changed. */
48         void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv = 0);
49
50         /** Updates the contents of the entire texture.  Storage must be defined
51         beforehand.  The image data must have dimensions and format matching the
52         defined storage. */
53         virtual void image(unsigned level, const void *data);
54
55         /** Updates a rectangular region of the texture.  Storage must be defined
56         beforehand.  The image data must be in a format mathing the defined storage
57         and the update region must be fully inside the texture. */
58         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data);
59
60         /** Updates the contents of the entire texture from an image.  If storage
61         has not been defined, it will be set to match the image.  Otherwise the
62         image must match the defined storage. */
63         virtual void image(const Graphics::Image &, unsigned lv = 0);
64
65         using Texture::image;
66
67         unsigned get_width() const { return width; }
68         unsigned get_height() const { return height; }
69
70 private:
71         unsigned get_n_levels() const;
72         LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
73
74 public:
75         virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
76         virtual std::uint64_t get_data_size() const;
77         using Texture2DBackend::unload;
78 };
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif